@cutticat/payload-field-ms 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/client/DurationCell.d.ts +0 -2
- package/dist/client/DurationCell.d.ts.map +1 -1
- package/dist/client/DurationCell.js +1 -2
- package/dist/client/DurationCell.js.map +1 -1
- package/dist/client/DurationField.d.ts +0 -2
- package/dist/client/DurationField.d.ts.map +1 -1
- package/dist/client/DurationField.js +2 -3
- package/dist/client/DurationField.js.map +1 -1
- package/dist/client/internal/chips.js +1 -2
- package/dist/client/internal/durations.js +1 -2
- package/dist/field/field.d.ts +0 -2
- package/dist/field/field.d.ts.map +1 -1
- package/dist/field/field.js +1 -2
- package/dist/field/field.js.map +1 -1
- package/dist/field/index.d.ts +7 -0
- package/dist/field/index.js +1 -1
- package/dist/field/internal/durations.js +1 -2
- package/dist/field/validation.d.ts +0 -2
- package/dist/field/validation.d.ts.map +1 -1
- package/dist/field/validation.js +1 -2
- package/dist/field/validation.js.map +1 -1
- package/package.json +14 -14
package/README.md
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { FC } from "react";
|
|
2
2
|
import { DefaultCellComponentProps, NumberFieldClient } from "payload";
|
|
3
|
-
//#region lib/client/DurationCell.d.ts
|
|
4
3
|
/**
|
|
5
4
|
* List-view cell of {@link durationField}: Payload's default cell with the stored durations in
|
|
6
5
|
* the input's format, joined with a comma. Links, row selection, and the empty state stay
|
|
@@ -10,5 +9,4 @@ import { DefaultCellComponentProps, NumberFieldClient } from "payload";
|
|
|
10
9
|
* @returns The cell.
|
|
11
10
|
*/
|
|
12
11
|
export declare const DurationCell: FC<DefaultCellComponentProps<NumberFieldClient>>;
|
|
13
|
-
//#endregion
|
|
14
12
|
//# sourceMappingURL=DurationCell.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DurationCell.d.ts","names":[],"sources":["../../lib/client/DurationCell.tsx"],"mappings":"
|
|
1
|
+
{"version":3,"file":"DurationCell.d.ts","names":[],"sources":["../../lib/client/DurationCell.tsx"],"mappings":";;;;;;;;;;qBAea,cAAc,GAAG,0BAA0B"}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { textsOf } from "./internal/durations.js";
|
|
3
3
|
import { DefaultCell } from "@payloadcms/ui";
|
|
4
4
|
import { jsx } from "react/jsx-runtime";
|
|
5
|
-
|
|
5
|
+
// lib/client/DurationCell.tsx
|
|
6
6
|
/**
|
|
7
7
|
* List-view cell of {@link durationField}: Payload's default cell with the stored durations in
|
|
8
8
|
* the input's format, joined with a comma. Links, row selection, and the empty state stay
|
|
@@ -15,7 +15,6 @@ const DurationCell = (props) => /* @__PURE__ */ jsx(DefaultCell, {
|
|
|
15
15
|
...props,
|
|
16
16
|
cellData: textsOf(props.cellData).join(", ")
|
|
17
17
|
});
|
|
18
|
-
//#endregion
|
|
19
18
|
export { DurationCell };
|
|
20
19
|
|
|
21
20
|
//# sourceMappingURL=DurationCell.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DurationCell.js","names":[],"sources":["../../lib/client/DurationCell.tsx"],"sourcesContent":["\"use client\"\n\nimport { DefaultCell } from \"@payloadcms/ui\"\nimport type { DefaultCellComponentProps, NumberFieldClient } from \"payload\"\nimport type { FC } from \"react\"\nimport { textsOf } from \"../internal/durations.
|
|
1
|
+
{"version":3,"file":"DurationCell.js","names":[],"sources":["../../lib/client/DurationCell.tsx"],"sourcesContent":["\"use client\"\n\nimport { DefaultCell } from \"@payloadcms/ui\"\nimport type { DefaultCellComponentProps, NumberFieldClient } from \"payload\"\nimport type { FC } from \"react\"\nimport { textsOf } from \"../internal/durations.ts\"\n\n/**\n * List-view cell of {@link durationField}: Payload's default cell with the stored durations in\n * the input's format, joined with a comma. Links, row selection, and the empty state stay\n * Payload's.\n *\n * @param props - Payload cell props.\n * @returns The cell.\n */\nexport const DurationCell: FC<DefaultCellComponentProps<NumberFieldClient>> = props => (\n <DefaultCell {...props} cellData={textsOf(props.cellData).join(\", \")} />\n)\n"],"mappings":";;;;;;;;;;;;;AAeA,MAAa,gBAAiE,UAC5E,oBAAC,aAAD;CAAa,GAAI;CAAO,UAAU,QAAQ,MAAM,QAAQ,CAAC,CAAC,KAAK,IAAI;AAAI,CAAA"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { NumberFieldClientComponent } from "payload";
|
|
2
|
-
//#region lib/client/DurationField.d.ts
|
|
3
2
|
/**
|
|
4
3
|
* Admin input of {@link durationField}: Payload's text input over a number field.
|
|
5
4
|
*
|
|
@@ -10,5 +9,4 @@ import { NumberFieldClientComponent } from "payload";
|
|
|
10
9
|
* @returns The field.
|
|
11
10
|
*/
|
|
12
11
|
export declare const DurationField: NumberFieldClientComponent;
|
|
13
|
-
//#endregion
|
|
14
12
|
//# sourceMappingURL=DurationField.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DurationField.d.ts","names":[],"sources":["../../lib/client/DurationField.tsx"],"mappings":"
|
|
1
|
+
{"version":3,"file":"DurationField.d.ts","names":[],"sources":["../../lib/client/DurationField.tsx"],"mappings":";;;;;;;;;;qBA0Ba,eAAe"}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { textsMatch, textsOf, valueOfText } from "./internal/durations.js";
|
|
3
2
|
import { chipOf, textOfChip } from "./internal/chips.js";
|
|
3
|
+
import { textsMatch, textsOf, valueOfText } from "./internal/durations.js";
|
|
4
4
|
import { TextInput, useField } from "@payloadcms/ui";
|
|
5
5
|
import { jsx } from "react/jsx-runtime";
|
|
6
6
|
import { mergeFieldStyles } from "@payloadcms/ui/shared";
|
|
7
7
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
8
8
|
import { omitUndefined } from "@cutticat/objects";
|
|
9
|
-
|
|
9
|
+
// lib/client/DurationField.tsx
|
|
10
10
|
/**
|
|
11
11
|
* Admin input of {@link durationField}: Payload's text input over a number field.
|
|
12
12
|
*
|
|
@@ -69,7 +69,6 @@ const DurationField = ({ field, onChange: onChangeFromProps, path: pathFromProps
|
|
|
69
69
|
valueToRender: texts.map(chipOf)
|
|
70
70
|
});
|
|
71
71
|
};
|
|
72
|
-
//#endregion
|
|
73
72
|
export { DurationField };
|
|
74
73
|
|
|
75
74
|
//# sourceMappingURL=DurationField.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DurationField.js","names":[],"sources":["../../lib/client/DurationField.tsx"],"sourcesContent":["\"use client\"\n\nimport { TextInput, type TextInputProps, useField } from \"@payloadcms/ui\"\nimport type { Option } from \"@payloadcms/ui/elements/ReactSelect\"\nimport { mergeFieldStyles } from \"@payloadcms/ui/shared\"\nimport type { NumberFieldClientComponent } from \"payload\"\nimport { type ChangeEvent, useCallback, useEffect, useMemo, useState } from \"react\"\nimport { omitUndefined } from \"@cutticat/objects\"\nimport { chipOf, textOfChip } from \"../internal/chips.
|
|
1
|
+
{"version":3,"file":"DurationField.js","names":[],"sources":["../../lib/client/DurationField.tsx"],"sourcesContent":["\"use client\"\n\nimport { TextInput, type TextInputProps, useField } from \"@payloadcms/ui\"\nimport type { Option } from \"@payloadcms/ui/elements/ReactSelect\"\nimport { mergeFieldStyles } from \"@payloadcms/ui/shared\"\nimport type { NumberFieldClientComponent } from \"payload\"\nimport { type ChangeEvent, useCallback, useEffect, useMemo, useState } from \"react\"\nimport { omitUndefined } from \"@cutticat/objects\"\nimport { chipOf, textOfChip } from \"../internal/chips.ts\"\nimport { type DurationItem, textsMatch, textsOf, valueOfText } from \"../internal/durations.ts\"\n\n/**\n * Form value: milliseconds, an array of them with `hasMany`, or `null` when empty. Text that\n * does not parse stays in it as `{ text }` until the server rejects it.\n */\ntype DurationValue = DurationItem | DurationItem[] | null\n\n/**\n * Admin input of {@link durationField}: Payload's text input over a number field.\n *\n * One value is a text box, `hasMany` is a list of chips. Text that does not parse stays in\n * the input and in the form value, and the field's server validation rejects it.\n *\n * @param props - Payload number field client props.\n * @returns The field.\n */\nexport const DurationField: NumberFieldClientComponent = ({\n field,\n onChange: onChangeFromProps,\n path: pathFromProps,\n readOnly,\n}) => {\n // State\n\n const {\n admin: { className, description, placeholder, readOnly: readOnlyFromConfig } = {},\n hasMany = false,\n label,\n localized,\n required,\n } = field\n const maxRows = \"maxRows\" in field ? field.maxRows : undefined\n\n const {\n customComponents: { AfterInput, BeforeInput, Description, Error: ErrorComponent, Label } = {},\n disabled,\n path,\n setValue,\n showError,\n value,\n } = useField<DurationValue>({ potentiallyStalePath: pathFromProps })\n\n // Payload hands a custom input only the parent's and the access read-only, not `admin.readOnly`\n const isReadOnly = readOnly || disabled || readOnlyFromConfig === true\n\n // What the user sees\n const [texts, setTexts] = useState(() => textsOf(value))\n const style = useMemo(() => mergeFieldStyles(field), [field])\n\n // Effects\n\n // Show a value set elsewhere (load, reset, another tab), but keep what the user is typing\n useEffect(() => {\n setTexts(current => (textsMatch(current, value) ? current : textsOf(value)))\n }, [value])\n\n // Handlers\n\n const handleChange = useCallback(\n (event: ChangeEvent<HTMLInputElement>) => {\n const next = valueOfText(event.target.value)\n\n setTexts([event.target.value])\n setValue(next)\n\n // Payload types `onChange` for a number only, so empty and unparsed text do not call it\n if (typeof next === \"number\") onChangeFromProps?.(next)\n },\n [onChangeFromProps, setValue]\n )\n\n // Payload's `hasMany` number input does not call `onChange` either\n const handleManyChange = useCallback(\n (selected: Option | Option[] | null) => {\n if (isReadOnly) return\n\n const nextTexts = (Array.isArray(selected) ? selected : selected == null ? [] : [selected])\n .map(textOfChip)\n .filter(text => text.trim() !== \"\")\n\n setTexts(nextTexts)\n setValue(nextTexts.flatMap(text => valueOfText(text) ?? []))\n },\n [isReadOnly, setValue]\n )\n\n // Render\n\n const shared = omitUndefined({\n AfterInput,\n BeforeInput,\n className,\n Description,\n description,\n Error: ErrorComponent,\n Label,\n label,\n localized,\n path,\n // Payload types the number field placeholder loosely; it is a string or a locale map\n placeholder: placeholder as TextInputProps[\"placeholder\"],\n readOnly: isReadOnly,\n required,\n showError,\n style,\n })\n\n if (!hasMany) return <TextInput {...shared} onChange={handleChange} value={texts[0] ?? \"\"} />\n\n return (\n <TextInput\n {...shared}\n hasMany\n {...omitUndefined({ maxRows })}\n onChange={handleManyChange}\n // Payload's input counts `value` against `maxRows`; its type is for a single text\n value={texts as unknown as string}\n valueToRender={texts.map(chipOf)}\n />\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA0BA,MAAa,iBAA6C,EACxD,OACA,UAAU,mBACV,MAAM,eACN,eACI;CAGJ,MAAM,EACJ,OAAO,EAAE,WAAW,aAAa,aAAa,UAAU,uBAAuB,CAAC,GAChF,UAAU,OACV,OACA,WACA,aACE;CACJ,MAAM,UAAU,aAAa,QAAQ,MAAM,UAAU,KAAA;CAErD,MAAM,EACJ,kBAAkB,EAAE,YAAY,aAAa,aAAa,OAAO,gBAAgB,UAAU,CAAC,GAC5F,UACA,MACA,UACA,WACA,UACE,SAAwB,EAAE,sBAAsB,cAAc,CAAC;CAGnE,MAAM,aAAa,YAAY,YAAY,uBAAuB;CAGlE,MAAM,CAAC,OAAO,YAAY,eAAe,QAAQ,KAAK,CAAC;CACvD,MAAM,QAAQ,cAAc,iBAAiB,KAAK,GAAG,CAAC,KAAK,CAAC;CAK5D,gBAAgB;EACd,UAAS,YAAY,WAAW,SAAS,KAAK,IAAI,UAAU,QAAQ,KAAK,CAAE;CAC7E,GAAG,CAAC,KAAK,CAAC;CAIV,MAAM,eAAe,aAClB,UAAyC;EACxC,MAAM,OAAO,YAAY,MAAM,OAAO,KAAK;EAE3C,SAAS,CAAC,MAAM,OAAO,KAAK,CAAC;EAC7B,SAAS,IAAI;EAGb,IAAI,OAAO,SAAS,UAAU,oBAAoB,IAAI;CACxD,GACA,CAAC,mBAAmB,QAAQ,CAC9B;CAGA,MAAM,mBAAmB,aACtB,aAAuC;EACtC,IAAI,YAAY;EAEhB,MAAM,aAAa,MAAM,QAAQ,QAAQ,IAAI,WAAW,YAAY,OAAO,CAAC,IAAI,CAAC,QAAQ,EAAA,CACtF,IAAI,UAAU,CAAC,CACf,QAAO,SAAQ,KAAK,KAAK,MAAM,EAAE;EAEpC,SAAS,SAAS;EAClB,SAAS,UAAU,SAAQ,SAAQ,YAAY,IAAI,KAAK,CAAC,CAAC,CAAC;CAC7D,GACA,CAAC,YAAY,QAAQ,CACvB;CAIA,MAAM,SAAS,cAAc;EAC3B;EACA;EACA;EACA;EACA;EACA,OAAO;EACP;EACA;EACA;EACA;EAEa;EACb,UAAU;EACV;EACA;EACA;CACF,CAAC;CAED,IAAI,CAAC,SAAS,OAAO,oBAAC,WAAD;EAAW,GAAI;EAAQ,UAAU;EAAc,OAAO,MAAM,MAAM;CAAK,CAAA;CAE5F,OACE,oBAAC,WAAD;EACE,GAAI;EACJ,SAAA;EACA,GAAI,cAAc,EAAE,QAAQ,CAAC;EAC7B,UAAU;EAEV,OAAO;EACP,eAAe,MAAM,IAAI,MAAM;CAChC,CAAA;AAEL"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// lib/internal/chips.ts
|
|
2
2
|
/**
|
|
3
3
|
* A chip for one text in the shape Payload's text input renders. The index keeps keys unique
|
|
4
4
|
* when two chips read the same.
|
|
@@ -30,7 +30,6 @@ function textOfChip(option) {
|
|
|
30
30
|
const isWrapped = value != null && typeof value === "object" && "value" in value;
|
|
31
31
|
return String(isWrapped ? value.value : value);
|
|
32
32
|
}
|
|
33
|
-
//#endregion
|
|
34
33
|
export { chipOf, textOfChip };
|
|
35
34
|
|
|
36
35
|
//# sourceMappingURL=chips.js.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ms } from "@cutticat/ms";
|
|
2
|
-
|
|
2
|
+
// lib/internal/durations.ts
|
|
3
3
|
/**
|
|
4
4
|
* Field value for typed text.
|
|
5
5
|
*
|
|
@@ -75,7 +75,6 @@ function textsMatch(texts, value) {
|
|
|
75
75
|
return typeof a === "number" ? a === b : isUnparsed(b) && a.text === b.text;
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
|
-
//#endregion
|
|
79
78
|
export { isDuration, isUnparsed, itemsOf, textsMatch, textsOf, valueOfText };
|
|
80
79
|
|
|
81
80
|
//# sourceMappingURL=durations.js.map
|
package/dist/field/field.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { NumberField } from "payload";
|
|
2
2
|
import { DistributiveOmit, FullyUndefinable } from "@cutticat/types";
|
|
3
|
-
//#region lib/field.d.ts
|
|
4
3
|
/**
|
|
5
4
|
* Options of {@link durationField}: a Payload `number` field config without `type`.
|
|
6
5
|
*
|
|
@@ -32,5 +31,4 @@ export type DurationFieldOptions = DistributiveOmit<NumberField, "type" | "min"
|
|
|
32
31
|
* ```
|
|
33
32
|
*/
|
|
34
33
|
export declare function durationField({ admin, hooks, max, min, validate, ...field }: Readonly<DurationFieldOptions>): NumberField;
|
|
35
|
-
//#endregion
|
|
36
34
|
//# sourceMappingURL=field.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"field.d.ts","names":[],"sources":["../../lib/field.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"field.d.ts","names":[],"sources":["../../lib/field.ts"],"mappings":";;;;;;;YAkBY,uBAAuB,iBAAiB,uCAClD;;EAEE;;EAEA;;;;;;;;;;;;;;;;;;;;;wBAsBY,gBACd,OACA,OACA,KACA,KACA,aACG,SAGF,SAAS,wBAAwB"}
|
package/dist/field/field.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createDurationValidation, dropUnparsedFromDraft } from "./validation.js";
|
|
2
|
-
|
|
2
|
+
// lib/field.ts
|
|
3
3
|
/** Import-map path of the admin input. */
|
|
4
4
|
const fieldComponent = "@cutticat/payload-field-ms/client#DurationField";
|
|
5
5
|
/** Import-map path of the list-view cell. */
|
|
@@ -47,7 +47,6 @@ function durationField({ admin, hooks, max = null, min = 0, validate, ...field }
|
|
|
47
47
|
}
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
|
-
//#endregion
|
|
51
50
|
export { durationField };
|
|
52
51
|
|
|
53
52
|
//# sourceMappingURL=field.js.map
|
package/dist/field/field.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"field.js","names":[],"sources":["../../lib/field.ts"],"sourcesContent":["import type { NumberField, NumberFieldValidation } from \"payload\"\nimport type { DistributiveOmit, FullyUndefinable } from \"@cutticat/types\"\nimport { createDurationValidation, dropUnparsedFromDraft } from \"./validation.
|
|
1
|
+
{"version":3,"file":"field.js","names":[],"sources":["../../lib/field.ts"],"sourcesContent":["import type { NumberField, NumberFieldValidation } from \"payload\"\nimport type { DistributiveOmit, FullyUndefinable } from \"@cutticat/types\"\nimport { createDurationValidation, dropUnparsedFromDraft } from \"./validation.ts\"\n\n/** Import-map path of the admin input. */\nconst fieldComponent = \"@cutticat/payload-field-ms/client#DurationField\"\n\n/** Import-map path of the list-view cell. */\nconst cellComponent = \"@cutticat/payload-field-ms/client#DurationCell\"\n\n/** Shown in an empty input when the config sets no placeholder. */\nconst defaultPlaceholder = \"10d\"\n\n/**\n * Options of {@link durationField}: a Payload `number` field config without `type`.\n *\n * `min` defaults to `0`. `min: null` allows negative durations.\n */\nexport type DurationFieldOptions = DistributiveOmit<NumberField, \"type\" | \"min\" | \"max\"> &\n FullyUndefinable<{\n /** Smallest allowed value in milliseconds, `null` for no bound. Defaults to `0`. */\n min: number | null\n /** Largest allowed value in milliseconds, `null` for no bound. Defaults to `null`. */\n max: number | null\n }>\n\n/**\n * Builds a Payload `number` field edited as an `@cutticat/ms` duration.\n *\n * The database stores milliseconds, or an array of them with `hasMany`. The admin input\n * accepts strings such as `10d` or `1h 30m`, and the list view shows the same format.\n *\n * Validation runs on the server. Text that does not parse is rejected with Payload's\n * `validation:invalidInput`; `min` and `max` with Payload's own messages, the numbers shown as\n * durations. Then Payload's number validation, or `validate` from the options, checks the rest.\n * A draft save that Payload does not validate drops unparsed text instead.\n *\n * @param options - Number field config; `min` defaults to `0`.\n * @returns A number field with the duration input, list cell, and validation.\n * @example\n * ```typescript\n * durationField({ name: \"timeoutMs\", required: true })\n * durationField({ name: \"offsetMs\", min: null })\n * ```\n */\nexport function durationField({\n admin,\n hooks,\n max = null,\n min = 0,\n validate,\n ...field\n // Readonly, not DeepReadonly: on Payload's field config type it exceeds TypeScript's\n // instantiation depth\n}: Readonly<DurationFieldOptions>): NumberField {\n return {\n ...field,\n ...(min == null ? {} : { min }),\n ...(max == null ? {} : { max }),\n type: \"number\",\n validate: createDurationValidation({\n // `validate` is typed for one `hasMany` variant; the value always matches the field's own\n validate: (validate as NumberFieldValidation | undefined) ?? null,\n }),\n hooks: {\n ...hooks,\n beforeChange: [dropUnparsedFromDraft, ...(hooks?.beforeChange ?? [])],\n },\n admin: {\n ...admin,\n placeholder: admin?.placeholder ?? defaultPlaceholder,\n components: {\n ...admin?.components,\n Cell: cellComponent,\n Field: fieldComponent,\n },\n },\n }\n}\n"],"mappings":";;;AAKA,MAAM,iBAAiB;;AAGvB,MAAM,gBAAgB;;AAGtB,MAAM,qBAAqB;;;;;;;;;;;;;;;;;;;;AAkC3B,SAAgB,cAAc,EAC5B,OACA,OACA,MAAM,MACN,MAAM,GACN,UACA,GAAG,SAG2C;CAC9C,OAAO;EACL,GAAG;EACH,GAAI,OAAO,OAAO,CAAC,IAAI,EAAE,IAAI;EAC7B,GAAI,OAAO,OAAO,CAAC,IAAI,EAAE,IAAI;EAC7B,MAAM;EACN,UAAU,yBAAyB,EAEjC,UAAW,YAAkD,KAC/D,CAAC;EACD,OAAO;GACL,GAAG;GACH,cAAc,CAAC,uBAAuB,GAAI,OAAO,gBAAgB,CAAC,CAAE;EACtE;EACA,OAAO;GACL,GAAG;GACH,aAAa,OAAO,eAAe;GACnC,YAAY;IACV,GAAG,OAAO;IACV,MAAM;IACN,OAAO;GACT;EACF;CACF;AACF"}
|
package/dist/field/index.d.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @cutticat/payload-field-ms — Payload number field edited as an `@cutticat/ms` duration.
|
|
3
|
+
*
|
|
4
|
+
* The database stores milliseconds. The admin input accepts strings such as `10d`.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
1
8
|
import { DurationFieldOptions, durationField } from "./field.js";
|
|
2
9
|
import { CreateDurationValidationOptions, createDurationValidation, dropUnparsedFromDraft } from "./validation.js";
|
|
3
10
|
export { CreateDurationValidationOptions, DurationFieldOptions, createDurationValidation, dropUnparsedFromDraft, durationField };
|
package/dist/field/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { createDurationValidation, dropUnparsedFromDraft } from "./validation.js";
|
|
2
1
|
import { durationField } from "./field.js";
|
|
2
|
+
import { createDurationValidation, dropUnparsedFromDraft } from "./validation.js";
|
|
3
3
|
export { createDurationValidation, dropUnparsedFromDraft, durationField };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import "@cutticat/ms";
|
|
2
|
-
|
|
2
|
+
// lib/internal/durations.ts
|
|
3
3
|
/**
|
|
4
4
|
* Whether an item is a duration that can be saved and shown.
|
|
5
5
|
*
|
|
@@ -9,7 +9,6 @@ import "@cutticat/ms";
|
|
|
9
9
|
function isDuration(item) {
|
|
10
10
|
return typeof item === "number" && Number.isFinite(item);
|
|
11
11
|
}
|
|
12
|
-
//#endregion
|
|
13
12
|
export { isDuration };
|
|
14
13
|
|
|
15
14
|
//# sourceMappingURL=durations.js.map
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { FieldHookArgs, NumberFieldValidation } from "payload";
|
|
2
2
|
import { FullyUndefinable } from "@cutticat/types";
|
|
3
|
-
//#region lib/validation.d.ts
|
|
4
3
|
/** Options of {@link createDurationValidation}. */
|
|
5
4
|
export type CreateDurationValidationOptions = FullyUndefinable<{
|
|
6
5
|
/**
|
|
@@ -44,5 +43,4 @@ export declare function createDurationValidation({ validate }?: Readonly<CreateD
|
|
|
44
43
|
* @returns The value to save.
|
|
45
44
|
*/
|
|
46
45
|
export declare function dropUnparsedFromDraft({ collection, data, global, previousValue, value }: FieldHookArgs): unknown;
|
|
47
|
-
//#endregion
|
|
48
46
|
//# sourceMappingURL=validation.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.d.ts","names":[],"sources":["../../lib/validation.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"validation.d.ts","names":[],"sources":["../../lib/validation.ts"],"mappings":";;;YAMY,kCAAkC;;;;;EAK5C,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;;wBA2BI,2BACd,aACC,SAAS,mCAAwC;;;;;;;;;;wBAqDpC,wBACd,YACA,MACA,QACA,eACA,SACC"}
|
package/dist/field/validation.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { isDuration } from "./internal/durations.js";
|
|
2
2
|
import { validations } from "payload";
|
|
3
3
|
import { ms } from "@cutticat/ms";
|
|
4
|
-
|
|
4
|
+
// lib/validation.ts
|
|
5
5
|
/**
|
|
6
6
|
* Validation of a duration field: what {@link durationField} sets as `validate`. Checks every
|
|
7
7
|
* item of the value against the field's `min` and `max`, then hands over to the number
|
|
@@ -80,7 +80,6 @@ function dropUnparsedFromDraft({ collection, data, global, previousValue, value
|
|
|
80
80
|
return typeof drafts === "object" && drafts.validate === true;
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
|
-
//#endregion
|
|
84
83
|
export { createDurationValidation, dropUnparsedFromDraft };
|
|
85
84
|
|
|
86
85
|
//# sourceMappingURL=validation.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.js","names":[],"sources":["../../lib/validation.ts"],"sourcesContent":["import { type FieldHookArgs, type NumberFieldValidation, validations } from \"payload\"\nimport { ms } from \"@cutticat/ms\"\nimport type { FullyUndefinable } from \"@cutticat/types\"\nimport { isDuration } from \"./internal/durations.
|
|
1
|
+
{"version":3,"file":"validation.js","names":[],"sources":["../../lib/validation.ts"],"sourcesContent":["import { type FieldHookArgs, type NumberFieldValidation, validations } from \"payload\"\nimport { ms } from \"@cutticat/ms\"\nimport type { FullyUndefinable } from \"@cutticat/types\"\nimport { isDuration } from \"./internal/durations.ts\"\n\n/** Options of {@link createDurationValidation}. */\nexport type CreateDurationValidationOptions = FullyUndefinable<{\n /**\n * Validation to run after the duration checks, `null` for Payload's number validation.\n * Defaults to `null`.\n */\n validate: NumberFieldValidation | null\n}>\n\n/**\n * Validation of a duration field: what {@link durationField} sets as `validate`. Checks every\n * item of the value against the field's `min` and `max`, then hands over to the number\n * validation. Use it for a number field built by hand with the duration input, together with\n * {@link dropUnparsedFromDraft}.\n *\n * An item that is not a finite number, such as text that did not parse, is rejected with\n * Payload's `validation:invalidInput`; `min` and `max` with Payload's own messages, the numbers\n * shown as durations.\n *\n * @param options - The validation to run next.\n * @returns A Payload number field validation.\n * @example\n * ```typescript\n * const field: NumberField = {\n * name: \"timeoutMs\",\n * type: \"number\",\n * min: 0,\n * validate: createDurationValidation(),\n * hooks: { beforeChange: [dropUnparsedFromDraft] },\n * admin: { components: { Field: \"@cutticat/payload-field-ms/client#DurationField\" } },\n * }\n * ```\n */\nexport function createDurationValidation({\n validate = null,\n}: Readonly<CreateDurationValidationOptions> = {}): NumberFieldValidation {\n return (value, args) => {\n // Payload passes the field config in the arguments\n const {\n max,\n min,\n req: { t },\n } = args\n const label = t(\"general:value\")\n\n for (const item of itemsToCheck(value)) {\n if (!isDuration(item)) {\n return t(\"validation:invalidInput\")\n }\n\n if (typeof min === \"number\" && item < min) {\n return t(\"validation:lessThanMin\", { label, min: ms(min), value: ms(item) })\n }\n\n if (typeof max === \"number\" && item > max) {\n return t(\"validation:greaterThanMax\", { label, max: ms(max), value: ms(item) })\n }\n }\n\n const next = validate ?? (validations.number as NumberFieldValidation)\n\n return next(value, args)\n }\n\n // Helpers\n\n /**\n * Items of a value as Payload hands it over, unparsed and malformed ones included.\n *\n * @param value - A single value, an array of them with `hasMany`, or empty.\n * @returns The items to check.\n */\n function itemsToCheck(value: unknown): unknown[] {\n if (Array.isArray(value)) return value\n\n return value == null ? [] : [value]\n }\n}\n\n/**\n * `beforeChange` hook that keeps unparsed text out of the database on a draft save that Payload\n * does not validate: the item is dropped, a single value keeps what was saved before. With\n * `versions.drafts.validate` the hook leaves the value to the validation. {@link durationField}\n * adds it; a field built by hand with the duration input needs it too.\n *\n * @param args - Payload field hook arguments.\n * @returns The value to save.\n */\nexport function dropUnparsedFromDraft({\n collection,\n data,\n global,\n previousValue,\n value,\n}: FieldHookArgs): unknown {\n if (data?._status !== \"draft\" || validatesDrafts()) return value\n if (Array.isArray(value)) return value.filter(isDuration)\n\n return value == null || isDuration(value) ? value : (previousValue ?? null)\n\n // Helpers\n\n /**\n * Whether Payload validates a draft save of this document, as `versions.drafts.validate` sets.\n *\n * @returns `true` when draft saves are validated.\n */\n function validatesDrafts(): boolean {\n const versions = (collection ?? global)?.versions\n const drafts = typeof versions === \"object\" ? versions.drafts : undefined\n\n return typeof drafts === \"object\" && drafts.validate === true\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA,SAAgB,yBAAyB,EACvC,WAAW,SACkC,CAAC,GAA0B;CACxE,QAAQ,OAAO,SAAS;EAEtB,MAAM,EACJ,KACA,KACA,KAAK,EAAE,QACL;EACJ,MAAM,QAAQ,EAAE,eAAe;EAE/B,KAAK,MAAM,QAAQ,aAAa,KAAK,GAAG;GACtC,IAAI,CAAC,WAAW,IAAI,GAClB,OAAO,EAAE,yBAAyB;GAGpC,IAAI,OAAO,QAAQ,YAAY,OAAO,KACpC,OAAO,EAAE,0BAA0B;IAAE;IAAO,KAAK,GAAG,GAAG;IAAG,OAAO,GAAG,IAAI;GAAE,CAAC;GAG7E,IAAI,OAAO,QAAQ,YAAY,OAAO,KACpC,OAAO,EAAE,6BAA6B;IAAE;IAAO,KAAK,GAAG,GAAG;IAAG,OAAO,GAAG,IAAI;GAAE,CAAC;EAElF;EAIA,QAFa,YAAa,YAAY,OAAA,CAE1B,OAAO,IAAI;CACzB;;;;;;;CAUA,SAAS,aAAa,OAA2B;EAC/C,IAAI,MAAM,QAAQ,KAAK,GAAG,OAAO;EAEjC,OAAO,SAAS,OAAO,CAAC,IAAI,CAAC,KAAK;CACpC;AACF;;;;;;;;;;AAWA,SAAgB,sBAAsB,EACpC,YACA,MACA,QACA,eACA,SACyB;CACzB,IAAI,MAAM,YAAY,WAAW,gBAAgB,GAAG,OAAO;CAC3D,IAAI,MAAM,QAAQ,KAAK,GAAG,OAAO,MAAM,OAAO,UAAU;CAExD,OAAO,SAAS,QAAQ,WAAW,KAAK,IAAI,QAAS,iBAAiB;;;;;;CAStE,SAAS,kBAA2B;EAClC,MAAM,YAAY,cAAc,OAAA,EAAS;EACzC,MAAM,SAAS,OAAO,aAAa,WAAW,SAAS,SAAS,KAAA;EAEhE,OAAO,OAAO,WAAW,YAAY,OAAO,aAAa;CAC3D;AACF"}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.
|
|
6
|
+
"version": "1.1.0",
|
|
7
7
|
"description": "Payload number field that edits a duration with @cutticat/ms",
|
|
8
8
|
"author": "Maksim Victorovich Fomin",
|
|
9
9
|
"repository": {
|
|
@@ -20,8 +20,11 @@
|
|
|
20
20
|
"cutticat"
|
|
21
21
|
],
|
|
22
22
|
"type": "module",
|
|
23
|
+
"imports": {
|
|
24
|
+
"#/*": "./lib/*"
|
|
25
|
+
},
|
|
23
26
|
"engines": {
|
|
24
|
-
"node": ">=
|
|
27
|
+
"node": ">=24.14.0"
|
|
25
28
|
},
|
|
26
29
|
"dependencies": {
|
|
27
30
|
"@cutticat/ms": "^1.1.0",
|
|
@@ -29,18 +32,16 @@
|
|
|
29
32
|
"@cutticat/types": "^2.1.0"
|
|
30
33
|
},
|
|
31
34
|
"devDependencies": {
|
|
32
|
-
"@biomejs/biome": "2.
|
|
33
|
-
"@cutticat/md-url-map": "^2.
|
|
35
|
+
"@biomejs/biome": "^2.6.7",
|
|
36
|
+
"@cutticat/md-url-map": "^2.1.0",
|
|
34
37
|
"@payloadcms/ui": "3.88.0",
|
|
35
|
-
"@types/node": "^
|
|
38
|
+
"@types/node": "^24.19.2",
|
|
36
39
|
"@types/react": "^19.2.14",
|
|
37
40
|
"payload": "3.88.0",
|
|
38
41
|
"react": "19.2.6",
|
|
39
42
|
"react-dom": "19.2.6",
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"tsx": "^4.19.2",
|
|
43
|
-
"typescript": "^5.9.3"
|
|
43
|
+
"tsdown": "0.23.7",
|
|
44
|
+
"typescript": "^7.0.2"
|
|
44
45
|
},
|
|
45
46
|
"peerDependencies": {
|
|
46
47
|
"@payloadcms/ui": ">=3.80.0 <4.0.0",
|
|
@@ -70,10 +71,9 @@
|
|
|
70
71
|
"lint:fix:unsafe": "biome check --write --unsafe .",
|
|
71
72
|
"format": "biome format --write .",
|
|
72
73
|
"format:check": "biome format .",
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
"test": "
|
|
76
|
-
"test:
|
|
77
|
-
"test:coverage": "tsx --test --experimental-test-coverage \"./tests/**/*.test.ts\""
|
|
74
|
+
"build": "tsc -p ./tsconfig.build.json && tsdown",
|
|
75
|
+
"test": "tsc && node --test \"./tests/**/*.test.ts\"",
|
|
76
|
+
"test:watch": "node --test --watch \"./tests/**/*.test.ts\"",
|
|
77
|
+
"test:coverage": "node --test --experimental-test-coverage \"./tests/**/*.test.ts\""
|
|
78
78
|
}
|
|
79
79
|
}
|