sanity-plugin-internationalized-array 1.9.0 → 1.10.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sanity-plugin-internationalized-array",
3
- "version": "1.9.0",
3
+ "version": "1.10.0",
4
4
  "description": "Store localized fields in an array to save on attributes",
5
5
  "keywords": [
6
6
  "sanity",
@@ -9,6 +9,7 @@ import {
9
9
  set,
10
10
  setIfMissing,
11
11
  useFormValue,
12
+ MemberItemError
12
13
  } from 'sanity'
13
14
 
14
15
  import {MAX_COLUMNS} from '../constants'
@@ -216,17 +217,14 @@ export default function InternationalizedArray(
216
217
  if (member.kind === 'item') {
217
218
  return (
218
219
  <ArrayOfObjectsItem
220
+ {...props}
219
221
  key={member.key}
220
222
  member={member}
221
- renderItem={props.renderItem}
222
- renderField={props.renderField}
223
- renderInput={props.renderInput}
224
- renderPreview={props.renderPreview}
225
223
  />
226
224
  )
227
225
  }
228
226
 
229
- return null
227
+ return <MemberItemError key={member.key} member={member} />
230
228
  })}
231
229
  </>
232
230
  ) : null}
@@ -58,7 +58,7 @@ export function InternationalizedArrayProvider(
58
58
  }
59
59
  return internationalizedArray.languages
60
60
  },
61
- [version, namespace],
61
+ [version, namespace, selectedValue],
62
62
  {equal}
63
63
  )
64
64
 
package/src/plugin.tsx CHANGED
@@ -7,6 +7,7 @@ import {internationalizedArrayFieldAction} from './fieldActions'
7
7
  import array from './schema/array'
8
8
  import object from './schema/object'
9
9
  import {PluginConfig} from './types'
10
+ import {flattenSchemaType} from './utils/flattenSchemaType'
10
11
 
11
12
  export const internationalizedArray = definePlugin<PluginConfig>((config) => {
12
13
  const pluginConfig = {...CONFIG_DEFAULT, ...config}
@@ -50,11 +51,10 @@ export const internationalizedArray = definePlugin<PluginConfig>((config) => {
50
51
  return props.renderDefault(props)
51
52
  }
52
53
 
53
- const rootFieldTypeNames = props.schemaType.fields.map(
54
+ const flatFieldTypeNames = flattenSchemaType(props.schemaType).map(
54
55
  (field) => field.type.name
55
56
  )
56
-
57
- const hasInternationalizedArray = rootFieldTypeNames.some((name) =>
57
+ const hasInternationalizedArray = flatFieldTypeNames.some((name) =>
58
58
  name.startsWith('internationalizedArray')
59
59
  )
60
60
 
package/src/types.ts CHANGED
@@ -118,10 +118,10 @@ export type PluginConfig = {
118
118
  * Locations where the "+ EN" add language buttons are visible
119
119
  * @defaultValue ['field']
120
120
  * */
121
- buttonLocations: ('field' | 'unstable__fieldAction' | 'document')[]
121
+ buttonLocations?: ('field' | 'unstable__fieldAction' | 'document')[]
122
122
  /**
123
123
  * Show or hide the "Add missing languages" button
124
124
  * @defaultValue true
125
125
  * */
126
- buttonAddAll: boolean
126
+ buttonAddAll?: boolean
127
127
  }
@@ -0,0 +1,59 @@
1
+ import {
2
+ isArraySchemaType,
3
+ isDocumentSchemaType,
4
+ ObjectField,
5
+ Path,
6
+ SchemaType,
7
+ } from 'sanity'
8
+
9
+ type ObjectFieldWithPath = ObjectField<SchemaType> & {path: Path}
10
+
11
+ /**
12
+ * Flattens a document's schema type into a flat array of fields and includes their path
13
+ */
14
+ export function flattenSchemaType(
15
+ schemaType: SchemaType
16
+ ): ObjectFieldWithPath[] {
17
+ if (!isDocumentSchemaType(schemaType)) {
18
+ console.error(`Schema type is not a document`)
19
+ return []
20
+ }
21
+
22
+ return extractInnerFields(schemaType.fields, [], 3)
23
+ }
24
+
25
+ function extractInnerFields(
26
+ fields: ObjectField<SchemaType>[],
27
+ path: Path,
28
+ maxDepth: number
29
+ ): ObjectFieldWithPath[] {
30
+ if (path.length >= maxDepth) {
31
+ return []
32
+ }
33
+
34
+ return fields.reduce<ObjectFieldWithPath[]>((acc, field) => {
35
+ const thisFieldWithPath = {path: [...path, field.name], ...field}
36
+
37
+ if (field.type.jsonType === 'object') {
38
+ const innerFields = extractInnerFields(
39
+ field.type.fields,
40
+ [...path, field.name],
41
+ maxDepth
42
+ )
43
+
44
+ return [...acc, thisFieldWithPath, ...innerFields]
45
+ } else if (field.type.jsonType === 'array' && isArraySchemaType(field)) {
46
+ const innerFields = extractInnerFields(
47
+ // TODO: Fix TS assertion for array fields
48
+ // @ts-expect-error
49
+ field.type.of,
50
+ [...path, field.name],
51
+ maxDepth
52
+ )
53
+
54
+ return [...acc, thisFieldWithPath, ...innerFields]
55
+ }
56
+
57
+ return [...acc, thisFieldWithPath]
58
+ }, [])
59
+ }