Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/components/InternationalizedArray.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
ArraySchemaType
>

export default function InternationalizedArray(

Check warning on line 30 in src/components/InternationalizedArray.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

Missing return type on function
props: InternationalizedArrayProps
) {
const {members, value, schemaType, onChange} = props
Expand Down Expand Up @@ -108,22 +108,27 @@
const {isDeleting} = useDocumentPane()

const addedLanguages = members.map(({key}) => key)
const hasAddedDefaultLanguages = defaultLanguages.every((language) =>
addedLanguages.includes(language)
)
const hasAddedDefaultLanguages = defaultLanguages
.filter((language) => languages.find((l) => l.id === language))
.every((language) => addedLanguages.includes(language))

useEffect(() => {
if (!isDeleting && !hasAddedDefaultLanguages) {
handleAddLanguage(defaultLanguages)
const languagesToAdd = defaultLanguages
.filter((language) => !addedLanguages.includes(language))
.filter((language) => languages.find((l) => l.id === language))
handleAddLanguage(languagesToAdd)
}
}, [
isDeleting,
hasAddedDefaultLanguages,
handleAddLanguage,
defaultLanguages,
addedLanguages,
languages,
])

// TODO: This is reordering and re-setting the whole array, it could be surgical

Check warning on line 131 in src/components/InternationalizedArray.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

Unexpected 'todo' comment: 'TODO: This is reordering and re-setting...'
const handleRestoreOrder = useCallback(() => {
if (!value?.length || !languages?.length) {
return
Expand Down
40 changes: 32 additions & 8 deletions src/components/InternationalizedInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
MenuItem,
Spinner,
Stack,
Text,
Tooltip,
} from '@sanity/ui'
import type React from 'react'
import {useCallback, useMemo} from 'react'
Expand All @@ -25,7 +27,7 @@
value: string
}

export default function InternationalizedInput(

Check warning on line 30 in src/components/InternationalizedInput.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

Missing return type on function
props: ObjectItemProps<InternationalizedValue>
) {
const parentValue = useFormValue(
Expand All @@ -39,14 +41,15 @@
(m) => m.kind === 'field' && m.name === 'value'
),
// This just overrides the type
// TODO: Remove this as it shouldn't be necessary?

Check warning on line 44 in src/components/InternationalizedInput.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

Unexpected 'todo' comment: 'TODO: Remove this as it shouldn't be...'
value: props.value as InternationalizedValue,
}

const {validation, value, onChange, readOnly} = inlineProps

// The parent array contains the languages from the plugin config
const {languages, languageDisplay} = useInternationalizedArrayContext()
const {languages, languageDisplay, defaultLanguages} =
useInternationalizedArrayContext()

const languageKeysInUse = useMemo(
() => parentValue?.map((v) => v._key) ?? [],
Expand Down Expand Up @@ -89,6 +92,18 @@
? getLanguageDisplay(languageDisplay, language.title, language.id)
: ''

const isDefault = defaultLanguages.includes(value._key)

const removeButton = (
<Button
mode="bleed"
icon={RemoveCircleIcon}
tone="critical"
disabled={readOnly || isDefault}
onClick={handleUnset}
/>
)

return (
<Card paddingTop={2} tone={getToneFromValidation(validation)}>
<Stack space={2}>
Expand Down Expand Up @@ -126,13 +141,22 @@
</Card>

<Card tone="inherit">
<Button
mode="bleed"
icon={RemoveCircleIcon}
tone="critical"
disabled={readOnly}
onClick={handleUnset}
/>
{isDefault ? (
<Tooltip
content={
<Text muted size={1}>
Can&apos;t remove default language
</Text>
}
fallbackPlacements={['right', 'left']}
placement="top"
portal
>
<span>{removeButton}</span>
</Tooltip>
) : (
removeButton
)}
</Card>
</Flex>
</Stack>
Expand Down
Loading