@madebywild/sanity-reference-collection-field 0.0.5 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +69 -0
- package/dist/index.cjs +48 -30
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +14 -12
- package/dist/index.d.ts +14 -12
- package/dist/index.js +49 -30
- package/dist/index.js.map +1 -1
- package/package.json +10 -7
- package/src/index.tsx +44 -13
- package/src/input.tsx +20 -15
- package/src/types.tsx +7 -11
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,70 @@
|
|
|
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
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
pnpm add @madebywild/sanity-reference-collection-field
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Configure Plugin
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { defineConfig } from "sanity";
|
|
18
|
+
import { wildSanityReferenceCollectionFieldPlugin } from "@madebywild/sanity-reference-collection-field";
|
|
19
|
+
|
|
20
|
+
export default defineConfig({
|
|
21
|
+
plugins: [
|
|
22
|
+
wildSanityReferenceCollectionFieldPlugin({
|
|
23
|
+
referenceKinds: [
|
|
24
|
+
"post",
|
|
25
|
+
{ title: "Projects", value: "project" },
|
|
26
|
+
{ title: "People", value: "person" },
|
|
27
|
+
],
|
|
28
|
+
}),
|
|
29
|
+
],
|
|
30
|
+
});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Use in Schema
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { defineField } from "sanity";
|
|
37
|
+
|
|
38
|
+
defineField({
|
|
39
|
+
name: "feed",
|
|
40
|
+
type: "wild.referenceCollection",
|
|
41
|
+
options: {
|
|
42
|
+
inline: true,
|
|
43
|
+
referenceKinds: ["post", { title: "Featured Projects", value: "project" }],
|
|
44
|
+
extensions: {
|
|
45
|
+
// each supports `true` or { enabled: true/false }
|
|
46
|
+
filtering: {
|
|
47
|
+
enabled: true,
|
|
48
|
+
filterByKinds: ["post", "project"],
|
|
49
|
+
},
|
|
50
|
+
pagination: {
|
|
51
|
+
enabled: true,
|
|
52
|
+
pageCount: 12,
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
// Example editor value shape
|
|
61
|
+
{
|
|
62
|
+
collectionRef: "post",
|
|
63
|
+
options: {
|
|
64
|
+
withFilters: true,
|
|
65
|
+
filterByKinds: ["post", "project"],
|
|
66
|
+
withPagination: true,
|
|
67
|
+
pageCount: 12,
|
|
68
|
+
},
|
|
69
|
+
}
|
|
70
|
+
```
|
package/dist/index.cjs
CHANGED
|
@@ -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 =
|
|
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()
|
|
42
|
-
if (!
|
|
44
|
+
} = useFieldCtx();
|
|
45
|
+
if (!options?.extensions || !Object.keys(options?.extensions).length)
|
|
43
46
|
return null;
|
|
44
47
|
let t0;
|
|
45
|
-
return $[0] !== props ? (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(
|
|
51
|
+
const $ = compilerRuntime.c(5), {
|
|
49
52
|
options
|
|
50
|
-
} = useFieldCtx();
|
|
53
|
+
} = useFieldCtx(), extensions = options?.extensions;
|
|
51
54
|
let t0;
|
|
52
|
-
$[0] !==
|
|
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
|
-
$[
|
|
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: 4, children: t0 }), $[3] = t0, $[4] = t1) : t1 = $[4], t1;
|
|
61
58
|
}
|
|
62
59
|
function CollectionRefInput(t0) {
|
|
63
60
|
const $ = compilerRuntime.c(14);
|
|
@@ -94,19 +91,16 @@ 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",
|
|
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({
|
|
104
98
|
name: typeName,
|
|
105
99
|
type: "object",
|
|
106
100
|
title: "Reference Collection",
|
|
107
|
-
description: "
|
|
101
|
+
description: "Choose a content type and control how the collection is displayed.",
|
|
108
102
|
icon: () => /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: "\u{1F503}" }),
|
|
109
|
-
validation: (R) => R.custom((
|
|
103
|
+
validation: (R) => R.custom((value) => value?.collectionRef ? !0 : {
|
|
110
104
|
message: "Select a reference.",
|
|
111
105
|
path: ["collectionRef"]
|
|
112
106
|
}),
|
|
@@ -117,8 +111,8 @@ const typeName = "wild.referenceCollection", ReferenceCollectionExtension = {
|
|
|
117
111
|
fields: [sanity.defineField({
|
|
118
112
|
name: "collectionRef",
|
|
119
113
|
type: "string",
|
|
120
|
-
title: "
|
|
121
|
-
description: "
|
|
114
|
+
title: "Collection Type",
|
|
115
|
+
description: "Choose which type of content this collection should show.",
|
|
122
116
|
components: {
|
|
123
117
|
input: (props) => /* @__PURE__ */ jsxRuntime.jsx(CollectionRefInput, { pluginConfig: config, ...props })
|
|
124
118
|
},
|
|
@@ -129,8 +123,8 @@ const typeName = "wild.referenceCollection", ReferenceCollectionExtension = {
|
|
|
129
123
|
}), sanity.defineField({
|
|
130
124
|
type: "object",
|
|
131
125
|
name: "options",
|
|
132
|
-
title: "
|
|
133
|
-
description: "
|
|
126
|
+
title: "Display Settings",
|
|
127
|
+
description: "Control filters and pagination for this collection.",
|
|
134
128
|
options: {
|
|
135
129
|
collapsed: !1,
|
|
136
130
|
collapsible: !1
|
|
@@ -142,21 +136,46 @@ const typeName = "wild.referenceCollection", ReferenceCollectionExtension = {
|
|
|
142
136
|
fields: [sanity.defineField({
|
|
143
137
|
name: "withFilters",
|
|
144
138
|
type: "boolean",
|
|
145
|
-
title: "
|
|
146
|
-
description: "
|
|
139
|
+
title: "Enable Filters",
|
|
140
|
+
description: "Show filter controls for this collection.",
|
|
147
141
|
initialValue: !1,
|
|
148
142
|
options: {
|
|
149
143
|
layout: "switch"
|
|
150
144
|
}
|
|
145
|
+
}), sanity.defineField({
|
|
146
|
+
name: "filterByKinds",
|
|
147
|
+
type: "array",
|
|
148
|
+
title: "Available Filter Types",
|
|
149
|
+
description: "Choose which content types appear as filter options.",
|
|
150
|
+
hidden: ({
|
|
151
|
+
parent
|
|
152
|
+
}) => !parent?.withFilters,
|
|
153
|
+
of: [sanity.defineArrayMember({
|
|
154
|
+
type: "string"
|
|
155
|
+
})],
|
|
156
|
+
options: {
|
|
157
|
+
layout: "tags"
|
|
158
|
+
},
|
|
159
|
+
validation: (R) => R.unique()
|
|
151
160
|
}), sanity.defineField({
|
|
152
161
|
name: "withPagination",
|
|
153
162
|
type: "boolean",
|
|
154
|
-
title: "
|
|
155
|
-
description: "
|
|
163
|
+
title: "Enable Pagination",
|
|
164
|
+
description: "Split collection results across multiple pages.",
|
|
156
165
|
initialValue: !1,
|
|
157
166
|
options: {
|
|
158
167
|
layout: "switch"
|
|
159
168
|
}
|
|
169
|
+
}), sanity.defineField({
|
|
170
|
+
name: "pageCount",
|
|
171
|
+
type: "number",
|
|
172
|
+
title: "Items Per Page",
|
|
173
|
+
description: "Set how many items to show on each page.",
|
|
174
|
+
hidden: ({
|
|
175
|
+
parent
|
|
176
|
+
}) => !parent?.withPagination,
|
|
177
|
+
initialValue: 12,
|
|
178
|
+
validation: (R) => R.integer().positive()
|
|
160
179
|
})]
|
|
161
180
|
})],
|
|
162
181
|
preview: {
|
|
@@ -175,7 +194,6 @@ const typeName = "wild.referenceCollection", ReferenceCollectionExtension = {
|
|
|
175
194
|
})]
|
|
176
195
|
}
|
|
177
196
|
}));
|
|
178
|
-
exports.ReferenceCollectionExtension = ReferenceCollectionExtension;
|
|
179
197
|
exports.typeName = typeName;
|
|
180
198
|
exports.wildSanityReferenceCollectionFieldPlugin = wildSanityReferenceCollectionFieldPlugin;
|
|
181
199
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/input.tsx","../src/types.tsx","../src/index.tsx"],"sourcesContent":["import { 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,UAAA,EAA8B,OAAAJ,IAC3BC,UAAAA,GAAAA,CACH,GAAwBN,OAAAK,IAAAL,OAAAM,IAAAN,OAAAQ,MAAAA,KAAAR,EAAA,CAAA,GAFxBQ;AAEwB;AAI5B,SAAAE,uBAAAX,OAAA;AAAA,QAAAC,IAAAC,kBAAA,CAAA,GACE;AAAA,IAAAG;AAAAA,EAAAA,IAAoBZ,YAAAA,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ACD;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/input.tsx","../src/types.tsx","../src/index.tsx"],"sourcesContent":["import { CompactObjectInput } from \"@madebywild/sanity-utils/compact-object-input\";\nimport { Stack } from \"@sanity/ui\";\nimport * as React from \"react\";\nimport type { ObjectFieldProps } from \"sanity\";\nimport { MemberField, type ObjectInputProps, type ObjectMember, type StringInputProps } from \"sanity\";\nimport type { CollectionReference, FieldOptions, PluginConfig, ReferenceCollectionExtensions } from \"./types\";\n\nfunction isExtensionEnabled(extensions: ReferenceCollectionExtensions, key: keyof ReferenceCollectionExtensions) {\n return extensions[key] && (extensions[key] === true || extensions[key].enabled !== false);\n}\n\nconst FieldContext = React.createContext<{ options?: FieldOptions }>({});\nconst useFieldCtx = () => React.useContext(FieldContext);\n\nfunction normalizeCollection(preset: CollectionReference) {\n return typeof preset === \"string\" ? { title: preset, value: preset } : preset;\n}\n\nfunction CollectionFieldInput(props: ObjectInputProps) {\n return (\n <FieldContext.Provider value={{ options: props.schemaType.options as FieldOptions | undefined }}>\n <CompactObjectInput space={4} {...props} />\n </FieldContext.Provider>\n );\n}\n\nfunction CollectionOptionsField(props: ObjectFieldProps) {\n const { options } = useFieldCtx();\n // We can hide the field if no extensions are enabled.\n if (!options?.extensions || !Object.keys(options?.extensions).length) return null;\n return <>{props.children}</>;\n}\n\nfunction CollectionOptionsInput(props: ObjectInputProps) {\n const { options } = useFieldCtx();\n const extensions = options?.extensions;\n\n return (\n <Stack space={4}>\n {props.members.map((member: ObjectMember) => {\n if (member.kind !== \"field\") return null;\n\n // Custom filtering extension.\n const filteringEnabled = extensions && isExtensionEnabled(extensions, \"filtering\");\n if (!filteringEnabled && member.name === \"withFilters\") return null;\n\n // Custom pagination extension.\n const paginationEnabled = extensions && isExtensionEnabled(extensions, \"pagination\");\n if (!paginationEnabled && member.name === \"withPagination\") return null;\n\n return <MemberField key={member.key} member={member} {...props} />;\n })}\n </Stack>\n );\n}\n\nfunction CollectionRefInput({\n pluginConfig,\n ...props\n}: StringInputProps & {\n pluginConfig?: PluginConfig;\n}) {\n const patchedSchemaType = React.useMemo(() => {\n // Local options take precedence over plugin config.\n const options = props.schemaType.options as FieldOptions | undefined;\n const references = options?.referenceKinds ?? pluginConfig?.referenceKinds;\n\n // No need to touch anything if the list is unchanged.\n if (!references?.length) return props.schemaType;\n\n return {\n ...props.schemaType,\n options: {\n ...props.schemaType.options,\n list: references.map(normalizeCollection),\n },\n };\n }, [props.schemaType]);\n\n return props.renderDefault({\n ...props,\n schemaType: patchedSchemaType,\n });\n}\n\nexport { CollectionRefInput, CollectionFieldInput, CollectionOptionsField, CollectionOptionsInput };\n","import type { ObjectDefinition, ObjectOptions } from \"sanity\";\n\n/** @public */\nexport const typeName = \"wild.referenceCollection\" as const;\n\n/** @public */\nexport type CollectionReference = string | { title: string; value: string };\n\n/** @public */\nexport type ReferenceCollectionExtensions = Partial<{\n /** Allows filtering the references. */\n filtering: boolean | { enabled?: boolean; filterByKinds?: string[] };\n /** Allows paginating the references. */\n pagination: boolean | { enabled?: boolean; pageCount?: number };\n}>;\n\n/** @public */\nexport type PluginConfig = {\n /**\n * A list of references the user can choose from.\n * Can be overridden per-field via `options.referenceKinds`.\n * @example\n * ```ts\n * referenceKinds: [\"blogPost\", \"some-type\"],\n * ```\n */\n referenceKinds: CollectionReference[];\n};\n\n/** @public */\nexport type FieldOptions = ObjectOptions & {\n /**\n * Whether the media field should be displayed inline.\n * This will render all children inline on the same level.\n */\n inline?: boolean;\n /**\n * A list of references the user can choose from.\n * Will take precedence over the plugin-level `referenceKinds`.\n * @example\n * ```ts\n * referenceKinds: [\"blogPost\", \"some-type\"],\n * ```\n */\n referenceKinds?: CollectionReference[];\n /**\n * List of extension that add additional behaviors to the link field.\n */\n extensions?: ReferenceCollectionExtensions;\n};\n\n// Add the custom field definition to Sanity's intrinsic definitions\n// so that type checking works correctly when using this field type.\ndeclare module \"sanity\" {\n export interface IntrinsicDefinitions {\n [typeName]: Omit<ObjectDefinition, \"type\" | \"fields\" | \"options\" | \"components\"> & {\n type: typeof typeName;\n options?: FieldOptions;\n };\n }\n}\n","import { defineArrayMember, defineField, definePlugin, defineType } from \"sanity\";\nimport { CollectionFieldInput, CollectionOptionsField, CollectionOptionsInput, CollectionRefInput } from \"./input\";\nimport {\n type CollectionReference,\n type FieldOptions,\n type PluginConfig,\n type ReferenceCollectionExtensions,\n typeName,\n} from \"./types\";\n\n/** @public */\nconst wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((config) => {\n return {\n name: \"@madebywild/sanity-reference-collection-field\",\n schema: {\n types: [\n defineType({\n name: typeName,\n type: \"object\",\n title: \"Reference Collection\",\n description: \"Choose a content type and control how the collection is displayed.\",\n icon: () => <>🔃</>,\n validation: (R) => {\n return R.custom((value) => {\n return !value?.collectionRef ? { message: \"Select a reference.\", path: [\"collectionRef\"] } : true;\n });\n },\n components: {\n field: (props) => (props.schemaType.options.inline ? props.children : props.renderDefault(props)),\n input: (props) => <CollectionFieldInput {...props} />,\n },\n fields: [\n defineField({\n name: \"collectionRef\",\n type: \"string\",\n title: \"Collection Type\",\n description: \"Choose which type of content this collection should show.\",\n components: {\n input: (props) => <CollectionRefInput pluginConfig={config} {...props} />,\n },\n options: {\n layout: \"dropdown\",\n list: config.referenceKinds,\n },\n }),\n defineField({\n type: \"object\",\n name: \"options\",\n title: \"Display Settings\",\n description: \"Control filters and pagination for this collection.\",\n options: {\n collapsed: false,\n collapsible: false,\n },\n components: {\n field: (props) => <CollectionOptionsField {...props} />,\n input: (props) => <CollectionOptionsInput {...props} />,\n },\n fields: [\n defineField({\n name: \"withFilters\",\n type: \"boolean\",\n title: \"Enable Filters\",\n description: \"Show filter controls for this collection.\",\n initialValue: false,\n options: { layout: \"switch\" },\n }),\n defineField({\n name: \"filterByKinds\",\n type: \"array\",\n title: \"Available Filter Types\",\n description: \"Choose which content types appear as filter options.\",\n hidden: ({ parent }) => !parent?.withFilters,\n of: [\n defineArrayMember({\n type: \"string\",\n }),\n ],\n options: { layout: \"tags\" },\n validation: (R) => R.unique(),\n }),\n defineField({\n name: \"withPagination\",\n type: \"boolean\",\n title: \"Enable Pagination\",\n description: \"Split collection results across multiple pages.\",\n initialValue: false,\n options: { layout: \"switch\" },\n }),\n defineField({\n name: \"pageCount\",\n type: \"number\",\n title: \"Items Per Page\",\n description: \"Set how many items to show on each page.\",\n hidden: ({ parent }) => !parent?.withPagination,\n initialValue: 12,\n validation: (R) => R.integer().positive(),\n }),\n ],\n }),\n ],\n preview: {\n select: {\n collection: \"collectionRef\",\n },\n prepare({ collection }) {\n return {\n title: \"Reference Collection\",\n subtitle: collection ? `Reference to ${collection}` : undefined,\n };\n },\n },\n }),\n ],\n },\n };\n});\n\nexport {\n wildSanityReferenceCollectionFieldPlugin,\n typeName,\n type ReferenceCollectionExtensions,\n type PluginConfig,\n type FieldOptions,\n type CollectionReference,\n};\n"],"names":["isExtensionEnabled","extensions","key","enabled","FieldContext","React","createContext","useFieldCtx","useContext","normalizeCollection","preset","title","value","CollectionFieldInput","props","$","_c","t0","schemaType","options","t1","t2","jsx","CompactObjectInput","t3","CollectionOptionsField","Object","keys","length","children","CollectionOptionsInput","members","map","member","kind","name","MemberField","Stack","CollectionRefInput","pluginConfig","bb0","references","referenceKinds","t4","t5","list","t6","patchedSchemaType","renderDefault","typeName","wildSanityReferenceCollectionFieldPlugin","definePlugin","config","schema","types","defineType","type","description","icon","Fragment","validation","R","custom","collectionRef","message","path","components","field","inline","input","fields","defineField","layout","collapsed","collapsible","initialValue","hidden","parent","withFilters","of","defineArrayMember","unique","withPagination","integer","positive","preview","select","collection","prepare","subtitle","undefined"],"mappings":";;;;;;;;;;;;;;;;;;;AAOA,SAASA,mBAAmBC,YAA2CC,KAA0C;AAC/G,SAAOD,WAAWC,GAAG,MAAMD,WAAWC,GAAG,MAAM,MAAQD,WAAWC,GAAG,EAAEC,YAAY;AACrF;AAEA,MAAMC,eAAeC,iBAAMC,cAA0C,CAAA,CAAE,GACjEC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
|
|
9
9
|
};
|
|
10
10
|
/** @public */
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
type ReferenceCollectionExtensions = Partial<{
|
|
12
|
+
/** Allows filtering the references. */
|
|
13
|
+
filtering: boolean | {
|
|
14
|
+
enabled?: boolean;
|
|
15
|
+
filterByKinds?: string[];
|
|
16
|
+
};
|
|
17
|
+
/** Allows paginating the references. */
|
|
18
|
+
pagination: boolean | {
|
|
19
|
+
enabled?: boolean;
|
|
20
|
+
pageCount?: number;
|
|
21
|
+
};
|
|
22
|
+
}>;
|
|
15
23
|
/** @public */
|
|
16
24
|
type PluginConfig = {
|
|
17
25
|
/**
|
|
@@ -42,14 +50,8 @@ type FieldOptions = ObjectOptions & {
|
|
|
42
50
|
referenceKinds?: CollectionReference[];
|
|
43
51
|
/**
|
|
44
52
|
* 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
53
|
*/
|
|
52
|
-
extensions?:
|
|
54
|
+
extensions?: ReferenceCollectionExtensions;
|
|
53
55
|
};
|
|
54
56
|
declare module "sanity" {
|
|
55
57
|
interface IntrinsicDefinitions {
|
|
@@ -61,4 +63,4 @@ declare module "sanity" {
|
|
|
61
63
|
}
|
|
62
64
|
/** @public */
|
|
63
65
|
declare const wildSanityReferenceCollectionFieldPlugin: sanity0.Plugin<PluginConfig>;
|
|
64
|
-
export { type CollectionReference, type FieldOptions, type PluginConfig,
|
|
66
|
+
export { type CollectionReference, type FieldOptions, type PluginConfig, type ReferenceCollectionExtensions, typeName, wildSanityReferenceCollectionFieldPlugin };
|
package/dist/index.d.ts
CHANGED
|
@@ -8,10 +8,18 @@ type CollectionReference = string | {
|
|
|
8
8
|
value: string;
|
|
9
9
|
};
|
|
10
10
|
/** @public */
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
type ReferenceCollectionExtensions = Partial<{
|
|
12
|
+
/** Allows filtering the references. */
|
|
13
|
+
filtering: boolean | {
|
|
14
|
+
enabled?: boolean;
|
|
15
|
+
filterByKinds?: string[];
|
|
16
|
+
};
|
|
17
|
+
/** Allows paginating the references. */
|
|
18
|
+
pagination: boolean | {
|
|
19
|
+
enabled?: boolean;
|
|
20
|
+
pageCount?: number;
|
|
21
|
+
};
|
|
22
|
+
}>;
|
|
15
23
|
/** @public */
|
|
16
24
|
type PluginConfig = {
|
|
17
25
|
/**
|
|
@@ -42,14 +50,8 @@ type FieldOptions = ObjectOptions & {
|
|
|
42
50
|
referenceKinds?: CollectionReference[];
|
|
43
51
|
/**
|
|
44
52
|
* 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
53
|
*/
|
|
52
|
-
extensions?:
|
|
54
|
+
extensions?: ReferenceCollectionExtensions;
|
|
53
55
|
};
|
|
54
56
|
declare module "sanity" {
|
|
55
57
|
interface IntrinsicDefinitions {
|
|
@@ -61,4 +63,4 @@ declare module "sanity" {
|
|
|
61
63
|
}
|
|
62
64
|
/** @public */
|
|
63
65
|
declare const wildSanityReferenceCollectionFieldPlugin: sanity0.Plugin<PluginConfig>;
|
|
64
|
-
export { type CollectionReference, type FieldOptions, type PluginConfig,
|
|
66
|
+
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
|
-
import { MemberField, definePlugin, defineType, defineField } from "sanity";
|
|
2
|
+
import { MemberField, definePlugin, defineType, defineField, defineArrayMember } from "sanity";
|
|
3
3
|
import { c } from "react/compiler-runtime";
|
|
4
|
+
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 =
|
|
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()
|
|
28
|
-
if (!
|
|
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.
|
|
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(
|
|
38
|
+
const $ = c(5), {
|
|
35
39
|
options
|
|
36
|
-
} = useFieldCtx();
|
|
40
|
+
} = useFieldCtx(), extensions = options?.extensions;
|
|
37
41
|
let t0;
|
|
38
|
-
$[0] !==
|
|
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
|
-
$[
|
|
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: 4, children: t0 }), $[3] = t0, $[4] = t1) : t1 = $[4], t1;
|
|
47
45
|
}
|
|
48
46
|
function CollectionRefInput(t0) {
|
|
49
47
|
const $ = c(14);
|
|
@@ -80,19 +78,16 @@ 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",
|
|
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({
|
|
90
85
|
name: typeName,
|
|
91
86
|
type: "object",
|
|
92
87
|
title: "Reference Collection",
|
|
93
|
-
description: "
|
|
88
|
+
description: "Choose a content type and control how the collection is displayed.",
|
|
94
89
|
icon: () => /* @__PURE__ */ jsx(Fragment, { children: "\u{1F503}" }),
|
|
95
|
-
validation: (R) => R.custom((
|
|
90
|
+
validation: (R) => R.custom((value) => value?.collectionRef ? !0 : {
|
|
96
91
|
message: "Select a reference.",
|
|
97
92
|
path: ["collectionRef"]
|
|
98
93
|
}),
|
|
@@ -103,8 +98,8 @@ const typeName = "wild.referenceCollection", ReferenceCollectionExtension = {
|
|
|
103
98
|
fields: [defineField({
|
|
104
99
|
name: "collectionRef",
|
|
105
100
|
type: "string",
|
|
106
|
-
title: "
|
|
107
|
-
description: "
|
|
101
|
+
title: "Collection Type",
|
|
102
|
+
description: "Choose which type of content this collection should show.",
|
|
108
103
|
components: {
|
|
109
104
|
input: (props) => /* @__PURE__ */ jsx(CollectionRefInput, { pluginConfig: config, ...props })
|
|
110
105
|
},
|
|
@@ -115,8 +110,8 @@ const typeName = "wild.referenceCollection", ReferenceCollectionExtension = {
|
|
|
115
110
|
}), defineField({
|
|
116
111
|
type: "object",
|
|
117
112
|
name: "options",
|
|
118
|
-
title: "
|
|
119
|
-
description: "
|
|
113
|
+
title: "Display Settings",
|
|
114
|
+
description: "Control filters and pagination for this collection.",
|
|
120
115
|
options: {
|
|
121
116
|
collapsed: !1,
|
|
122
117
|
collapsible: !1
|
|
@@ -128,21 +123,46 @@ const typeName = "wild.referenceCollection", ReferenceCollectionExtension = {
|
|
|
128
123
|
fields: [defineField({
|
|
129
124
|
name: "withFilters",
|
|
130
125
|
type: "boolean",
|
|
131
|
-
title: "
|
|
132
|
-
description: "
|
|
126
|
+
title: "Enable Filters",
|
|
127
|
+
description: "Show filter controls for this collection.",
|
|
133
128
|
initialValue: !1,
|
|
134
129
|
options: {
|
|
135
130
|
layout: "switch"
|
|
136
131
|
}
|
|
132
|
+
}), defineField({
|
|
133
|
+
name: "filterByKinds",
|
|
134
|
+
type: "array",
|
|
135
|
+
title: "Available Filter Types",
|
|
136
|
+
description: "Choose which content types appear as filter options.",
|
|
137
|
+
hidden: ({
|
|
138
|
+
parent
|
|
139
|
+
}) => !parent?.withFilters,
|
|
140
|
+
of: [defineArrayMember({
|
|
141
|
+
type: "string"
|
|
142
|
+
})],
|
|
143
|
+
options: {
|
|
144
|
+
layout: "tags"
|
|
145
|
+
},
|
|
146
|
+
validation: (R) => R.unique()
|
|
137
147
|
}), defineField({
|
|
138
148
|
name: "withPagination",
|
|
139
149
|
type: "boolean",
|
|
140
|
-
title: "
|
|
141
|
-
description: "
|
|
150
|
+
title: "Enable Pagination",
|
|
151
|
+
description: "Split collection results across multiple pages.",
|
|
142
152
|
initialValue: !1,
|
|
143
153
|
options: {
|
|
144
154
|
layout: "switch"
|
|
145
155
|
}
|
|
156
|
+
}), defineField({
|
|
157
|
+
name: "pageCount",
|
|
158
|
+
type: "number",
|
|
159
|
+
title: "Items Per Page",
|
|
160
|
+
description: "Set how many items to show on each page.",
|
|
161
|
+
hidden: ({
|
|
162
|
+
parent
|
|
163
|
+
}) => !parent?.withPagination,
|
|
164
|
+
initialValue: 12,
|
|
165
|
+
validation: (R) => R.integer().positive()
|
|
146
166
|
})]
|
|
147
167
|
})],
|
|
148
168
|
preview: {
|
|
@@ -162,7 +182,6 @@ const typeName = "wild.referenceCollection", ReferenceCollectionExtension = {
|
|
|
162
182
|
}
|
|
163
183
|
}));
|
|
164
184
|
export {
|
|
165
|
-
ReferenceCollectionExtension,
|
|
166
185
|
typeName,
|
|
167
186
|
wildSanityReferenceCollectionFieldPlugin
|
|
168
187
|
};
|
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,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,KAAA,oBAAA,aAAA,UAAA,EAA8B,OAAAH,IAC3BC,UAAAA,GAAAA,CACH,GAAwBN,OAAAK,IAAAL,OAAAM,IAAAN,OAAAQ,MA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AA;AAGR,EACD;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/input.tsx","../src/types.tsx","../src/index.tsx"],"sourcesContent":["import { CompactObjectInput } from \"@madebywild/sanity-utils/compact-object-input\";\nimport { Stack } from \"@sanity/ui\";\nimport * as React from \"react\";\nimport type { ObjectFieldProps } from \"sanity\";\nimport { MemberField, type ObjectInputProps, type ObjectMember, type StringInputProps } from \"sanity\";\nimport type { CollectionReference, FieldOptions, PluginConfig, ReferenceCollectionExtensions } from \"./types\";\n\nfunction isExtensionEnabled(extensions: ReferenceCollectionExtensions, key: keyof ReferenceCollectionExtensions) {\n return extensions[key] && (extensions[key] === true || extensions[key].enabled !== false);\n}\n\nconst FieldContext = React.createContext<{ options?: FieldOptions }>({});\nconst useFieldCtx = () => React.useContext(FieldContext);\n\nfunction normalizeCollection(preset: CollectionReference) {\n return typeof preset === \"string\" ? { title: preset, value: preset } : preset;\n}\n\nfunction CollectionFieldInput(props: ObjectInputProps) {\n return (\n <FieldContext.Provider value={{ options: props.schemaType.options as FieldOptions | undefined }}>\n <CompactObjectInput space={4} {...props} />\n </FieldContext.Provider>\n );\n}\n\nfunction CollectionOptionsField(props: ObjectFieldProps) {\n const { options } = useFieldCtx();\n // We can hide the field if no extensions are enabled.\n if (!options?.extensions || !Object.keys(options?.extensions).length) return null;\n return <>{props.children}</>;\n}\n\nfunction CollectionOptionsInput(props: ObjectInputProps) {\n const { options } = useFieldCtx();\n const extensions = options?.extensions;\n\n return (\n <Stack space={4}>\n {props.members.map((member: ObjectMember) => {\n if (member.kind !== \"field\") return null;\n\n // Custom filtering extension.\n const filteringEnabled = extensions && isExtensionEnabled(extensions, \"filtering\");\n if (!filteringEnabled && member.name === \"withFilters\") return null;\n\n // Custom pagination extension.\n const paginationEnabled = extensions && isExtensionEnabled(extensions, \"pagination\");\n if (!paginationEnabled && member.name === \"withPagination\") return null;\n\n return <MemberField key={member.key} member={member} {...props} />;\n })}\n </Stack>\n );\n}\n\nfunction CollectionRefInput({\n pluginConfig,\n ...props\n}: StringInputProps & {\n pluginConfig?: PluginConfig;\n}) {\n const patchedSchemaType = React.useMemo(() => {\n // Local options take precedence over plugin config.\n const options = props.schemaType.options as FieldOptions | undefined;\n const references = options?.referenceKinds ?? pluginConfig?.referenceKinds;\n\n // No need to touch anything if the list is unchanged.\n if (!references?.length) return props.schemaType;\n\n return {\n ...props.schemaType,\n options: {\n ...props.schemaType.options,\n list: references.map(normalizeCollection),\n },\n };\n }, [props.schemaType]);\n\n return props.renderDefault({\n ...props,\n schemaType: patchedSchemaType,\n });\n}\n\nexport { CollectionRefInput, CollectionFieldInput, CollectionOptionsField, CollectionOptionsInput };\n","import type { ObjectDefinition, ObjectOptions } from \"sanity\";\n\n/** @public */\nexport const typeName = \"wild.referenceCollection\" as const;\n\n/** @public */\nexport type CollectionReference = string | { title: string; value: string };\n\n/** @public */\nexport type ReferenceCollectionExtensions = Partial<{\n /** Allows filtering the references. */\n filtering: boolean | { enabled?: boolean; filterByKinds?: string[] };\n /** Allows paginating the references. */\n pagination: boolean | { enabled?: boolean; pageCount?: number };\n}>;\n\n/** @public */\nexport type PluginConfig = {\n /**\n * A list of references the user can choose from.\n * Can be overridden per-field via `options.referenceKinds`.\n * @example\n * ```ts\n * referenceKinds: [\"blogPost\", \"some-type\"],\n * ```\n */\n referenceKinds: CollectionReference[];\n};\n\n/** @public */\nexport type FieldOptions = ObjectOptions & {\n /**\n * Whether the media field should be displayed inline.\n * This will render all children inline on the same level.\n */\n inline?: boolean;\n /**\n * A list of references the user can choose from.\n * Will take precedence over the plugin-level `referenceKinds`.\n * @example\n * ```ts\n * referenceKinds: [\"blogPost\", \"some-type\"],\n * ```\n */\n referenceKinds?: CollectionReference[];\n /**\n * List of extension that add additional behaviors to the link field.\n */\n extensions?: ReferenceCollectionExtensions;\n};\n\n// Add the custom field definition to Sanity's intrinsic definitions\n// so that type checking works correctly when using this field type.\ndeclare module \"sanity\" {\n export interface IntrinsicDefinitions {\n [typeName]: Omit<ObjectDefinition, \"type\" | \"fields\" | \"options\" | \"components\"> & {\n type: typeof typeName;\n options?: FieldOptions;\n };\n }\n}\n","import { defineArrayMember, defineField, definePlugin, defineType } from \"sanity\";\nimport { CollectionFieldInput, CollectionOptionsField, CollectionOptionsInput, CollectionRefInput } from \"./input\";\nimport {\n type CollectionReference,\n type FieldOptions,\n type PluginConfig,\n type ReferenceCollectionExtensions,\n typeName,\n} from \"./types\";\n\n/** @public */\nconst wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((config) => {\n return {\n name: \"@madebywild/sanity-reference-collection-field\",\n schema: {\n types: [\n defineType({\n name: typeName,\n type: \"object\",\n title: \"Reference Collection\",\n description: \"Choose a content type and control how the collection is displayed.\",\n icon: () => <>🔃</>,\n validation: (R) => {\n return R.custom((value) => {\n return !value?.collectionRef ? { message: \"Select a reference.\", path: [\"collectionRef\"] } : true;\n });\n },\n components: {\n field: (props) => (props.schemaType.options.inline ? props.children : props.renderDefault(props)),\n input: (props) => <CollectionFieldInput {...props} />,\n },\n fields: [\n defineField({\n name: \"collectionRef\",\n type: \"string\",\n title: \"Collection Type\",\n description: \"Choose which type of content this collection should show.\",\n components: {\n input: (props) => <CollectionRefInput pluginConfig={config} {...props} />,\n },\n options: {\n layout: \"dropdown\",\n list: config.referenceKinds,\n },\n }),\n defineField({\n type: \"object\",\n name: \"options\",\n title: \"Display Settings\",\n description: \"Control filters and pagination for this collection.\",\n options: {\n collapsed: false,\n collapsible: false,\n },\n components: {\n field: (props) => <CollectionOptionsField {...props} />,\n input: (props) => <CollectionOptionsInput {...props} />,\n },\n fields: [\n defineField({\n name: \"withFilters\",\n type: \"boolean\",\n title: \"Enable Filters\",\n description: \"Show filter controls for this collection.\",\n initialValue: false,\n options: { layout: \"switch\" },\n }),\n defineField({\n name: \"filterByKinds\",\n type: \"array\",\n title: \"Available Filter Types\",\n description: \"Choose which content types appear as filter options.\",\n hidden: ({ parent }) => !parent?.withFilters,\n of: [\n defineArrayMember({\n type: \"string\",\n }),\n ],\n options: { layout: \"tags\" },\n validation: (R) => R.unique(),\n }),\n defineField({\n name: \"withPagination\",\n type: \"boolean\",\n title: \"Enable Pagination\",\n description: \"Split collection results across multiple pages.\",\n initialValue: false,\n options: { layout: \"switch\" },\n }),\n defineField({\n name: \"pageCount\",\n type: \"number\",\n title: \"Items Per Page\",\n description: \"Set how many items to show on each page.\",\n hidden: ({ parent }) => !parent?.withPagination,\n initialValue: 12,\n validation: (R) => R.integer().positive(),\n }),\n ],\n }),\n ],\n preview: {\n select: {\n collection: \"collectionRef\",\n },\n prepare({ collection }) {\n return {\n title: \"Reference Collection\",\n subtitle: collection ? `Reference to ${collection}` : undefined,\n };\n },\n },\n }),\n ],\n },\n };\n});\n\nexport {\n wildSanityReferenceCollectionFieldPlugin,\n typeName,\n type ReferenceCollectionExtensions,\n type PluginConfig,\n type FieldOptions,\n type CollectionReference,\n};\n"],"names":["isExtensionEnabled","extensions","key","enabled","FieldContext","React","createContext","useFieldCtx","useContext","normalizeCollection","preset","title","value","CollectionFieldInput","props","$","_c","t0","schemaType","options","t1","t2","t3","CollectionOptionsField","Object","keys","length","children","CollectionOptionsInput","members","map","member","kind","name","CollectionRefInput","pluginConfig","bb0","references","referenceKinds","t4","t5","list","t6","patchedSchemaType","renderDefault","typeName","wildSanityReferenceCollectionFieldPlugin","definePlugin","config","schema","types","defineType","type","description","icon","validation","R","custom","collectionRef","message","path","components","field","inline","input","fields","defineField","layout","collapsed","collapsible","initialValue","hidden","parent","withFilters","of","defineArrayMember","unique","withPagination","integer","positive","preview","select","collection","prepare","subtitle","undefined"],"mappings":";;;;;;AAOA,SAASA,mBAAmBC,YAA2CC,KAA0C;AAC/G,SAAOD,WAAWC,GAAG,MAAMD,WAAWC,GAAG,MAAM,MAAQD,WAAWC,GAAG,EAAEC,YAAY;AACrF;AAEA,MAAMC,eAAeC,MAAMC,cAA0C,CAAA,CAAE,GACjEC,cAAcA,MAAMF,MAAKG,WAAYJ,YAAY;AAEvD,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AChBvB,MAAM;AAAA,UAAA,CACP,CAAC;AAAA,UAEJjC,SAAS;AAAA,YAAEgD,QAAQ;AAAA,UAAA;AAAA,UACnBZ,YAAaC,CAAAA,MAAMA,EAAEoB,OAAAA;AAAAA,QAAO,CAC7B,GACDV,YAAY;AAAA,UACVjC,MAAM;AAAA,UACNmB,MAAM;AAAA,UACNzC,OAAO;AAAA,UACP0C,aAAa;AAAA,UACbiB,cAAc;AAAA,UACdnD,SAAS;AAAA,YAAEgD,QAAQ;AAAA,UAAA;AAAA,QAAS,CAC7B,GACDD,YAAY;AAAA,UACVjC,MAAM;AAAA,UACNmB,MAAM;AAAA,UACNzC,OAAO;AAAA,UACP0C,aAAa;AAAA,UACbkB,QAAQA,CAAC;AAAA,YAAEC;AAAAA,UAAAA,MAAa,CAACA,QAAQK;AAAAA,UACjCP,cAAc;AAAA,UACdf,YAAaC,CAAAA,MAAMA,EAAEsB,QAAAA,EAAUC,SAAAA;AAAAA,QAAS,CACzC,CAAC;AAAA,MAAA,CAEL,CAAC;AAAA,MAEJC,SAAS;AAAA,QACPC,QAAQ;AAAA,UACNC,YAAY;AAAA,QAAA;AAAA,QAEdC,QAAQ;AAAA,UAAED;AAAAA,QAAAA,GAAc;AACtB,iBAAO;AAAA,YACLvE,OAAO;AAAA,YACPyE,UAAUF,aAAa,gBAAgBA,UAAU,KAAKG;AAAAA,UAAAA;AAAAA,QAE1D;AAAA,MAAA;AAAA,IACF,CACD,CAAC;AAAA,EAAA;AAGR,EACD;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@madebywild/sanity-reference-collection-field",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.1.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
|
-
import { defineField, definePlugin, defineType } from "sanity";
|
|
1
|
+
import { defineArrayMember, defineField, definePlugin, defineType } from "sanity";
|
|
2
2
|
import { CollectionFieldInput, CollectionOptionsField, CollectionOptionsInput, CollectionRefInput } from "./input";
|
|
3
|
-
import {
|
|
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) => {
|
|
@@ -12,10 +18,12 @@ const wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((con
|
|
|
12
18
|
name: typeName,
|
|
13
19
|
type: "object",
|
|
14
20
|
title: "Reference Collection",
|
|
15
|
-
description: "
|
|
21
|
+
description: "Choose a content type and control how the collection is displayed.",
|
|
16
22
|
icon: () => <>🔃</>,
|
|
17
23
|
validation: (R) => {
|
|
18
|
-
return R.custom((
|
|
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)),
|
|
@@ -25,8 +33,8 @@ const wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((con
|
|
|
25
33
|
defineField({
|
|
26
34
|
name: "collectionRef",
|
|
27
35
|
type: "string",
|
|
28
|
-
title: "
|
|
29
|
-
description: "
|
|
36
|
+
title: "Collection Type",
|
|
37
|
+
description: "Choose which type of content this collection should show.",
|
|
30
38
|
components: {
|
|
31
39
|
input: (props) => <CollectionRefInput pluginConfig={config} {...props} />,
|
|
32
40
|
},
|
|
@@ -38,8 +46,8 @@ const wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((con
|
|
|
38
46
|
defineField({
|
|
39
47
|
type: "object",
|
|
40
48
|
name: "options",
|
|
41
|
-
title: "
|
|
42
|
-
description: "
|
|
49
|
+
title: "Display Settings",
|
|
50
|
+
description: "Control filters and pagination for this collection.",
|
|
43
51
|
options: {
|
|
44
52
|
collapsed: false,
|
|
45
53
|
collapsible: false,
|
|
@@ -52,19 +60,42 @@ const wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((con
|
|
|
52
60
|
defineField({
|
|
53
61
|
name: "withFilters",
|
|
54
62
|
type: "boolean",
|
|
55
|
-
title: "
|
|
56
|
-
description: "
|
|
63
|
+
title: "Enable Filters",
|
|
64
|
+
description: "Show filter controls for this collection.",
|
|
57
65
|
initialValue: false,
|
|
58
66
|
options: { layout: "switch" },
|
|
59
67
|
}),
|
|
68
|
+
defineField({
|
|
69
|
+
name: "filterByKinds",
|
|
70
|
+
type: "array",
|
|
71
|
+
title: "Available Filter Types",
|
|
72
|
+
description: "Choose which content types appear as filter options.",
|
|
73
|
+
hidden: ({ parent }) => !parent?.withFilters,
|
|
74
|
+
of: [
|
|
75
|
+
defineArrayMember({
|
|
76
|
+
type: "string",
|
|
77
|
+
}),
|
|
78
|
+
],
|
|
79
|
+
options: { layout: "tags" },
|
|
80
|
+
validation: (R) => R.unique(),
|
|
81
|
+
}),
|
|
60
82
|
defineField({
|
|
61
83
|
name: "withPagination",
|
|
62
84
|
type: "boolean",
|
|
63
|
-
title: "
|
|
64
|
-
description: "
|
|
85
|
+
title: "Enable Pagination",
|
|
86
|
+
description: "Split collection results across multiple pages.",
|
|
65
87
|
initialValue: false,
|
|
66
88
|
options: { layout: "switch" },
|
|
67
89
|
}),
|
|
90
|
+
defineField({
|
|
91
|
+
name: "pageCount",
|
|
92
|
+
type: "number",
|
|
93
|
+
title: "Items Per Page",
|
|
94
|
+
description: "Set how many items to show on each page.",
|
|
95
|
+
hidden: ({ parent }) => !parent?.withPagination,
|
|
96
|
+
initialValue: 12,
|
|
97
|
+
validation: (R) => R.integer().positive(),
|
|
98
|
+
}),
|
|
68
99
|
],
|
|
69
100
|
}),
|
|
70
101
|
],
|
|
@@ -88,7 +119,7 @@ const wildSanityReferenceCollectionFieldPlugin = definePlugin<PluginConfig>((con
|
|
|
88
119
|
export {
|
|
89
120
|
wildSanityReferenceCollectionFieldPlugin,
|
|
90
121
|
typeName,
|
|
91
|
-
|
|
122
|
+
type ReferenceCollectionExtensions,
|
|
92
123
|
type PluginConfig,
|
|
93
124
|
type FieldOptions,
|
|
94
125
|
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
|
|
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
|
-
|
|
25
|
-
|
|
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
|
|
38
|
-
const withFiltering = options?.extensions?.includes("filtering");
|
|
36
|
+
const extensions = options?.extensions;
|
|
39
37
|
|
|
40
38
|
return (
|
|
41
39
|
<Stack space={4}>
|
|
42
40
|
{props.members.map((member: ObjectMember) => {
|
|
43
41
|
if (member.kind !== "field") return null;
|
|
44
|
-
|
|
45
|
-
|
|
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
|
|
11
|
-
filtering
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
export type ReferenceCollectionExtensions = Partial<{
|
|
11
|
+
/** Allows filtering the references. */
|
|
12
|
+
filtering: boolean | { enabled?: boolean; filterByKinds?: string[] };
|
|
13
|
+
/** Allows paginating the references. */
|
|
14
|
+
pagination: boolean | { enabled?: boolean; pageCount?: number };
|
|
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?:
|
|
49
|
+
extensions?: ReferenceCollectionExtensions;
|
|
54
50
|
};
|
|
55
51
|
|
|
56
52
|
// Add the custom field definition to Sanity's intrinsic definitions
|