sanity-plugin-internationalized-array 1.10.8 → 2.0.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/README.md +49 -0
- package/lib/index.d.mts +142 -0
- package/lib/index.esm.js +656 -9502
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +666 -9502
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +657 -0
- package/lib/index.mjs.map +1 -0
- package/package.json +34 -34
- package/src/components/AddButtons.tsx +2 -2
- package/src/components/DocumentAddButtons.tsx +5 -3
- package/src/components/InternationalizedArray.tsx +18 -24
- package/src/components/InternationalizedArrayContext.tsx +9 -3
- package/src/components/InternationalizedField.tsx +1 -1
- package/src/components/InternationalizedInput.tsx +4 -3
- package/src/components/getToneFromValidation.ts +2 -2
- package/src/fieldActions/index.ts +4 -4
- package/src/schema/array.ts +1 -3
- package/src/schema/object.ts +1 -4
- package/src/types.ts +1 -0
- package/src/utils/flattenSchemaType.ts +7 -3
package/README.md
CHANGED
|
@@ -8,6 +8,24 @@ A plugin to register array fields with a custom input component to store field v
|
|
|
8
8
|
|
|
9
9
|

|
|
10
10
|
|
|
11
|
+
- [sanity-plugin-internationalized-array](#sanity-plugin-internationalized-array)
|
|
12
|
+
- [Installation](#installation)
|
|
13
|
+
- [Usage for simple field types](#usage-for-simple-field-types)
|
|
14
|
+
- [Loading languages](#loading-languages)
|
|
15
|
+
- [Configuring the "Add translation" buttons](#configuring-the-add-translation-buttons)
|
|
16
|
+
- [Using complex field configurations](#using-complex-field-configurations)
|
|
17
|
+
- [Creating internationalized objects](#creating-internationalized-objects)
|
|
18
|
+
- [Validation of individual array items](#validation-of-individual-array-items)
|
|
19
|
+
- [Usage with @sanity/language-filter](#usage-with-sanitylanguage-filter)
|
|
20
|
+
- [Shape of stored data](#shape-of-stored-data)
|
|
21
|
+
- [Querying data](#querying-data)
|
|
22
|
+
- [Migrate from objects to arrays](#migrate-from-objects-to-arrays)
|
|
23
|
+
- [Why store localized field data like this?](#why-store-localized-field-data-like-this)
|
|
24
|
+
- [License](#license)
|
|
25
|
+
- [Develop \& test](#develop--test)
|
|
26
|
+
- [Release new version](#release-new-version)
|
|
27
|
+
- [License](#license-1)
|
|
28
|
+
|
|
11
29
|
## Installation
|
|
12
30
|
|
|
13
31
|
```
|
|
@@ -222,6 +240,37 @@ export default defineType({
|
|
|
222
240
|
})
|
|
223
241
|
```
|
|
224
242
|
|
|
243
|
+
## Validation of individual array items
|
|
244
|
+
|
|
245
|
+
You may wish to validate individual language fields for various reasons. From the internationalized array field, add a validation rule that can look through all the array items, and return item-specific validation messages at the path of that array item.
|
|
246
|
+
|
|
247
|
+
```ts
|
|
248
|
+
defineField({
|
|
249
|
+
name: 'title',
|
|
250
|
+
type: 'internationalizedArrayString',
|
|
251
|
+
description: `Use fewer than 5 words.`,
|
|
252
|
+
validation: (rule) =>
|
|
253
|
+
rule.custom<{value: string; _type: string; _key: string}[]>((value) => {
|
|
254
|
+
if (!value) {
|
|
255
|
+
return 'Title is required'
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const invalidItems = value.filter(
|
|
259
|
+
(item) => item.value.split(' ').length > 5,
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
if (invalidItems.length) {
|
|
263
|
+
return invalidItems.map((item) => ({
|
|
264
|
+
message: `Title is too long. Must be 5 words or fewer.`,
|
|
265
|
+
path: [{_key: item._key}, 'value'],
|
|
266
|
+
}))
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return true
|
|
270
|
+
}),
|
|
271
|
+
}),
|
|
272
|
+
```
|
|
273
|
+
|
|
225
274
|
## Usage with @sanity/language-filter
|
|
226
275
|
|
|
227
276
|
If you have many languages and authors that predominately write in only a few, [@sanity/language-filter](https://github.com/sanity-io/language-filter) can be used to reduce the number of language fields shown in the document form.
|
package/lib/index.d.mts
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import type {FieldDefinition} from 'sanity'
|
|
2
|
+
import {Plugin as Plugin_2} from 'sanity'
|
|
3
|
+
import type {Rule} from 'sanity'
|
|
4
|
+
import type {RuleTypeConstraint} from 'sanity'
|
|
5
|
+
import type {SanityClient} from 'sanity'
|
|
6
|
+
|
|
7
|
+
export declare type AllowedType =
|
|
8
|
+
| 'string'
|
|
9
|
+
| 'number'
|
|
10
|
+
| 'boolean'
|
|
11
|
+
| 'text'
|
|
12
|
+
| 'reference'
|
|
13
|
+
|
|
14
|
+
export declare type ArrayConfig = {
|
|
15
|
+
name: string
|
|
16
|
+
type: AllowedType
|
|
17
|
+
languages: Language[]
|
|
18
|
+
title?: string
|
|
19
|
+
group?: string
|
|
20
|
+
hidden?: boolean | (() => boolean)
|
|
21
|
+
readOnly?: boolean | (() => boolean)
|
|
22
|
+
validation?: Rule | Rule[]
|
|
23
|
+
field?: {
|
|
24
|
+
[key: string]: any
|
|
25
|
+
options: {
|
|
26
|
+
[key: string]: any
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export declare const clear: () => void
|
|
32
|
+
|
|
33
|
+
export declare const internationalizedArray: Plugin_2<PluginConfig>
|
|
34
|
+
|
|
35
|
+
export declare type Language = {
|
|
36
|
+
id: Intl.UnicodeBCP47LocaleIdentifier
|
|
37
|
+
title: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export declare type LanguageCallback = (
|
|
41
|
+
client: SanityClient,
|
|
42
|
+
selectedValue: Record<string, unknown>
|
|
43
|
+
) => Promise<Language[]>
|
|
44
|
+
|
|
45
|
+
export declare type PluginConfig = {
|
|
46
|
+
/**
|
|
47
|
+
* https://www.sanity.io/docs/api-versioning
|
|
48
|
+
* @defaultValue '2022-11-27'
|
|
49
|
+
*/
|
|
50
|
+
apiVersion?: string
|
|
51
|
+
/**
|
|
52
|
+
* Specify fields that should be available in the language callback:
|
|
53
|
+
* ```tsx
|
|
54
|
+
* {
|
|
55
|
+
* select: {
|
|
56
|
+
* markets: 'markets'
|
|
57
|
+
* },
|
|
58
|
+
* languages: (client, {markets}) =>
|
|
59
|
+
* query.fetch(groq`*[_type == "language" && market in $markets]{id,title}`, {markets})
|
|
60
|
+
* }
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
select?: Record<string, string>
|
|
64
|
+
/**
|
|
65
|
+
* You can give it an array of language definitions:
|
|
66
|
+
* ```tsx
|
|
67
|
+
* {
|
|
68
|
+
* languages: [
|
|
69
|
+
* {id: 'en', title: 'English'},
|
|
70
|
+
* {id: 'fr', title: 'French'}
|
|
71
|
+
* ]
|
|
72
|
+
* }
|
|
73
|
+
* ```
|
|
74
|
+
* You can load them async by passing a function that returns a promise:
|
|
75
|
+
* ```tsx
|
|
76
|
+
* {
|
|
77
|
+
* languages: async () => {
|
|
78
|
+
* const response = await fetch('https://example.com/languages')
|
|
79
|
+
* return response.json()
|
|
80
|
+
* }
|
|
81
|
+
* }
|
|
82
|
+
* ```
|
|
83
|
+
* You can query your dataset for languages::
|
|
84
|
+
* ```tsx
|
|
85
|
+
* {
|
|
86
|
+
* languages: (client) =>
|
|
87
|
+
* query.fetch(groq`*[_type == "language"]{id,title}`)
|
|
88
|
+
* }
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
languages: Language[] | LanguageCallback
|
|
92
|
+
/**
|
|
93
|
+
* You can specify a list of language IDs that should be pre-filled when creating a new document
|
|
94
|
+
* ```tsx
|
|
95
|
+
* {
|
|
96
|
+
* defaultLanguages: ['en']
|
|
97
|
+
* }
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
defaultLanguages?: string[]
|
|
101
|
+
/**
|
|
102
|
+
* Can be a string matching core field types, as well as custom ones:
|
|
103
|
+
* ```tsx
|
|
104
|
+
* {
|
|
105
|
+
* fieldTypes: [
|
|
106
|
+
* "date", "datetime", "file", "image", "number", "string", "text", "url"
|
|
107
|
+
* ]
|
|
108
|
+
* }
|
|
109
|
+
* ```
|
|
110
|
+
* You can also define a type directly:
|
|
111
|
+
* ```tsx
|
|
112
|
+
* {
|
|
113
|
+
* fieldTypes: [
|
|
114
|
+
* defineField({
|
|
115
|
+
* name: 'featuredProduct',
|
|
116
|
+
* type: 'reference',
|
|
117
|
+
* to: [{type: 'product'}]
|
|
118
|
+
* hidden: (({document}) => !document?.title)
|
|
119
|
+
* })
|
|
120
|
+
* ]
|
|
121
|
+
* }
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
fieldTypes: (string | RuleTypeConstraint | FieldDefinition)[]
|
|
125
|
+
/**
|
|
126
|
+
* Locations where the "+ EN" add language buttons are visible
|
|
127
|
+
* @defaultValue ['field']
|
|
128
|
+
* */
|
|
129
|
+
buttonLocations?: ('field' | 'unstable__fieldAction' | 'document')[]
|
|
130
|
+
/**
|
|
131
|
+
* Show or hide the "Add missing languages" button
|
|
132
|
+
* @defaultValue true
|
|
133
|
+
* */
|
|
134
|
+
buttonAddAll?: boolean
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export declare type Value = {
|
|
138
|
+
_key: string
|
|
139
|
+
value?: string
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export {}
|