@madebywild/sanity-reference-collection-field 1.0.0 → 1.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 +32 -6
- package/dist/index.cjs +35 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +36 -11
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.tsx +33 -10
- package/src/input.tsx +1 -1
- package/src/types.tsx +2 -2
package/README.md
CHANGED
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
|
|
6
6
|
Sanity field for selecting and configuring predefined reference collections.
|
|
7
7
|
|
|
8
|
-
|
|
9
8
|
## Install
|
|
10
9
|
|
|
11
10
|
```bash
|
|
@@ -15,12 +14,17 @@ pnpm add @madebywild/sanity-reference-collection-field
|
|
|
15
14
|
## Configure Plugin
|
|
16
15
|
|
|
17
16
|
```ts
|
|
17
|
+
import { defineConfig } from "sanity";
|
|
18
18
|
import { wildSanityReferenceCollectionFieldPlugin } from "@madebywild/sanity-reference-collection-field";
|
|
19
19
|
|
|
20
20
|
export default defineConfig({
|
|
21
21
|
plugins: [
|
|
22
22
|
wildSanityReferenceCollectionFieldPlugin({
|
|
23
|
-
referenceKinds: [
|
|
23
|
+
referenceKinds: [
|
|
24
|
+
"post",
|
|
25
|
+
{ title: "Projects", value: "project" },
|
|
26
|
+
{ title: "People", value: "person" },
|
|
27
|
+
],
|
|
24
28
|
}),
|
|
25
29
|
],
|
|
26
30
|
});
|
|
@@ -29,16 +33,38 @@ export default defineConfig({
|
|
|
29
33
|
## Use in Schema
|
|
30
34
|
|
|
31
35
|
```ts
|
|
36
|
+
import { defineField } from "sanity";
|
|
37
|
+
|
|
32
38
|
defineField({
|
|
33
39
|
name: "feed",
|
|
34
40
|
type: "wild.referenceCollection",
|
|
35
41
|
options: {
|
|
36
|
-
|
|
37
|
-
referenceKinds: ["post", "project"],
|
|
42
|
+
inline: true,
|
|
43
|
+
referenceKinds: ["post", { title: "Featured Projects", value: "project" }],
|
|
38
44
|
extensions: {
|
|
39
|
-
|
|
40
|
-
|
|
45
|
+
// each supports `true` or { enabled: true/false }
|
|
46
|
+
filtering: {
|
|
47
|
+
enabled: true,
|
|
48
|
+
filterByKinds: ["post", "project"],
|
|
49
|
+
},
|
|
50
|
+
pagination: {
|
|
51
|
+
enabled: true,
|
|
52
|
+
pageCount: 12,
|
|
53
|
+
},
|
|
41
54
|
},
|
|
42
55
|
},
|
|
43
56
|
});
|
|
44
57
|
```
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
// Example editor value shape
|
|
61
|
+
{
|
|
62
|
+
collectionRef: "post",
|
|
63
|
+
options: {
|
|
64
|
+
withFilters: true,
|
|
65
|
+
filterByKinds: ["post", "project"],
|
|
66
|
+
withPagination: true,
|
|
67
|
+
pageCount: 12,
|
|
68
|
+
},
|
|
69
|
+
}
|
|
70
|
+
```
|
package/dist/index.cjs
CHANGED
|
@@ -54,7 +54,7 @@ function CollectionOptionsInput(props) {
|
|
|
54
54
|
let t0;
|
|
55
55
|
$[0] !== extensions || $[1] !== props ? (t0 = props.members.map((member) => member.kind !== "field" || !(extensions && isExtensionEnabled(extensions, "filtering")) && member.name === "withFilters" || !(extensions && isExtensionEnabled(extensions, "pagination")) && member.name === "withPagination" ? null : /* @__PURE__ */ jsxRuntime.jsx(sanity.MemberField, { member, ...props }, member.key)), $[0] = extensions, $[1] = props, $[2] = t0) : t0 = $[2];
|
|
56
56
|
let t1;
|
|
57
|
-
return $[3] !== t0 ? (t1 = /* @__PURE__ */ jsxRuntime.jsx(ui.Stack, { space:
|
|
57
|
+
return $[3] !== t0 ? (t1 = /* @__PURE__ */ jsxRuntime.jsx(ui.Stack, { space: 4, children: t0 }), $[3] = t0, $[4] = t1) : t1 = $[4], t1;
|
|
58
58
|
}
|
|
59
59
|
function CollectionRefInput(t0) {
|
|
60
60
|
const $ = compilerRuntime.c(14);
|
|
@@ -98,7 +98,7 @@ const typeName = "wild.referenceCollection", wildSanityReferenceCollectionFieldP
|
|
|
98
98
|
name: typeName,
|
|
99
99
|
type: "object",
|
|
100
100
|
title: "Reference Collection",
|
|
101
|
-
description: "
|
|
101
|
+
description: "Choose a content type and control how the collection is displayed.",
|
|
102
102
|
icon: () => /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: "\u{1F503}" }),
|
|
103
103
|
validation: (R) => R.custom((value) => value?.collectionRef ? !0 : {
|
|
104
104
|
message: "Select a reference.",
|
|
@@ -111,8 +111,8 @@ const typeName = "wild.referenceCollection", wildSanityReferenceCollectionFieldP
|
|
|
111
111
|
fields: [sanity.defineField({
|
|
112
112
|
name: "collectionRef",
|
|
113
113
|
type: "string",
|
|
114
|
-
title: "
|
|
115
|
-
description: "
|
|
114
|
+
title: "Collection Type",
|
|
115
|
+
description: "Choose which type of content this collection should show.",
|
|
116
116
|
components: {
|
|
117
117
|
input: (props) => /* @__PURE__ */ jsxRuntime.jsx(CollectionRefInput, { pluginConfig: config, ...props })
|
|
118
118
|
},
|
|
@@ -123,8 +123,8 @@ const typeName = "wild.referenceCollection", wildSanityReferenceCollectionFieldP
|
|
|
123
123
|
}), sanity.defineField({
|
|
124
124
|
type: "object",
|
|
125
125
|
name: "options",
|
|
126
|
-
title: "
|
|
127
|
-
description: "
|
|
126
|
+
title: "Display Settings",
|
|
127
|
+
description: "Control filters and pagination for this collection.",
|
|
128
128
|
options: {
|
|
129
129
|
collapsed: !1,
|
|
130
130
|
collapsible: !1
|
|
@@ -136,21 +136,46 @@ const typeName = "wild.referenceCollection", wildSanityReferenceCollectionFieldP
|
|
|
136
136
|
fields: [sanity.defineField({
|
|
137
137
|
name: "withFilters",
|
|
138
138
|
type: "boolean",
|
|
139
|
-
title: "
|
|
140
|
-
description: "
|
|
139
|
+
title: "Enable Filters",
|
|
140
|
+
description: "Show filter controls for this collection.",
|
|
141
141
|
initialValue: !1,
|
|
142
142
|
options: {
|
|
143
143
|
layout: "switch"
|
|
144
144
|
}
|
|
145
|
+
}), sanity.defineField({
|
|
146
|
+
name: "filterByKinds",
|
|
147
|
+
type: "array",
|
|
148
|
+
title: "Available Filter Types",
|
|
149
|
+
description: "Choose which content types appear as filter options.",
|
|
150
|
+
hidden: ({
|
|
151
|
+
parent
|
|
152
|
+
}) => !parent?.withFilters,
|
|
153
|
+
of: [sanity.defineArrayMember({
|
|
154
|
+
type: "string"
|
|
155
|
+
})],
|
|
156
|
+
options: {
|
|
157
|
+
layout: "tags"
|
|
158
|
+
},
|
|
159
|
+
validation: (R) => R.unique()
|
|
145
160
|
}), sanity.defineField({
|
|
146
161
|
name: "withPagination",
|
|
147
162
|
type: "boolean",
|
|
148
|
-
title: "
|
|
149
|
-
description: "
|
|
163
|
+
title: "Enable Pagination",
|
|
164
|
+
description: "Split collection results across multiple pages.",
|
|
150
165
|
initialValue: !1,
|
|
151
166
|
options: {
|
|
152
167
|
layout: "switch"
|
|
153
168
|
}
|
|
169
|
+
}), sanity.defineField({
|
|
170
|
+
name: "pageCount",
|
|
171
|
+
type: "number",
|
|
172
|
+
title: "Items Per Page",
|
|
173
|
+
description: "Set how many items to show on each page.",
|
|
174
|
+
hidden: ({
|
|
175
|
+
parent
|
|
176
|
+
}) => !parent?.withPagination,
|
|
177
|
+
initialValue: 12,
|
|
178
|
+
validation: (R) => R.integer().positive()
|
|
154
179
|
})]
|
|
155
180
|
})],
|
|
156
181
|
preview: {
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/input.tsx","../src/types.tsx","../src/index.tsx"],"sourcesContent":["import { CompactObjectInput } from \"@madebywild/sanity-utils/compact-object-input\";\nimport { Stack } from \"@sanity/ui\";\nimport * as React from \"react\";\nimport type { ObjectFieldProps } from \"sanity\";\nimport { MemberField, type ObjectInputProps, type ObjectMember, type StringInputProps } from \"sanity\";\nimport type { CollectionReference, FieldOptions, PluginConfig, ReferenceCollectionExtensions } from \"./types\";\n\nfunction isExtensionEnabled(extensions: ReferenceCollectionExtensions, key: keyof ReferenceCollectionExtensions) {\n return extensions[key] && (extensions[key] === true || extensions[key].enabled !== false);\n}\n\nconst FieldContext = React.createContext<{ options?: FieldOptions }>({});\nconst useFieldCtx = () => React.useContext(FieldContext);\n\nfunction normalizeCollection(preset: CollectionReference) {\n return typeof preset === \"string\" ? { title: preset, value: preset } : preset;\n}\n\nfunction CollectionFieldInput(props: ObjectInputProps) {\n return (\n <FieldContext.Provider value={{ options: props.schemaType.options as FieldOptions | undefined }}>\n <CompactObjectInput space={4} {...props} />\n </FieldContext.Provider>\n );\n}\n\nfunction CollectionOptionsField(props: ObjectFieldProps) {\n const { options } = useFieldCtx();\n // We can hide the field if no extensions are enabled.\n if (!options?.extensions || !Object.keys(options?.extensions).length) return null;\n return <>{props.children}</>;\n}\n\nfunction CollectionOptionsInput(props: ObjectInputProps) {\n const { options } = useFieldCtx();\n const extensions = options?.extensions;\n\n return (\n <Stack space={2}>\n {props.members.map((member: ObjectMember) => {\n if (member.kind !== \"field\") return null;\n\n // Custom filtering extension.\n const filteringEnabled = extensions && isExtensionEnabled(extensions, \"filtering\");\n if (!filteringEnabled && member.name === \"withFilters\") return null;\n\n // Custom pagination extension.\n const paginationEnabled = extensions && isExtensionEnabled(extensions, \"pagination\");\n if (!paginationEnabled && member.name === \"withPagination\") return null;\n\n return <MemberField key={member.key} member={member} {...props} />;\n })}\n </Stack>\n );\n}\n\nfunction CollectionRefInput({\n pluginConfig,\n ...props\n}: StringInputProps & {\n pluginConfig?: PluginConfig;\n}) {\n const patchedSchemaType = React.useMemo(() => {\n // Local options take precedence over plugin config.\n const options = props.schemaType.options as FieldOptions | undefined;\n const references = options?.referenceKinds ?? pluginConfig?.referenceKinds;\n\n // No need to touch anything if the list is unchanged.\n if (!references?.length) return props.schemaType;\n\n return {\n ...props.schemaType,\n options: {\n ...props.schemaType.options,\n list: references.map(normalizeCollection),\n },\n };\n }, [props.schemaType]);\n\n return props.renderDefault({\n ...props,\n schemaType: patchedSchemaType,\n });\n}\n\nexport { CollectionRefInput, CollectionFieldInput, CollectionOptionsField, CollectionOptionsInput };\n","import type { ObjectDefinition, ObjectOptions } from \"sanity\";\n\n/** @public */\nexport const typeName = \"wild.referenceCollection\" as const;\n\n/** @public */\nexport type CollectionReference = string | { title: string; value: string };\n\n/** @public */\nexport type ReferenceCollectionExtensions = Partial<{\n /** Allows filtering the references. */\n filtering: boolean | { enabled?: boolean };\n /** Allows paginating the references. */\n pagination: boolean | { enabled?: boolean };\n}>;\n\n/** @public */\nexport type PluginConfig = {\n /**\n * A list of references the user can choose from.\n * Can be overridden per-field via `options.referenceKinds`.\n * @example\n * ```ts\n * referenceKinds: [\"blogPost\", \"some-type\"],\n * ```\n */\n referenceKinds: CollectionReference[];\n};\n\n/** @public */\nexport type FieldOptions = ObjectOptions & {\n /**\n * Whether the media field should be displayed inline.\n * This will render all children inline on the same level.\n */\n inline?: boolean;\n /**\n * A list of references the user can choose from.\n * Will take precedence over the plugin-level `referenceKinds`.\n * @example\n * ```ts\n * referenceKinds: [\"blogPost\", \"some-type\"],\n * ```\n */\n referenceKinds?: CollectionReference[];\n /**\n * List of extension that add additional behaviors to the link field.\n */\n extensions?: ReferenceCollectionExtensions;\n};\n\n// Add the custom field definition to Sanity's intrinsic definitions\n// so that type checking works correctly when using this field type.\ndeclare module \"sanity\" {\n export interface IntrinsicDefinitions {\n [typeName]: Omit<ObjectDefinition, \"type\" | \"fields\" | \"options\" | \"components\"> & {\n type: typeof typeName;\n options?: FieldOptions;\n };\n }\n}\n","import { defineField, definePlugin, defineType } from \"sanity\";\nimport { CollectionFieldInput, CollectionOptionsField, CollectionOptionsInput, CollectionRefInput } from \"./input\";\nimport {\n type CollectionReference,\n type FieldOptions,\n type PluginConfig,\n type ReferenceCollectionExtensions,\n typeName,\n} from \"./types\";\n\n/** @public */\nconst wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((config) => {\n return {\n name: \"@madebywild/sanity-reference-collection-field\",\n schema: {\n types: [\n defineType({\n name: typeName,\n type: \"object\",\n title: \"Reference Collection\",\n description: \"Create a collection of specific kinds.\",\n icon: () => <>🔃</>,\n validation: (R) => {\n return R.custom((value) => {\n return !value?.collectionRef ? { message: \"Select a reference.\", path: [\"collectionRef\"] } : true;\n });\n },\n components: {\n field: (props) => (props.schemaType.options.inline ? props.children : props.renderDefault(props)),\n input: (props) => <CollectionFieldInput {...props} />,\n },\n fields: [\n defineField({\n name: \"collectionRef\",\n type: \"string\",\n title: \"Reference\",\n description: \"Select the kind of references for the collection to display.\",\n components: {\n input: (props) => <CollectionRefInput pluginConfig={config} {...props} />,\n },\n options: {\n layout: \"dropdown\",\n list: config.referenceKinds,\n },\n }),\n defineField({\n type: \"object\",\n name: \"options\",\n title: \"Options\",\n description: \"Configure how to render the collection.\",\n options: {\n collapsed: false,\n collapsible: false,\n },\n components: {\n field: (props) => <CollectionOptionsField {...props} />,\n input: (props) => <CollectionOptionsInput {...props} />,\n },\n fields: [\n defineField({\n name: \"withFilters\",\n type: \"boolean\",\n title: \"With Filters\",\n description: \"Enable filters for the collection.\",\n initialValue: false,\n options: { layout: \"switch\" },\n }),\n defineField({\n name: \"withPagination\",\n type: \"boolean\",\n title: \"With Pagination\",\n description: \"Enable pagination for the collection.\",\n initialValue: false,\n options: { layout: \"switch\" },\n }),\n ],\n }),\n ],\n preview: {\n select: {\n collection: \"collectionRef\",\n },\n prepare({ collection }) {\n return {\n title: \"Reference Collection\",\n subtitle: collection ? `Reference to ${collection}` : undefined,\n };\n },\n },\n }),\n ],\n },\n };\n});\n\nexport {\n wildSanityReferenceCollectionFieldPlugin,\n typeName,\n type ReferenceCollectionExtensions,\n type PluginConfig,\n type FieldOptions,\n type CollectionReference,\n};\n"],"names":["isExtensionEnabled","extensions","key","enabled","FieldContext","React","createContext","useFieldCtx","useContext","normalizeCollection","preset","title","value","CollectionFieldInput","props","$","_c","t0","schemaType","options","t1","t2","jsx","CompactObjectInput","t3","CollectionOptionsField","Object","keys","length","children","CollectionOptionsInput","members","map","member","kind","name","MemberField","Stack","CollectionRefInput","pluginConfig","bb0","references","referenceKinds","t4","t5","list","t6","patchedSchemaType","renderDefault","typeName","wildSanityReferenceCollectionFieldPlugin","definePlugin","config","schema","types","defineType","type","description","icon","Fragment","validation","R","custom","collectionRef","message","path","components","field","inline","input","fields","defineField","layout","collapsed","collapsible","initialValue","preview","select","collection","prepare","subtitle","undefined"],"mappings":";;;;;;;;;;;;;;;;;;;AAOA,SAASA,mBAAmBC,YAA2CC,KAA0C;AAC/G,SAAOD,WAAWC,GAAG,MAAMD,WAAWC,GAAG,MAAM,MAAQD,WAAWC,GAAG,EAAEC,YAAY;AACrF;AAEA,MAAMC,eAAeC,iBAAMC,cAA0C,CAAA,CAAE,GACjEC,cAAcA,MAAMF,iBAAKG,WAAYJ,YAAY;AAEvD,SAASK,oBAAoBC,QAA6B;AACxD,SAAO,OAAOA,UAAW,WAAW;AAAA,IAAEC,OAAOD;AAAAA,IAAQE,OAAOF;AAAAA,EAAAA,IAAWA;AACzE;AAEA,SAAAG,qBAAAC,OAAA;AAAA,QAAAC,IAAAC,gBAAAA,EAAA,CAAA,GAE6CC,KAAAH,MAAKI,WAAWC;AAAoC,MAAAC;AAAAL,WAAAE,MAA/DG,KAAA;AAAA,IAAAD,SAAWF;AAAAA,EAAAA,GAAsDF,OAAAE,IAAAF,OAAAK,MAAAA,KAAAL,EAAA,CAAA;AAAA,MAAAM;AAAAN,WAAAD,SAC7FO,KAAAC,+BAACC,mBAAAA,oBAAA,EAA0B,OAAA,GAAC,GAAMT,MAAAA,CAAK,GAAIC,OAAAD,OAAAC,OAAAM,MAAAA,KAAAN,EAAA,CAAA;AAAA,MAAAS;AAAA,SAAAT,EAAA,CAAA,MAAAK,MAAAL,SAAAM,MAD7CG,KAAAF,2BAAAA,IAAA,aAAA,UAAA,EAA8B,OAAAF,IAC5BC,UAAAA,GAAAA,CACF,GAAwBN,O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AO;AAAA,QACP8C,aAAa;AAAA,QACbtC,SAAS;AAAA,UACPsD,WAAW;AAAA,UACXC,aAAa;AAAA,QAAA;AAAA,QAEfR,YAAY;AAAA,UACVC,OAAQrD,CAAAA,UAAUQ,2BAAAA,IAAC,wBAAA,EAAuB,GAAIR,OAAM;AAAA,UACpDuD,OAAQvD,CAAAA,UAAUQ,2BAAAA,IAAC,wBAAA,EAAuB,GAAIR,MAAAA,CAAM;AAAA,QAAA;AAAA,QAEtDwD,QAAQ,CACNC,OAAAA,YAAY;AAAA,UACVpC,MAAM;AAAA,UACNqB,MAAM;AAAA,UACN7C,OAAO;AAAA,UACP8C,aAAa;AAAA,UACbkB,cAAc;AAAA,UACdxD,SAAS;AAAA,YAAEqD,QAAQ;AAAA,UAAA;AAAA,QAAS,CAC7B,GACDD,OAAAA,YAAY;AAAA,UACVpC,MAAM;AAAA,UACNqB,MAAM;AAAA,UACN7C,OAAO;AAAA,UACP8C,aAAa;AAAA,UACbkB,cAAc;AAAA,UACdxD,SAAS;AAAA,YAAEqD,QAAQ;AAAA,UAAA;AAAA,QAAS,CAC7B,CAAC;AAAA,MAAA,CAEL,CAAC;AAAA,MAEJI,SAAS;AAAA,QACPC,QAAQ;AAAA,UACNC,YAAY;AAAA,QAAA;AAAA,QAEdC,QAAQ;AAAA,UAAED;AAAAA,QAAAA,GAAc;AACtB,iBAAO;AAAA,YACLnE,OAAO;AAAA,YACPqE,UAAUF,aAAa,gBAAgBA,UAAU,KAAKG;AAAAA,UAAAA;AAAAA,QAE1D;AAAA,MAAA;AAAA,IACF,CACD,CAAC;AAAA,EAAA;AAGR,EACD;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/input.tsx","../src/types.tsx","../src/index.tsx"],"sourcesContent":["import { CompactObjectInput } from \"@madebywild/sanity-utils/compact-object-input\";\nimport { Stack } from \"@sanity/ui\";\nimport * as React from \"react\";\nimport type { ObjectFieldProps } from \"sanity\";\nimport { MemberField, type ObjectInputProps, type ObjectMember, type StringInputProps } from \"sanity\";\nimport type { CollectionReference, FieldOptions, PluginConfig, ReferenceCollectionExtensions } from \"./types\";\n\nfunction isExtensionEnabled(extensions: ReferenceCollectionExtensions, key: keyof ReferenceCollectionExtensions) {\n return extensions[key] && (extensions[key] === true || extensions[key].enabled !== false);\n}\n\nconst FieldContext = React.createContext<{ options?: FieldOptions }>({});\nconst useFieldCtx = () => React.useContext(FieldContext);\n\nfunction normalizeCollection(preset: CollectionReference) {\n return typeof preset === \"string\" ? { title: preset, value: preset } : preset;\n}\n\nfunction CollectionFieldInput(props: ObjectInputProps) {\n return (\n <FieldContext.Provider value={{ options: props.schemaType.options as FieldOptions | undefined }}>\n <CompactObjectInput space={4} {...props} />\n </FieldContext.Provider>\n );\n}\n\nfunction CollectionOptionsField(props: ObjectFieldProps) {\n const { options } = useFieldCtx();\n // We can hide the field if no extensions are enabled.\n if (!options?.extensions || !Object.keys(options?.extensions).length) return null;\n return <>{props.children}</>;\n}\n\nfunction CollectionOptionsInput(props: ObjectInputProps) {\n const { options } = useFieldCtx();\n const extensions = options?.extensions;\n\n return (\n <Stack space={4}>\n {props.members.map((member: ObjectMember) => {\n if (member.kind !== \"field\") return null;\n\n // Custom filtering extension.\n const filteringEnabled = extensions && isExtensionEnabled(extensions, \"filtering\");\n if (!filteringEnabled && member.name === \"withFilters\") return null;\n\n // Custom pagination extension.\n const paginationEnabled = extensions && isExtensionEnabled(extensions, \"pagination\");\n if (!paginationEnabled && member.name === \"withPagination\") return null;\n\n return <MemberField key={member.key} member={member} {...props} />;\n })}\n </Stack>\n );\n}\n\nfunction CollectionRefInput({\n pluginConfig,\n ...props\n}: StringInputProps & {\n pluginConfig?: PluginConfig;\n}) {\n const patchedSchemaType = React.useMemo(() => {\n // Local options take precedence over plugin config.\n const options = props.schemaType.options as FieldOptions | undefined;\n const references = options?.referenceKinds ?? pluginConfig?.referenceKinds;\n\n // No need to touch anything if the list is unchanged.\n if (!references?.length) return props.schemaType;\n\n return {\n ...props.schemaType,\n options: {\n ...props.schemaType.options,\n list: references.map(normalizeCollection),\n },\n };\n }, [props.schemaType]);\n\n return props.renderDefault({\n ...props,\n schemaType: patchedSchemaType,\n });\n}\n\nexport { CollectionRefInput, CollectionFieldInput, CollectionOptionsField, CollectionOptionsInput };\n","import type { ObjectDefinition, ObjectOptions } from \"sanity\";\n\n/** @public */\nexport const typeName = \"wild.referenceCollection\" as const;\n\n/** @public */\nexport type CollectionReference = string | { title: string; value: string };\n\n/** @public */\nexport type ReferenceCollectionExtensions = Partial<{\n /** Allows filtering the references. */\n filtering: boolean | { enabled?: boolean; filterByKinds?: string[] };\n /** Allows paginating the references. */\n pagination: boolean | { enabled?: boolean; pageCount?: number };\n}>;\n\n/** @public */\nexport type PluginConfig = {\n /**\n * A list of references the user can choose from.\n * Can be overridden per-field via `options.referenceKinds`.\n * @example\n * ```ts\n * referenceKinds: [\"blogPost\", \"some-type\"],\n * ```\n */\n referenceKinds: CollectionReference[];\n};\n\n/** @public */\nexport type FieldOptions = ObjectOptions & {\n /**\n * Whether the media field should be displayed inline.\n * This will render all children inline on the same level.\n */\n inline?: boolean;\n /**\n * A list of references the user can choose from.\n * Will take precedence over the plugin-level `referenceKinds`.\n * @example\n * ```ts\n * referenceKinds: [\"blogPost\", \"some-type\"],\n * ```\n */\n referenceKinds?: CollectionReference[];\n /**\n * List of extension that add additional behaviors to the link field.\n */\n extensions?: ReferenceCollectionExtensions;\n};\n\n// Add the custom field definition to Sanity's intrinsic definitions\n// so that type checking works correctly when using this field type.\ndeclare module \"sanity\" {\n export interface IntrinsicDefinitions {\n [typeName]: Omit<ObjectDefinition, \"type\" | \"fields\" | \"options\" | \"components\"> & {\n type: typeof typeName;\n options?: FieldOptions;\n };\n }\n}\n","import { defineArrayMember, defineField, definePlugin, defineType } from \"sanity\";\nimport { CollectionFieldInput, CollectionOptionsField, CollectionOptionsInput, CollectionRefInput } from \"./input\";\nimport {\n type CollectionReference,\n type FieldOptions,\n type PluginConfig,\n type ReferenceCollectionExtensions,\n typeName,\n} from \"./types\";\n\n/** @public */\nconst wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((config) => {\n return {\n name: \"@madebywild/sanity-reference-collection-field\",\n schema: {\n types: [\n defineType({\n name: typeName,\n type: \"object\",\n title: \"Reference Collection\",\n description: \"Choose a content type and control how the collection is displayed.\",\n icon: () => <>🔃</>,\n validation: (R) => {\n return R.custom((value) => {\n return !value?.collectionRef ? { message: \"Select a reference.\", path: [\"collectionRef\"] } : true;\n });\n },\n components: {\n field: (props) => (props.schemaType.options.inline ? props.children : props.renderDefault(props)),\n input: (props) => <CollectionFieldInput {...props} />,\n },\n fields: [\n defineField({\n name: \"collectionRef\",\n type: \"string\",\n title: \"Collection Type\",\n description: \"Choose which type of content this collection should show.\",\n components: {\n input: (props) => <CollectionRefInput pluginConfig={config} {...props} />,\n },\n options: {\n layout: \"dropdown\",\n list: config.referenceKinds,\n },\n }),\n defineField({\n type: \"object\",\n name: \"options\",\n title: \"Display Settings\",\n description: \"Control filters and pagination for this collection.\",\n options: {\n collapsed: false,\n collapsible: false,\n },\n components: {\n field: (props) => <CollectionOptionsField {...props} />,\n input: (props) => <CollectionOptionsInput {...props} />,\n },\n fields: [\n defineField({\n name: \"withFilters\",\n type: \"boolean\",\n title: \"Enable Filters\",\n description: \"Show filter controls for this collection.\",\n initialValue: false,\n options: { layout: \"switch\" },\n }),\n defineField({\n name: \"filterByKinds\",\n type: \"array\",\n title: \"Available Filter Types\",\n description: \"Choose which content types appear as filter options.\",\n hidden: ({ parent }) => !parent?.withFilters,\n of: [\n defineArrayMember({\n type: \"string\",\n }),\n ],\n options: { layout: \"tags\" },\n validation: (R) => R.unique(),\n }),\n defineField({\n name: \"withPagination\",\n type: \"boolean\",\n title: \"Enable Pagination\",\n description: \"Split collection results across multiple pages.\",\n initialValue: false,\n options: { layout: \"switch\" },\n }),\n defineField({\n name: \"pageCount\",\n type: \"number\",\n title: \"Items Per Page\",\n description: \"Set how many items to show on each page.\",\n hidden: ({ parent }) => !parent?.withPagination,\n initialValue: 12,\n validation: (R) => R.integer().positive(),\n }),\n ],\n }),\n ],\n preview: {\n select: {\n collection: \"collectionRef\",\n },\n prepare({ collection }) {\n return {\n title: \"Reference Collection\",\n subtitle: collection ? `Reference to ${collection}` : undefined,\n };\n },\n },\n }),\n ],\n },\n };\n});\n\nexport {\n wildSanityReferenceCollectionFieldPlugin,\n typeName,\n type ReferenceCollectionExtensions,\n type PluginConfig,\n type FieldOptions,\n type CollectionReference,\n};\n"],"names":["isExtensionEnabled","extensions","key","enabled","FieldContext","React","createContext","useFieldCtx","useContext","normalizeCollection","preset","title","value","CollectionFieldInput","props","$","_c","t0","schemaType","options","t1","t2","jsx","CompactObjectInput","t3","CollectionOptionsField","Object","keys","length","children","CollectionOptionsInput","members","map","member","kind","name","MemberField","Stack","CollectionRefInput","pluginConfig","bb0","references","referenceKinds","t4","t5","list","t6","patchedSchemaType","renderDefault","typeName","wildSanityReferenceCollectionFieldPlugin","definePlugin","config","schema","types","defineType","type","description","icon","Fragment","validation","R","custom","collectionRef","message","path","components","field","inline","input","fields","defineField","layout","collapsed","collapsible","initialValue","hidden","parent","withFilters","of","defineArrayMember","unique","withPagination","integer","positive","preview","select","collection","prepare","subtitle","undefined"],"mappings":";;;;;;;;;;;;;;;;;;;AAOA,SAASA,mBAAmBC,YAA2CC,KAA0C;AAC/G,SAAOD,WAAWC,GAAG,MAAMD,WAAWC,GAAG,MAAM,MAAQD,WAAWC,GAAG,EAAEC,YAAY;AACrF;AAEA,MAAMC,eAAeC,iBAAMC,cAA0C,CAAA,CAAE,GACjEC,cAAcA,MAAMF,iBAAKG,WAAYJ,YAAY;AAEvD,SAASK,oBAAoBC,QAA6B;AACxD,SAAO,OAAOA,UAAW,WAAW;AAAA,IAAEC,OAAOD;AAAAA,IAAQE,OAAOF;AAAAA,EAAAA,IAAWA;AACzE;AAEA,SAAAG,qBAAAC,OAAA;AAAA,QAAAC,IAAAC,gBAAAA,EAAA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ACP,CAAC;AAAA,UAEJrC,SAAS;AAAA,YAAEqD,QAAQ;AAAA,UAAA;AAAA,UACnBZ,YAAaC,CAAAA,MAAMA,EAAEoB,OAAAA;AAAAA,QAAO,CAC7B,GACDV,OAAAA,YAAY;AAAA,UACVpC,MAAM;AAAA,UACNqB,MAAM;AAAA,UACN7C,OAAO;AAAA,UACP8C,aAAa;AAAA,UACbkB,cAAc;AAAA,UACdxD,SAAS;AAAA,YAAEqD,QAAQ;AAAA,UAAA;AAAA,QAAS,CAC7B,GACDD,OAAAA,YAAY;AAAA,UACVpC,MAAM;AAAA,UACNqB,MAAM;AAAA,UACN7C,OAAO;AAAA,UACP8C,aAAa;AAAA,UACbmB,QAAQA,CAAC;AAAA,YAAEC;AAAAA,UAAAA,MAAa,CAACA,QAAQK;AAAAA,UACjCP,cAAc;AAAA,UACdf,YAAaC,CAAAA,MAAMA,EAAEsB,QAAAA,EAAUC,SAAAA;AAAAA,QAAS,CACzC,CAAC;AAAA,MAAA,CAEL,CAAC;AAAA,MAEJC,SAAS;AAAA,QACPC,QAAQ;AAAA,UACNC,YAAY;AAAA,QAAA;AAAA,QAEdC,QAAQ;AAAA,UAAED;AAAAA,QAAAA,GAAc;AACtB,iBAAO;AAAA,YACL5E,OAAO;AAAA,YACP8E,UAAUF,aAAa,gBAAgBA,UAAU,KAAKG;AAAAA,UAAAA;AAAAA,QAE1D;AAAA,MAAA;AAAA,IACF,CACD,CAAC;AAAA,EAAA;AAGR,EACD;;;"}
|
package/dist/index.d.cts
CHANGED
|
@@ -12,10 +12,12 @@ type ReferenceCollectionExtensions = Partial<{
|
|
|
12
12
|
/** Allows filtering the references. */
|
|
13
13
|
filtering: boolean | {
|
|
14
14
|
enabled?: boolean;
|
|
15
|
+
filterByKinds?: string[];
|
|
15
16
|
};
|
|
16
17
|
/** Allows paginating the references. */
|
|
17
18
|
pagination: boolean | {
|
|
18
19
|
enabled?: boolean;
|
|
20
|
+
pageCount?: number;
|
|
19
21
|
};
|
|
20
22
|
}>;
|
|
21
23
|
/** @public */
|
package/dist/index.d.ts
CHANGED
|
@@ -12,10 +12,12 @@ type ReferenceCollectionExtensions = Partial<{
|
|
|
12
12
|
/** Allows filtering the references. */
|
|
13
13
|
filtering: boolean | {
|
|
14
14
|
enabled?: boolean;
|
|
15
|
+
filterByKinds?: string[];
|
|
15
16
|
};
|
|
16
17
|
/** Allows paginating the references. */
|
|
17
18
|
pagination: boolean | {
|
|
18
19
|
enabled?: boolean;
|
|
20
|
+
pageCount?: number;
|
|
19
21
|
};
|
|
20
22
|
}>;
|
|
21
23
|
/** @public */
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx, Fragment } from "react/jsx-runtime";
|
|
2
|
-
import { MemberField, definePlugin, defineType, defineField } from "sanity";
|
|
2
|
+
import { MemberField, definePlugin, defineType, defineField, defineArrayMember } from "sanity";
|
|
3
3
|
import { c } from "react/compiler-runtime";
|
|
4
4
|
import { CompactObjectInput } from "@madebywild/sanity-utils/compact-object-input";
|
|
5
5
|
import { Stack } from "@sanity/ui";
|
|
@@ -41,7 +41,7 @@ function CollectionOptionsInput(props) {
|
|
|
41
41
|
let t0;
|
|
42
42
|
$[0] !== extensions || $[1] !== props ? (t0 = props.members.map((member) => member.kind !== "field" || !(extensions && isExtensionEnabled(extensions, "filtering")) && member.name === "withFilters" || !(extensions && isExtensionEnabled(extensions, "pagination")) && member.name === "withPagination" ? null : /* @__PURE__ */ jsx(MemberField, { member, ...props }, member.key)), $[0] = extensions, $[1] = props, $[2] = t0) : t0 = $[2];
|
|
43
43
|
let t1;
|
|
44
|
-
return $[3] !== t0 ? (t1 = /* @__PURE__ */ jsx(Stack, { space:
|
|
44
|
+
return $[3] !== t0 ? (t1 = /* @__PURE__ */ jsx(Stack, { space: 4, children: t0 }), $[3] = t0, $[4] = t1) : t1 = $[4], t1;
|
|
45
45
|
}
|
|
46
46
|
function CollectionRefInput(t0) {
|
|
47
47
|
const $ = c(14);
|
|
@@ -85,7 +85,7 @@ const typeName = "wild.referenceCollection", wildSanityReferenceCollectionFieldP
|
|
|
85
85
|
name: typeName,
|
|
86
86
|
type: "object",
|
|
87
87
|
title: "Reference Collection",
|
|
88
|
-
description: "
|
|
88
|
+
description: "Choose a content type and control how the collection is displayed.",
|
|
89
89
|
icon: () => /* @__PURE__ */ jsx(Fragment, { children: "\u{1F503}" }),
|
|
90
90
|
validation: (R) => R.custom((value) => value?.collectionRef ? !0 : {
|
|
91
91
|
message: "Select a reference.",
|
|
@@ -98,8 +98,8 @@ const typeName = "wild.referenceCollection", wildSanityReferenceCollectionFieldP
|
|
|
98
98
|
fields: [defineField({
|
|
99
99
|
name: "collectionRef",
|
|
100
100
|
type: "string",
|
|
101
|
-
title: "
|
|
102
|
-
description: "
|
|
101
|
+
title: "Collection Type",
|
|
102
|
+
description: "Choose which type of content this collection should show.",
|
|
103
103
|
components: {
|
|
104
104
|
input: (props) => /* @__PURE__ */ jsx(CollectionRefInput, { pluginConfig: config, ...props })
|
|
105
105
|
},
|
|
@@ -110,8 +110,8 @@ const typeName = "wild.referenceCollection", wildSanityReferenceCollectionFieldP
|
|
|
110
110
|
}), defineField({
|
|
111
111
|
type: "object",
|
|
112
112
|
name: "options",
|
|
113
|
-
title: "
|
|
114
|
-
description: "
|
|
113
|
+
title: "Display Settings",
|
|
114
|
+
description: "Control filters and pagination for this collection.",
|
|
115
115
|
options: {
|
|
116
116
|
collapsed: !1,
|
|
117
117
|
collapsible: !1
|
|
@@ -123,21 +123,46 @@ const typeName = "wild.referenceCollection", wildSanityReferenceCollectionFieldP
|
|
|
123
123
|
fields: [defineField({
|
|
124
124
|
name: "withFilters",
|
|
125
125
|
type: "boolean",
|
|
126
|
-
title: "
|
|
127
|
-
description: "
|
|
126
|
+
title: "Enable Filters",
|
|
127
|
+
description: "Show filter controls for this collection.",
|
|
128
128
|
initialValue: !1,
|
|
129
129
|
options: {
|
|
130
130
|
layout: "switch"
|
|
131
131
|
}
|
|
132
|
+
}), defineField({
|
|
133
|
+
name: "filterByKinds",
|
|
134
|
+
type: "array",
|
|
135
|
+
title: "Available Filter Types",
|
|
136
|
+
description: "Choose which content types appear as filter options.",
|
|
137
|
+
hidden: ({
|
|
138
|
+
parent
|
|
139
|
+
}) => !parent?.withFilters,
|
|
140
|
+
of: [defineArrayMember({
|
|
141
|
+
type: "string"
|
|
142
|
+
})],
|
|
143
|
+
options: {
|
|
144
|
+
layout: "tags"
|
|
145
|
+
},
|
|
146
|
+
validation: (R) => R.unique()
|
|
132
147
|
}), defineField({
|
|
133
148
|
name: "withPagination",
|
|
134
149
|
type: "boolean",
|
|
135
|
-
title: "
|
|
136
|
-
description: "
|
|
150
|
+
title: "Enable Pagination",
|
|
151
|
+
description: "Split collection results across multiple pages.",
|
|
137
152
|
initialValue: !1,
|
|
138
153
|
options: {
|
|
139
154
|
layout: "switch"
|
|
140
155
|
}
|
|
156
|
+
}), defineField({
|
|
157
|
+
name: "pageCount",
|
|
158
|
+
type: "number",
|
|
159
|
+
title: "Items Per Page",
|
|
160
|
+
description: "Set how many items to show on each page.",
|
|
161
|
+
hidden: ({
|
|
162
|
+
parent
|
|
163
|
+
}) => !parent?.withPagination,
|
|
164
|
+
initialValue: 12,
|
|
165
|
+
validation: (R) => R.integer().positive()
|
|
141
166
|
})]
|
|
142
167
|
})],
|
|
143
168
|
preview: {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/input.tsx","../src/types.tsx","../src/index.tsx"],"sourcesContent":["import { CompactObjectInput } from \"@madebywild/sanity-utils/compact-object-input\";\nimport { Stack } from \"@sanity/ui\";\nimport * as React from \"react\";\nimport type { ObjectFieldProps } from \"sanity\";\nimport { MemberField, type ObjectInputProps, type ObjectMember, type StringInputProps } from \"sanity\";\nimport type { CollectionReference, FieldOptions, PluginConfig, ReferenceCollectionExtensions } from \"./types\";\n\nfunction isExtensionEnabled(extensions: ReferenceCollectionExtensions, key: keyof ReferenceCollectionExtensions) {\n return extensions[key] && (extensions[key] === true || extensions[key].enabled !== false);\n}\n\nconst FieldContext = React.createContext<{ options?: FieldOptions }>({});\nconst useFieldCtx = () => React.useContext(FieldContext);\n\nfunction normalizeCollection(preset: CollectionReference) {\n return typeof preset === \"string\" ? { title: preset, value: preset } : preset;\n}\n\nfunction CollectionFieldInput(props: ObjectInputProps) {\n return (\n <FieldContext.Provider value={{ options: props.schemaType.options as FieldOptions | undefined }}>\n <CompactObjectInput space={4} {...props} />\n </FieldContext.Provider>\n );\n}\n\nfunction CollectionOptionsField(props: ObjectFieldProps) {\n const { options } = useFieldCtx();\n // We can hide the field if no extensions are enabled.\n if (!options?.extensions || !Object.keys(options?.extensions).length) return null;\n return <>{props.children}</>;\n}\n\nfunction CollectionOptionsInput(props: ObjectInputProps) {\n const { options } = useFieldCtx();\n const extensions = options?.extensions;\n\n return (\n <Stack space={2}>\n {props.members.map((member: ObjectMember) => {\n if (member.kind !== \"field\") return null;\n\n // Custom filtering extension.\n const filteringEnabled = extensions && isExtensionEnabled(extensions, \"filtering\");\n if (!filteringEnabled && member.name === \"withFilters\") return null;\n\n // Custom pagination extension.\n const paginationEnabled = extensions && isExtensionEnabled(extensions, \"pagination\");\n if (!paginationEnabled && member.name === \"withPagination\") return null;\n\n return <MemberField key={member.key} member={member} {...props} />;\n })}\n </Stack>\n );\n}\n\nfunction CollectionRefInput({\n pluginConfig,\n ...props\n}: StringInputProps & {\n pluginConfig?: PluginConfig;\n}) {\n const patchedSchemaType = React.useMemo(() => {\n // Local options take precedence over plugin config.\n const options = props.schemaType.options as FieldOptions | undefined;\n const references = options?.referenceKinds ?? pluginConfig?.referenceKinds;\n\n // No need to touch anything if the list is unchanged.\n if (!references?.length) return props.schemaType;\n\n return {\n ...props.schemaType,\n options: {\n ...props.schemaType.options,\n list: references.map(normalizeCollection),\n },\n };\n }, [props.schemaType]);\n\n return props.renderDefault({\n ...props,\n schemaType: patchedSchemaType,\n });\n}\n\nexport { CollectionRefInput, CollectionFieldInput, CollectionOptionsField, CollectionOptionsInput };\n","import type { ObjectDefinition, ObjectOptions } from \"sanity\";\n\n/** @public */\nexport const typeName = \"wild.referenceCollection\" as const;\n\n/** @public */\nexport type CollectionReference = string | { title: string; value: string };\n\n/** @public */\nexport type ReferenceCollectionExtensions = Partial<{\n /** Allows filtering the references. */\n filtering: boolean | { enabled?: boolean };\n /** Allows paginating the references. */\n pagination: boolean | { enabled?: boolean };\n}>;\n\n/** @public */\nexport type PluginConfig = {\n /**\n * A list of references the user can choose from.\n * Can be overridden per-field via `options.referenceKinds`.\n * @example\n * ```ts\n * referenceKinds: [\"blogPost\", \"some-type\"],\n * ```\n */\n referenceKinds: CollectionReference[];\n};\n\n/** @public */\nexport type FieldOptions = ObjectOptions & {\n /**\n * Whether the media field should be displayed inline.\n * This will render all children inline on the same level.\n */\n inline?: boolean;\n /**\n * A list of references the user can choose from.\n * Will take precedence over the plugin-level `referenceKinds`.\n * @example\n * ```ts\n * referenceKinds: [\"blogPost\", \"some-type\"],\n * ```\n */\n referenceKinds?: CollectionReference[];\n /**\n * List of extension that add additional behaviors to the link field.\n */\n extensions?: ReferenceCollectionExtensions;\n};\n\n// Add the custom field definition to Sanity's intrinsic definitions\n// so that type checking works correctly when using this field type.\ndeclare module \"sanity\" {\n export interface IntrinsicDefinitions {\n [typeName]: Omit<ObjectDefinition, \"type\" | \"fields\" | \"options\" | \"components\"> & {\n type: typeof typeName;\n options?: FieldOptions;\n };\n }\n}\n","import { defineField, definePlugin, defineType } from \"sanity\";\nimport { CollectionFieldInput, CollectionOptionsField, CollectionOptionsInput, CollectionRefInput } from \"./input\";\nimport {\n type CollectionReference,\n type FieldOptions,\n type PluginConfig,\n type ReferenceCollectionExtensions,\n typeName,\n} from \"./types\";\n\n/** @public */\nconst wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((config) => {\n return {\n name: \"@madebywild/sanity-reference-collection-field\",\n schema: {\n types: [\n defineType({\n name: typeName,\n type: \"object\",\n title: \"Reference Collection\",\n description: \"Create a collection of specific kinds.\",\n icon: () => <>🔃</>,\n validation: (R) => {\n return R.custom((value) => {\n return !value?.collectionRef ? { message: \"Select a reference.\", path: [\"collectionRef\"] } : true;\n });\n },\n components: {\n field: (props) => (props.schemaType.options.inline ? props.children : props.renderDefault(props)),\n input: (props) => <CollectionFieldInput {...props} />,\n },\n fields: [\n defineField({\n name: \"collectionRef\",\n type: \"string\",\n title: \"Reference\",\n description: \"Select the kind of references for the collection to display.\",\n components: {\n input: (props) => <CollectionRefInput pluginConfig={config} {...props} />,\n },\n options: {\n layout: \"dropdown\",\n list: config.referenceKinds,\n },\n }),\n defineField({\n type: \"object\",\n name: \"options\",\n title: \"Options\",\n description: \"Configure how to render the collection.\",\n options: {\n collapsed: false,\n collapsible: false,\n },\n components: {\n field: (props) => <CollectionOptionsField {...props} />,\n input: (props) => <CollectionOptionsInput {...props} />,\n },\n fields: [\n defineField({\n name: \"withFilters\",\n type: \"boolean\",\n title: \"With Filters\",\n description: \"Enable filters for the collection.\",\n initialValue: false,\n options: { layout: \"switch\" },\n }),\n defineField({\n name: \"withPagination\",\n type: \"boolean\",\n title: \"With Pagination\",\n description: \"Enable pagination for the collection.\",\n initialValue: false,\n options: { layout: \"switch\" },\n }),\n ],\n }),\n ],\n preview: {\n select: {\n collection: \"collectionRef\",\n },\n prepare({ collection }) {\n return {\n title: \"Reference Collection\",\n subtitle: collection ? `Reference to ${collection}` : undefined,\n };\n },\n },\n }),\n ],\n },\n };\n});\n\nexport {\n wildSanityReferenceCollectionFieldPlugin,\n typeName,\n type ReferenceCollectionExtensions,\n type PluginConfig,\n type FieldOptions,\n type CollectionReference,\n};\n"],"names":["isExtensionEnabled","extensions","key","enabled","FieldContext","React","createContext","useFieldCtx","useContext","normalizeCollection","preset","title","value","CollectionFieldInput","props","$","_c","t0","schemaType","options","t1","t2","t3","CollectionOptionsField","Object","keys","length","children","CollectionOptionsInput","members","map","member","kind","name","CollectionRefInput","pluginConfig","bb0","references","referenceKinds","t4","t5","list","t6","patchedSchemaType","renderDefault","typeName","wildSanityReferenceCollectionFieldPlugin","definePlugin","config","schema","types","defineType","type","description","icon","validation","R","custom","collectionRef","message","path","components","field","inline","input","fields","defineField","layout","collapsed","collapsible","initialValue","preview","select","collection","prepare","subtitle","undefined"],"mappings":";;;;;;AAOA,SAASA,mBAAmBC,YAA2CC,KAA0C;AAC/G,SAAOD,WAAWC,GAAG,MAAMD,WAAWC,GAAG,MAAM,MAAQD,WAAWC,GAAG,EAAEC,YAAY;AACrF;AAEA,MAAMC,eAAeC,MAAMC,cAA0C,CAAA,CAAE,GACjEC,cAAcA,MAAMF,MAAKG,WAAYJ,YAAY;AAEvD,SAASK,oBAAoBC,QAA6B;AACxD,SAAO,OAAOA,UAAW,WAAW;AAAA,IAAEC,OAAOD;AAAAA,IAAQE,OAAOF;AAAAA,EAAAA,IAAWA;AACzE;AAEA,SAAAG,qBAAAC,OAAA;AAAA,QAAAC,IAAAC,EAAA,CAAA,GAE6CC,KAAAH,MAAKI,WAAWC;AAAoC,MAAAC;AAAAL,WAAAE,MAA/DG,KAAA;AAAA,IAAAD,SAAWF;AAAAA,EAAAA,GAAsDF,OAAAE,IAAAF,OAAAK,MAAAA,KAAAL,EAAA,CAAA;AAAA,MAAAM;AAAAN,WAAAD,SAC7FO,KAAA,oBAAC,oBAAA,EAA0B,OAAA,GAAC,GAAMP,MAAAA,CAAK,GAAIC,OAAAD,OAAAC,OAAAM,MAAAA,KAAAN,EAAA,CAAA;AAAA,MAAAO;AAAA,SAAAP,EAAA,CAAA,MAAAK,MAAAL,SAAAM,MAD7CC,KAAA,oBAAA,aAAA,UAAA,EAA8B,OAAAF,IAC5BC,UAAAA,GAAAA,CACF,GAAwBN,OAAAK,IAAAL,OAAAM,IAAAN,OAAAO,MAAAA,KAAAP,EAAA,CAAA,GAFxBO;AAEwB;AAI5B,SAAAC,uBAAAT,OAAA;AAAA,QAAAC,IAAAC,EAAA,CAAA,GACE;AA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GAAIA,MAAAA,CAAM;AAAA,QAAA;AAAA,QAEtDmD,QAAQ,CACNC,YAAY;AAAA,UACVjC,MAAM;AAAA,UACNmB,MAAM;AAAA,UACNzC,OAAO;AAAA,UACP0C,aAAa;AAAA,UACbiB,cAAc;AAAA,UACdnD,SAAS;AAAA,YAAEgD,QAAQ;AAAA,UAAA;AAAA,QAAS,CAC7B,GACDD,YAAY;AAAA,UACVjC,MAAM;AAAA,UACNmB,MAAM;AAAA,UACNzC,OAAO;AAAA,UACP0C,aAAa;AAAA,UACbiB,cAAc;AAAA,UACdnD,SAAS;AAAA,YAAEgD,QAAQ;AAAA,UAAA;AAAA,QAAS,CAC7B,CAAC;AAAA,MAAA,CAEL,CAAC;AAAA,MAEJI,SAAS;AAAA,QACPC,QAAQ;AAAA,UACNC,YAAY;AAAA,QAAA;AAAA,QAEdC,QAAQ;AAAA,UAAED;AAAAA,QAAAA,GAAc;AACtB,iBAAO;AAAA,YACL9D,OAAO;AAAA,YACPgE,UAAUF,aAAa,gBAAgBA,UAAU,KAAKG;AAAAA,UAAAA;AAAAA,QAE1D;AAAA,MAAA;AAAA,IACF,CACD,CAAC;AAAA,EAAA;AAGR,EACD;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/input.tsx","../src/types.tsx","../src/index.tsx"],"sourcesContent":["import { CompactObjectInput } from \"@madebywild/sanity-utils/compact-object-input\";\nimport { Stack } from \"@sanity/ui\";\nimport * as React from \"react\";\nimport type { ObjectFieldProps } from \"sanity\";\nimport { MemberField, type ObjectInputProps, type ObjectMember, type StringInputProps } from \"sanity\";\nimport type { CollectionReference, FieldOptions, PluginConfig, ReferenceCollectionExtensions } from \"./types\";\n\nfunction isExtensionEnabled(extensions: ReferenceCollectionExtensions, key: keyof ReferenceCollectionExtensions) {\n return extensions[key] && (extensions[key] === true || extensions[key].enabled !== false);\n}\n\nconst FieldContext = React.createContext<{ options?: FieldOptions }>({});\nconst useFieldCtx = () => React.useContext(FieldContext);\n\nfunction normalizeCollection(preset: CollectionReference) {\n return typeof preset === \"string\" ? { title: preset, value: preset } : preset;\n}\n\nfunction CollectionFieldInput(props: ObjectInputProps) {\n return (\n <FieldContext.Provider value={{ options: props.schemaType.options as FieldOptions | undefined }}>\n <CompactObjectInput space={4} {...props} />\n </FieldContext.Provider>\n );\n}\n\nfunction CollectionOptionsField(props: ObjectFieldProps) {\n const { options } = useFieldCtx();\n // We can hide the field if no extensions are enabled.\n if (!options?.extensions || !Object.keys(options?.extensions).length) return null;\n return <>{props.children}</>;\n}\n\nfunction CollectionOptionsInput(props: ObjectInputProps) {\n const { options } = useFieldCtx();\n const extensions = options?.extensions;\n\n return (\n <Stack space={4}>\n {props.members.map((member: ObjectMember) => {\n if (member.kind !== \"field\") return null;\n\n // Custom filtering extension.\n const filteringEnabled = extensions && isExtensionEnabled(extensions, \"filtering\");\n if (!filteringEnabled && member.name === \"withFilters\") return null;\n\n // Custom pagination extension.\n const paginationEnabled = extensions && isExtensionEnabled(extensions, \"pagination\");\n if (!paginationEnabled && member.name === \"withPagination\") return null;\n\n return <MemberField key={member.key} member={member} {...props} />;\n })}\n </Stack>\n );\n}\n\nfunction CollectionRefInput({\n pluginConfig,\n ...props\n}: StringInputProps & {\n pluginConfig?: PluginConfig;\n}) {\n const patchedSchemaType = React.useMemo(() => {\n // Local options take precedence over plugin config.\n const options = props.schemaType.options as FieldOptions | undefined;\n const references = options?.referenceKinds ?? pluginConfig?.referenceKinds;\n\n // No need to touch anything if the list is unchanged.\n if (!references?.length) return props.schemaType;\n\n return {\n ...props.schemaType,\n options: {\n ...props.schemaType.options,\n list: references.map(normalizeCollection),\n },\n };\n }, [props.schemaType]);\n\n return props.renderDefault({\n ...props,\n schemaType: patchedSchemaType,\n });\n}\n\nexport { CollectionRefInput, CollectionFieldInput, CollectionOptionsField, CollectionOptionsInput };\n","import type { ObjectDefinition, ObjectOptions } from \"sanity\";\n\n/** @public */\nexport const typeName = \"wild.referenceCollection\" as const;\n\n/** @public */\nexport type CollectionReference = string | { title: string; value: string };\n\n/** @public */\nexport type ReferenceCollectionExtensions = Partial<{\n /** Allows filtering the references. */\n filtering: boolean | { enabled?: boolean; filterByKinds?: string[] };\n /** Allows paginating the references. */\n pagination: boolean | { enabled?: boolean; pageCount?: number };\n}>;\n\n/** @public */\nexport type PluginConfig = {\n /**\n * A list of references the user can choose from.\n * Can be overridden per-field via `options.referenceKinds`.\n * @example\n * ```ts\n * referenceKinds: [\"blogPost\", \"some-type\"],\n * ```\n */\n referenceKinds: CollectionReference[];\n};\n\n/** @public */\nexport type FieldOptions = ObjectOptions & {\n /**\n * Whether the media field should be displayed inline.\n * This will render all children inline on the same level.\n */\n inline?: boolean;\n /**\n * A list of references the user can choose from.\n * Will take precedence over the plugin-level `referenceKinds`.\n * @example\n * ```ts\n * referenceKinds: [\"blogPost\", \"some-type\"],\n * ```\n */\n referenceKinds?: CollectionReference[];\n /**\n * List of extension that add additional behaviors to the link field.\n */\n extensions?: ReferenceCollectionExtensions;\n};\n\n// Add the custom field definition to Sanity's intrinsic definitions\n// so that type checking works correctly when using this field type.\ndeclare module \"sanity\" {\n export interface IntrinsicDefinitions {\n [typeName]: Omit<ObjectDefinition, \"type\" | \"fields\" | \"options\" | \"components\"> & {\n type: typeof typeName;\n options?: FieldOptions;\n };\n }\n}\n","import { defineArrayMember, defineField, definePlugin, defineType } from \"sanity\";\nimport { CollectionFieldInput, CollectionOptionsField, CollectionOptionsInput, CollectionRefInput } from \"./input\";\nimport {\n type CollectionReference,\n type FieldOptions,\n type PluginConfig,\n type ReferenceCollectionExtensions,\n typeName,\n} from \"./types\";\n\n/** @public */\nconst wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((config) => {\n return {\n name: \"@madebywild/sanity-reference-collection-field\",\n schema: {\n types: [\n defineType({\n name: typeName,\n type: \"object\",\n title: \"Reference Collection\",\n description: \"Choose a content type and control how the collection is displayed.\",\n icon: () => <>🔃</>,\n validation: (R) => {\n return R.custom((value) => {\n return !value?.collectionRef ? { message: \"Select a reference.\", path: [\"collectionRef\"] } : true;\n });\n },\n components: {\n field: (props) => (props.schemaType.options.inline ? props.children : props.renderDefault(props)),\n input: (props) => <CollectionFieldInput {...props} />,\n },\n fields: [\n defineField({\n name: \"collectionRef\",\n type: \"string\",\n title: \"Collection Type\",\n description: \"Choose which type of content this collection should show.\",\n components: {\n input: (props) => <CollectionRefInput pluginConfig={config} {...props} />,\n },\n options: {\n layout: \"dropdown\",\n list: config.referenceKinds,\n },\n }),\n defineField({\n type: \"object\",\n name: \"options\",\n title: \"Display Settings\",\n description: \"Control filters and pagination for this collection.\",\n options: {\n collapsed: false,\n collapsible: false,\n },\n components: {\n field: (props) => <CollectionOptionsField {...props} />,\n input: (props) => <CollectionOptionsInput {...props} />,\n },\n fields: [\n defineField({\n name: \"withFilters\",\n type: \"boolean\",\n title: \"Enable Filters\",\n description: \"Show filter controls for this collection.\",\n initialValue: false,\n options: { layout: \"switch\" },\n }),\n defineField({\n name: \"filterByKinds\",\n type: \"array\",\n title: \"Available Filter Types\",\n description: \"Choose which content types appear as filter options.\",\n hidden: ({ parent }) => !parent?.withFilters,\n of: [\n defineArrayMember({\n type: \"string\",\n }),\n ],\n options: { layout: \"tags\" },\n validation: (R) => R.unique(),\n }),\n defineField({\n name: \"withPagination\",\n type: \"boolean\",\n title: \"Enable Pagination\",\n description: \"Split collection results across multiple pages.\",\n initialValue: false,\n options: { layout: \"switch\" },\n }),\n defineField({\n name: \"pageCount\",\n type: \"number\",\n title: \"Items Per Page\",\n description: \"Set how many items to show on each page.\",\n hidden: ({ parent }) => !parent?.withPagination,\n initialValue: 12,\n validation: (R) => R.integer().positive(),\n }),\n ],\n }),\n ],\n preview: {\n select: {\n collection: \"collectionRef\",\n },\n prepare({ collection }) {\n return {\n title: \"Reference Collection\",\n subtitle: collection ? `Reference to ${collection}` : undefined,\n };\n },\n },\n }),\n ],\n },\n };\n});\n\nexport {\n wildSanityReferenceCollectionFieldPlugin,\n typeName,\n type ReferenceCollectionExtensions,\n type PluginConfig,\n type FieldOptions,\n type CollectionReference,\n};\n"],"names":["isExtensionEnabled","extensions","key","enabled","FieldContext","React","createContext","useFieldCtx","useContext","normalizeCollection","preset","title","value","CollectionFieldInput","props","$","_c","t0","schemaType","options","t1","t2","t3","CollectionOptionsField","Object","keys","length","children","CollectionOptionsInput","members","map","member","kind","name","CollectionRefInput","pluginConfig","bb0","references","referenceKinds","t4","t5","list","t6","patchedSchemaType","renderDefault","typeName","wildSanityReferenceCollectionFieldPlugin","definePlugin","config","schema","types","defineType","type","description","icon","validation","R","custom","collectionRef","message","path","components","field","inline","input","fields","defineField","layout","collapsed","collapsible","initialValue","hidden","parent","withFilters","of","defineArrayMember","unique","withPagination","integer","positive","preview","select","collection","prepare","subtitle","undefined"],"mappings":";;;;;;AAOA,SAASA,mBAAmBC,YAA2CC,KAA0C;AAC/G,SAAOD,WAAWC,GAAG,MAAMD,WAAWC,GAAG,MAAM,MAAQD,WAAWC,GAAG,EAAEC,YAAY;AACrF;AAEA,MAAMC,eAAeC,MAAMC,cAA0C,CAAA,CAAE,GACjEC,cAAcA,MAAMF,MAAKG,WAAYJ,YAAY;AAEvD,SAASK,oBAAoBC,QAA6B;AACxD,SAAO,OAAOA,UAAW,WAAW;AAAA,IAAEC,OAAOD;AAAAA,IAAQE,OAAOF;AAAAA,EAAAA,IAAWA;AACzE;AAEA,SAAAG,qBAAAC,OAAA;AAAA,QAAAC,IAAAC,EAAA,CAAA,GAE6CC,KAAAH,MAAKI,WAAWC;AAAoC,MAAAC;AAAAL,WAAAE,MAA/DG,KAAA;AAAA,IAAAD,SAAWF;AAAAA,EAAAA,GAAsDF,OAAAE,IAAAF,OAAAK,MAAAA,KAAAL,EAAA,CAAA;AAAA,MAAAM;AAAAN,WAAAD,SAC7FO,KAAA,oBAAC,oBAAA,EAA0B,OAAA,GAAC,GAAMP,MAAAA,CAAK,GAAIC,OAAAD,OAAAC,OAAAM,MAAAA,KAAAN,EAAA,CAAA;AAAA,MAAA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,UACjCP,cAAc;AAAA,UACdf,YAAaC,CAAAA,MAAMA,EAAEsB,QAAAA,EAAUC,SAAAA;AAAAA,QAAS,CACzC,CAAC;AAAA,MAAA,CAEL,CAAC;AAAA,MAEJC,SAAS;AAAA,QACPC,QAAQ;AAAA,UACNC,YAAY;AAAA,QAAA;AAAA,QAEdC,QAAQ;AAAA,UAAED;AAAAA,QAAAA,GAAc;AACtB,iBAAO;AAAA,YACLvE,OAAO;AAAA,YACPyE,UAAUF,aAAa,gBAAgBA,UAAU,KAAKG;AAAAA,UAAAA;AAAAA,QAE1D;AAAA,MAAA;AAAA,IACF,CACD,CAAC;AAAA,EAAA;AAGR,EACD;"}
|
package/package.json
CHANGED
package/src/index.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { defineField, definePlugin, defineType } from "sanity";
|
|
1
|
+
import { defineArrayMember, defineField, definePlugin, defineType } from "sanity";
|
|
2
2
|
import { CollectionFieldInput, CollectionOptionsField, CollectionOptionsInput, CollectionRefInput } from "./input";
|
|
3
3
|
import {
|
|
4
4
|
type CollectionReference,
|
|
@@ -18,7 +18,7 @@ const wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((con
|
|
|
18
18
|
name: typeName,
|
|
19
19
|
type: "object",
|
|
20
20
|
title: "Reference Collection",
|
|
21
|
-
description: "
|
|
21
|
+
description: "Choose a content type and control how the collection is displayed.",
|
|
22
22
|
icon: () => <>🔃</>,
|
|
23
23
|
validation: (R) => {
|
|
24
24
|
return R.custom((value) => {
|
|
@@ -33,8 +33,8 @@ const wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((con
|
|
|
33
33
|
defineField({
|
|
34
34
|
name: "collectionRef",
|
|
35
35
|
type: "string",
|
|
36
|
-
title: "
|
|
37
|
-
description: "
|
|
36
|
+
title: "Collection Type",
|
|
37
|
+
description: "Choose which type of content this collection should show.",
|
|
38
38
|
components: {
|
|
39
39
|
input: (props) => <CollectionRefInput pluginConfig={config} {...props} />,
|
|
40
40
|
},
|
|
@@ -46,8 +46,8 @@ const wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((con
|
|
|
46
46
|
defineField({
|
|
47
47
|
type: "object",
|
|
48
48
|
name: "options",
|
|
49
|
-
title: "
|
|
50
|
-
description: "
|
|
49
|
+
title: "Display Settings",
|
|
50
|
+
description: "Control filters and pagination for this collection.",
|
|
51
51
|
options: {
|
|
52
52
|
collapsed: false,
|
|
53
53
|
collapsible: false,
|
|
@@ -60,19 +60,42 @@ const wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((con
|
|
|
60
60
|
defineField({
|
|
61
61
|
name: "withFilters",
|
|
62
62
|
type: "boolean",
|
|
63
|
-
title: "
|
|
64
|
-
description: "
|
|
63
|
+
title: "Enable Filters",
|
|
64
|
+
description: "Show filter controls for this collection.",
|
|
65
65
|
initialValue: false,
|
|
66
66
|
options: { layout: "switch" },
|
|
67
67
|
}),
|
|
68
|
+
defineField({
|
|
69
|
+
name: "filterByKinds",
|
|
70
|
+
type: "array",
|
|
71
|
+
title: "Available Filter Types",
|
|
72
|
+
description: "Choose which content types appear as filter options.",
|
|
73
|
+
hidden: ({ parent }) => !parent?.withFilters,
|
|
74
|
+
of: [
|
|
75
|
+
defineArrayMember({
|
|
76
|
+
type: "string",
|
|
77
|
+
}),
|
|
78
|
+
],
|
|
79
|
+
options: { layout: "tags" },
|
|
80
|
+
validation: (R) => R.unique(),
|
|
81
|
+
}),
|
|
68
82
|
defineField({
|
|
69
83
|
name: "withPagination",
|
|
70
84
|
type: "boolean",
|
|
71
|
-
title: "
|
|
72
|
-
description: "
|
|
85
|
+
title: "Enable Pagination",
|
|
86
|
+
description: "Split collection results across multiple pages.",
|
|
73
87
|
initialValue: false,
|
|
74
88
|
options: { layout: "switch" },
|
|
75
89
|
}),
|
|
90
|
+
defineField({
|
|
91
|
+
name: "pageCount",
|
|
92
|
+
type: "number",
|
|
93
|
+
title: "Items Per Page",
|
|
94
|
+
description: "Set how many items to show on each page.",
|
|
95
|
+
hidden: ({ parent }) => !parent?.withPagination,
|
|
96
|
+
initialValue: 12,
|
|
97
|
+
validation: (R) => R.integer().positive(),
|
|
98
|
+
}),
|
|
76
99
|
],
|
|
77
100
|
}),
|
|
78
101
|
],
|
package/src/input.tsx
CHANGED
package/src/types.tsx
CHANGED
|
@@ -9,9 +9,9 @@ export type CollectionReference = string | { title: string; value: string };
|
|
|
9
9
|
/** @public */
|
|
10
10
|
export type ReferenceCollectionExtensions = Partial<{
|
|
11
11
|
/** Allows filtering the references. */
|
|
12
|
-
filtering: boolean | { enabled?: boolean };
|
|
12
|
+
filtering: boolean | { enabled?: boolean; filterByKinds?: string[] };
|
|
13
13
|
/** Allows paginating the references. */
|
|
14
|
-
pagination: boolean | { enabled?: boolean };
|
|
14
|
+
pagination: boolean | { enabled?: boolean; pageCount?: number };
|
|
15
15
|
}>;
|
|
16
16
|
|
|
17
17
|
/** @public */
|