sanity-plugin-internationalized-array 3.1.2 → 3.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,14 +1,57 @@
1
- import type {FieldProps} from 'sanity'
1
+ import type {ReactNode} from 'react'
2
+ import {useMemo} from 'react'
3
+ import {type FieldProps} from 'sanity'
2
4
 
3
- export default function InternationalizedField(props: FieldProps) {
4
- // Show reference field selector if there's a value
5
- if (props.schemaType.name === 'reference' && props.value) {
6
- return props.renderDefault({
5
+ import {useInternationalizedArrayContext} from './InternationalizedArrayContext'
6
+
7
+ export default function InternationalizedField(props: FieldProps): ReactNode {
8
+ const {languages} = useInternationalizedArrayContext()
9
+
10
+ // hide titles for 'value' fields within valid language entries
11
+ const customProps = useMemo(() => {
12
+ const pathSegment = props.path.slice(0, -1)[1]
13
+ const languageId =
14
+ typeof pathSegment === 'object' && '_key' in pathSegment
15
+ ? pathSegment._key
16
+ : undefined
17
+ const hasValidLanguageId = languageId
18
+ ? languages.some((l) => l.id === languageId)
19
+ : false
20
+ const shouldHideTitle =
21
+ props.title?.toLowerCase() === 'value' && hasValidLanguageId
22
+
23
+ return {
7
24
  ...props,
25
+ title: shouldHideTitle ? '' : props.title,
26
+ }
27
+ }, [props, languages])
28
+
29
+ if (!customProps.schemaType.name.startsWith('internationalizedArray')) {
30
+ return customProps.renderDefault(customProps)
31
+ }
32
+
33
+ // Show reference field selector if there's a value
34
+ if (customProps.schemaType.name === 'reference' && customProps.value) {
35
+ return customProps.renderDefault({
36
+ ...customProps,
8
37
  title: '',
9
- level: 0,
38
+ level: 0, // Reset the level to avoid nested styling
10
39
  })
11
40
  }
12
41
 
13
- return props.children
42
+ // For basic field types, we can use children to keep the simple input
43
+ if (
44
+ customProps.schemaType.name === 'string' ||
45
+ customProps.schemaType.name === 'number' ||
46
+ customProps.schemaType.name === 'text'
47
+ ) {
48
+ return customProps.children
49
+ }
50
+
51
+ // For complex fields (like markdown), we need to use renderDefault
52
+ // to get all the field's functionality
53
+ return customProps.renderDefault({
54
+ ...customProps,
55
+ level: 0, // Reset the level to avoid nested styling
56
+ })
14
57
  }
@@ -13,7 +13,7 @@ import {
13
13
  Tooltip,
14
14
  } from '@sanity/ui'
15
15
  import type React from 'react'
16
- import {useCallback, useMemo} from 'react'
16
+ import {ReactNode, useCallback, useMemo} from 'react'
17
17
  import {type ObjectItemProps, useFormValue} from 'sanity'
18
18
  import {set, unset} from 'sanity'
19
19
 
@@ -21,7 +21,7 @@ import {getLanguageDisplay} from '../utils/getLanguageDisplay'
21
21
  import {getToneFromValidation} from './getToneFromValidation'
22
22
  import {useInternationalizedArrayContext} from './InternationalizedArrayContext'
23
23
 
24
- type InternationalizedValue = {
24
+ export type InternationalizedValue = {
25
25
  _type: string
26
26
  _key: string
27
27
  value: string
@@ -29,7 +29,7 @@ type InternationalizedValue = {
29
29
 
30
30
  export default function InternationalizedInput(
31
31
  props: ObjectItemProps<InternationalizedValue>
32
- ) {
32
+ ): ReactNode {
33
33
  const parentValue = useFormValue(
34
34
  props.path.slice(0, -1)
35
35
  ) as InternationalizedValue[]
@@ -41,7 +41,7 @@ export default function InternationalizedInput(
41
41
  (m) => m.kind === 'field' && m.name === 'value'
42
42
  ),
43
43
  // This just overrides the type
44
- // TODO: Remove this as it shouldn't be necessary?
44
+ // Remove this as it shouldn't be necessary?
45
45
  value: props.value as InternationalizedValue,
46
46
  }
47
47
 
@@ -61,7 +61,7 @@ export default function InternationalizedInput(
61
61
 
62
62
  // Changes the key of this item, ideally to a valid language
63
63
  const handleKeyChange = useCallback(
64
- (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
64
+ (event: React.MouseEvent<HTMLButtonElement, MouseEvent>): void => {
65
65
  const languageId = event?.currentTarget?.value
66
66
 
67
67
  if (
@@ -78,7 +78,7 @@ export default function InternationalizedInput(
78
78
  )
79
79
 
80
80
  // Removes this item from the array
81
- const handleUnset = useCallback(() => {
81
+ const handleUnset = useCallback((): void => {
82
82
  onChange(unset())
83
83
  }, [onChange])
84
84
 
package/src/plugin.tsx CHANGED
@@ -1,6 +1,7 @@
1
1
  import {definePlugin, isObjectInputProps} from 'sanity'
2
2
 
3
3
  import {InternationalizedArrayProvider} from './components/InternationalizedArrayContext'
4
+ import InternationalizedField from './components/InternationalizedField'
4
5
  import Preload from './components/Preload'
5
6
  import {CONFIG_DEFAULT} from './constants'
6
7
  import {internationalizedArrayFieldAction} from './fieldActions'
@@ -44,6 +45,8 @@ export const internationalizedArray = definePlugin<PluginConfig>((config) => {
44
45
  // Wrap document editor with a language provider
45
46
  form: {
46
47
  components: {
48
+ field: (props) => <InternationalizedField {...props} />,
49
+
47
50
  input: (props) => {
48
51
  const isRootInput = props.id === 'root' && isObjectInputProps(props)
49
52
 
@@ -1,7 +1,6 @@
1
1
  import {defineField, FieldDefinition} from 'sanity'
2
2
 
3
3
  import {createFieldName} from '../components/createFieldName'
4
- import InternationalizedField from '../components/InternationalizedField'
5
4
  import InternationalizedInput from '../components/InternationalizedInput'
6
5
 
7
6
  type ObjectFactoryConfig = {
@@ -22,23 +21,10 @@ export default (config: ObjectFactoryConfig): FieldDefinition<'object'> => {
22
21
  item: InternationalizedInput,
23
22
  },
24
23
  fields: [
25
- typeof type === `string`
26
- ? // Define a simple field if all we have is the name as a string
27
- defineField({
28
- name: 'value',
29
- type,
30
- components: {
31
- field: InternationalizedField,
32
- },
33
- })
34
- : // Pass in the configured options, but overwrite the name
35
- {
36
- ...type,
37
- name: 'value',
38
- components: {
39
- field: InternationalizedField,
40
- },
41
- },
24
+ defineField({
25
+ ...(typeof type === 'string' ? {type} : type),
26
+ name: 'value',
27
+ }),
42
28
  ],
43
29
  preview: {
44
30
  select: {