@sanity/personalization-plugin 2.1.0-field-names.1 → 2.1.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 +73 -19
- package/dist/index.js +10 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +10 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/components/ExperimentField.tsx +0 -1
- package/src/components/VariantInput.tsx +15 -68
- package/src/fieldExperiments.tsx +2 -0
package/package.json
CHANGED
|
@@ -1,71 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
useFormValue,
|
|
10
|
-
} from 'sanity'
|
|
11
|
-
|
|
12
|
-
import {VariantType} from '../types'
|
|
13
|
-
import {useExperimentContext} from './ExperimentContext'
|
|
14
|
-
import {Select} from './Select'
|
|
15
|
-
|
|
16
|
-
const formatlistOptions = (varants: VariantType[]) =>
|
|
17
|
-
varants.map((variant) => ({
|
|
18
|
-
title: variant.label,
|
|
19
|
-
value: variant.id,
|
|
20
|
-
}))
|
|
21
|
-
|
|
22
|
-
export const VariantInput = (props: StringInputProps) => {
|
|
23
|
-
const experimentPath = props.path.slice(0, -3)
|
|
24
|
-
|
|
25
|
-
const experimentValue = useFormValue([...experimentPath, 'experimentValue'])
|
|
26
|
-
|
|
27
|
-
const id = useFormValue(['_id']) as string
|
|
28
|
-
|
|
29
|
-
const {patch} = useDocumentOperation(id.replace('drafts.', ''), props.schemaType.name)
|
|
30
|
-
|
|
31
|
-
const {experiments} = useExperimentContext()
|
|
32
|
-
|
|
33
|
-
const handleChange = useCallback(
|
|
34
|
-
(
|
|
35
|
-
event: FormEvent<Element>,
|
|
36
|
-
onChange: (patchchange: FormPatch | FormPatch[] | PatchEvent) => void,
|
|
37
|
-
) => {
|
|
38
|
-
const target = event.currentTarget as HTMLSelectElement
|
|
39
|
-
const inputValue = target.value
|
|
40
|
-
const variantExperimentId = props.id.replace('variantId', 'experimentId')
|
|
41
|
-
|
|
42
|
-
if (inputValue) {
|
|
43
|
-
onChange(set(inputValue))
|
|
44
|
-
const patchEvent = {
|
|
45
|
-
set: {[variantExperimentId]: experimentValue},
|
|
46
|
-
}
|
|
47
|
-
patch.execute([patchEvent])
|
|
48
|
-
} else {
|
|
49
|
-
onChange(unset())
|
|
50
|
-
const patchEvent = {
|
|
51
|
-
unset: [variantExperimentId],
|
|
52
|
-
}
|
|
53
|
-
patch.execute([patchEvent])
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
[experimentValue, patch, props.id],
|
|
57
|
-
)
|
|
58
|
-
|
|
59
|
-
const filteredVariants =
|
|
60
|
-
experiments.find((option) => {
|
|
61
|
-
return option.id === experimentValue
|
|
62
|
-
})?.variants || []
|
|
63
|
-
|
|
1
|
+
import {Button, Inline, Stack} from '@sanity/ui'
|
|
2
|
+
import {ObjectInputProps, set, useFormValue} from 'sanity'
|
|
3
|
+
|
|
4
|
+
export const VariantInput = (props: ObjectInputProps) => {
|
|
5
|
+
const defaultValue = useFormValue([props.path[0], 'default'])
|
|
6
|
+
const handleClick = () => {
|
|
7
|
+
props.onChange(set(defaultValue, ['value']))
|
|
8
|
+
}
|
|
64
9
|
return (
|
|
65
|
-
<
|
|
66
|
-
{
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
10
|
+
<Stack space={3}>
|
|
11
|
+
{props.renderDefault(props)}
|
|
12
|
+
|
|
13
|
+
<Inline space={1}>
|
|
14
|
+
<Button text="Copy default" mode="ghost" onClick={() => handleClick()} />
|
|
15
|
+
</Inline>
|
|
16
|
+
</Stack>
|
|
70
17
|
)
|
|
71
18
|
}
|
package/src/fieldExperiments.tsx
CHANGED
|
@@ -11,6 +11,7 @@ import {ArrayInput} from './components/Array'
|
|
|
11
11
|
import {CONFIG_DEFAULT, ExperimentProvider} from './components/ExperimentContext'
|
|
12
12
|
import {ExperimentField} from './components/ExperimentField'
|
|
13
13
|
import {ExperimentInput} from './components/ExperimentInput'
|
|
14
|
+
import {VariantInput} from './components/VariantInput'
|
|
14
15
|
import {VariantPreview} from './components/VariantPreview'
|
|
15
16
|
import {FieldPluginConfig} from './types'
|
|
16
17
|
import {flattenSchemaType} from './utils/flattenSchemaType'
|
|
@@ -121,6 +122,7 @@ const createVariantType = ({
|
|
|
121
122
|
type: 'object',
|
|
122
123
|
components: {
|
|
123
124
|
preview: VariantPreview,
|
|
125
|
+
input: VariantInput,
|
|
124
126
|
},
|
|
125
127
|
fields: [
|
|
126
128
|
{
|