@madebywild/sanity-reference-collection-field 0.0.1 → 0.0.4

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/dist/index.cjs CHANGED
@@ -1,12 +1,64 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var jsxRuntime = require("react/jsx-runtime"), sanity = require("sanity"), compilerRuntime = require("react/compiler-runtime");
3
+ var jsxRuntime = require("react/jsx-runtime"), sanity = require("sanity"), compilerRuntime = require("react/compiler-runtime"), ui = require("@sanity/ui"), React = require("react");
4
+ function _interopNamespaceCompat(e) {
5
+ if (e && typeof e == "object" && "default" in e) return e;
6
+ var n = /* @__PURE__ */ Object.create(null);
7
+ return e && Object.keys(e).forEach(function(k) {
8
+ if (k !== "default") {
9
+ var d = Object.getOwnPropertyDescriptor(e, k);
10
+ Object.defineProperty(n, k, d.get ? d : {
11
+ enumerable: !0,
12
+ get: function() {
13
+ return e[k];
14
+ }
15
+ });
16
+ }
17
+ }), n.default = e, Object.freeze(n);
18
+ }
19
+ var React__namespace = /* @__PURE__ */ _interopNamespaceCompat(React);
20
+ const FieldContext = React__namespace.createContext({}), useFieldCtx = () => React__namespace.useContext(FieldContext);
4
21
  function normalizeCollection(preset) {
5
22
  return typeof preset == "string" ? {
6
23
  title: preset,
7
24
  value: preset
8
25
  } : preset;
9
26
  }
27
+ function CollectionFieldInput(props) {
28
+ const $ = compilerRuntime.c(7), t0 = props.schemaType.options;
29
+ let t1;
30
+ $[0] !== t0 ? (t1 = {
31
+ options: t0
32
+ }, $[0] = t0, $[1] = t1) : t1 = $[1];
33
+ let t2;
34
+ $[2] !== props ? (t2 = props.renderDefault(props), $[2] = props, $[3] = t2) : t2 = $[3];
35
+ let t3;
36
+ return $[4] !== t1 || $[5] !== t2 ? (t3 = /* @__PURE__ */ jsxRuntime.jsx(FieldContext.Provider, { value: t1, children: t2 }), $[4] = t1, $[5] = t2, $[6] = t3) : t3 = $[6], t3;
37
+ }
38
+ function CollectionOptionsField(props) {
39
+ const $ = compilerRuntime.c(2), {
40
+ options
41
+ } = useFieldCtx(), withFiltering = options?.extensions?.includes("filtering"), withPagination = options?.extensions?.includes("pagination");
42
+ if (!withFiltering && !withPagination)
43
+ return null;
44
+ let t0;
45
+ return $[0] !== props ? (t0 = props.renderDefault(props), $[0] = props, $[1] = t0) : t0 = $[1], t0;
46
+ }
47
+ function CollectionOptionsInput(props) {
48
+ const $ = compilerRuntime.c(10), {
49
+ options
50
+ } = useFieldCtx();
51
+ let t0;
52
+ $[0] !== options?.extensions ? (t0 = options?.extensions?.includes("pagination"), $[0] = options?.extensions, $[1] = t0) : t0 = $[1];
53
+ const withPagination = t0;
54
+ let t1;
55
+ $[2] !== options?.extensions ? (t1 = options?.extensions?.includes("filtering"), $[2] = options?.extensions, $[3] = t1) : t1 = $[3];
56
+ const withFiltering = t1;
57
+ let t2;
58
+ $[4] !== props || $[5] !== withFiltering || $[6] !== withPagination ? (t2 = props.members.map((member) => member.kind !== "field" || !withFiltering && member.name === "filtering" || !withPagination && member.name === "pagination" ? null : /* @__PURE__ */ jsxRuntime.jsx(sanity.MemberField, { member, ...props }, member.key)), $[4] = props, $[5] = withFiltering, $[6] = withPagination, $[7] = t2) : t2 = $[7];
59
+ let t3;
60
+ return $[8] !== t2 ? (t3 = /* @__PURE__ */ jsxRuntime.jsx(ui.Stack, { space: 4, children: t2 }), $[8] = t2, $[9] = t3) : t3 = $[9], t3;
61
+ }
10
62
  function CollectionRefInput(t0) {
11
63
  const $ = compilerRuntime.c(14);
12
64
  let pluginConfig, props;
@@ -42,7 +94,10 @@ function CollectionRefInput(t0) {
42
94
  schemaType: patchedSchemaType
43
95
  }), $[11] = patchedSchemaType, $[12] = props, $[13] = t2) : t2 = $[13], t2;
