Skip to content

Commit

Permalink
fix: split tabs components
Browse files Browse the repository at this point in the history
  • Loading branch information
maximeperrault committed Oct 28, 2024
1 parent a0bf2e5 commit 971d6f1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 29 deletions.
34 changes: 5 additions & 29 deletions frontend/src/features/Dashboard/components/DashboardTab.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useAppDispatch } from '@hooks/useAppDispatch'
import { useEscapeKey } from '@hooks/useEscapeKey'
import { Icon, TextInput, useClickOutsideEffect, useNewWindow } from '@mtes-mct/monitor-ui'
import { useRef, useState } from 'react'
import { Icon } from '@mtes-mct/monitor-ui'
import { useState } from 'react'
import styled from 'styled-components'

import { dashboardActions } from '../slice'
import { TabNameInput } from './TabNameInput'

export function DashboardTab({
isEditing,
Expand All @@ -19,28 +19,15 @@ export function DashboardTab({
}) {
const dispatch = useAppDispatch()

const ref = useRef<HTMLDivElement>(null)
const { newWindowContainerRef } = useNewWindow()

const [updatedName, setUpdatedName] = useState<string | undefined>()
const [updatedName, setUpdatedName] = useState<string | undefined>(name)

const validateName = () => {
if (updatedName) {
dispatch(dashboardActions.setName({ key: tabKey, name: updatedName }))
setUpdatedName(undefined)
}
onEdit(false)
}

useClickOutsideEffect(
ref,
() => {
validateName()
},
newWindowContainerRef.current
)
useEscapeKey({ onEnter: () => validateName(), ref })

const editName = e => {
e.stopPropagation()
e.preventDefault()
Expand All @@ -51,15 +38,7 @@ export function DashboardTab({
return (
<>
{isEditing ? (
<StyledTextInput
inputRef={ref}
isLabelHidden
isTransparent
label="Nom du tableau de bord"
name="name"
onChange={value => setUpdatedName(value)}
value={updatedName}
/>
<TabNameInput onChange={setUpdatedName} validate={validateName} value={updatedName} />
) : (
<Container>
<DashboardName>{name}</DashboardName>
Expand All @@ -81,6 +60,3 @@ const DashboardName = styled.span`
text-overflow: ellipsis;
white-space: nowrap;
`
const StyledTextInput = styled(TextInput)`
flex-grow: 1;
`
44 changes: 44 additions & 0 deletions frontend/src/features/Dashboard/components/TabNameInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useEscapeKey } from '@hooks/useEscapeKey'
import { TextInput, useClickOutsideEffect, useNewWindow } from '@mtes-mct/monitor-ui'
import { useRef } from 'react'
import styled from 'styled-components'

export function TabNameInput({
onChange,
validate,
value
}: {
onChange: (name: string | undefined) => void
validate: () => void
value: string | undefined
}) {
const ref = useRef<HTMLInputElement>(null)
const { newWindowContainerRef } = useNewWindow()

useClickOutsideEffect(
ref,
() => {
validate()
},
newWindowContainerRef.current
)

useEscapeKey({ onEnter: () => validate(), ref })

return (
<StyledTextInput
autoFocus
inputRef={ref}
isLabelHidden
isTransparent
label="Nom du tableau de bord"
name="name"
onChange={onChange}
value={value}
/>
)
}

const StyledTextInput = styled(TextInput)`
flex-grow: 1;
`

0 comments on commit 971d6f1

Please sign in to comment.