sanity-plugin-internationalized-array 1.10.3 → 1.10.5

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.10.3",
3
+ "version": "1.10.5",
4
4
  "description": "Store localized fields in an array to save on attributes",
5
5
  "keywords": [
6
6
  "sanity",
@@ -88,7 +88,7 @@
88
88
  "typescript": "^4.9.5"
89
89
  },
90
90
  "peerDependencies": {
91
- "@sanity/ui": "^1.2.2",
91
+ "@sanity/ui": "^1.2.2 || ^2.0.0",
92
92
  "react": "^18",
93
93
  "sanity": "^3.0.0",
94
94
  "styled-components": "^5.3.6"
@@ -103,8 +103,9 @@ export default function InternationalizedArray(
103
103
 
104
104
  // Create default fields if the document is not yet created
105
105
  const documentCreatedAt = useFormValue(['_createdAt'])
106
- const [hasAddedDefaultLanguages, setHasAddedDefaultLanguages] =
107
- useState(Boolean(documentCreatedAt))
106
+ const [hasAddedDefaultLanguages, setHasAddedDefaultLanguages] = useState(
107
+ Boolean(documentCreatedAt)
108
+ )
108
109
 
109
110
  // Write default languages
110
111
  useEffect(() => {
package/src/plugin.tsx CHANGED
@@ -36,11 +36,11 @@ export const internationalizedArray = definePlugin<PluginConfig>((config) => {
36
36
  },
37
37
  },
38
38
  // Optional: render "add language" buttons as field actions
39
- document: {
40
- unstable_fieldActions: buttonLocations.includes('unstable__fieldAction')
41
- ? (prev) => [...prev, internationalizedArrayFieldAction]
42
- : undefined,
43
- },
39
+ // document: {
40
+ // unstable_fieldActions: buttonLocations.includes('unstable__fieldAction')
41
+ // ? (prev) => [...prev, internationalizedArrayFieldAction]
42
+ // : undefined,
43
+ // },
44
44
  // Wrap document editor with a language provider
45
45
  form: {
46
46
  components: {
@@ -6,6 +6,7 @@ import {createFieldName} from '../components/createFieldName'
6
6
  import {getSelectedValue} from '../components/getSelectedValue'
7
7
  import InternationalizedArray from '../components/InternationalizedArray'
8
8
  import {Language, LanguageCallback, Value} from '../types'
9
+ import {getLanguagesFieldOption} from '../utils/getLanguagesFieldOption'
9
10
 
10
11
  type ArrayFactoryConfig = {
11
12
  apiVersion: string
@@ -15,6 +16,11 @@ type ArrayFactoryConfig = {
15
16
  type: string | FieldDefinition
16
17
  }
17
18
 
19
+ export type ArrayFieldOptions = Pick<
20
+ ArrayFactoryConfig,
21
+ 'apiVersion' | 'select' | 'languages'
22
+ >
23
+
18
24
  export default (config: ArrayFactoryConfig): FieldDefinition<'array'> => {
19
25
  const {apiVersion, select, languages, type} = config
20
26
  const typeName = typeof type === `string` ? type : type.name
@@ -47,13 +53,17 @@ export default (config: ArrayFactoryConfig): FieldDefinition<'array'> => {
47
53
 
48
54
  const selectedValue = getSelectedValue(select, context.document)
49
55
  const client = context.getClient({apiVersion})
50
- const contextLanguages: Language[] = Array.isArray(
51
- context?.type?.options?.languages
52
- )
53
- ? context!.type!.options.languages
54
- : Array.isArray(peek(selectedValue))
55
- ? peek(selectedValue)
56
- : await context?.type?.options.languages(client, selectedValue)
56
+
57
+ let contextLanguages: Language[] = []
58
+ const languagesFieldOption = getLanguagesFieldOption(context?.type)
59
+
60
+ if (Array.isArray(languagesFieldOption)) {
61
+ contextLanguages = languagesFieldOption
62
+ } else if (Array.isArray(peek(selectedValue))) {
63
+ contextLanguages = peek(selectedValue) || []
64
+ } else if (typeof languagesFieldOption === 'function') {
65
+ contextLanguages = await languagesFieldOption(client, selectedValue)
66
+ }
57
67
 
58
68
  if (value && value.length > contextLanguages.length) {
59
69
  return `Cannot be more than ${
@@ -0,0 +1,16 @@
1
+ import {SchemaType} from 'sanity'
2
+
3
+ import {ArrayFieldOptions} from '../schema/array'
4
+
5
+ export function getLanguagesFieldOption(
6
+ schemaType: SchemaType | undefined
7
+ ): ArrayFieldOptions['languages'] | undefined {
8
+ if (!schemaType) {
9
+ return undefined
10
+ }
11
+ const languagesOption = (schemaType.options as ArrayFieldOptions)?.languages
12
+ if (languagesOption) {
13
+ return languagesOption
14
+ }
15
+ return getLanguagesFieldOption(schemaType.type)
16
+ }