44
96
  }
45
- const typeName = "wild.referenceCollection", wildSanityReferenceCollectionFieldPlugin = sanity.definePlugin((config) => ({
97
+ const typeName = "wild.referenceCollection", ReferenceCollectionExtension = {
98
+ filtering: "filtering",
99
+ pagination: "pagination"
100
+ }, wildSanityReferenceCollectionFieldPlugin = sanity.definePlugin((config) => ({
46
101
  name: "@madebywild/sanity-reference-collection-field",
47
102
  schema: {
48
103
  types: [sanity.defineType({
@@ -55,6 +110,10 @@ const typeName = "wild.referenceCollection", wildSanityReferenceCollectionFieldP
55
110
  message: "Select a reference.",
56
111
  path: ["collectionRef"]
57
112
  }),
113
+ components: {
114
+ field: (props) => props.schemaType.options.inline ? props.children : props.renderDefault(props),
115
+ input: (props) => /* @__PURE__ */ jsxRuntime.jsx(CollectionFieldInput, { ...props })
116
+ },
58
117
  fields: [sanity.defineField({
59
118
  name: "collectionRef",
60
119
  type: "string",
@@ -67,6 +126,38 @@ const typeName = "wild.referenceCollection", wildSanityReferenceCollectionFieldP
67
126
  layout: "dropdown",
68
127
  list: config.referenceKinds
69
128
  }
129
+ }), sanity.defineField({
130
+ type: "object",
131
+ name: "options",
132
+ title: "Options",
133
+ description: "Configure how to render the collection.",
134
+ options: {
135
+ collapsed: !1,
136
+ collapsible: !1
137
+ },
138
+ components: {
139
+ field: (props) => /* @__PURE__ */ jsxRuntime.jsx(CollectionOptionsField, { ...props }),
140
+ input: (props) => /* @__PURE__ */ jsxRuntime.jsx(CollectionOptionsInput, { ...props })
141
+ },
142
+ fields: [sanity.defineField({
143
+ name: "withFilters",
144
+ type: "boolean",
145
+ title: "With Filters",
146
+ description: "Enable filters for the collection.",
147
+ initialValue: !1,
148
+ options: {
149
+ layout: "switch"
150
+ }
151
+ }), sanity.defineField({
152
+ name: "withPagination",
153
+ type: "boolean",
154
+ title: "With Pagination",
155
+ description: "Enable pagination for the collection.",
156
+ initialValue: !1,
157
+ options: {
158
+ layout: "switch"
159
+ }
160
+ })]
70
161
  })],
71
162
  preview: {
72
163
  select: {
@@ -84,6 +175,7 @@ const typeName = "wild.referenceCollection", wildSanityReferenceCollectionFieldP
84
175
  })]
85
176
  }
86
177
  }));
178
+ exports.ReferenceCollectionExtension = ReferenceCollectionExtension;
87
179
  exports.typeName = typeName;
88
180
  exports.wildSanityReferenceCollectionFieldPlugin = wildSanityReferenceCollectionFieldPlugin;
89
181
  //# sourceMappingURL=index.cjs.map
@@ -1 +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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AAa,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 { 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 } from \"./types\";\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 {props.renderDefault(props)}\n </FieldContext.Provider>\n );\n}\n\nfunction CollectionOptionsField(props: ObjectFieldProps) {\n const { options } = useFieldCtx();\n const withFiltering = options?.extensions?.includes(\"filtering\");\n const withPagination = options?.extensions?.includes(\"pagination\");\n\n // No extensions means we can hide the whole field.\n if (!withFiltering && !withPagination) {\n return null;\n }\n\n return props.renderDefault(props);\n}\n\nfunction CollectionOptionsInput(props: ObjectInputProps) {\n const { options } = useFieldCtx();\n const withPagination = options?.extensions?.includes(\"pagination\");\n const withFiltering = options?.extensions?.includes(\"filtering\");\n\n return (\n <Stack space={4}>\n {props.members.map((member: ObjectMember) => {\n if (member.kind !== \"field\") return null;\n if (!withFiltering && member.name === \"filtering\") return null;\n if (!withPagination && member.name === \"pagination\") return null;\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 const ReferenceCollectionExtension = {\n filtering: \"filtering\",\n pagination: \"pagination\",\n} as const;\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 * - `filtering` - Allows filtering the references.\n * - `pagination` - Allows paginating the references.\n * @example\n * ```ts\n * extensions: [\"filtering\", \"pagination\"],\n * ```\n */\n extensions?: (keyof typeof ReferenceCollectionExtension)[];\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 { type CollectionReference, type FieldOptions, type PluginConfig, ReferenceCollectionExtension, 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 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 ReferenceCollectionExtension,\n type PluginConfig,\n type FieldOptions,\n type CollectionReference,\n};\n"],"names":["FieldContext","React","createContext","useFieldCtx","useContext","normalizeCollection","preset","title","value","CollectionFieldInput","props","$","_c","t0","schemaType","options","t1","t2","renderDefault","t3","jsx","CollectionOptionsField","withFiltering","extensions","includes","withPagination","CollectionOptionsInput","members","map","member","kind","name","MemberField","key","Stack","CollectionRefInput","pluginConfig","bb0","references","referenceKinds","length","t4","t5","list","t6","patchedSchemaType","typeName","ReferenceCollectionExtension","filtering","pagination","wildSanityReferenceCollectionFieldPlugin","definePlugin","config","schema","types","defineType","type","description","icon","Fragment","validation","R","custom","ctx","collectionRef","message","path","components","field","inline","children","input","fields","defineField","layout","collapsed","collapsible","initialValue","preview","select","collection","prepare","subtitle","undefined"],"mappings":";;;;;;;;;;;;;;;;;;;AAMA,MAAMA,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,SAC5FO,KAAAP,MAAKQ,cAAeR,KAAK,GAACC,OAAAD,OAAAC,OAAAM,MAAAA,KAAAN,EAAA,CAAA;AAAA,MAAAQ;AAAA,SAAAR,EAAA,CAAA,MAAAK,MAAAL,SAAAM,MAD7BE,KAAAC,2BAAAA,IAAA,aAAA,U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;AAAA,UACNC,YAAY;AAAA,QAAA;AAAA,QAEdC,QAAQ;AAAA,UAAED;AAAAA,QAAAA,GAAc;AACtB,iBAAO;AAAA,YACLzE,OAAO;AAAA,YACP2E,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
@@ -8,6 +8,11 @@ type CollectionReference = string | {
8
8
  value: string;
9
9
  };
10
10
  /** @public */
11
+ declare const ReferenceCollectionExtension: {
12
+ readonly filtering: "filtering";
13
+ readonly pagination: "pagination";
14
+ };
15
+ /** @public */
11
16
  type PluginConfig = {
12
17
  /**
13
18
  * A list of references the user can choose from.
@@ -21,6 +26,11 @@ type PluginConfig = {
21
26
  };
22
27
  /** @public */
23
28
  type FieldOptions = ObjectOptions & {
29
+ /**
30
+ * Whether the media field should be displayed inline.
31
+ * This will render all children inline on the same level.
32
+ */
33
+ inline?: boolean;
24
34
  /**
25
35
  * A list of references the user can choose from.
26
36
  * Will take precedence over the plugin-level `referenceKinds`.
@@ -30,10 +40,20 @@ type FieldOptions = ObjectOptions & {
30
40
  * ```
31
41
  */
32
42
  referenceKinds?: CollectionReference[];
43
+ /**
44
+ * List of extension that add additional behaviors to the link field.
45
+ * - `filtering` - Allows filtering the references.
46
+ * - `pagination` - Allows paginating the references.
47
+ * @example
48
+ * ```ts
49
+ * extensions: ["filtering", "pagination"],
50
+ * ```
51
+ */
52
+ extensions?: (keyof typeof ReferenceCollectionExtension)[];
33
53
  };
34
54
  declare module "sanity" {
35
55
  interface IntrinsicDefinitions {
36
- [typeName]: Omit<ObjectDefinition, "type" | "fields" | "options"> & {
56
+ [typeName]: Omit<ObjectDefinition, "type" | "fields" | "options" | "components"> & {
37
57
  type: typeof typeName;
38
58
  options?: FieldOptions;
39
59
  };
@@ -41,4 +61,4 @@ declare module "sanity" {
41
61
  }
42
62
  /** @public */
43
63
  declare const wildSanityReferenceCollectionFieldPlugin: sanity0.Plugin<PluginConfig>;
44
- export { type CollectionReference, type FieldOptions, type PluginConfig, typeName, wildSanityReferenceCollectionFieldPlugin };
64
+ export { type CollectionReference, type FieldOptions, type PluginConfig, ReferenceCollectionExtension, typeName, wildSanityReferenceCollectionFieldPlugin };
package/dist/index.d.ts CHANGED
@@ -8,6 +8,11 @@ type CollectionReference = string | {
8
8
  value: string;
9
9
  };
10
10
  /** @public */
11
+ declare const ReferenceCollectionExtension: {
12
+ readonly filtering: "filtering";
13
+ readonly pagination: "pagination";
14
+ };
15
+ /** @public */
11
16
  type PluginConfig = {
12
17
  /**
13
18
  * A list of references the user can choose from.
@@ -21,6 +26,11 @@ type PluginConfig = {
21
26
  };
22
27
  /** @public */
23
28
  type FieldOptions = ObjectOptions & {
29
+ /**
30
+ * Whether the media field should be displayed inline.
31
+ * This will render all children inline on the same level.
32
+ */
33
+ inline?: boolean;
24
34
  /**
25
35
  * A list of references the user can choose from.
26
36
  * Will take precedence over the plugin-level `referenceKinds`.
@@ -30,10 +40,20 @@ type FieldOptions = ObjectOptions & {
30
40
  * ```
31
41
  */
32
42
  referenceKinds?: CollectionReference[];
43
+ /**
44
+ * List of extension that add additional behaviors to the link field.
45
+ * - `filtering` - Allows filtering the references.
46
+ * - `pagination` - Allows paginating the references.
47
+ * @example
48
+ * ```ts
49
+ * extensions: ["filtering", "pagination"],
50
+ * ```
51
+ */
52
+ extensions?: (keyof typeof ReferenceCollectionExtension)[];
33
53
  };
34
54
  declare module "sanity" {
35
55
  interface IntrinsicDefinitions {
36
- [typeName]: Omit<ObjectDefinition, "type" | "fields" | "options"> & {
56
+ [typeName]: Omit<ObjectDefinition, "type" | "fields" | "options" | "components"> & {
37
57
  type: typeof typeName;
38
58
  options?: FieldOptions;
39
59
  };
@@ -41,4 +61,4 @@ declare module "sanity" {
41
61
  }
42
62
  /** @public */
43
63
  declare const wildSanityReferenceCollectionFieldPlugin: sanity0.Plugin<PluginConfig>;
44
- export { type CollectionReference, type FieldOptions, type PluginConfig, typeName, wildSanityReferenceCollectionFieldPlugin };
64
+ export { type CollectionReference, type FieldOptions, type PluginConfig, ReferenceCollectionExtension, typeName, wildSanityReferenceCollectionFieldPlugin };
package/dist/index.js CHANGED
@@ -1,12 +1,50 @@
1
1
  import { jsx, Fragment } from "react/jsx-runtime";
2
- import { definePlugin, defineType, defineField } from "sanity";
2
+ import { MemberField, definePlugin, defineType, defineField } from "sanity";
3
3
  import { c } from "react/compiler-runtime";
4
+ import { Stack } from "@sanity/ui";
5
+ import * as React from "react";
6
+ const FieldContext = React.createContext({}), useFieldCtx = () => React.useContext(FieldContext);
4
7
  function normalizeCollection(preset) {
5
8
  return typeof preset == "string" ? {
6
9
  title: preset,
7
10
  value: preset
8
11
  } : preset;
9
12
  }
13
+ function CollectionFieldInput(props) {
14
+ const $ = c(7), t0 = props.schemaType.options;
15
+ let t1;
16
+ $[0] !== t0 ? (t1 = {
17
+ options: t0
18
+ }, $[0] = t0, $[1] = t1) : t1 = $[1];
19
+ let t2;
20
+ $[2] !== props ? (t2 = props.renderDefault(props), $[2] = props, $[3] = t2) : t2 = $[3];
21
+ let t3;
22
+ return $[4] !== t1 || $[5] !== t2 ? (t3 = /* @__PURE__ */ jsx(FieldContext.Provider, { value: t1, children: t2 }), $[4] = t1, $[5] = t2, $[6] = t3) : t3 = $[6], t3;
23
+ }
24
+ function CollectionOptionsField(props) {
25
+ const $ = c(2), {
26
+ options
27
+ } = useFieldCtx(), withFiltering = options?.extensions?.includes("filtering"), withPagination = options?.extensions?.includes("pagination");
28
+ if (!withFiltering && !withPagination)
29
+ return null;
30
+ let t0;
31
+ return $[0] !== props ? (t0 = props.renderDefault(props), $[0] = props, $[1] = t0) : t0 = $[1], t0;
32
+ }
33
+ function CollectionOptionsInput(props) {
34
+ const $ = c(10), {
35
+ options
36
+ } = useFieldCtx();
37
+ let t0;
38
+ $[0] !== options?.extensions ? (t0 = options?.extensions?.includes("pagination"), $[0] = options?.extensions, $[1] = t0) : t0 = $[1];
39
+ const withPagination = t0;
40
+ let t1;
41
+ $[2] !== options?.extensions ? (t1 = options?.extensions?.includes("filtering"), $[2] = options?.extensions, $[3] = t1) : t1 = $[3];
42
+ const withFiltering = t1;
43
+ let t2;
44
+ $[4] !== props || $[5] !== withFiltering || $[6] !== withPagination ? (t2 = props.members.map((member) => member.kind !== "field" || !withFiltering && member.name === "filtering" || !withPagination && member.name === "pagination" ? null : /* @__PURE__ */ jsx(MemberField, { member, ...props }, member.key)), $[4] = props, $[5] = withFiltering, $[6] = withPagination, $[7] = t2) : t2 = $[7];
45
+ let t3;
46
+ return $[8] !== t2 ? (t3 = /* @__PURE__ */ jsx(Stack, { space: 4, children: t2 }), $[8] = t2, $[9] = t3) : t3 = $[9], t3;
47
+ }
10
48
  function CollectionRefInput(t0) {
11
49
  const $ = c(14);
12
50
  let pluginConfig, props;
@@ -42,7 +80,10 @@ function CollectionRefInput(t0) {
42
80
  schemaType: patchedSchemaType
43
81
  }), $[11] = patchedSchemaType, $[12] = props, $[13] = t2) : t2 = $[13], t2;
44
82
  }
45
- const typeName = "wild.referenceCollection", wildSanityReferenceCollectionFieldPlugin = definePlugin((config) => ({
83
+ const typeName = "wild.referenceCollection", ReferenceCollectionExtension = {
84
+ filtering: "filtering",
85
+ pagination: "pagination"
86
+ }, wildSanityReferenceCollectionFieldPlugin = definePlugin((config) => ({
46
87
  name: "@madebywild/sanity-reference-collection-field",
47
88
  schema: {
48
89
  types: [defineType({
@@ -55,6 +96,10 @@ const typeName = "wild.referenceCollection", wildSanityReferenceCollectionFieldP
55
96
  message: "Select a reference.",
56
97
  path: ["collectionRef"]
57
98
  }),
99
+ components: {
100
+ field: (props) => props.schemaType.options.inline ? props.children : props.renderDefault(props),
101
+ input: (props) => /* @__PURE__ */ jsx(CollectionFieldInput, { ...props })
102
+ },
58
103
  fields: [defineField({
59
104
  name: "collectionRef",
60
105
  type: "string",
@@ -67,6 +112,38 @@ const typeName = "wild.referenceCollection", wildSanityReferenceCollectionFieldP
67
112
  layout: "dropdown",
68
113
  list: config.referenceKinds
69
114
  }
115
+ }), defineField({
116
+ type: "object",
117
+ name: "options",
118
+ title: "Options",
119
+ description: "Configure how to render the collection.",
120
+ options: {
121
+ collapsed: !1,
122
+ collapsible: !1
123
+ },
124
+ components: {
125
+ field: (props) => /* @__PURE__ */ jsx(CollectionOptionsField, { ...props }),
126
+ input: (props) => /* @__PURE__ */ jsx(CollectionOptionsInput, { ...props })
127
+ },
128
+ fields: [defineField({
129
+ name: "withFilters",
130
+ type: "boolean",
131
+ title: "With Filters",
132
+ description: "Enable filters for the collection.",
133
+ initialValue: !1,
134
+ options: {
135
+ layout: "switch"
136
+ }
137
+ }), defineField({
138
+ name: "withPagination",
139
+ type: "boolean",
140
+ title: "With Pagination",
141
+ description: "Enable pagination for the collection.",
142
+ initialValue: !1,
143
+ options: {
144
+ layout: "switch"
145
+ }
146
+ })]
70
147
  })],
71
148
  preview: {
72
149
  select: {
@@ -85,6 +162,7 @@ const typeName = "wild.referenceCollection", wildSanityReferenceCollectionFieldP
85
162
  }
86
163
  }));
87
164
  export {
165
+ ReferenceCollectionExtension,
88
166
  typeName,
89
167
  wildSanityReferenceCollectionFieldPlugin
90
168
  };
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 * 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;AAAEN,WAAOD,MAAKI;AAAZ,YAAAF;AAAAA,IAAwB;AAG5C,UAAA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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;"}
1
+ {"version":3,"file":"index.js","sources":["../src/input.tsx","../src/types.tsx","../src/index.tsx"],"sourcesContent":["import { 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 } from \"./types\";\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 {props.renderDefault(props)}\n </FieldContext.Provider>\n );\n}\n\nfunction CollectionOptionsField(props: ObjectFieldProps) {\n const { options } = useFieldCtx();\n const withFiltering = options?.extensions?.includes(\"filtering\");\n const withPagination = options?.extensions?.includes(\"pagination\");\n\n // No extensions means we can hide the whole field.\n if (!withFiltering && !withPagination) {\n return null;\n }\n\n return props.renderDefault(props);\n}\n\nfunction CollectionOptionsInput(props: ObjectInputProps) {\n const { options } = useFieldCtx();\n const withPagination = options?.extensions?.includes(\"pagination\");\n const withFiltering = options?.extensions?.includes(\"filtering\");\n\n return (\n <Stack space={4}>\n {props.members.map((member: ObjectMember) => {\n if (member.kind !== \"field\") return null;\n if (!withFiltering && member.name === \"filtering\") return null;\n if (!withPagination && member.name === \"pagination\") return null;\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 const ReferenceCollectionExtension = {\n filtering: \"filtering\",\n pagination: \"pagination\",\n} as const;\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 * - `filtering` - Allows filtering the references.\n * - `pagination` - Allows paginating the references.\n * @example\n * ```ts\n * extensions: [\"filtering\", \"pagination\"],\n * ```\n */\n extensions?: (keyof typeof ReferenceCollectionExtension)[];\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 { type CollectionReference, type FieldOptions, type PluginConfig, ReferenceCollectionExtension, 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 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 ReferenceCollectionExtension,\n type PluginConfig,\n type FieldOptions,\n type CollectionReference,\n};\n"],"names":["FieldContext","React","createContext","useFieldCtx","useContext","normalizeCollection","preset","title","value","CollectionFieldInput","props","$","_c","t0","schemaType","options","t1","t2","renderDefault","t3","CollectionOptionsField","withFiltering","extensions","includes","withPagination","CollectionOptionsInput","members","map","member","kind","name","key","CollectionRefInput","pluginConfig","bb0","references","referenceKinds","length","t4","t5","list","t6","patchedSchemaType","typeName","ReferenceCollectionExtension","filtering","pagination","wildSanityReferenceCollectionFieldPlugin","definePlugin","config","schema","types","defineType","type","description","icon","validation","R","custom","ctx","collectionRef","message","path","components","field","inline","children","input","fields","defineField","layout","collapsed","collapsible","initialValue","preview","select","collection","prepare","subtitle","undefined"],"mappings":";;;;;AAMA,MAAMA,eAAeC,MAAMC,cAA0C,CAAA,CAAE,GACjEC,cAAcA,MAAMF,MAAKG,WAAYJ,YAAY;AAEvD,SAASK,oB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VvC,MAAM;AAAA,UACNuB,MAAM;AAAA,UACN9C,OAAO;AAAA,UACP+C,aAAa;AAAA,UACbmB,cAAc;AAAA,UACd1D,SAAS;AAAA,YAAEuD,QAAQ;AAAA,UAAA;AAAA,QAAS,CAC7B,GACDD,YAAY;AAAA,UACVvC,MAAM;AAAA,UACNuB,MAAM;AAAA,UACN9C,OAAO;AAAA,UACP+C,aAAa;AAAA,UACbmB,cAAc;AAAA,UACd1D,SAAS;AAAA,YAAEuD,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,YACLrE,OAAO;AAAA,YACPuE,UAAUF,aAAa,gBAAgBA,UAAU,KAAKG;AAAAA,UAAAA;AAAAA,QAE1D;AAAA,MAAA;AAAA,IACF,CACD,CAAC;AAAA,EAAA;AAGR,EACD;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@madebywild/sanity-reference-collection-field",
3
- "version": "0.0.1",
3
+ "version": "0.0.4",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "license": "UNLICENSED",
@@ -18,7 +18,7 @@
18
18
  "src"
19
19
  ],
20
20
  "publishConfig": {
21
- "access": "restricted"
21
+ "access": "public"
22
22
  },
23
23
  "peerDependencies": {
24
24
  "@sanity/ui": "^3.1",
package/src/index.tsx CHANGED
@@ -1,6 +1,6 @@
1
1
  import { defineField, definePlugin, defineType } from "sanity";
2
- import { CollectionRefInput } from "./input";
3
- import { type CollectionReference, type FieldOptions, type PluginConfig, typeName } from "./types";
2
+ import { CollectionFieldInput, CollectionOptionsField, CollectionOptionsInput, CollectionRefInput } from "./input";
3
+ import { type CollectionReference, type FieldOptions, type PluginConfig, ReferenceCollectionExtension, typeName } from "./types";
4
4
 
5
5
  /** @public */
6
6
  const wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((config) => {
@@ -17,6 +17,10 @@ const wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((con
17
17
  validation: (R) => {
18
18
  return R.custom((ctx) => (!ctx?.collectionRef ? { message: "Select a reference.", path: ["collectionRef"] } : true));
19
19
  },
20
+ components: {
21
+ field: (props) => (props.schemaType.options.inline ? props.children : props.renderDefault(props)),
22
+ input: (props) => <CollectionFieldInput {...props} />,
23
+ },
20
24
  fields: [
21
25
  defineField({
22
26
  name: "collectionRef",
@@ -31,6 +35,38 @@ const wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((con
31
35
  list: config.referenceKinds,
32
36
  },
33
37
  }),
38
+ defineField({
39
+ type: "object",
40
+ name: "options",
41
+ title: "Options",
42
+ description: "Configure how to render the collection.",
43
+ options: {
44
+ collapsed: false,
45
+ collapsible: false,
46
+ },
47
+ components: {
48
+ field: (props) => <CollectionOptionsField {...props} />,
49
+ input: (props) => <CollectionOptionsInput {...props} />,
50
+ },
51
+ fields: [
52
+ defineField({
53
+ name: "withFilters",
54
+ type: "boolean",
55
+ title: "With Filters",
56
+ description: "Enable filters for the collection.",
57
+ initialValue: false,
58
+ options: { layout: "switch" },
59
+ }),
60
+ defineField({
61
+ name: "withPagination",
62
+ type: "boolean",
63
+ title: "With Pagination",
64
+ description: "Enable pagination for the collection.",
65
+ initialValue: false,
66
+ options: { layout: "switch" },
67
+ }),
68
+ ],
69
+ }),
34
70
  ],
35
71
  preview: {
36
72
  select: {
@@ -49,4 +85,11 @@ const wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((con
49
85
  };
50
86
  });
51
87
 
52
- export { wildSanityReferenceCollectionFieldPlugin, typeName, type PluginConfig, type FieldOptions, type CollectionReference };
88
+ export {
89
+ wildSanityReferenceCollectionFieldPlugin,
90
+ typeName,
91
+ ReferenceCollectionExtension,
92
+ type PluginConfig,
93
+ type FieldOptions,
94
+ type CollectionReference,
95
+ };
package/src/input.tsx CHANGED
@@ -1,17 +1,59 @@
1
+ import { Stack } from "@sanity/ui";
1
2
  import * as React from "react";
2
- import type { StringInputProps } from "sanity";
3
+ import type { ObjectFieldProps } from "sanity";
4
+ import { MemberField, type ObjectInputProps, type ObjectMember, type StringInputProps } from "sanity";
3
5
  import type { CollectionReference, FieldOptions, PluginConfig } from "./types";
4
6
 
7
+ const FieldContext = React.createContext<{ options?: FieldOptions }>({});
8
+ const useFieldCtx = () => React.useContext(FieldContext);
9
+
5
10
  function normalizeCollection(preset: CollectionReference) {
6
11
  return typeof preset === "string" ? { title: preset, value: preset } : preset;
7
12
  }
8
13
 
14
+ function CollectionFieldInput(props: ObjectInputProps) {
15
+ return (
16
+ <FieldContext.Provider value={{ options: props.schemaType.options as FieldOptions | undefined }}>
17
+ {props.renderDefault(props)}
18
+ </FieldContext.Provider>
19
+ );
20
+ }
21
+
22
+ function CollectionOptionsField(props: ObjectFieldProps) {
23
+ const { options } = useFieldCtx();
24
+ const withFiltering = options?.extensions?.includes("filtering");
25
+ const withPagination = options?.extensions?.includes("pagination");
26
+
27
+ // No extensions means we can hide the whole field.
28
+ if (!withFiltering && !withPagination) {
29
+ return null;
30
+ }
31
+
32
+ return props.renderDefault(props);
33
+ }
34
+
35
+ function CollectionOptionsInput(props: ObjectInputProps) {
36
+ const { options } = useFieldCtx();
37
+ const withPagination = options?.extensions?.includes("pagination");
38
+ const withFiltering = options?.extensions?.includes("filtering");
39
+
40
+ return (
41
+ <Stack space={4}>
42
+ {props.members.map((member: ObjectMember) => {
43
+ if (member.kind !== "field") return null;
44
+ if (!withFiltering && member.name === "filtering") return null;
45
+ if (!withPagination && member.name === "pagination") return null;
46
+ return <MemberField key={member.key} member={member} {...props} />;
47
+ })}
48
+ </Stack>
49
+ );
50
+ }
51
+
9
52
  function CollectionRefInput({
10
53
  pluginConfig,
11
54
  ...props
12
55
  }: StringInputProps & {
13
- // biome-ignore lint/suspicious/noConfusingVoidType: it can be void.
14
- pluginConfig?: PluginConfig | void;
56
+ pluginConfig?: PluginConfig;
15
57
  }) {
16
58
  const patchedSchemaType = React.useMemo(() => {
17
59
  // Local options take precedence over plugin config.
@@ -36,4 +78,4 @@ function CollectionRefInput({
36
78
  });
37
79
  }
38
80
 
39
- export { CollectionRefInput };
81
+ export { CollectionRefInput, CollectionFieldInput, CollectionOptionsField, CollectionOptionsInput };
package/src/types.tsx CHANGED
@@ -6,6 +6,12 @@ export const typeName = "wild.referenceCollection" as const;
6
6
  /** @public */
7
7
  export type CollectionReference = string | { title: string; value: string };
8
8
 
9
+ /** @public */
10
+ export const ReferenceCollectionExtension = {
11
+ filtering: "filtering",
12
+ pagination: "pagination",
13
+ } as const;
14
+
9
15
  /** @public */
10
16
  export type PluginConfig = {
11
17
  /**
@@ -21,6 +27,11 @@ export type PluginConfig = {
21
27
 
22
28
  /** @public */
23
29
  export type FieldOptions = ObjectOptions & {
30
+ /**
31
+ * Whether the media field should be displayed inline.
32
+ * This will render all children inline on the same level.
33
+ */
34
+ inline?: boolean;
24
35
  /**
25
36
  * A list of references the user can choose from.
26
37
  * Will take precedence over the plugin-level `referenceKinds`.
@@ -30,13 +41,23 @@ export type FieldOptions = ObjectOptions & {
30
41
  * ```
31
42
  */
32
43
  referenceKinds?: CollectionReference[];
44
+ /**
45
+ * List of extension that add additional behaviors to the link field.
46
+ * - `filtering` - Allows filtering the references.
47
+ * - `pagination` - Allows paginating the references.
48
+ * @example
49
+ * ```ts
50
+ * extensions: ["filtering", "pagination"],
51
+ * ```
52
+ */
53
+ extensions?: (keyof typeof ReferenceCollectionExtension)[];
33
54
  };
34
55
 
35
56
  // Add the custom field definition to Sanity's intrinsic definitions
36
57
  // so that type checking works correctly when using this field type.
37
58
  declare module "sanity" {
38
59
  export interface IntrinsicDefinitions {
39
- [typeName]: Omit<ObjectDefinition, "type" | "fields" | "options"> & {
60
+ [typeName]: Omit<ObjectDefinition, "type" | "fields" | "options" | "components"> & {
40
61
  type: typeof typeName;
41
62
  options?: FieldOptions;
42
63
  };