@madebywild/sanity-reference-collection-field 0.0.1

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 ADDED
@@ -0,0 +1 @@
1
+ # @madebywild/sanity-reference-collection-field
package/dist/index.cjs ADDED
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: !0 });
3
+ var jsxRuntime = require("react/jsx-runtime"), sanity = require("sanity"), compilerRuntime = require("react/compiler-runtime");
4
+ function normalizeCollection(preset) {
5
+ return typeof preset == "string" ? {
6
+ title: preset,
7
+ value: preset
8
+ } : preset;
9
+ }
10
+ function CollectionRefInput(t0) {
11
+ const $ = compilerRuntime.c(14);
12
+ let pluginConfig, props;
13
+ $[0] !== t0 ? ({
14
+ pluginConfig,
15
+ ...props
16
+ } = t0, $[0] = t0, $[1] = pluginConfig, $[2] = props) : (pluginConfig = $[1], props = $[2]);
17
+ let t1;
18
+ bb0: {
19
+ const references = props.schemaType.options?.referenceKinds ?? pluginConfig?.referenceKinds;
20
+ if (!references?.length) {
21
+ t1 = props.schemaType;
22
+ break bb0;
23
+ }
24
+ const t22 = props.schemaType, t3 = props.schemaType.options;
25
+ let t4;
26
+ $[3] !== references ? (t4 = references.map(normalizeCollection), $[3] = references, $[4] = t4) : t4 = $[4];
27
+ let t5;
28
+ $[5] !== props.schemaType.options || $[6] !== t4 ? (t5 = {
29
+ ...t3,
30
+ list: t4
31
+ }, $[5] = props.schemaType.options, $[6] = t4, $[7] = t5) : t5 = $[7];
32
+ let t6;
33
+ $[8] !== props.schemaType || $[9] !== t5 ? (t6 = {
34
+ ...t22,
35
+ options: t5
36
+ }, $[8] = props.schemaType, $[9] = t5, $[10] = t6) : t6 = $[10], t1 = t6;
37
+ }
38
+ const patchedSchemaType = t1;
39
+ let t2;
40
+ return $[11] !== patchedSchemaType || $[12] !== props ? (t2 = props.renderDefault({
41
+ ...props,
42
+ schemaType: patchedSchemaType
43
+ }), $[11] = patchedSchemaType, $[12] = props, $[13] = t2) : t2 = $[13], t2;
44
+ }
45
+ const typeName = "wild.referenceCollection", wildSanityReferenceCollectionFieldPlugin = sanity.definePlugin((config) => ({
46
+ name: "@madebywild/sanity-reference-collection-field",
47
+ schema: {
48
+ types: [sanity.defineType({
49
+ name: typeName,
50
+ type: "object",
51
+ title: "Reference Collection",
52
+ description: "Create a collection of specific kinds.",
53
+ icon: () => /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: "\u{1F503}" }),
54
+ validation: (R) => R.custom((ctx) => ctx?.collectionRef ? !0 : {
55
+ message: "Select a reference.",
56
+ path: ["collectionRef"]
57
+ }),
58
+ fields: [sanity.defineField({
59
+ name: "collectionRef",
60
+ type: "string",
61
+ title: "Reference",
62
+ description: "Select the kind of references for the collection to display.",
63
+ components: {
64
+ input: (props) => /* @__PURE__ */ jsxRuntime.jsx(CollectionRefInput, { pluginConfig: config, ...props })
65
+ },
66
+ options: {
67
+ layout: "dropdown",
68
+ list: config.referenceKinds
69
+ }
70
+ })],
71
+ preview: {
72
+ select: {
73
+ collection: "collectionRef"
74
+ },
75
+ prepare({
76
+ collection
77
+ }) {
78
+ return {
79
+ title: "Reference Collection",
80
+ subtitle: collection ? `Reference to ${collection}` : void 0
81
+ };
82
+ }
83
+ }
84
+ })]
85
+ }
86
+ }));
87
+ exports.typeName = typeName;
88
+ exports.wildSanityReferenceCollectionFieldPlugin = wildSanityReferenceCollectionFieldPlugin;
89
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["../src/input.tsx","../src/types.tsx","../src/index.tsx"],"sourcesContent":["import * as React from \"react\";\nimport type { StringInputProps } from \"sanity\";\nimport type { CollectionReference, FieldOptions, PluginConfig } from \"./types\";\n\nfunction normalizeCollection(preset: CollectionReference) {\n return typeof preset === \"string\" ? { title: preset, value: preset } : preset;\n}\n\nfunction CollectionRefInput({\n pluginConfig,\n ...props\n}: StringInputProps & {\n // biome-ignore lint/suspicious/noConfusingVoidType: it can be void.\n pluginConfig?: PluginConfig | void;\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 };\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 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 * 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\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\"> & {\n type: typeof typeName;\n options?: FieldOptions;\n };\n }\n}\n","import { defineField, definePlugin, defineType } from \"sanity\";\nimport { CollectionRefInput } from \"./input\";\nimport { type CollectionReference, type FieldOptions, type PluginConfig, typeName } 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((ctx) => (!ctx?.collectionRef ? { message: \"Select a reference.\", path: [\"collectionRef\"] } : true));\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 ],\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 { wildSanityReferenceCollectionFieldPlugin, typeName, type PluginConfig, type FieldOptions, type CollectionReference };\n"],"names":["normalizeCollection","preset","title","value","CollectionRefInput","t0","$","_c","pluginConfig","props","t1","bb0","references","schemaType","options","referenceKinds","length","t2","t3","t4","map","t5","list","t6","patchedSchemaType","renderDefault","typeName","wildSanityReferenceCollectionFieldPlugin","definePlugin","config","name","schema","types","defineType","type","description","icon","jsx","Fragment","validation","R","custom","ctx","collectionRef","message","path","fields","defineField","components","input","layout","preview","select","collection","prepare","subtitle","undefined"],"mappings":";;;AAIA,SAASA,oBAAoBC,QAA6B;AACxD,SAAO,OAAOA,UAAW,WAAW;AAAA,IAAEC,OAAOD;AAAAA,IAAQE,OAAOF;AAAAA,EAAAA,IAAWA;AACzE;AAEA,SAAAG,mBAAAC,IAAA;AAAA,QAAAC,IAAAC,gBAAAA,EAAA,EAAA;AAAA,MAAAC,cAAAC;AAAAH,WAAAD,MAA4B;AAAA,IAAAG;AAAAA,IAAA,GAAAC;AAAAA,EAAAA,IAAAJ,IAM3BC,OAAAD,IAAAC,OAAAE,cAAAF,OAAAG,UAAAD,eAAAF,EAAA,CAAA,GAAAG,QAAAH,EAAA,CAAA;AAAA,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AAA,MACTH,MAAMJ;AAAAA,MACNQ,MAAM;AAAA,MACNhC,OAAO;AAAA,MACPiC,aAAa;AAAA,MACbC,MAAMA,MAAMC,2BAAAA,IAAAC,WAAAA,UAAA,EAAE,UAAA,YAAA,CAAE;AAAA,MAChBC,YAAaC,CAAAA,MACJA,EAAEC,OAAQC,CAAAA,QAAUA,KAAKC,gBAA8E,KAA9D;AAAA,QAAEC,SAAS;AAAA,QAAuBC,MAAM,CAAC,eAAe;AAAA,MAAA,CAAW;AAAA,MAErHC,QAAQ,CACNC,OAAAA,YAAY;AAAA,QACVjB,MAAM;AAAA,QACNI,MAAM;AAAA,QACNhC,OAAO;AAAA,QACPiC,aAAa;AAAA,QACba,YAAY;AAAA,UACVC,OAAQxC,CAAAA,UAAU4B,+BAAC,sBAAmB,cAAcR,QAAQ,GAAIpB,MAAAA,CAAM;AAAA,QAAA;AAAA,QAExEK,SAAS;AAAA,UACPoC,QAAQ;AAAA,UACR5B,MAAMO,OAAOd;AAAAA,QAAAA;AAAAA,MACf,CACD,CAAC;AAAA,MAEJoC,SAAS;AAAA,QACPC,QAAQ;AAAA,UACNC,YAAY;AAAA,QAAA;AAAA,QAEdC,QAAQ;AAAA,UAAED;AAAAA,QAAAA,GAAc;AACtB,iBAAO;AAAA,YACLnD,OAAO;AAAA,YACPqD,UAAUF,aAAa,gBAAgBA,UAAU,KAAKG;AAAAA,UAAAA;AAAAA,QAE1D;AAAA,MAAA;AAAA,IACF,CACD,CAAC;AAAA,EAAA;AAGR,EACD;;;"}
@@ -0,0 +1,44 @@
1
+ import * as sanity0 from "sanity";
2
+ import { ObjectDefinition, ObjectOptions } from "sanity";
3
+ /** @public */
4
+ declare const typeName: "wild.referenceCollection";
5
+ /** @public */
6
+ type CollectionReference = string | {
7
+ title: string;
8
+ value: string;
9
+ };
10
+ /** @public */
11
+ type PluginConfig = {
12
+ /**
13
+ * A list of references the user can choose from.
14
+ * Can be overridden per-field via `options.referenceKinds`.
15
+ * @example
16
+ * ```ts
17
+ * referenceKinds: ["blogPost", "some-type"],
18
+ * ```
19
+ */
20
+ referenceKinds: CollectionReference[];
21
+ };
22
+ /** @public */
23
+ type FieldOptions = ObjectOptions & {
24
+ /**
25
+ * A list of references the user can choose from.
26
+ * Will take precedence over the plugin-level `referenceKinds`.
27
+ * @example
28
+ * ```ts
29
+ * referenceKinds: ["blogPost", "some-type"],
30
+ * ```
31
+ */
32
+ referenceKinds?: CollectionReference[];
33
+ };
34
+ declare module "sanity" {
35
+ interface IntrinsicDefinitions {
36
+ [typeName]: Omit<ObjectDefinition, "type" | "fields" | "options"> & {
37
+ type: typeof typeName;
38
+ options?: FieldOptions;
39
+ };
40
+ }
41
+ }
42
+ /** @public */
43
+ declare const wildSanityReferenceCollectionFieldPlugin: sanity0.Plugin<PluginConfig>;
44
+ export { type CollectionReference, type FieldOptions, type PluginConfig, typeName, wildSanityReferenceCollectionFieldPlugin };
@@ -0,0 +1,44 @@
1
+ import * as sanity0 from "sanity";
2
+ import { ObjectDefinition, ObjectOptions } from "sanity";
3
+ /** @public */
4
+ declare const typeName: "wild.referenceCollection";
5
+ /** @public */
6
+ type CollectionReference = string | {
7
+ title: string;
8
+ value: string;
9
+ };
10
+ /** @public */
11
+ type PluginConfig = {
12
+ /**
13
+ * A list of references the user can choose from.
14
+ * Can be overridden per-field via `options.referenceKinds`.
15
+ * @example
16
+ * ```ts
17
+ * referenceKinds: ["blogPost", "some-type"],
18
+ * ```
19
+ */
20
+ referenceKinds: CollectionReference[];
21
+ };
22
+ /** @public */
23
+ type FieldOptions = ObjectOptions & {
24
+ /**
25
+ * A list of references the user can choose from.
26
+ * Will take precedence over the plugin-level `referenceKinds`.
27
+ * @example
28
+ * ```ts
29
+ * referenceKinds: ["blogPost", "some-type"],
30
+ * ```
31
+ */
32
+ referenceKinds?: CollectionReference[];
33
+ };
34
+ declare module "sanity" {
35
+ interface IntrinsicDefinitions {
36
+ [typeName]: Omit<ObjectDefinition, "type" | "fields" | "options"> & {
37
+ type: typeof typeName;
38
+ options?: FieldOptions;
39
+ };
40
+ }
41
+ }
42
+ /** @public */
43
+ declare const wildSanityReferenceCollectionFieldPlugin: sanity0.Plugin<PluginConfig>;
44
+ export { type CollectionReference, type FieldOptions, type PluginConfig, typeName, wildSanityReferenceCollectionFieldPlugin };
package/dist/index.js ADDED
@@ -0,0 +1,91 @@
1
+ import { jsx, Fragment } from "react/jsx-runtime";
2
+ import { definePlugin, defineType, defineField } from "sanity";
3
+ import { c } from "react/compiler-runtime";
4
+ function normalizeCollection(preset) {
5
+ return typeof preset == "string" ? {
6
+ title: preset,
7
+ value: preset
8
+ } : preset;
9
+ }
10
+ function CollectionRefInput(t0) {
11
+ const $ = c(14);
12
+ let pluginConfig, props;
13
+ $[0] !== t0 ? ({
14
+ pluginConfig,
15
+ ...props
16
+ } = t0, $[0] = t0, $[1] = pluginConfig, $[2] = props) : (pluginConfig = $[1], props = $[2]);
17
+ let t1;
18
+ bb0: {
19
+ const references = props.schemaType.options?.referenceKinds ?? pluginConfig?.referenceKinds;
20
+ if (!references?.length) {
21
+ t1 = props.schemaType;
22
+ break bb0;
23
+ }
24
+ const t22 = props.schemaType, t3 = props.schemaType.options;
25
+ let t4;
26
+ $[3] !== references ? (t4 = references.map(normalizeCollection), $[3] = references, $[4] = t4) : t4 = $[4];
27
+ let t5;
28
+ $[5] !== props.schemaType.options || $[6] !== t4 ? (t5 = {
29
+ ...t3,
30
+ list: t4
31
+ }, $[5] = props.schemaType.options, $[6] = t4, $[7] = t5) : t5 = $[7];
32
+ let t6;
33
+ $[8] !== props.schemaType || $[9] !== t5 ? (t6 = {
34
+ ...t22,
35
+ options: t5
36
+ }, $[8] = props.schemaType, $[9] = t5, $[10] = t6) : t6 = $[10], t1 = t6;
37
+ }
38
+ const patchedSchemaType = t1;
39
+ let t2;
40
+ return $[11] !== patchedSchemaType || $[12] !== props ? (t2 = props.renderDefault({
41
+ ...props,
42
+ schemaType: patchedSchemaType
43
+ }), $[11] = patchedSchemaType, $[12] = props, $[13] = t2) : t2 = $[13], t2;
44
+ }
45
+ const typeName = "wild.referenceCollection", wildSanityReferenceCollectionFieldPlugin = definePlugin((config) => ({
46
+ name: "@madebywild/sanity-reference-collection-field",
47
+ schema: {
48
+ types: [defineType({
49
+ name: typeName,
50
+ type: "object",
51
+ title: "Reference Collection",
52
+ description: "Create a collection of specific kinds.",
53
+ icon: () => /* @__PURE__ */ jsx(Fragment, { children: "\u{1F503}" }),
54
+ validation: (R) => R.custom((ctx) => ctx?.collectionRef ? !0 : {
55
+ message: "Select a reference.",
56
+ path: ["collectionRef"]
57
+ }),
58
+ fields: [defineField({
59
+ name: "collectionRef",
60
+ type: "string",
61
+ title: "Reference",
62
+ description: "Select the kind of references for the collection to display.",
63
+ components: {
64
+ input: (props) => /* @__PURE__ */ jsx(CollectionRefInput, { pluginConfig: config, ...props })
65
+ },
66
+ options: {
67
+ layout: "dropdown",
68
+ list: config.referenceKinds
69
+ }
70
+ })],
71
+ preview: {
72
+ select: {
73
+ collection: "collectionRef"
74
+ },
75
+ prepare({
76
+ collection
77
+ }) {
78
+ return {
79
+ title: "Reference Collection",
80
+ subtitle: collection ? `Reference to ${collection}` : void 0
81
+ };
82
+ }
83
+ }
84
+ })]
85
+ }
86
+ }));
87
+ export {
88
+ typeName,
89
+ wildSanityReferenceCollectionFieldPlugin
90
+ };
91
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/input.tsx","../src/types.tsx","../src/index.tsx"],"sourcesContent":["import * as React from \"react\";\nimport type { StringInputProps } from \"sanity\";\nimport type { CollectionReference, FieldOptions, PluginConfig } from \"./types\";\n\nfunction normalizeCollection(preset: CollectionReference) {\n return typeof preset === \"string\" ? { title: preset, value: preset } : preset;\n}\n\nfunction CollectionRefInput({\n pluginConfig,\n ...props\n}: StringInputProps & {\n // biome-ignore lint/suspicious/noConfusingVoidType: it can be void.\n pluginConfig?: PluginConfig | void;\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 };\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 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 * 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\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\"> & {\n type: typeof typeName;\n options?: FieldOptions;\n };\n }\n}\n","import { defineField, definePlugin, defineType } from \"sanity\";\nimport { CollectionRefInput } from \"./input\";\nimport { type CollectionReference, type FieldOptions, type PluginConfig, typeName } 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((ctx) => (!ctx?.collectionRef ? { message: \"Select a reference.\", path: [\"collectionRef\"] } : true));\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 ],\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 { wildSanityReferenceCollectionFieldPlugin, typeName, type PluginConfig, type FieldOptions, type CollectionReference };\n"],"names":["normalizeCollection","preset","title","value","CollectionRefInput","t0","$","_c","pluginConfig","props","t1","bb0","references","schemaType","options","referenceKinds","length","t2","t3","t4","map","t5","list","t6","patchedSchemaType","renderDefault","typeName","wildSanityReferenceCollectionFieldPlugin","definePlugin","config","name","schema","types","defineType","type","description","icon","validation","R","custom","ctx","collectionRef","message","path","fields","defineField","components","input","layout","preview","select","collection","prepare","subtitle","undefined"],"mappings":";;;AAIA,SAASA,oBAAoBC,QAA6B;AACxD,SAAO,OAAOA,UAAW,WAAW;AAAA,IAAEC,OAAOD;AAAAA,IAAQE,OAAOF;AAAAA,EAAAA,IAAWA;AACzE;AAEA,SAAAG,mBAAAC,IAAA;AAAA,QAAAC,IAAAC,EAAA,EAAA;AAAA,MAAAC,cAAAC;AAAAH,WAAAD,MAA4B;AAAA,IAAAG;AAAAA,IAAA,GAAAC;AAAAA,EAAAA,IAAAJ,IAM3BC,OAAAD,IAAAC,OAAAE,cAAAF,OAAAG,UAAAD,eAAAF,EAAA,CAAA,GAAAG,QAAAH,EAAA,CAAA;AAAA,MAAAI;AAAAC,OAAA;AAIG,UAAAC,aADgBH,MAAKI,WAAWC,SACNC,kBAAoBP,cAAYO;AAG1D,QAAI,CAACH,YAAUI,QAAQ;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A,YAAA,CAAE;AAAA,MAChBC,YAAaC,CAAAA,MACJA,EAAEC,OAAQC,CAAAA,QAAUA,KAAKC,gBAA8E,KAA9D;AAAA,QAAEC,SAAS;AAAA,QAAuBC,MAAM,CAAC,eAAe;AAAA,MAAA,CAAW;AAAA,MAErHC,QAAQ,CACNC,YAAY;AAAA,QACVf,MAAM;AAAA,QACNI,MAAM;AAAA,QACNhC,OAAO;AAAA,QACPiC,aAAa;AAAA,QACbW,YAAY;AAAA,UACVC,OAAQtC,CAAAA,UAAU,oBAAC,sBAAmB,cAAcoB,QAAQ,GAAIpB,MAAAA,CAAM;AAAA,QAAA;AAAA,QAExEK,SAAS;AAAA,UACPkC,QAAQ;AAAA,UACR1B,MAAMO,OAAOd;AAAAA,QAAAA;AAAAA,MACf,CACD,CAAC;AAAA,MAEJkC,SAAS;AAAA,QACPC,QAAQ;AAAA,UACNC,YAAY;AAAA,QAAA;AAAA,QAEdC,QAAQ;AAAA,UAAED;AAAAA,QAAAA,GAAc;AACtB,iBAAO;AAAA,YACLjD,OAAO;AAAA,YACPmD,UAAUF,aAAa,gBAAgBA,UAAU,KAAKG;AAAAA,UAAAA;AAAAA,QAE1D;AAAA,MAAA;AAAA,IACF,CACD,CAAC;AAAA,EAAA;AAGR,EACD;"}
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@madebywild/sanity-reference-collection-field",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "sideEffects": false,
6
+ "license": "UNLICENSED",
7
+ "browserslist": "extends @sanity/browserslist-config",
8
+ "exports": {
9
+ "./package.json": "./package.json",
10
+ ".": {
11
+ "source": "./src/index.tsx",
12
+ "require": "./dist/index.cjs",
13
+ "default": "./dist/index.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "src"
19
+ ],
20
+ "publishConfig": {
21
+ "access": "restricted"
22
+ },
23
+ "peerDependencies": {
24
+ "@sanity/ui": "^3.1",
25
+ "react": "^19",
26
+ "react-dom": "^19",
27
+ "sanity": "^4.17 || ^5.0.0"
28
+ },
29
+ "devDependencies": {
30
+ "@sanity/pkg-utils": "^9.2",
31
+ "@types/react": "^19",
32
+ "@types/react-dom": "^19",
33
+ "babel-plugin-react-compiler": "1.0.0",
34
+ "react": "^19",
35
+ "react-dom": "^19",
36
+ "sanity": "^4.17 || ^5.0.0",
37
+ "typescript": "^5"
38
+ },
39
+ "scripts": {
40
+ "build": "pkg-utils build --strict",
41
+ "clear": "rm -rf dist && rm -rf node_modules",
42
+ "check": "tsc --noEmit && pkg-utils check"
43
+ }
44
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,52 @@
1
+ import { defineField, definePlugin, defineType } from "sanity";
2
+ import { CollectionRefInput } from "./input";
3
+ import { type CollectionReference, type FieldOptions, type PluginConfig, typeName } from "./types";
4
+
5
+ /** @public */
6
+ const wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((config) => {
7
+ return {
8
+ name: "@madebywild/sanity-reference-collection-field",
9
+ schema: {
10
+ types: [
11
+ defineType({
12
+ name: typeName,
13
+ type: "object",
14
+ title: "Reference Collection",
15
+ description: "Create a collection of specific kinds.",
16
+ icon: () => <>🔃</>,
17
+ validation: (R) => {
18
+ return R.custom((ctx) => (!ctx?.collectionRef ? { message: "Select a reference.", path: ["collectionRef"] } : true));
19
+ },
20
+ fields: [
21
+ defineField({
22
+ name: "collectionRef",
23
+ type: "string",
24
+ title: "Reference",
25
+ description: "Select the kind of references for the collection to display.",
26
+ components: {
27
+ input: (props) => <CollectionRefInput pluginConfig={config} {...props} />,
28
+ },
29
+ options: {
30
+ layout: "dropdown",
31
+ list: config.referenceKinds,
32
+ },
33
+ }),
34
+ ],
35
+ preview: {
36
+ select: {
37
+ collection: "collectionRef",
38
+ },
39
+ prepare({ collection }) {
40
+ return {
41
+ title: "Reference Collection",
42
+ subtitle: collection ? `Reference to ${collection}` : undefined,
43
+ };
44
+ },
45
+ },
46
+ }),
47
+ ],
48
+ },
49
+ };
50
+ });
51
+
52
+ export { wildSanityReferenceCollectionFieldPlugin, typeName, type PluginConfig, type FieldOptions, type CollectionReference };
package/src/input.tsx ADDED
@@ -0,0 +1,39 @@
1
+ import * as React from "react";
2
+ import type { StringInputProps } from "sanity";
3
+ import type { CollectionReference, FieldOptions, PluginConfig } from "./types";
4
+
5
+ function normalizeCollection(preset: CollectionReference) {
6
+ return typeof preset === "string" ? { title: preset, value: preset } : preset;
7
+ }
8
+
9
+ function CollectionRefInput({
10
+ pluginConfig,
11
+ ...props
12
+ }: StringInputProps & {
13
+ // biome-ignore lint/suspicious/noConfusingVoidType: it can be void.
14
+ pluginConfig?: PluginConfig | void;
15
+ }) {
16
+ const patchedSchemaType = React.useMemo(() => {
17
+ // Local options take precedence over plugin config.
18
+ const options = props.schemaType.options as FieldOptions | undefined;
19
+ const references = options?.referenceKinds ?? pluginConfig?.referenceKinds;
20
+
21
+ // No need to touch anything if the list is unchanged.
22
+ if (!references?.length) return props.schemaType;
23
+
24
+ return {
25
+ ...props.schemaType,
26
+ options: {
27
+ ...props.schemaType.options,
28
+ list: references.map(normalizeCollection),
29
+ },
30
+ };
31
+ }, [props.schemaType]);
32
+
33
+ return props.renderDefault({
34
+ ...props,
35
+ schemaType: patchedSchemaType,
36
+ });
37
+ }
38
+
39
+ export { CollectionRefInput };
package/src/types.tsx ADDED
@@ -0,0 +1,44 @@
1
+ import type { ObjectDefinition, ObjectOptions } from "sanity";
2
+
3
+ /** @public */
4
+ export const typeName = "wild.referenceCollection" as const;
5
+
6
+ /** @public */
7
+ export type CollectionReference = string | { title: string; value: string };
8
+
9
+ /** @public */
10
+ export type PluginConfig = {
11
+ /**
12
+ * A list of references the user can choose from.
13
+ * Can be overridden per-field via `options.referenceKinds`.
14
+ * @example
15
+ * ```ts
16
+ * referenceKinds: ["blogPost", "some-type"],
17
+ * ```
18
+ */
19
+ referenceKinds: CollectionReference[];
20
+ };
21
+
22
+ /** @public */
23
+ export type FieldOptions = ObjectOptions & {
24
+ /**
25
+ * A list of references the user can choose from.
26
+ * Will take precedence over the plugin-level `referenceKinds`.
27
+ * @example
28
+ * ```ts
29
+ * referenceKinds: ["blogPost", "some-type"],
30
+ * ```
31
+ */
32
+ referenceKinds?: CollectionReference[];
33
+ };
34
+
35
+ // Add the custom field definition to Sanity's intrinsic definitions
36
+ // so that type checking works correctly when using this field type.
37
+ declare module "sanity" {
38
+ export interface IntrinsicDefinitions {
39
+ [typeName]: Omit<ObjectDefinition, "type" | "fields" | "options"> & {
40
+ type: typeof typeName;
41
+ options?: FieldOptions;
42
+ };
43
+ }
44
+ }