@contember/react-form 2.1.0-alpha.30 → 2.1.0-alpha.35
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/package.json +10 -10
- package/src/components/FormCheckbox.tsx +2 -1
- package/src/components/FormError.tsx +1 -1
- package/src/components/FormFieldStateProvider.tsx +0 -2
- package/src/components/FormInput.tsx +35 -32
- package/src/components/FormRadioInput.tsx +1 -1
- package/src/contexts.tsx +0 -2
- package/src/hooks/useFormInputValidationHandler.tsx +1 -1
- package/src/internal/useFormInputHandler.ts +6 -6
- package/dist/development/index.cjs +0 -30
- package/dist/development/index.cjs.map +0 -1
- package/dist/development/index.js +0 -30
- package/dist/development/index.js.map +0 -1
- package/dist/development/src/components/FormCheckbox.cjs +0 -83
- package/dist/development/src/components/FormCheckbox.cjs.map +0 -1
- package/dist/development/src/components/FormCheckbox.js +0 -67
- package/dist/development/src/components/FormCheckbox.js.map +0 -1
- package/dist/development/src/components/FormError.cjs +0 -22
- package/dist/development/src/components/FormError.cjs.map +0 -1
- package/dist/development/src/components/FormError.js +0 -22
- package/dist/development/src/components/FormError.js.map +0 -1
- package/dist/development/src/components/FormFieldScope.cjs +0 -33
- package/dist/development/src/components/FormFieldScope.cjs.map +0 -1
- package/dist/development/src/components/FormFieldScope.js +0 -33
- package/dist/development/src/components/FormFieldScope.js.map +0 -1
- package/dist/development/src/components/FormFieldStateProvider.cjs +0 -56
- package/dist/development/src/components/FormFieldStateProvider.cjs.map +0 -1
- package/dist/development/src/components/FormFieldStateProvider.js +0 -56
- package/dist/development/src/components/FormFieldStateProvider.js.map +0 -1
- package/dist/development/src/components/FormHasManyRelationScope.cjs +0 -33
- package/dist/development/src/components/FormHasManyRelationScope.cjs.map +0 -1
- package/dist/development/src/components/FormHasManyRelationScope.js +0 -33
- package/dist/development/src/components/FormHasManyRelationScope.js.map +0 -1
- package/dist/development/src/components/FormHasOneRelationScope.cjs +0 -33
- package/dist/development/src/components/FormHasOneRelationScope.cjs.map +0 -1
- package/dist/development/src/components/FormHasOneRelationScope.js +0 -33
- package/dist/development/src/components/FormHasOneRelationScope.js.map +0 -1
- package/dist/development/src/components/FormInput.cjs +0 -59
- package/dist/development/src/components/FormInput.cjs.map +0 -1
- package/dist/development/src/components/FormInput.js +0 -59
- package/dist/development/src/components/FormInput.js.map +0 -1
- package/dist/development/src/components/FormLabel.cjs +0 -34
- package/dist/development/src/components/FormLabel.cjs.map +0 -1
- package/dist/development/src/components/FormLabel.js +0 -34
- package/dist/development/src/components/FormLabel.js.map +0 -1
- package/dist/development/src/components/FormRadioInput.cjs +0 -55
- package/dist/development/src/components/FormRadioInput.cjs.map +0 -1
- package/dist/development/src/components/FormRadioInput.js +0 -55
- package/dist/development/src/components/FormRadioInput.js.map +0 -1
- package/dist/development/src/contexts.cjs +0 -13
- package/dist/development/src/contexts.cjs.map +0 -1
- package/dist/development/src/contexts.js +0 -13
- package/dist/development/src/contexts.js.map +0 -1
- package/dist/development/src/hooks/useFormInputValidationHandler.cjs +0 -80
- package/dist/development/src/hooks/useFormInputValidationHandler.cjs.map +0 -1
- package/dist/development/src/hooks/useFormInputValidationHandler.js +0 -64
- package/dist/development/src/hooks/useFormInputValidationHandler.js.map +0 -1
- package/dist/development/src/internal/useFormInputHandler.cjs +0 -122
- package/dist/development/src/internal/useFormInputHandler.cjs.map +0 -1
- package/dist/development/src/internal/useFormInputHandler.js +0 -122
- package/dist/development/src/internal/useFormInputHandler.js.map +0 -1
- package/dist/production/index.cjs +0 -30
- package/dist/production/index.cjs.map +0 -1
- package/dist/production/index.js +0 -30
- package/dist/production/index.js.map +0 -1
- package/dist/production/src/components/FormCheckbox.cjs +0 -71
- package/dist/production/src/components/FormCheckbox.cjs.map +0 -1
- package/dist/production/src/components/FormCheckbox.js +0 -55
- package/dist/production/src/components/FormCheckbox.js.map +0 -1
- package/dist/production/src/components/FormError.cjs +0 -22
- package/dist/production/src/components/FormError.cjs.map +0 -1
- package/dist/production/src/components/FormError.js +0 -22
- package/dist/production/src/components/FormError.js.map +0 -1
- package/dist/production/src/components/FormFieldScope.cjs +0 -25
- package/dist/production/src/components/FormFieldScope.cjs.map +0 -1
- package/dist/production/src/components/FormFieldScope.js +0 -25
- package/dist/production/src/components/FormFieldScope.js.map +0 -1
- package/dist/production/src/components/FormFieldStateProvider.cjs +0 -34
- package/dist/production/src/components/FormFieldStateProvider.cjs.map +0 -1
- package/dist/production/src/components/FormFieldStateProvider.js +0 -34
- package/dist/production/src/components/FormFieldStateProvider.js.map +0 -1
- package/dist/production/src/components/FormHasManyRelationScope.cjs +0 -25
- package/dist/production/src/components/FormHasManyRelationScope.cjs.map +0 -1
- package/dist/production/src/components/FormHasManyRelationScope.js +0 -25
- package/dist/production/src/components/FormHasManyRelationScope.js.map +0 -1
- package/dist/production/src/components/FormHasOneRelationScope.cjs +0 -25
- package/dist/production/src/components/FormHasOneRelationScope.cjs.map +0 -1
- package/dist/production/src/components/FormHasOneRelationScope.js +0 -25
- package/dist/production/src/components/FormHasOneRelationScope.js.map +0 -1
- package/dist/production/src/components/FormInput.cjs +0 -47
- package/dist/production/src/components/FormInput.cjs.map +0 -1
- package/dist/production/src/components/FormInput.js +0 -47
- package/dist/production/src/components/FormInput.js.map +0 -1
- package/dist/production/src/components/FormLabel.cjs +0 -26
- package/dist/production/src/components/FormLabel.cjs.map +0 -1
- package/dist/production/src/components/FormLabel.js +0 -26
- package/dist/production/src/components/FormLabel.js.map +0 -1
- package/dist/production/src/components/FormRadioInput.cjs +0 -43
- package/dist/production/src/components/FormRadioInput.cjs.map +0 -1
- package/dist/production/src/components/FormRadioInput.js +0 -43
- package/dist/production/src/components/FormRadioInput.js.map +0 -1
- package/dist/production/src/contexts.cjs +0 -13
- package/dist/production/src/contexts.cjs.map +0 -1
- package/dist/production/src/contexts.js +0 -13
- package/dist/production/src/contexts.js.map +0 -1
- package/dist/production/src/hooks/useFormInputValidationHandler.cjs +0 -80
- package/dist/production/src/hooks/useFormInputValidationHandler.cjs.map +0 -1
- package/dist/production/src/hooks/useFormInputValidationHandler.js +0 -64
- package/dist/production/src/hooks/useFormInputValidationHandler.js.map +0 -1
- package/dist/production/src/internal/useFormInputHandler.cjs +0 -122
- package/dist/production/src/internal/useFormInputHandler.cjs.map +0 -1
- package/dist/production/src/internal/useFormInputHandler.js +0 -122
- package/dist/production/src/internal/useFormInputHandler.js.map +0 -1
- package/dist/types/components/FormCheckbox.d.ts +0 -11
- package/dist/types/components/FormCheckbox.d.ts.map +0 -1
- package/dist/types/components/FormError.d.ts +0 -7
- package/dist/types/components/FormError.d.ts.map +0 -1
- package/dist/types/components/FormFieldScope.d.ts +0 -9
- package/dist/types/components/FormFieldScope.d.ts.map +0 -1
- package/dist/types/components/FormFieldStateProvider.d.ts +0 -36
- package/dist/types/components/FormFieldStateProvider.d.ts.map +0 -1
- package/dist/types/components/FormHasManyRelationScope.d.ts +0 -12
- package/dist/types/components/FormHasManyRelationScope.d.ts.map +0 -1
- package/dist/types/components/FormHasOneRelationScope.d.ts +0 -9
- package/dist/types/components/FormHasOneRelationScope.d.ts.map +0 -1
- package/dist/types/components/FormInput.d.ts +0 -13
- package/dist/types/components/FormInput.d.ts.map +0 -1
- package/dist/types/components/FormLabel.d.ts +0 -5
- package/dist/types/components/FormLabel.d.ts.map +0 -1
- package/dist/types/components/FormRadioInput.d.ts +0 -11
- package/dist/types/components/FormRadioInput.d.ts.map +0 -1
- package/dist/types/components/index.d.ts +0 -10
- package/dist/types/components/index.d.ts.map +0 -1
- package/dist/types/contexts.d.ts +0 -12
- package/dist/types/contexts.d.ts.map +0 -1
- package/dist/types/hooks/index.d.ts +0 -2
- package/dist/types/hooks/index.d.ts.map +0 -1
- package/dist/types/hooks/useFormInputValidationHandler.d.ts +0 -8
- package/dist/types/hooks/useFormInputValidationHandler.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -5
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/internal/useFormInputHandler.d.ts +0 -4
- package/dist/types/internal/useFormInputHandler.d.ts.map +0 -1
- package/dist/types/tsconfig.tsbuildinfo +0 -1
- package/dist/types/types/index.d.ts +0 -23
- package/dist/types/types/index.d.ts.map +0 -1
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const jsxRuntime = require("react/jsx-runtime");
|
|
4
|
-
const reactSlot = require("@radix-ui/react-slot");
|
|
5
|
-
const utilities = require("@contember/utilities");
|
|
6
|
-
const contexts = require("../contexts.cjs");
|
|
7
|
-
const reactBinding = require("@contember/react-binding");
|
|
8
|
-
const FormLabel = (props) => {
|
|
9
|
-
const formState = contexts.useFormFieldState();
|
|
10
|
-
if (!formState) {
|
|
11
|
-
throw new reactBinding.BindingError("FormError must be used inside a FormField");
|
|
12
|
-
}
|
|
13
|
-
const { errors, htmlId, dirty, required } = formState;
|
|
14
|
-
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
15
|
-
reactSlot.Slot,
|
|
16
|
-
{
|
|
17
|
-
"data-invalid": utilities.dataAttribute(errors.length > 0),
|
|
18
|
-
"data-dirty": utilities.dataAttribute(dirty),
|
|
19
|
-
"data-required": utilities.dataAttribute(required),
|
|
20
|
-
...htmlId ? { htmlFor: `${htmlId}-input` } : {},
|
|
21
|
-
...props
|
|
22
|
-
}
|
|
23
|
-
);
|
|
24
|
-
};
|
|
25
|
-
exports.FormLabel = FormLabel;
|
|
26
|
-
//# sourceMappingURL=FormLabel.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"FormLabel.cjs","sources":["../../../../../packages/react-form/src/components/FormLabel.tsx"],"sourcesContent":["import * as React from 'react'\nimport { Slot } from '@radix-ui/react-slot'\nimport { dataAttribute } from '@contember/utilities'\nimport { useFormFieldState } from '../contexts'\nimport { BindingError } from '@contember/react-binding'\n\nexport const FormLabel = (props: {\n\tchildren: React.ReactElement\n}) => {\n\tconst formState = useFormFieldState()\n\tif (!formState) {\n\t\tthrow new BindingError('FormError must be used inside a FormField')\n\t}\n\tconst { errors, htmlId, dirty, required } = formState\n\treturn (\n\t\t<Slot\n\t\t\tdata-invalid={dataAttribute(errors.length > 0)}\n\t\t\tdata-dirty={dataAttribute(dirty)}\n\t\t\tdata-required={dataAttribute(required)}\n\t\t\t{...(htmlId ? { htmlFor: `${htmlId}-input` } : {})}\n\t\t\t{...props}\n\t\t/>\n\t)\n}\n"],"names":["useFormFieldState","BindingError","jsx","Slot","dataAttribute"],"mappings":";;;;;;;AAMa,MAAA,YAAY,CAAC,UAEpB;AACL,QAAM,YAAYA,SAAAA,kBAAkB;AACpC,MAAI,CAAC,WAAW;AACT,UAAA,IAAIC,0BAAa,2CAA2C;AAAA,EAAA;AAEnE,QAAM,EAAE,QAAQ,QAAQ,OAAO,SAAa,IAAA;AAE3C,SAAAC,2BAAA;AAAA,IAACC,UAAA;AAAA,IAAA;AAAA,MACA,gBAAcC,UAAA,cAAc,OAAO,SAAS,CAAC;AAAA,MAC7C,cAAYA,wBAAc,KAAK;AAAA,MAC/B,iBAAeA,wBAAc,QAAQ;AAAA,MACpC,GAAI,SAAS,EAAE,SAAS,GAAG,MAAM,SAAA,IAAa,CAAC;AAAA,MAC/C,GAAG;AAAA,IAAA;AAAA,EACL;AAEF;;"}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { jsx } from "react/jsx-runtime";
|
|
2
|
-
import { Slot } from "@radix-ui/react-slot";
|
|
3
|
-
import { dataAttribute } from "@contember/utilities";
|
|
4
|
-
import { useFormFieldState } from "../contexts.js";
|
|
5
|
-
import { BindingError } from "@contember/react-binding";
|
|
6
|
-
const FormLabel = (props) => {
|
|
7
|
-
const formState = useFormFieldState();
|
|
8
|
-
if (!formState) {
|
|
9
|
-
throw new BindingError("FormError must be used inside a FormField");
|
|
10
|
-
}
|
|
11
|
-
const { errors, htmlId, dirty, required } = formState;
|
|
12
|
-
return /* @__PURE__ */ jsx(
|
|
13
|
-
Slot,
|
|
14
|
-
{
|
|
15
|
-
"data-invalid": dataAttribute(errors.length > 0),
|
|
16
|
-
"data-dirty": dataAttribute(dirty),
|
|
17
|
-
"data-required": dataAttribute(required),
|
|
18
|
-
...htmlId ? { htmlFor: `${htmlId}-input` } : {},
|
|
19
|
-
...props
|
|
20
|
-
}
|
|
21
|
-
);
|
|
22
|
-
};
|
|
23
|
-
export {
|
|
24
|
-
FormLabel
|
|
25
|
-
};
|
|
26
|
-
//# sourceMappingURL=FormLabel.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"FormLabel.js","sources":["../../../../../packages/react-form/src/components/FormLabel.tsx"],"sourcesContent":["import * as React from 'react'\nimport { Slot } from '@radix-ui/react-slot'\nimport { dataAttribute } from '@contember/utilities'\nimport { useFormFieldState } from '../contexts'\nimport { BindingError } from '@contember/react-binding'\n\nexport const FormLabel = (props: {\n\tchildren: React.ReactElement\n}) => {\n\tconst formState = useFormFieldState()\n\tif (!formState) {\n\t\tthrow new BindingError('FormError must be used inside a FormField')\n\t}\n\tconst { errors, htmlId, dirty, required } = formState\n\treturn (\n\t\t<Slot\n\t\t\tdata-invalid={dataAttribute(errors.length > 0)}\n\t\t\tdata-dirty={dataAttribute(dirty)}\n\t\t\tdata-required={dataAttribute(required)}\n\t\t\t{...(htmlId ? { htmlFor: `${htmlId}-input` } : {})}\n\t\t\t{...props}\n\t\t/>\n\t)\n}\n"],"names":[],"mappings":";;;;;AAMa,MAAA,YAAY,CAAC,UAEpB;AACL,QAAM,YAAY,kBAAkB;AACpC,MAAI,CAAC,WAAW;AACT,UAAA,IAAI,aAAa,2CAA2C;AAAA,EAAA;AAEnE,QAAM,EAAE,QAAQ,QAAQ,OAAO,SAAa,IAAA;AAE3C,SAAA;AAAA,IAAC;AAAA,IAAA;AAAA,MACA,gBAAc,cAAc,OAAO,SAAS,CAAC;AAAA,MAC7C,cAAY,cAAc,KAAK;AAAA,MAC/B,iBAAe,cAAc,QAAQ;AAAA,MACpC,GAAI,SAAS,EAAE,SAAS,GAAG,MAAM,SAAA,IAAa,CAAC;AAAA,MAC/C,GAAG;AAAA,IAAA;AAAA,EACL;AAEF;"}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const jsxRuntime = require("react/jsx-runtime");
|
|
4
|
-
const reactBinding = require("@contember/react-binding");
|
|
5
|
-
const utilities = require("@contember/utilities");
|
|
6
|
-
const reactSlot = require("@radix-ui/react-slot");
|
|
7
|
-
const React = require("react");
|
|
8
|
-
const contexts = require("../contexts.cjs");
|
|
9
|
-
const useFormInputValidationHandler = require("../hooks/useFormInputValidationHandler.cjs");
|
|
10
|
-
const SlotInput = reactSlot.Slot;
|
|
11
|
-
const FormRadioInput = reactBinding.Component(({ field, value, defaultValue, isNonbearing, ...props }) => {
|
|
12
|
-
const accessor = reactBinding.useField(field);
|
|
13
|
-
const formState = contexts.useFormFieldState();
|
|
14
|
-
const id = formState?.htmlId;
|
|
15
|
-
const hasErrors = (formState?.errors.length ?? accessor.errors?.errors?.length ?? 0) > 0;
|
|
16
|
-
const dirty = formState?.dirty ?? accessor.hasUnpersistedChanges;
|
|
17
|
-
const accessorGetter = accessor.getAccessor;
|
|
18
|
-
const { ref, onFocus, onBlur } = useFormInputValidationHandler.useFormInputValidationHandler(accessor);
|
|
19
|
-
const handleChange = React.useCallback((e) => {
|
|
20
|
-
accessorGetter().updateValue(value);
|
|
21
|
-
}, [accessorGetter, value]);
|
|
22
|
-
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
23
|
-
SlotInput,
|
|
24
|
-
{
|
|
25
|
-
ref,
|
|
26
|
-
type: "radio",
|
|
27
|
-
value: typeof value === "string" ? value : void 0,
|
|
28
|
-
checked: accessor.value === value,
|
|
29
|
-
"data-invalid": utilities.dataAttribute(hasErrors),
|
|
30
|
-
"data-dirty": utilities.dataAttribute(dirty),
|
|
31
|
-
name: id ? id + "-input" : void 0,
|
|
32
|
-
id: id ? `${id}-input` : void 0,
|
|
33
|
-
onFocus,
|
|
34
|
-
onBlur,
|
|
35
|
-
onChange: handleChange,
|
|
36
|
-
...props
|
|
37
|
-
}
|
|
38
|
-
);
|
|
39
|
-
}, ({ field, isNonbearing, defaultValue }) => {
|
|
40
|
-
return /* @__PURE__ */ jsxRuntime.jsx(reactBinding.Field, { field, isNonbearing, defaultValue });
|
|
41
|
-
});
|
|
42
|
-
exports.FormRadioInput = FormRadioInput;
|
|
43
|
-
//# sourceMappingURL=FormRadioInput.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"FormRadioInput.cjs","sources":["../../../../../packages/react-form/src/components/FormRadioInput.tsx"],"sourcesContent":["import { Component, Field, OptionallyVariableFieldValue, SugaredRelativeSingleField, useField } from '@contember/react-binding'\nimport { dataAttribute } from '@contember/utilities'\nimport { Slot } from '@radix-ui/react-slot'\nimport * as React from 'react'\nimport { ChangeEventHandler, useCallback } from 'react'\nimport { useFormFieldState } from '../contexts'\nimport { useFormInputValidationHandler } from '../hooks'\n\nconst SlotInput = Slot as React.ForwardRefExoticComponent<React.RefAttributes<HTMLInputElement> & React.InputHTMLAttributes<HTMLInputElement>>\n\nexport interface FormRadioItemProps {\n\tfield: SugaredRelativeSingleField['field']\n\tvalue: string | null | number | boolean\n\tisNonbearing?: boolean\n\tdefaultValue?: OptionallyVariableFieldValue\n\tchildren: React.ReactNode\n}\n\nexport const FormRadioInput = Component<FormRadioItemProps>(({ field, value, defaultValue, isNonbearing, ...props }) => {\n\tconst accessor = useField(field)\n\n\tconst formState = useFormFieldState()\n\tconst id = formState?.htmlId\n\tconst hasErrors = (formState?.errors.length ?? accessor.errors?.errors?.length ?? 0) > 0\n\tconst dirty = formState?.dirty ?? accessor.hasUnpersistedChanges\n\n\tconst accessorGetter = accessor.getAccessor\n\tconst { ref, onFocus, onBlur } = useFormInputValidationHandler(accessor)\n\n\tconst handleChange = useCallback<ChangeEventHandler<HTMLInputElement>>(e => {\n\t\taccessorGetter().updateValue(value)\n\t}, [accessorGetter, value])\n\n\treturn (\n\t\t<SlotInput\n\t\t\tref={ref}\n\t\t\ttype=\"radio\"\n\t\t\tvalue={typeof value === 'string' ? value : undefined}\n\t\t\tchecked={accessor.value === value}\n\t\t\tdata-invalid={dataAttribute(hasErrors)}\n\t\t\tdata-dirty={dataAttribute(dirty)}\n\t\t\tname={id ? id + '-input' : undefined}\n\t\t\tid={id ? `${id}-input` : undefined}\n\t\t\tonFocus={onFocus}\n\t\t\tonBlur={onBlur}\n\t\t\tonChange={handleChange}\n\t\t\t{...props}\n\t\t/>\n\t)\n}, ({ field, isNonbearing, defaultValue }) => {\n\treturn <Field field={field} isNonbearing={isNonbearing} defaultValue={defaultValue}/>\n})\n"],"names":["Slot","Component","useField","useFormFieldState","useFormInputValidationHandler","useCallback","jsx","dataAttribute","Field"],"mappings":";;;;;;;;;AAQA,MAAM,YAAYA,UAAA;AAUL,MAAA,iBAAiBC,aAAAA,UAA8B,CAAC,EAAE,OAAO,OAAO,cAAc,cAAc,GAAG,YAAY;AACjH,QAAA,WAAWC,sBAAS,KAAK;AAE/B,QAAM,YAAYC,SAAAA,kBAAkB;AACpC,QAAM,KAAK,WAAW;AAChB,QAAA,aAAa,WAAW,OAAO,UAAU,SAAS,QAAQ,QAAQ,UAAU,KAAK;AACjF,QAAA,QAAQ,WAAW,SAAS,SAAS;AAE3C,QAAM,iBAAiB,SAAS;AAChC,QAAM,EAAE,KAAK,SAAS,OAAO,IAAIC,8BAAAA,8BAA8B,QAAQ;AAEjE,QAAA,eAAeC,kBAAkD,CAAK,MAAA;AAC5D,mBAAA,EAAE,YAAY,KAAK;AAAA,EAAA,GAChC,CAAC,gBAAgB,KAAK,CAAC;AAGzB,SAAAC,2BAAA;AAAA,IAAC;AAAA,IAAA;AAAA,MACA;AAAA,MACA,MAAK;AAAA,MACL,OAAO,OAAO,UAAU,WAAW,QAAQ;AAAA,MAC3C,SAAS,SAAS,UAAU;AAAA,MAC5B,gBAAcC,wBAAc,SAAS;AAAA,MACrC,cAAYA,wBAAc,KAAK;AAAA,MAC/B,MAAM,KAAK,KAAK,WAAW;AAAA,MAC3B,IAAI,KAAK,GAAG,EAAE,WAAW;AAAA,MACzB;AAAA,MACA;AAAA,MACA,UAAU;AAAA,MACT,GAAG;AAAA,IAAA;AAAA,EACL;AAEF,GAAG,CAAC,EAAE,OAAO,cAAc,mBAAmB;AAC7C,SAAQD,2BAAAA,IAAAE,aAAAA,OAAA,EAAM,OAAc,cAA4B,aAA2B,CAAA;AACpF,CAAC;;"}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { jsx } from "react/jsx-runtime";
|
|
2
|
-
import { Component, useField, Field } from "@contember/react-binding";
|
|
3
|
-
import { dataAttribute } from "@contember/utilities";
|
|
4
|
-
import { Slot } from "@radix-ui/react-slot";
|
|
5
|
-
import { useCallback } from "react";
|
|
6
|
-
import { useFormFieldState } from "../contexts.js";
|
|
7
|
-
import { useFormInputValidationHandler } from "../hooks/useFormInputValidationHandler.js";
|
|
8
|
-
const SlotInput = Slot;
|
|
9
|
-
const FormRadioInput = Component(({ field, value, defaultValue, isNonbearing, ...props }) => {
|
|
10
|
-
const accessor = useField(field);
|
|
11
|
-
const formState = useFormFieldState();
|
|
12
|
-
const id = formState?.htmlId;
|
|
13
|
-
const hasErrors = (formState?.errors.length ?? accessor.errors?.errors?.length ?? 0) > 0;
|
|
14
|
-
const dirty = formState?.dirty ?? accessor.hasUnpersistedChanges;
|
|
15
|
-
const accessorGetter = accessor.getAccessor;
|
|
16
|
-
const { ref, onFocus, onBlur } = useFormInputValidationHandler(accessor);
|
|
17
|
-
const handleChange = useCallback((e) => {
|
|
18
|
-
accessorGetter().updateValue(value);
|
|
19
|
-
}, [accessorGetter, value]);
|
|
20
|
-
return /* @__PURE__ */ jsx(
|
|
21
|
-
SlotInput,
|
|
22
|
-
{
|
|
23
|
-
ref,
|
|
24
|
-
type: "radio",
|
|
25
|
-
value: typeof value === "string" ? value : void 0,
|
|
26
|
-
checked: accessor.value === value,
|
|
27
|
-
"data-invalid": dataAttribute(hasErrors),
|
|
28
|
-
"data-dirty": dataAttribute(dirty),
|
|
29
|
-
name: id ? id + "-input" : void 0,
|
|
30
|
-
id: id ? `${id}-input` : void 0,
|
|
31
|
-
onFocus,
|
|
32
|
-
onBlur,
|
|
33
|
-
onChange: handleChange,
|
|
34
|
-
...props
|
|
35
|
-
}
|
|
36
|
-
);
|
|
37
|
-
}, ({ field, isNonbearing, defaultValue }) => {
|
|
38
|
-
return /* @__PURE__ */ jsx(Field, { field, isNonbearing, defaultValue });
|
|
39
|
-
});
|
|
40
|
-
export {
|
|
41
|
-
FormRadioInput
|
|
42
|
-
};
|
|
43
|
-
//# sourceMappingURL=FormRadioInput.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"FormRadioInput.js","sources":["../../../../../packages/react-form/src/components/FormRadioInput.tsx"],"sourcesContent":["import { Component, Field, OptionallyVariableFieldValue, SugaredRelativeSingleField, useField } from '@contember/react-binding'\nimport { dataAttribute } from '@contember/utilities'\nimport { Slot } from '@radix-ui/react-slot'\nimport * as React from 'react'\nimport { ChangeEventHandler, useCallback } from 'react'\nimport { useFormFieldState } from '../contexts'\nimport { useFormInputValidationHandler } from '../hooks'\n\nconst SlotInput = Slot as React.ForwardRefExoticComponent<React.RefAttributes<HTMLInputElement> & React.InputHTMLAttributes<HTMLInputElement>>\n\nexport interface FormRadioItemProps {\n\tfield: SugaredRelativeSingleField['field']\n\tvalue: string | null | number | boolean\n\tisNonbearing?: boolean\n\tdefaultValue?: OptionallyVariableFieldValue\n\tchildren: React.ReactNode\n}\n\nexport const FormRadioInput = Component<FormRadioItemProps>(({ field, value, defaultValue, isNonbearing, ...props }) => {\n\tconst accessor = useField(field)\n\n\tconst formState = useFormFieldState()\n\tconst id = formState?.htmlId\n\tconst hasErrors = (formState?.errors.length ?? accessor.errors?.errors?.length ?? 0) > 0\n\tconst dirty = formState?.dirty ?? accessor.hasUnpersistedChanges\n\n\tconst accessorGetter = accessor.getAccessor\n\tconst { ref, onFocus, onBlur } = useFormInputValidationHandler(accessor)\n\n\tconst handleChange = useCallback<ChangeEventHandler<HTMLInputElement>>(e => {\n\t\taccessorGetter().updateValue(value)\n\t}, [accessorGetter, value])\n\n\treturn (\n\t\t<SlotInput\n\t\t\tref={ref}\n\t\t\ttype=\"radio\"\n\t\t\tvalue={typeof value === 'string' ? value : undefined}\n\t\t\tchecked={accessor.value === value}\n\t\t\tdata-invalid={dataAttribute(hasErrors)}\n\t\t\tdata-dirty={dataAttribute(dirty)}\n\t\t\tname={id ? id + '-input' : undefined}\n\t\t\tid={id ? `${id}-input` : undefined}\n\t\t\tonFocus={onFocus}\n\t\t\tonBlur={onBlur}\n\t\t\tonChange={handleChange}\n\t\t\t{...props}\n\t\t/>\n\t)\n}, ({ field, isNonbearing, defaultValue }) => {\n\treturn <Field field={field} isNonbearing={isNonbearing} defaultValue={defaultValue}/>\n})\n"],"names":[],"mappings":";;;;;;;AAQA,MAAM,YAAY;AAUL,MAAA,iBAAiB,UAA8B,CAAC,EAAE,OAAO,OAAO,cAAc,cAAc,GAAG,YAAY;AACjH,QAAA,WAAW,SAAS,KAAK;AAE/B,QAAM,YAAY,kBAAkB;AACpC,QAAM,KAAK,WAAW;AAChB,QAAA,aAAa,WAAW,OAAO,UAAU,SAAS,QAAQ,QAAQ,UAAU,KAAK;AACjF,QAAA,QAAQ,WAAW,SAAS,SAAS;AAE3C,QAAM,iBAAiB,SAAS;AAChC,QAAM,EAAE,KAAK,SAAS,OAAO,IAAI,8BAA8B,QAAQ;AAEjE,QAAA,eAAe,YAAkD,CAAK,MAAA;AAC5D,mBAAA,EAAE,YAAY,KAAK;AAAA,EAAA,GAChC,CAAC,gBAAgB,KAAK,CAAC;AAGzB,SAAA;AAAA,IAAC;AAAA,IAAA;AAAA,MACA;AAAA,MACA,MAAK;AAAA,MACL,OAAO,OAAO,UAAU,WAAW,QAAQ;AAAA,MAC3C,SAAS,SAAS,UAAU;AAAA,MAC5B,gBAAc,cAAc,SAAS;AAAA,MACrC,cAAY,cAAc,KAAK;AAAA,MAC/B,MAAM,KAAK,KAAK,WAAW;AAAA,MAC3B,IAAI,KAAK,GAAG,EAAE,WAAW;AAAA,MACzB;AAAA,MACA;AAAA,MACA,UAAU;AAAA,MACT,GAAG;AAAA,IAAA;AAAA,EACL;AAEF,GAAG,CAAC,EAAE,OAAO,cAAc,mBAAmB;AAC7C,SAAQ,oBAAA,OAAA,EAAM,OAAc,cAA4B,aAA2B,CAAA;AACpF,CAAC;"}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const reactUtils = require("@contember/react-utils");
|
|
4
|
-
const FormFieldStateContext_ = reactUtils.createContext("FormFieldStateContext", void 0);
|
|
5
|
-
const FormFieldStateContext = FormFieldStateContext_[0];
|
|
6
|
-
const useFormFieldState = FormFieldStateContext_[1];
|
|
7
|
-
const useFormFieldId = () => useFormFieldState()?.htmlId;
|
|
8
|
-
const useFormError = () => useFormFieldState()?.errors;
|
|
9
|
-
exports.FormFieldStateContext = FormFieldStateContext;
|
|
10
|
-
exports.useFormError = useFormError;
|
|
11
|
-
exports.useFormFieldId = useFormFieldId;
|
|
12
|
-
exports.useFormFieldState = useFormFieldState;
|
|
13
|
-
//# sourceMappingURL=contexts.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"contexts.cjs","sources":["../../../../packages/react-form/src/contexts.tsx"],"sourcesContent":["import { createContext } from '@contember/react-utils'\nimport { FormFieldState } from './types'\n\n\nconst FormFieldStateContext_ = createContext<FormFieldState | undefined>('FormFieldStateContext', undefined)\nexport const FormFieldStateContext = FormFieldStateContext_[0]\nexport const useFormFieldState = FormFieldStateContext_[1]\n\n\n/**\n * @deprecated use `useFormState` instead\n */\nexport const useFormFieldId = () => useFormFieldState()?.htmlId\n\n/**\n * @deprecated use `useFormState` instead\n */\nexport const useFormError = () => useFormFieldState()?.errors\n"],"names":["createContext"],"mappings":";;;AAIA,MAAM,yBAAyBA,WAA0C,cAAA,yBAAyB,MAAS;AAC9F,MAAA,wBAAwB,uBAAuB,CAAC;AAChD,MAAA,oBAAoB,uBAAuB,CAAC;AAM5C,MAAA,iBAAiB,MAAM,qBAAqB;AAK5C,MAAA,eAAe,MAAM,qBAAqB;;;;;"}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { createContext } from "@contember/react-utils";
|
|
2
|
-
const FormFieldStateContext_ = createContext("FormFieldStateContext", void 0);
|
|
3
|
-
const FormFieldStateContext = FormFieldStateContext_[0];
|
|
4
|
-
const useFormFieldState = FormFieldStateContext_[1];
|
|
5
|
-
const useFormFieldId = () => useFormFieldState()?.htmlId;
|
|
6
|
-
const useFormError = () => useFormFieldState()?.errors;
|
|
7
|
-
export {
|
|
8
|
-
FormFieldStateContext,
|
|
9
|
-
useFormError,
|
|
10
|
-
useFormFieldId,
|
|
11
|
-
useFormFieldState
|
|
12
|
-
};
|
|
13
|
-
//# sourceMappingURL=contexts.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"contexts.js","sources":["../../../../packages/react-form/src/contexts.tsx"],"sourcesContent":["import { createContext } from '@contember/react-utils'\nimport { FormFieldState } from './types'\n\n\nconst FormFieldStateContext_ = createContext<FormFieldState | undefined>('FormFieldStateContext', undefined)\nexport const FormFieldStateContext = FormFieldStateContext_[0]\nexport const useFormFieldState = FormFieldStateContext_[1]\n\n\n/**\n * @deprecated use `useFormState` instead\n */\nexport const useFormFieldId = () => useFormFieldState()?.htmlId\n\n/**\n * @deprecated use `useFormState` instead\n */\nexport const useFormError = () => useFormFieldState()?.errors\n"],"names":[],"mappings":";AAIA,MAAM,yBAAyB,cAA0C,yBAAyB,MAAS;AAC9F,MAAA,wBAAwB,uBAAuB,CAAC;AAChD,MAAA,oBAAoB,uBAAuB,CAAC;AAM5C,MAAA,iBAAiB,MAAM,qBAAqB;AAK5C,MAAA,eAAe,MAAM,qBAAqB;"}
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const React = require("react");
|
|
4
|
-
const reactBinding = require("@contember/react-binding");
|
|
5
|
-
function _interopNamespaceDefault(e) {
|
|
6
|
-
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
7
|
-
if (e) {
|
|
8
|
-
for (const k in e) {
|
|
9
|
-
if (k !== "default") {
|
|
10
|
-
const d = Object.getOwnPropertyDescriptor(e, k);
|
|
11
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
12
|
-
enumerable: true,
|
|
13
|
-
get: () => e[k]
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
n.default = e;
|
|
19
|
-
return Object.freeze(n);
|
|
20
|
-
}
|
|
21
|
-
const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
|
|
22
|
-
const useFormInputValidationHandler = (field) => {
|
|
23
|
-
const accessorGetter = field.getAccessor;
|
|
24
|
-
const validationMessage = React.useRef();
|
|
25
|
-
const [focus, setFocus] = React__namespace.useState(false);
|
|
26
|
-
const inputRef = React__namespace.useRef(null);
|
|
27
|
-
reactBinding.useEntityBeforePersist(React.useCallback(() => {
|
|
28
|
-
if (validationMessage.current) {
|
|
29
|
-
accessorGetter().addError(validationMessage.current);
|
|
30
|
-
}
|
|
31
|
-
}, [accessorGetter]));
|
|
32
|
-
React.useEffect(() => {
|
|
33
|
-
if (!inputRef.current) {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
const input = inputRef.current;
|
|
37
|
-
const valid = input.validity?.valid;
|
|
38
|
-
const message = valid ? void 0 : input?.validationMessage;
|
|
39
|
-
const previousMessage = validationMessage.current;
|
|
40
|
-
validationMessage.current = message;
|
|
41
|
-
const accessor = accessorGetter();
|
|
42
|
-
if (!message) {
|
|
43
|
-
if (previousMessage) {
|
|
44
|
-
clearSpecificError(accessor, previousMessage);
|
|
45
|
-
}
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
if (message === previousMessage) {
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
if (!field.isTouched) {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
if (!focus || !!previousMessage) {
|
|
55
|
-
accessor.clearErrors();
|
|
56
|
-
accessor.addError(message);
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
return {
|
|
60
|
-
ref: inputRef,
|
|
61
|
-
onFocus: React.useCallback(() => {
|
|
62
|
-
setFocus(true);
|
|
63
|
-
}, []),
|
|
64
|
-
onBlur: React.useCallback(() => {
|
|
65
|
-
setFocus(false);
|
|
66
|
-
accessorGetter().clearErrors();
|
|
67
|
-
if (validationMessage.current) {
|
|
68
|
-
accessorGetter().addError(validationMessage.current);
|
|
69
|
-
}
|
|
70
|
-
}, [accessorGetter])
|
|
71
|
-
};
|
|
72
|
-
};
|
|
73
|
-
const clearSpecificError = (field, message) => {
|
|
74
|
-
const errors = field.errors?.errors ?? [];
|
|
75
|
-
const error = errors.filter((e) => !(e.type === "validation" && e.message === message));
|
|
76
|
-
field.clearErrors();
|
|
77
|
-
error.forEach((e) => field.addError(e));
|
|
78
|
-
};
|
|
79
|
-
exports.useFormInputValidationHandler = useFormInputValidationHandler;
|
|
80
|
-
//# sourceMappingURL=useFormInputValidationHandler.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useFormInputValidationHandler.cjs","sources":["../../../../../packages/react-form/src/hooks/useFormInputValidationHandler.tsx"],"sourcesContent":["import * as React from 'react'\nimport { useCallback, useEffect, useRef } from 'react'\nimport { FieldAccessor, useEntityBeforePersist } from '@contember/react-binding'\n\nexport const useFormInputValidationHandler = (field: FieldAccessor<any>) => {\n\tconst accessorGetter = field.getAccessor\n\tconst validationMessage = useRef<string>()\n\tconst [focus, setFocus] = React.useState(false)\n\tconst inputRef = React.useRef<HTMLInputElement>(null)\n\tuseEntityBeforePersist(useCallback(() => {\n\t\tif (validationMessage.current) {\n\t\t\taccessorGetter().addError(validationMessage.current)\n\t\t}\n\t}, [accessorGetter]))\n\n\tuseEffect(() => {\n\t\tif (!inputRef.current) {\n\t\t\treturn\n\t\t}\n\n\t\tconst input = inputRef.current\n\t\tconst valid = input.validity?.valid\n\n\t\tconst message = valid ? undefined : input?.validationMessage\n\t\tconst previousMessage = validationMessage.current\n\t\tvalidationMessage.current = message\n\n\t\tconst accessor = accessorGetter()\n\t\t// if there is no message, we want to clear the error\n\t\tif (!message) {\n\t\t\tif (previousMessage) {\n\t\t\t\tclearSpecificError(accessor, previousMessage)\n\t\t\t}\n\t\t\treturn\n\t\t}\n\t\tif (message === previousMessage) {\n\t\t\treturn\n\t\t}\n\t\t// if the input is not touched, we don't want to show the error message\n\t\tif (!field.isTouched) {\n\t\t\treturn\n\t\t}\n\n\t\t// if the input is not focused, we want to show the error message\n\t\t// also, even the input is focused, we want to replace the previous message\n\t\tif (!focus || !!previousMessage) {\n\t\t\taccessor.clearErrors()\n\t\t\taccessor.addError(message)\n\t\t}\n\t})\n\n\treturn {\n\t\tref: inputRef,\n\t\tonFocus: useCallback(() => {\n\t\t\tsetFocus(true)\n\t\t}, []),\n\t\tonBlur: useCallback(() => {\n\t\t\tsetFocus(false)\n\t\t\taccessorGetter().clearErrors()\n\t\t\tif (validationMessage.current) {\n\t\t\t\taccessorGetter().addError(validationMessage.current)\n\t\t\t}\n\t\t}, [accessorGetter]),\n\t}\n}\n\nconst clearSpecificError = (field: FieldAccessor<any>, message: string) => {\n\tconst errors = field.errors?.errors ?? []\n\tconst error = errors.filter(e => !(e.type === 'validation' && e.message === message))\n\tfield.clearErrors()\n\terror.forEach(e => field.addError(e))\n}\n"],"names":["useRef","React","useE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CAAC;AAAA,EACpB;AACD;AAEA,MAAM,qBAAqB,CAAC,OAA2B,YAAoB;AAC1E,QAAM,SAAS,MAAM,QAAQ,UAAU,CAAC;AAClC,QAAA,QAAQ,OAAO,OAAO,CAAK,MAAA,EAAE,EAAE,SAAS,gBAAgB,EAAE,YAAY,QAAQ;AACpF,QAAM,YAAY;AAClB,QAAM,QAAQ,CAAA,MAAK,MAAM,SAAS,CAAC,CAAC;AACrC;;"}
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import { useRef, useCallback, useEffect } from "react";
|
|
3
|
-
import { useEntityBeforePersist } from "@contember/react-binding";
|
|
4
|
-
const useFormInputValidationHandler = (field) => {
|
|
5
|
-
const accessorGetter = field.getAccessor;
|
|
6
|
-
const validationMessage = useRef();
|
|
7
|
-
const [focus, setFocus] = React.useState(false);
|
|
8
|
-
const inputRef = React.useRef(null);
|
|
9
|
-
useEntityBeforePersist(useCallback(() => {
|
|
10
|
-
if (validationMessage.current) {
|
|
11
|
-
accessorGetter().addError(validationMessage.current);
|
|
12
|
-
}
|
|
13
|
-
}, [accessorGetter]));
|
|
14
|
-
useEffect(() => {
|
|
15
|
-
if (!inputRef.current) {
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
const input = inputRef.current;
|
|
19
|
-
const valid = input.validity?.valid;
|
|
20
|
-
const message = valid ? void 0 : input?.validationMessage;
|
|
21
|
-
const previousMessage = validationMessage.current;
|
|
22
|
-
validationMessage.current = message;
|
|
23
|
-
const accessor = accessorGetter();
|
|
24
|
-
if (!message) {
|
|
25
|
-
if (previousMessage) {
|
|
26
|
-
clearSpecificError(accessor, previousMessage);
|
|
27
|
-
}
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
if (message === previousMessage) {
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
if (!field.isTouched) {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
if (!focus || !!previousMessage) {
|
|
37
|
-
accessor.clearErrors();
|
|
38
|
-
accessor.addError(message);
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
return {
|
|
42
|
-
ref: inputRef,
|
|
43
|
-
onFocus: useCallback(() => {
|
|
44
|
-
setFocus(true);
|
|
45
|
-
}, []),
|
|
46
|
-
onBlur: useCallback(() => {
|
|
47
|
-
setFocus(false);
|
|
48
|
-
accessorGetter().clearErrors();
|
|
49
|
-
if (validationMessage.current) {
|
|
50
|
-
accessorGetter().addError(validationMessage.current);
|
|
51
|
-
}
|
|
52
|
-
}, [accessorGetter])
|
|
53
|
-
};
|
|
54
|
-
};
|
|
55
|
-
const clearSpecificError = (field, message) => {
|
|
56
|
-
const errors = field.errors?.errors ?? [];
|
|
57
|
-
const error = errors.filter((e) => !(e.type === "validation" && e.message === message));
|
|
58
|
-
field.clearErrors();
|
|
59
|
-
error.forEach((e) => field.addError(e));
|
|
60
|
-
};
|
|
61
|
-
export {
|
|
62
|
-
useFormInputValidationHandler
|
|
63
|
-
};
|
|
64
|
-
//# sourceMappingURL=useFormInputValidationHandler.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useFormInputValidationHandler.js","sources":["../../../../../packages/react-form/src/hooks/useFormInputValidationHandler.tsx"],"sourcesContent":["import * as React from 'react'\nimport { useCallback, useEffect, useRef } from 'react'\nimport { FieldAccessor, useEntityBeforePersist } from '@contember/react-binding'\n\nexport const useFormInputValidationHandler = (field: FieldAccessor<any>) => {\n\tconst accessorGetter = field.getAccessor\n\tconst validationMessage = useRef<string>()\n\tconst [focus, setFocus] = React.useState(false)\n\tconst inputRef = React.useRef<HTMLInputElement>(null)\n\tuseEntityBeforePersist(useCallback(() => {\n\t\tif (validationMessage.current) {\n\t\t\taccessorGetter().addError(validationMessage.current)\n\t\t}\n\t}, [accessorGetter]))\n\n\tuseEffect(() => {\n\t\tif (!inputRef.current) {\n\t\t\treturn\n\t\t}\n\n\t\tconst input = inputRef.current\n\t\tconst valid = input.validity?.valid\n\n\t\tconst message = valid ? undefined : input?.validationMessage\n\t\tconst previousMessage = validationMessage.current\n\t\tvalidationMessage.current = message\n\n\t\tconst accessor = accessorGetter()\n\t\t// if there is no message, we want to clear the error\n\t\tif (!message) {\n\t\t\tif (previousMessage) {\n\t\t\t\tclearSpecificError(accessor, previousMessage)\n\t\t\t}\n\t\t\treturn\n\t\t}\n\t\tif (message === previousMessage) {\n\t\t\treturn\n\t\t}\n\t\t// if the input is not touched, we don't want to show the error message\n\t\tif (!field.isTouched) {\n\t\t\treturn\n\t\t}\n\n\t\t// if the input is not focused, we want to show the error message\n\t\t// also, even the input is focused, we want to replace the previous message\n\t\tif (!focus || !!previousMessage) {\n\t\t\taccessor.clearErrors()\n\t\t\taccessor.addError(message)\n\t\t}\n\t})\n\n\treturn {\n\t\tref: inputRef,\n\t\tonFocus: useCallback(() => {\n\t\t\tsetFocus(true)\n\t\t}, []),\n\t\tonBlur: useCallback(() => {\n\t\t\tsetFocus(false)\n\t\t\taccessorGetter().clearErrors()\n\t\t\tif (validationMessage.current) {\n\t\t\t\taccessorGetter().addError(validationMessage.current)\n\t\t\t}\n\t\t}, [accessorGetter]),\n\t}\n}\n\nconst clearSpecificError = (field: FieldAccessor<any>, message: string) => {\n\tconst errors = field.errors?.errors ?? []\n\tconst error = errors.filter(e => !(e.type === 'validation' && e.message === message))\n\tfield.clearErrors()\n\terror.forEach(e => field.addError(e))\n}\n"],"names":[],"mappings":";;;AAIa,MAAA,gCAAgC,CAAC,UAA8B;AAC3E,QAAM,iBAAiB,MAAM;AAC7B,QAAM,oBAAoB,OAAe;AACzC,QAAM,CAAC,OAAO,QAAQ,IAAI,MAAM,SAAS,KAAK;AACxC,QAAA,WAAW,MAAM,OAAyB,IAAI;AACpD,yBAAuB,YAAY,MAAM;AACxC,QAAI,kBAAkB,SAAS;AACf,uBAAE,SAAS,kBAAkB,OAAO;AAAA,IAAA;AAAA,EACpD,GACE,CAAC,cAAc,CAAC,CAAC;AA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useFormInputHandler = (field, { formatValue, parseValue }) => {
|
|
5
|
-
return React.useMemo(() => {
|
|
6
|
-
const schema = field.schema;
|
|
7
|
-
const columnType = schema.type;
|
|
8
|
-
const handlerFactory = ColumnTypeHandlerFactories[columnType] ?? defaultHandlerFactory;
|
|
9
|
-
const handler = handlerFactory(schema, field.getAccessor);
|
|
10
|
-
return {
|
|
11
|
-
defaultInputProps: handler.defaultInputProps,
|
|
12
|
-
formatValue: formatValue ?? handler.formatValue,
|
|
13
|
-
parseValue: parseValue ?? handler.parseValue
|
|
14
|
-
};
|
|
15
|
-
}, [field.getAccessor, field.schema, formatValue, parseValue]);
|
|
16
|
-
};
|
|
17
|
-
const defaultHandlerFactory = (schema, field) => ({
|
|
18
|
-
parseValue: (value) => {
|
|
19
|
-
if (value === "" && schema.nullable && field().valueOnServer === null) {
|
|
20
|
-
return null;
|
|
21
|
-
}
|
|
22
|
-
return value;
|
|
23
|
-
},
|
|
24
|
-
formatValue: (value) => value ?? ""
|
|
25
|
-
});
|
|
26
|
-
const ColumnTypeHandlerFactories = {
|
|
27
|
-
String: defaultHandlerFactory,
|
|
28
|
-
Integer: () => ({
|
|
29
|
-
parseValue: (value) => {
|
|
30
|
-
if (value === "") {
|
|
31
|
-
return null;
|
|
32
|
-
}
|
|
33
|
-
const parsed = parseInt(value, 10);
|
|
34
|
-
return isNaN(parsed) ? null : parsed;
|
|
35
|
-
},
|
|
36
|
-
formatValue: (value) => {
|
|
37
|
-
return value === null ? "" : value.toString(10);
|
|
38
|
-
},
|
|
39
|
-
defaultInputProps: {
|
|
40
|
-
type: "number"
|
|
41
|
-
}
|
|
42
|
-
}),
|
|
43
|
-
Double: () => ({
|
|
44
|
-
parseValue: (value, ctx) => {
|
|
45
|
-
if (value === "") {
|
|
46
|
-
ctx.setState({ rawValue: "", parsedValue: null });
|
|
47
|
-
return null;
|
|
48
|
-
}
|
|
49
|
-
const parsed = parseFloat(value);
|
|
50
|
-
const parsedValue = isNaN(parsed) ? null : parsed;
|
|
51
|
-
ctx.setState({ rawValue: value, parsedValue });
|
|
52
|
-
return parsedValue;
|
|
53
|
-
},
|
|
54
|
-
formatValue: (value, ctx) => {
|
|
55
|
-
if (value === ctx.state?.parsedValue) {
|
|
56
|
-
return ctx.state.rawValue;
|
|
57
|
-
}
|
|
58
|
-
return value === null ? "" : value.toString(10);
|
|
59
|
-
},
|
|
60
|
-
defaultInputProps: {
|
|
61
|
-
type: "number"
|
|
62
|
-
}
|
|
63
|
-
}),
|
|
64
|
-
Date: () => ({
|
|
65
|
-
parseValue: (value) => {
|
|
66
|
-
if (value === "") {
|
|
67
|
-
return null;
|
|
68
|
-
}
|
|
69
|
-
const parsed = Date.parse(value);
|
|
70
|
-
return isNaN(parsed) ? null : new Date(parsed).toISOString().split("T")[0];
|
|
71
|
-
},
|
|
72
|
-
formatValue: (value) => {
|
|
73
|
-
const parsed = value ? Date.parse(value) : null;
|
|
74
|
-
return !parsed || isNaN(parsed) ? "" : new Date(parsed).toISOString().split("T")[0];
|
|
75
|
-
},
|
|
76
|
-
defaultInputProps: {
|
|
77
|
-
type: "date"
|
|
78
|
-
}
|
|
79
|
-
}),
|
|
80
|
-
DateTime: () => ({
|
|
81
|
-
parseValue: (value) => {
|
|
82
|
-
if (value === "") {
|
|
83
|
-
return null;
|
|
84
|
-
}
|
|
85
|
-
const parsed = Date.parse(value);
|
|
86
|
-
return isNaN(parsed) ? null : new Date(parsed).toISOString();
|
|
87
|
-
},
|
|
88
|
-
formatValue: (value) => {
|
|
89
|
-
const parsed = value ? Date.parse(value) : null;
|
|
90
|
-
return !parsed || isNaN(parsed) ? "" : toLocalDate(new Date(parsed));
|
|
91
|
-
},
|
|
92
|
-
defaultInputProps: {
|
|
93
|
-
type: "datetime-local"
|
|
94
|
-
}
|
|
95
|
-
}),
|
|
96
|
-
Time: () => ({
|
|
97
|
-
parseValue: (value) => {
|
|
98
|
-
if (value === "") {
|
|
99
|
-
return null;
|
|
100
|
-
}
|
|
101
|
-
return value;
|
|
102
|
-
},
|
|
103
|
-
formatValue: (value) => {
|
|
104
|
-
return value ?? "";
|
|
105
|
-
},
|
|
106
|
-
defaultInputProps: {
|
|
107
|
-
type: "time"
|
|
108
|
-
}
|
|
109
|
-
}),
|
|
110
|
-
Bool: void 0,
|
|
111
|
-
Enum: void 0,
|
|
112
|
-
Uuid: void 0
|
|
113
|
-
};
|
|
114
|
-
const toLocalDate = (date) => {
|
|
115
|
-
const pad = (num, length = 2) => {
|
|
116
|
-
const str = num.toString();
|
|
117
|
-
return "0".repeat(Math.max(0, length - str.length)) + str;
|
|
118
|
-
};
|
|
119
|
-
return pad(date.getFullYear(), 4) + "-" + pad(date.getMonth() + 1) + "-" + pad(date.getDate()) + "T" + pad(date.getHours()) + ":" + pad(date.getMinutes()) + ":" + pad(date.getSeconds());
|
|
120
|
-
};
|
|
121
|
-
exports.useFormInputHandler = useFormInputHandler;
|
|
122
|
-
//# sourceMappingURL=useFormInputHandler.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useFormInputHandler.cjs","sources":["../../../../../packages/react-form/src/internal/useFormInputHandler.ts"],"sourcesContent":["import { FieldAccessor, SchemaColumn, SchemaKnownColumnType } from '@contember/react-binding'\nimport { useMemo } from 'react'\nimport { FormInputHandler } from '../types'\n\nexport const useFormInputHandler = (field: FieldAccessor, { formatValue, parseValue }: Partial<FormInputHandler>): FormInputHandler => {\n\treturn useMemo(() => {\n\t\tconst schema = field.schema\n\t\tconst columnType = schema.type\n\t\tconst handlerFactory = ColumnTypeHandlerFactories[columnType as SchemaKnownColumnType] ?? defaultHandlerFactory\n\t\tconst handler = handlerFactory(schema, field.getAccessor)\n\t\treturn {\n\t\t\tdefaultInputProps: handler.defaultInputProps,\n\t\t\tformatValue: formatValue ?? handler.formatValue,\n\t\t\tparseValue: parseValue ?? handler.parseValue,\n\t\t}\n\t}, [field.getAccessor, field.schema, formatValue, parseValue])\n}\n\ntype ColumnTypeHandlerFactory = (column: SchemaColumn, getAccessor: FieldAccessor.GetFieldAccessor) => FormInputHandler<any>\n\nconst defaultHandlerFactory: ColumnTypeHandlerFactory = (schema, field) => ({\n\tparseValue: (value: string) => {\n\t\tif (value === '' && schema.nullable && field().valueOnServer === null) {\n\t\t\treturn null\n\t\t}\n\t\treturn value\n\t},\n\tformatValue: (value: string | null) => value ?? '',\n})\n\nconst ColumnTypeHandlerFactories: Record<SchemaKnownColumnType, ColumnTypeHandlerFactory | undefined> = {\n\tString: defaultHandlerFactory,\n\tInteger: () => ({\n\t\tparseValue: (value: string) => {\n\t\t\tif (value === '') {\n\t\t\t\treturn null\n\t\t\t}\n\t\t\tconst parsed = parseInt(value, 10)\n\t\t\treturn isNaN(parsed) ? null : parsed\n\t\t},\n\t\tformatValue: (value: number | null) => {\n\t\t\treturn value === null ? '' : value.toString(10)\n\t\t},\n\t\tdefaultInputProps: {\n\t\t\ttype: 'number',\n\t\t},\n\t}),\n\tDouble: (): FormInputHandler<{ rawValue: string; parsedValue: number | null }> => ({\n\t\tparseValue: (value: string, ctx) => {\n\t\t\tif (value === '') {\n\t\t\t\tctx.setState({ rawValue: '', parsedValue: null })\n\t\t\t\treturn null\n\t\t\t}\n\t\t\tconst parsed = parseFloat(value)\n\t\t\tconst parsedValue = isNaN(parsed) ? null : parsed\n\t\t\tctx.setState({ rawValue: value, parsedValue })\n\t\t\treturn parsedValue\n\t\t},\n\t\tformatValue: (value: number | null, ctx) => {\n\t\t\tif (value === ctx.state?.parsedValue) {\n\t\t\t\treturn ctx.state.rawValue\n\t\t\t}\n\t\t\treturn value === null ? '' : value.toString(10)\n\t\t},\n\t\tdefaultInputProps: {\n\t\t\ttype: 'number',\n\t\t},\n\t}),\n\n\tDate: () => ({\n\t\tparseValue: (value: string) => {\n\t\t\tif (value === '') {\n\t\t\t\treturn null\n\t\t\t}\n\t\t\tconst parsed = Date.parse(value)\n\t\t\treturn isNaN(parsed) ? null : (new Date(parsed)).toISOString().split('T')[0]\n\t\t},\n\t\tformatValue: (value: string | null) => {\n\t\t\tconst parsed = value ? Date.parse(value) : null\n\t\t\treturn !parsed || isNaN(parsed) ? '' : new Date(parsed).toISOString().split('T')[0]\n\t\t},\n\t\tdefaultInputProps: {\n\t\t\ttype: 'date',\n\t\t},\n\t}),\n\tDateTime: () => ({\n\t\tparseValue: (value: string) => {\n\t\t\tif (value === '') {\n\t\t\t\treturn null\n\t\t\t}\n\n\t\t\tconst parsed = Date.parse(value)\n\t\t\treturn isNaN(parsed) ? null : (new Date(parsed)).toISOString()\n\t\t},\n\t\tformatValue: (value: string | null) => {\n\t\t\tconst parsed = value ? Date.parse(value) : null\n\t\t\treturn !parsed || isNaN(parsed) ? '' : toLocalDate(new Date(parsed))\n\t\t},\n\t\tdefaultInputProps: {\n\t\t\ttype: 'datetime-local',\n\t\t},\n\t}),\n\tTime: () => ({\n\t\tparseValue: (value: string) => {\n\t\t\tif (value === '') {\n\t\t\t\treturn null\n\t\t\t}\n\t\t\treturn value\n\t\t},\n\t\tformatValue: (value: string | null) => {\n\t\t\treturn value ?? ''\n\t\t},\n\t\tdefaultInputProps: {\n\t\t\ttype: 'time',\n\t\t},\n\t}),\n\tBool: undefined,\n\tEnum: undefined,\n\tUuid: undefined,\n}\n\nconst toLocalDate = (date: Date) => {\n\tconst pad = (num: number, length: number = 2) => {\n\t\tconst str = num.toString()\n\t\treturn '0'.repeat(Math.max(0, length - str.length)) + str\n\t}\n\n\treturn pad(date.getFullYear(), 4) +\n\t\t'-' + pad(date.getMonth() + 1) +\n\t\t'-' + pad(date.getDate()) +\n\t\t'T' + pad(date.getHours()) +\n\t\t':' + pad(date.getMinutes()) +\n\t\t':' + pad(date.getSeconds())\n}\n"],"names":["useMemo"],"mappings":";;;AAIO,MAAM,sBAAsB,CAAC,OAAsB,EAAE,aAAa,iBAA8D;AACtI,SAAOA,cAAQ,MAAM;AACpB,UAAM,SAAS,MAAM;AACrB,UAAM,aAAa,OAAO;AACpB,UAAA,iBAAiB,2BAA2B,UAAmC,KAAK;AA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,CAAC,UAAyB;AACtC,aAAO,SAAS;AAAA,IACjB;AAAA,IACA,mBAAmB;AAAA,MAClB,MAAM;AAAA,IAAA;AAAA,EACP;AAAA,EAED,MAAM;AAAA,EACN,MAAM;AAAA,EACN,MAAM;AACP;AAEA,MAAM,cAAc,CAAC,SAAe;AACnC,QAAM,MAAM,CAAC,KAAa,SAAiB,MAAM;AAC1C,UAAA,MAAM,IAAI,SAAS;AAClB,WAAA,IAAI,OAAO,KAAK,IAAI,GAAG,SAAS,IAAI,MAAM,CAAC,IAAI;AAAA,EACvD;AAEA,SAAO,IAAI,KAAK,YAAY,GAAG,CAAC,IAC/B,MAAM,IAAI,KAAK,SAAa,IAAA,CAAC,IAC7B,MAAM,IAAI,KAAK,QAAS,CAAA,IACxB,MAAM,IAAI,KAAK,SAAU,CAAA,IACzB,MAAM,IAAI,KAAK,WAAY,CAAA,IAC3B,MAAM,IAAI,KAAK,YAAY;AAC7B;;"}
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { useMemo } from "react";
|
|
2
|
-
const useFormInputHandler = (field, { formatValue, parseValue }) => {
|
|
3
|
-
return useMemo(() => {
|
|
4
|
-
const schema = field.schema;
|
|
5
|
-
const columnType = schema.type;
|
|
6
|
-
const handlerFactory = ColumnTypeHandlerFactories[columnType] ?? defaultHandlerFactory;
|
|
7
|
-
const handler = handlerFactory(schema, field.getAccessor);
|
|
8
|
-
return {
|
|
9
|
-
defaultInputProps: handler.defaultInputProps,
|
|
10
|
-
formatValue: formatValue ?? handler.formatValue,
|
|
11
|
-
parseValue: parseValue ?? handler.parseValue
|
|
12
|
-
};
|
|
13
|
-
}, [field.getAccessor, field.schema, formatValue, parseValue]);
|
|
14
|
-
};
|
|
15
|
-
const defaultHandlerFactory = (schema, field) => ({
|
|
16
|
-
parseValue: (value) => {
|
|
17
|
-
if (value === "" && schema.nullable && field().valueOnServer === null) {
|
|
18
|
-
return null;
|
|
19
|
-
}
|
|
20
|
-
return value;
|
|
21
|
-
},
|
|
22
|
-
formatValue: (value) => value ?? ""
|
|
23
|
-
});
|
|
24
|
-
const ColumnTypeHandlerFactories = {
|
|
25
|
-
String: defaultHandlerFactory,
|
|
26
|
-
Integer: () => ({
|
|
27
|
-
parseValue: (value) => {
|
|
28
|
-
if (value === "") {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
const parsed = parseInt(value, 10);
|
|
32
|
-
return isNaN(parsed) ? null : parsed;
|
|
33
|
-
},
|
|
34
|
-
formatValue: (value) => {
|
|
35
|
-
return value === null ? "" : value.toString(10);
|
|
36
|
-
},
|
|
37
|
-
defaultInputProps: {
|
|
38
|
-
type: "number"
|
|
39
|
-
}
|
|
40
|
-
}),
|
|
41
|
-
Double: () => ({
|
|
42
|
-
parseValue: (value, ctx) => {
|
|
43
|
-
if (value === "") {
|
|
44
|
-
ctx.setState({ rawValue: "", parsedValue: null });
|
|
45
|
-
return null;
|
|
46
|
-
}
|
|
47
|
-
const parsed = parseFloat(value);
|
|
48
|
-
const parsedValue = isNaN(parsed) ? null : parsed;
|
|
49
|
-
ctx.setState({ rawValue: value, parsedValue });
|
|
50
|
-
return parsedValue;
|
|
51
|
-
},
|
|
52
|
-
formatValue: (value, ctx) => {
|
|
53
|
-
if (value === ctx.state?.parsedValue) {
|
|
54
|
-
return ctx.state.rawValue;
|
|
55
|
-
}
|
|
56
|
-
return value === null ? "" : value.toString(10);
|
|
57
|
-
},
|
|
58
|
-
defaultInputProps: {
|
|
59
|
-
type: "number"
|
|
60
|
-
}
|
|
61
|
-
}),
|
|
62
|
-
Date: () => ({
|
|
63
|
-
parseValue: (value) => {
|
|
64
|
-
if (value === "") {
|
|
65
|
-
return null;
|
|
66
|
-
}
|
|
67
|
-
const parsed = Date.parse(value);
|
|
68
|
-
return isNaN(parsed) ? null : new Date(parsed).toISOString().split("T")[0];
|
|
69
|
-
},
|
|
70
|
-
formatValue: (value) => {
|
|
71
|
-
const parsed = value ? Date.parse(value) : null;
|
|
72
|
-
return !parsed || isNaN(parsed) ? "" : new Date(parsed).toISOString().split("T")[0];
|
|
73
|
-
},
|
|
74
|
-
defaultInputProps: {
|
|
75
|
-
type: "date"
|
|
76
|
-
}
|
|
77
|
-
}),
|
|
78
|
-
DateTime: () => ({
|
|
79
|
-
parseValue: (value) => {
|
|
80
|
-
if (value === "") {
|
|
81
|
-
return null;
|
|
82
|
-
}
|
|
83
|
-
const parsed = Date.parse(value);
|
|
84
|
-
return isNaN(parsed) ? null : new Date(parsed).toISOString();
|
|
85
|
-
},
|
|
86
|
-
formatValue: (value) => {
|
|
87
|
-
const parsed = value ? Date.parse(value) : null;
|
|
88
|
-
return !parsed || isNaN(parsed) ? "" : toLocalDate(new Date(parsed));
|
|
89
|
-
},
|
|
90
|
-
defaultInputProps: {
|
|
91
|
-
type: "datetime-local"
|
|
92
|
-
}
|
|
93
|
-
}),
|
|
94
|
-
Time: () => ({
|
|
95
|
-
parseValue: (value) => {
|
|
96
|
-
if (value === "") {
|
|
97
|
-
return null;
|
|
98
|
-
}
|
|
99
|
-
return value;
|
|
100
|
-
},
|
|
101
|
-
formatValue: (value) => {
|
|
102
|
-
return value ?? "";
|
|
103
|
-
},
|
|
104
|
-
defaultInputProps: {
|
|
105
|
-
type: "time"
|
|
106
|
-
}
|
|
107
|
-
}),
|
|
108
|
-
Bool: void 0,
|
|
109
|
-
Enum: void 0,
|
|
110
|
-
Uuid: void 0
|
|
111
|
-
};
|
|
112
|
-
const toLocalDate = (date) => {
|
|
113
|
-
const pad = (num, length = 2) => {
|
|
114
|
-
const str = num.toString();
|
|
115
|
-
return "0".repeat(Math.max(0, length - str.length)) + str;
|
|
116
|
-
};
|
|
117
|
-
return pad(date.getFullYear(), 4) + "-" + pad(date.getMonth() + 1) + "-" + pad(date.getDate()) + "T" + pad(date.getHours()) + ":" + pad(date.getMinutes()) + ":" + pad(date.getSeconds());
|
|
118
|
-
};
|
|
119
|
-
export {
|
|
120
|
-
useFormInputHandler
|
|
121
|
-
};
|
|
122
|
-
//# sourceMappingURL=useFormInputHandler.js.map
|