@madebywild/sanity-reference-collection-field 0.0.5 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Made by Wild
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1 +1,44 @@
1
+ > [!IMPORTANT]
2
+ > This package is primarily intended for internal use.
3
+
1
4
  # @madebywild/sanity-reference-collection-field
5
+
6
+ Sanity field for selecting and configuring predefined reference collections.
7
+
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ pnpm add @madebywild/sanity-reference-collection-field
13
+ ```
14
+
15
+ ## Configure Plugin
16
+
17
+ ```ts
18
+ import { wildSanityReferenceCollectionFieldPlugin } from "@madebywild/sanity-reference-collection-field";
19
+
20
+ export default defineConfig({
21
+ plugins: [
22
+ wildSanityReferenceCollectionFieldPlugin({
23
+ referenceKinds: ["post", "project", "teamMember"],
24
+ }),
25
+ ],
26
+ });
27
+ ```
28
+
29
+ ## Use in Schema
30
+
31
+ ```ts
32
+ defineField({
33
+ name: "feed",
34
+ type: "wild.referenceCollection",
35
+ options: {
36
+ // Can be narrower than the plugin-level `referenceKinds`.
37
+ referenceKinds: ["post", "project"],
38
+ extensions: {
39
+ filtering: true,
40
+ pagination: true,
41
+ },
42
+ },
43
+ });
44
+ ```
package/dist/index.cjs CHANGED
@@ -1,6 +1,6 @@
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"), ui = require("@sanity/ui"), React = require("react");
3
+ var jsxRuntime = require("react/jsx-runtime"), sanity = require("sanity"), compilerRuntime = require("react/compiler-runtime"), compactObjectInput = require("@madebywild/sanity-utils/compact-object-input"), ui = require("@sanity/ui"), React = require("react");
4
4
  function _interopNamespaceCompat(e) {
5
5
  if (e && typeof e == "object" && "default" in e) return e;
6
6
  var n = /* @__PURE__ */ Object.create(null);
@@ -17,6 +17,9 @@ function _interopNamespaceCompat(e) {
17
17
  }), n.default = e, Object.freeze(n);
18
18
  }
19
19
  var React__namespace = /* @__PURE__ */ _interopNamespaceCompat(React);
20
+ function isExtensionEnabled(extensions, key) {
21
+ return extensions[key] && (extensions[key] === !0 || extensions[key].enabled !== !1);
22
+ }
20
23
  const FieldContext = React__namespace.createContext({}), useFieldCtx = () => React__namespace.useContext(FieldContext);
21
24
  function normalizeCollection(preset) {
22
25
  return typeof preset == "string" ? {
@@ -31,33 +34,27 @@ function CollectionFieldInput(props) {
31
34
  options: t0
32
35
  }, $[0] = t0, $[1] = t1) : t1 = $[1];
33
36
  let t2;
34
- $[2] !== props ? (t2 = props.renderDefault(props), $[2] = props, $[3] = t2) : t2 = $[3];
37
+ $[2] !== props ? (t2 = /* @__PURE__ */ jsxRuntime.jsx(compactObjectInput.CompactObjectInput, { space: 4, ...props }), $[2] = props, $[3] = t2) : t2 = $[3];
35
38
  let t3;
36
39
  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
40
  }
38
41
  function CollectionOptionsField(props) {
39
42
  const $ = compilerRuntime.c(2), {
40
43
  options
41
- } = useFieldCtx(), withFiltering = options?.extensions?.includes("filtering"), withPagination = options?.extensions?.includes("pagination");
42
- if (!withFiltering && !withPagination)
44
+ } = useFieldCtx();
45
+ if (!options?.extensions || !Object.keys(options?.extensions).length)
43
46
  return null;
44
47
  let t0;
45
- return $[0] !== props ? (t0 = props.renderDefault(props), $[0] = props, $[1] = t0) : t0 = $[1], t0;
48
+ return $[0] !== props.children ? (t0 = /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: props.children }), $[0] = props.children, $[1] = t0) : t0 = $[1], t0;
46
49
  }
47
50
  function CollectionOptionsInput(props) {
48
- const $ = compilerRuntime.c(10), {
51
+ const $ = compilerRuntime.c(5), {
49
52
  options
50
- } = useFieldCtx();
53
+ } = useFieldCtx(), extensions = options?.extensions;
51
54
  let t0;
52
- $[0] !== options?.extensions ? (t0 = options?.extensions?.includes("pagination"), $[0] = options?.extensions, $[1] = t0) : t0 = $[1];
53
- const withPagination = t0;
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];
54
56
  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;
57
+ return $[3] !== t0 ? (t1 = /* @__PURE__ */ jsxRuntime.jsx(ui.Stack, { space: 2, children: t0 }), $[3] = t0, $[4] = t1) : t1 = $[4], t1;
61
58
  }
62
59
  function CollectionRefInput(t0) {
63
60
  const $ = compilerRuntime.c(14);
@@ -94,10 +91,7 @@ function CollectionRefInput(t0) {
94
91
  schemaType: patchedSchemaType
95
92
  }), $[11] = patchedSchemaType, $[12] = props, $[13] = t2) : t2 = $[13], t2;
96
93
  }
97
- const typeName = "wild.referenceCollection", ReferenceCollectionExtension = {
98
- filtering: "filtering",
99
- pagination: "pagination"
100
- }, wildSanityReferenceCollectionFieldPlugin = sanity.definePlugin((config) => ({
94
+ const typeName = "wild.referenceCollection", wildSanityReferenceCollectionFieldPlugin = sanity.definePlugin((config) => ({
101
95
  name: "@madebywild/sanity-reference-collection-field",
102
96
  schema: {
103
97
  types: [sanity.defineType({
@@ -106,7 +100,7 @@ const typeName = "wild.referenceCollection", ReferenceCollectionExtension = {
106
100
  title: "Reference Collection",
107
101
  description: "Create a collection of specific kinds.",
108
102
  icon: () => /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: "\u{1F503}" }),
109
- validation: (R) => R.custom((ctx) => ctx?.collectionRef ? !0 : {
103
+ validation: (R) => R.custom((value) => value?.collectionRef ? !0 : {
110
104
  message: "Select a reference.",
111
105
  path: ["collectionRef"]
112
106
  }),
@@ -175,7 +169,6 @@ const typeName = "wild.referenceCollection", ReferenceCollectionExtension = {
175
169
  })]
176
170
  }
177
171
  }));
178
- exports.ReferenceCollectionExtension = ReferenceCollectionExtension;
179
172
  exports.typeName = typeName;
180
173
  exports.wildSanityReferenceCollectionFieldPlugin = wildSanityReferenceCollectionFieldPlugin;
181
174
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
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,MA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AS;AAAA,YAAE2D,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,YACLzE,OAAO;AAAA,YACP2E,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={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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O;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;;;"}
package/dist/index.d.cts CHANGED
@@ -8,10 +8,16 @@ 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
- };
11
+ type ReferenceCollectionExtensions = Partial<{
12
+ /** Allows filtering the references. */
13
+ filtering: boolean | {
14
+ enabled?: boolean;
15
+ };
16
+ /** Allows paginating the references. */
17
+ pagination: boolean | {
18
+ enabled?: boolean;
19
+ };
20
+ }>;
15
21
  /** @public */
16
22
  type PluginConfig = {
17
23
  /**
@@ -42,14 +48,8 @@ type FieldOptions = ObjectOptions & {
42
48
  referenceKinds?: CollectionReference[];
43
49
  /**
44
50
  * 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
51
  */
52
- extensions?: (keyof typeof ReferenceCollectionExtension)[];
52
+ extensions?: ReferenceCollectionExtensions;
53
53
  };
54
54
  declare module "sanity" {
55
55
  interface IntrinsicDefinitions {
@@ -61,4 +61,4 @@ declare module "sanity" {
61
61
  }
62
62
  /** @public */
63
63
  declare const wildSanityReferenceCollectionFieldPlugin: sanity0.Plugin<PluginConfig>;
64
- export { type CollectionReference, type FieldOptions, type PluginConfig, ReferenceCollectionExtension, typeName, wildSanityReferenceCollectionFieldPlugin };
64
+ export { type CollectionReference, type FieldOptions, type PluginConfig, type ReferenceCollectionExtensions, typeName, wildSanityReferenceCollectionFieldPlugin };
package/dist/index.d.ts CHANGED
@@ -8,10 +8,16 @@ 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
- };
11
+ type ReferenceCollectionExtensions = Partial<{
12
+ /** Allows filtering the references. */
13
+ filtering: boolean | {
14
+ enabled?: boolean;
15
+ };
16
+ /** Allows paginating the references. */
17
+ pagination: boolean | {
18
+ enabled?: boolean;
19
+ };
20
+ }>;
15
21
  /** @public */
16
22
  type PluginConfig = {
17
23
  /**
@@ -42,14 +48,8 @@ type FieldOptions = ObjectOptions & {
42
48
  referenceKinds?: CollectionReference[];
43
49
  /**
44
50
  * 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
51
  */
52
- extensions?: (keyof typeof ReferenceCollectionExtension)[];
52
+ extensions?: ReferenceCollectionExtensions;
53
53
  };
54
54
  declare module "sanity" {
55
55
  interface IntrinsicDefinitions {
@@ -61,4 +61,4 @@ declare module "sanity" {
61
61
  }
62
62
  /** @public */
63
63
  declare const wildSanityReferenceCollectionFieldPlugin: sanity0.Plugin<PluginConfig>;
64
- export { type CollectionReference, type FieldOptions, type PluginConfig, ReferenceCollectionExtension, typeName, wildSanityReferenceCollectionFieldPlugin };
64
+ export { type CollectionReference, type FieldOptions, type PluginConfig, type ReferenceCollectionExtensions, typeName, wildSanityReferenceCollectionFieldPlugin };
package/dist/index.js CHANGED
@@ -1,8 +1,12 @@
1
1
  import { jsx, Fragment } from "react/jsx-runtime";
2
2
  import { MemberField, definePlugin, defineType, defineField } from "sanity";
3
3
  import { c } from "react/compiler-runtime";
4
+ import { CompactObjectInput } from "@madebywild/sanity-utils/compact-object-input";
4
5
  import { Stack } from "@sanity/ui";
5
6
  import * as React from "react";
7
+ function isExtensionEnabled(extensions, key) {
8
+ return extensions[key] && (extensions[key] === !0 || extensions[key].enabled !== !1);
9
+ }
6
10
  const FieldContext = React.createContext({}), useFieldCtx = () => React.useContext(FieldContext);
7
11
  function normalizeCollection(preset) {
8
12
  return typeof preset == "string" ? {
@@ -17,33 +21,27 @@ function CollectionFieldInput(props) {
17
21
  options: t0
18
22
  }, $[0] = t0, $[1] = t1) : t1 = $[1];
19
23
  let t2;
20
- $[2] !== props ? (t2 = props.renderDefault(props), $[2] = props, $[3] = t2) : t2 = $[3];
24
+ $[2] !== props ? (t2 = /* @__PURE__ */ jsx(CompactObjectInput, { space: 4, ...props }), $[2] = props, $[3] = t2) : t2 = $[3];
21
25
  let t3;
22
26
  return $[4] !== t1 || $[5] !== t2 ? (t3 = /* @__PURE__ */ jsx(FieldContext.Provider, { value: t1, children: t2 }), $[4] = t1, $[5] = t2, $[6] = t3) : t3 = $[6], t3;
23
27
  }
24
28
  function CollectionOptionsField(props) {
25
29
  const $ = c(2), {
26
30
  options
27
- } = useFieldCtx(), withFiltering = options?.extensions?.includes("filtering"), withPagination = options?.extensions?.includes("pagination");
28
- if (!withFiltering && !withPagination)
31
+ } = useFieldCtx();
32
+ if (!options?.extensions || !Object.keys(options?.extensions).length)
29
33
  return null;
30
34
  let t0;
31
- return $[0] !== props ? (t0 = props.renderDefault(props), $[0] = props, $[1] = t0) : t0 = $[1], t0;
35
+ return $[0] !== props.children ? (t0 = /* @__PURE__ */ jsx(Fragment, { children: props.children }), $[0] = props.children, $[1] = t0) : t0 = $[1], t0;
32
36
  }
33
37
  function CollectionOptionsInput(props) {
34
- const $ = c(10), {
38
+ const $ = c(5), {
35
39
  options
36
- } = useFieldCtx();
40
+ } = useFieldCtx(), extensions = options?.extensions;
37
41
  let t0;
38
- $[0] !== options?.extensions ? (t0 = options?.extensions?.includes("pagination"), $[0] = options?.extensions, $[1] = t0) : t0 = $[1];
39
- const withPagination = t0;
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];
40
43
  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;
44
+ return $[3] !== t0 ? (t1 = /* @__PURE__ */ jsx(Stack, { space: 2, children: t0 }), $[3] = t0, $[4] = t1) : t1 = $[4], t1;
47
45
  }
48
46
  function CollectionRefInput(t0) {
49
47
  const $ = c(14);
@@ -80,10 +78,7 @@ function CollectionRefInput(t0) {
80
78
  schemaType: patchedSchemaType
81
79
  }), $[11] = patchedSchemaType, $[12] = props, $[13] = t2) : t2 = $[13], t2;
82
80
  }
83
- const typeName = "wild.referenceCollection", ReferenceCollectionExtension = {
84
- filtering: "filtering",
85
- pagination: "pagination"
86
- }, wildSanityReferenceCollectionFieldPlugin = definePlugin((config) => ({
81
+ const typeName = "wild.referenceCollection", wildSanityReferenceCollectionFieldPlugin = definePlugin((config) => ({
87
82
  name: "@madebywild/sanity-reference-collection-field",
88
83
  schema: {
89
84
  types: [defineType({
@@ -92,7 +87,7 @@ const typeName = "wild.referenceCollection", ReferenceCollectionExtension = {
92
87
  title: "Reference Collection",
93
88
  description: "Create a collection of specific kinds.",
94
89
  icon: () => /* @__PURE__ */ jsx(Fragment, { children: "\u{1F503}" }),
95
- validation: (R) => R.custom((ctx) => ctx?.collectionRef ? !0 : {
90
+ validation: (R) => R.custom((value) => value?.collectionRef ? !0 : {
96
91
  message: "Select a reference.",
97
92
  path: ["collectionRef"]
98
93
  }),
@@ -162,7 +157,6 @@ const typeName = "wild.referenceCollection", ReferenceCollectionExtension = {
162
157
  }
163
158
  }));
164
159
  export {
165
- ReferenceCollectionExtension,
166
160
  typeName,
167
161
  wildSanityReferenceCollectionFieldPlugin
168
162
  };
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 { 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,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,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A,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;"}
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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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;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@madebywild/sanity-reference-collection-field",
3
- "version": "0.0.5",
3
+ "version": "1.0.0",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "license": "UNLICENSED",
@@ -20,19 +20,22 @@
20
20
  "publishConfig": {
21
21
  "access": "public"
22
22
  },
23
+ "dependencies": {
24
+ "@madebywild/sanity-utils": "1.0.0"
25
+ },
23
26
  "peerDependencies": {
24
27
  "@sanity/ui": "^3.1",
25
- "react": "^19",
26
- "react-dom": "^19",
28
+ "react": "^19.2.2",
29
+ "react-dom": "^19.2.2",
27
30
  "sanity": "^4.17 || ^5.0.0"
28
31
  },
29
32
  "devDependencies": {
30
33
  "@sanity/pkg-utils": "^9.2",
31
- "@types/react": "^19",
32
- "@types/react-dom": "^19",
34
+ "@types/react": "^19.2.2",
35
+ "@types/react-dom": "^19.2.2",
33
36
  "babel-plugin-react-compiler": "1.0.0",
34
- "react": "^19",
35
- "react-dom": "^19",
37
+ "react": "^19.2.2",
38
+ "react-dom": "^19.2.2",
36
39
  "sanity": "^4.17 || ^5.0.0",
37
40
  "typescript": "^5"
38
41
  },
package/src/index.tsx CHANGED
@@ -1,6 +1,12 @@
1
1
  import { defineField, definePlugin, defineType } from "sanity";
2
2
  import { CollectionFieldInput, CollectionOptionsField, CollectionOptionsInput, CollectionRefInput } from "./input";
3
- import { type CollectionReference, type FieldOptions, type PluginConfig, ReferenceCollectionExtension, typeName } from "./types";
3
+ import {
4
+ type CollectionReference,
5
+ type FieldOptions,
6
+ type PluginConfig,
7
+ type ReferenceCollectionExtensions,
8
+ typeName,
9
+ } from "./types";
4
10
 
5
11
  /** @public */
6
12
  const wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((config) => {
@@ -15,7 +21,9 @@ const wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((con
15
21
  description: "Create a collection of specific kinds.",
16
22
  icon: () => <>🔃</>,
17
23
  validation: (R) => {
18
- return R.custom((ctx) => (!ctx?.collectionRef ? { message: "Select a reference.", path: ["collectionRef"] } : true));
24
+ return R.custom((value) => {
25
+ return !value?.collectionRef ? { message: "Select a reference.", path: ["collectionRef"] } : true;
26
+ });
19
27
  },
20
28
  components: {
21
29
  field: (props) => (props.schemaType.options.inline ? props.children : props.renderDefault(props)),
@@ -88,7 +96,7 @@ const wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((con
88
96
  export {
89
97
  wildSanityReferenceCollectionFieldPlugin,
90
98
  typeName,
91
- ReferenceCollectionExtension,
99
+ type ReferenceCollectionExtensions,
92
100
  type PluginConfig,
93
101
  type FieldOptions,
94
102
  type CollectionReference,
package/src/input.tsx CHANGED
@@ -1,8 +1,13 @@
1
+ import { CompactObjectInput } from "@madebywild/sanity-utils/compact-object-input";
1
2
  import { Stack } from "@sanity/ui";
2
3
  import * as React from "react";
3
4
  import type { ObjectFieldProps } from "sanity";
4
5
  import { MemberField, type ObjectInputProps, type ObjectMember, type StringInputProps } from "sanity";
5
- import type { CollectionReference, FieldOptions, PluginConfig } from "./types";
6
+ import type { CollectionReference, FieldOptions, PluginConfig, ReferenceCollectionExtensions } from "./types";
7
+
8
+ function isExtensionEnabled(extensions: ReferenceCollectionExtensions, key: keyof ReferenceCollectionExtensions) {
9
+ return extensions[key] && (extensions[key] === true || extensions[key].enabled !== false);
10
+ }
6
11
 
7
12
  const FieldContext = React.createContext<{ options?: FieldOptions }>({});
8
13
  const useFieldCtx = () => React.useContext(FieldContext);
@@ -14,35 +19,35 @@ function normalizeCollection(preset: CollectionReference) {
14
19
  function CollectionFieldInput(props: ObjectInputProps) {
15
20
  return (
16
21
  <FieldContext.Provider value={{ options: props.schemaType.options as FieldOptions | undefined }}>
17
- {props.renderDefault(props)}
22
+ <CompactObjectInput space={4} {...props} />
18
23
  </FieldContext.Provider>
19
24
  );
20
25
  }
21
26
 
22
27
  function CollectionOptionsField(props: ObjectFieldProps) {
23
28
  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);
29
+ // We can hide the field if no extensions are enabled.
30
+ if (!options?.extensions || !Object.keys(options?.extensions).length) return null;
31
+ return <>{props.children}</>;
33
32
  }
34
33
 
35
34
  function CollectionOptionsInput(props: ObjectInputProps) {
36
35
  const { options } = useFieldCtx();
37
- const withPagination = options?.extensions?.includes("pagination");
38
- const withFiltering = options?.extensions?.includes("filtering");
36
+ const extensions = options?.extensions;
39
37
 
40
38
  return (
41
- <Stack space={4}>
39
+ <Stack space={2}>
42
40
  {props.members.map((member: ObjectMember) => {
43
41
  if (member.kind !== "field") return null;
44
- if (!withFiltering && member.name === "filtering") return null;
45
- if (!withPagination && member.name === "pagination") return null;
42
+
43
+ // Custom filtering extension.
44
+ const filteringEnabled = extensions && isExtensionEnabled(extensions, "filtering");
45
+ if (!filteringEnabled && member.name === "withFilters") return null;
46
+
47
+ // Custom pagination extension.
48
+ const paginationEnabled = extensions && isExtensionEnabled(extensions, "pagination");
49
+ if (!paginationEnabled && member.name === "withPagination") return null;
50
+
46
51
  return <MemberField key={member.key} member={member} {...props} />;
47
52
  })}
48
53
  </Stack>
package/src/types.tsx CHANGED
@@ -7,10 +7,12 @@ export const typeName = "wild.referenceCollection" as const;
7
7
  export type CollectionReference = string | { title: string; value: string };
8
8
 
9
9
  /** @public */
10
- export const ReferenceCollectionExtension = {
11
- filtering: "filtering",
12
- pagination: "pagination",
13
- } as const;
10
+ export type ReferenceCollectionExtensions = Partial<{
11
+ /** Allows filtering the references. */
12
+ filtering: boolean | { enabled?: boolean };
13
+ /** Allows paginating the references. */
14
+ pagination: boolean | { enabled?: boolean };
15
+ }>;
14
16
 
15
17
  /** @public */
16
18
  export type PluginConfig = {
@@ -43,14 +45,8 @@ export type FieldOptions = ObjectOptions & {
43
45
  referenceKinds?: CollectionReference[];
44
46
  /**
45
47
  * 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
48
  */
53
- extensions?: (keyof typeof ReferenceCollectionExtension)[];
49
+ extensions?: ReferenceCollectionExtensions;
54
50
  };
55
51
 
56
52
  // Add the custom field definition to Sanity's intrinsic definitions