Files
faunosteo/apps/app/src/components/editor/views/ViewNames.tsx
T

45 lines
1.2 KiB
TypeScript

import type { SheetView } from '@faunosteo/core-wasm/types'
import TextField from '@/components/editor/fields/TextField'
import { isBlank } from '@/editor/sheetDocument'
import type { ViewUpdateApi } from '@/editor/useViewUpdate'
import type { Locale } from '@/i18n/types'
import { useT } from '@/i18n/useT'
/** The two names of a view (required); each commits on blur or Enter. */
export default function ViewNames({
view,
readOnly,
views,
}: {
view: SheetView
readOnly: boolean
views: ViewUpdateApi
}) {
const t = useT()
const rename = (locale: Locale) => (value: string) =>
isBlank(value)
? views.reject(locale, 'editor.field.required')
: views.update(
locale,
view.id,
{ name: { ...view.name, [locale]: value } },
false,
)
return (
<>
{(['fr', 'en'] as const).map((locale) => (
<TextField
key={locale}
id={`view-${view.id}-name-${locale}`}
label={t(`editor.views.name_${locale}`)}
value={view.name[locale]}
onCommit={rename(locale)}
readOnly={readOnly}
trim
error={views.error(locale)}
/>
))}
</>
)
}