@contember/react-form 2.0.0-alpha.4 → 2.0.0-alpha.40
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/api-extractor.json +3 -0
- package/dist/development/index.cjs +16 -12
- package/dist/development/index.cjs.map +1 -1
- package/dist/development/index.js +14 -10
- package/dist/development/index.js.map +1 -1
- package/dist/development/{components → src/components}/FormCheckbox.cjs +12 -9
- package/dist/development/src/components/FormCheckbox.cjs.map +1 -0
- package/dist/development/{components → src/components}/FormCheckbox.js +13 -10
- package/dist/development/src/components/FormCheckbox.js.map +1 -0
- package/dist/development/{components → src/components}/FormError.cjs +5 -5
- package/dist/development/src/components/FormError.cjs.map +1 -0
- package/dist/development/{components → src/components}/FormError.js +6 -6
- package/dist/development/src/components/FormError.js.map +1 -0
- package/dist/development/src/components/FormFieldScope.cjs +33 -0
- package/dist/development/src/components/FormFieldScope.cjs.map +1 -0
- package/dist/development/src/components/FormFieldScope.js +33 -0
- package/dist/development/src/components/FormFieldScope.js.map +1 -0
- package/dist/development/src/components/FormFieldStateProvider.cjs +56 -0
- package/dist/development/src/components/FormFieldStateProvider.cjs.map +1 -0
- package/dist/development/src/components/FormFieldStateProvider.js +56 -0
- package/dist/development/src/components/FormFieldStateProvider.js.map +1 -0
- package/dist/development/src/components/FormHasManyRelationScope.cjs +33 -0
- package/dist/development/src/components/FormHasManyRelationScope.cjs.map +1 -0
- package/dist/development/src/components/FormHasManyRelationScope.js +33 -0
- package/dist/development/src/components/FormHasManyRelationScope.js.map +1 -0
- package/dist/development/src/components/FormHasOneRelationScope.cjs +33 -0
- package/dist/development/src/components/FormHasOneRelationScope.cjs.map +1 -0
- package/dist/development/src/components/FormHasOneRelationScope.js +33 -0
- package/dist/development/src/components/FormHasOneRelationScope.js.map +1 -0
- package/dist/development/{components → src/components}/FormInput.cjs +13 -8
- package/dist/development/src/components/FormInput.cjs.map +1 -0
- package/dist/development/{components → src/components}/FormInput.js +14 -9
- package/dist/development/src/components/FormInput.js.map +1 -0
- package/dist/development/{components → src/components}/FormLabel.cjs +8 -6
- package/dist/development/src/components/FormLabel.cjs.map +1 -0
- package/dist/development/{components → src/components}/FormLabel.js +9 -7
- package/dist/development/src/components/FormLabel.js.map +1 -0
- package/dist/development/{components → src/components}/FormRadioInput.cjs +19 -15
- package/dist/development/src/components/FormRadioInput.cjs.map +1 -0
- package/dist/development/{components → src/components}/FormRadioInput.js +20 -16
- package/dist/development/src/components/FormRadioInput.js.map +1 -0
- package/dist/development/src/contexts.cjs +13 -0
- package/dist/development/src/contexts.cjs.map +1 -0
- package/dist/development/src/contexts.js +13 -0
- package/dist/development/src/contexts.js.map +1 -0
- package/dist/{production → development/src}/hooks/useFormInputValidationHandler.cjs +12 -3
- package/dist/development/src/hooks/useFormInputValidationHandler.cjs.map +1 -0
- package/dist/{production → development/src}/hooks/useFormInputValidationHandler.js +12 -3
- package/dist/development/src/hooks/useFormInputValidationHandler.js.map +1 -0
- package/dist/development/src/internal/useFormInputHandler.cjs.map +1 -0
- package/dist/development/src/internal/useFormInputHandler.js.map +1 -0
- package/dist/production/index.cjs +16 -12
- package/dist/production/index.cjs.map +1 -1
- package/dist/production/index.js +14 -10
- package/dist/production/index.js.map +1 -1
- package/dist/production/{components → src/components}/FormCheckbox.cjs +7 -4
- package/dist/production/src/components/FormCheckbox.cjs.map +1 -0
- package/dist/production/{components → src/components}/FormCheckbox.js +8 -5
- package/dist/production/src/components/FormCheckbox.js.map +1 -0
- package/dist/production/{components → src/components}/FormError.cjs +5 -5
- package/dist/production/src/components/FormError.cjs.map +1 -0
- package/dist/production/{components → src/components}/FormError.js +6 -6
- package/dist/production/src/components/FormError.js.map +1 -0
- package/dist/production/src/components/FormFieldScope.cjs +25 -0
- package/dist/production/src/components/FormFieldScope.cjs.map +1 -0
- package/dist/production/src/components/FormFieldScope.js +25 -0
- package/dist/production/src/components/FormFieldScope.js.map +1 -0
- package/dist/production/src/components/FormFieldStateProvider.cjs +34 -0
- package/dist/production/src/components/FormFieldStateProvider.cjs.map +1 -0
- package/dist/production/src/components/FormFieldStateProvider.js +34 -0
- package/dist/production/src/components/FormFieldStateProvider.js.map +1 -0
- package/dist/production/src/components/FormHasManyRelationScope.cjs +25 -0
- package/dist/production/src/components/FormHasManyRelationScope.cjs.map +1 -0
- package/dist/production/src/components/FormHasManyRelationScope.js +25 -0
- package/dist/production/src/components/FormHasManyRelationScope.js.map +1 -0
- package/dist/production/src/components/FormHasOneRelationScope.cjs +25 -0
- package/dist/production/src/components/FormHasOneRelationScope.cjs.map +1 -0
- package/dist/production/src/components/FormHasOneRelationScope.js +25 -0
- package/dist/production/src/components/FormHasOneRelationScope.js.map +1 -0
- package/dist/production/{components → src/components}/FormInput.cjs +9 -4
- package/dist/production/src/components/FormInput.cjs.map +1 -0
- package/dist/production/{components → src/components}/FormInput.js +10 -5
- package/dist/production/src/components/FormInput.js.map +1 -0
- package/dist/production/{components → src/components}/FormLabel.cjs +7 -5
- package/dist/production/src/components/FormLabel.cjs.map +1 -0
- package/dist/production/{components → src/components}/FormLabel.js +8 -6
- package/dist/production/src/components/FormLabel.js.map +1 -0
- package/dist/production/{components → src/components}/FormRadioInput.cjs +15 -11
- package/dist/production/src/components/FormRadioInput.cjs.map +1 -0
- package/dist/production/{components → src/components}/FormRadioInput.js +16 -12
- package/dist/production/src/components/FormRadioInput.js.map +1 -0
- package/dist/production/src/contexts.cjs +13 -0
- package/dist/production/src/contexts.cjs.map +1 -0
- package/dist/production/src/contexts.js +13 -0
- package/dist/production/src/contexts.js.map +1 -0
- package/dist/{development → production/src}/hooks/useFormInputValidationHandler.cjs +12 -3
- package/dist/production/src/hooks/useFormInputValidationHandler.cjs.map +1 -0
- package/dist/{development → production/src}/hooks/useFormInputValidationHandler.js +12 -3
- package/dist/production/src/hooks/useFormInputValidationHandler.js.map +1 -0
- package/dist/production/src/internal/useFormInputHandler.cjs.map +1 -0
- package/dist/production/src/internal/useFormInputHandler.js.map +1 -0
- package/dist/types/components/FormCheckbox.d.ts.map +1 -1
- package/dist/types/components/FormError.d.ts.map +1 -1
- package/dist/types/components/FormFieldScope.d.ts +2 -1
- package/dist/types/components/FormFieldScope.d.ts.map +1 -1
- package/dist/types/components/FormFieldStateProvider.d.ts +36 -0
- package/dist/types/components/FormFieldStateProvider.d.ts.map +1 -0
- package/dist/types/components/FormHasManyRelationScope.d.ts +5 -1
- package/dist/types/components/FormHasManyRelationScope.d.ts.map +1 -1
- package/dist/types/components/FormHasOneRelationScope.d.ts +2 -1
- package/dist/types/components/FormHasOneRelationScope.d.ts.map +1 -1
- package/dist/types/components/FormInput.d.ts.map +1 -1
- package/dist/types/components/FormLabel.d.ts.map +1 -1
- package/dist/types/components/FormRadioInput.d.ts +1 -1
- package/dist/types/components/FormRadioInput.d.ts.map +1 -1
- package/dist/types/components/index.d.ts +1 -0
- package/dist/types/components/index.d.ts.map +1 -1
- package/dist/types/contexts.d.ts +10 -6
- package/dist/types/contexts.d.ts.map +1 -1
- package/dist/types/hooks/useFormInputValidationHandler.d.ts.map +1 -1
- package/dist/types/internal/useFormInputHandler.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/dist/types/types/index.d.ts +12 -0
- package/dist/types/types/index.d.ts.map +1 -1
- package/package.json +10 -17
- package/src/components/FormCheckbox.tsx +32 -21
- package/src/components/FormError.tsx +6 -6
- package/src/components/FormFieldScope.tsx +17 -12
- package/src/components/FormFieldStateProvider.tsx +71 -0
- package/src/components/FormHasManyRelationScope.tsx +22 -14
- package/src/components/FormHasOneRelationScope.tsx +17 -13
- package/src/components/FormInput.tsx +12 -5
- package/src/components/FormLabel.tsx +8 -6
- package/src/components/FormRadioInput.tsx +22 -15
- package/src/components/index.ts +1 -0
- package/src/contexts.tsx +18 -0
- package/src/hooks/useFormInputValidationHandler.tsx +13 -3
- package/src/tsconfig.json +5 -2
- package/src/types/index.ts +13 -0
- package/tsconfig.json +7 -0
- package/tsdoc.json +6 -0
- package/dist/development/components/FormCheckbox.cjs.map +0 -1
- package/dist/development/components/FormCheckbox.js.map +0 -1
- package/dist/development/components/FormError.cjs.map +0 -1
- package/dist/development/components/FormError.js.map +0 -1
- package/dist/development/components/FormFieldScope.cjs +0 -23
- package/dist/development/components/FormFieldScope.cjs.map +0 -1
- package/dist/development/components/FormFieldScope.js +0 -23
- package/dist/development/components/FormFieldScope.js.map +0 -1
- package/dist/development/components/FormHasManyRelationScope.cjs +0 -23
- package/dist/development/components/FormHasManyRelationScope.cjs.map +0 -1
- package/dist/development/components/FormHasManyRelationScope.js +0 -23
- package/dist/development/components/FormHasManyRelationScope.js.map +0 -1
- package/dist/development/components/FormHasOneRelationScope.cjs +0 -23
- package/dist/development/components/FormHasOneRelationScope.cjs.map +0 -1
- package/dist/development/components/FormHasOneRelationScope.js +0 -23
- package/dist/development/components/FormHasOneRelationScope.js.map +0 -1
- package/dist/development/components/FormInput.cjs.map +0 -1
- package/dist/development/components/FormInput.js.map +0 -1
- package/dist/development/components/FormLabel.cjs.map +0 -1
- package/dist/development/components/FormLabel.js.map +0 -1
- package/dist/development/components/FormRadioInput.cjs.map +0 -1
- package/dist/development/components/FormRadioInput.js.map +0 -1
- package/dist/development/contexts.cjs +0 -14
- package/dist/development/contexts.cjs.map +0 -1
- package/dist/development/contexts.js +0 -14
- package/dist/development/contexts.js.map +0 -1
- package/dist/development/hooks/useFormInputValidationHandler.cjs.map +0 -1
- package/dist/development/hooks/useFormInputValidationHandler.js.map +0 -1
- package/dist/development/internal/useFormInputHandler.cjs.map +0 -1
- package/dist/development/internal/useFormInputHandler.js.map +0 -1
- package/dist/production/components/FormCheckbox.cjs.map +0 -1
- package/dist/production/components/FormCheckbox.js.map +0 -1
- package/dist/production/components/FormError.cjs.map +0 -1
- package/dist/production/components/FormError.js.map +0 -1
- package/dist/production/components/FormFieldScope.cjs +0 -15
- package/dist/production/components/FormFieldScope.cjs.map +0 -1
- package/dist/production/components/FormFieldScope.js +0 -15
- package/dist/production/components/FormFieldScope.js.map +0 -1
- package/dist/production/components/FormHasManyRelationScope.cjs +0 -15
- package/dist/production/components/FormHasManyRelationScope.cjs.map +0 -1
- package/dist/production/components/FormHasManyRelationScope.js +0 -15
- package/dist/production/components/FormHasManyRelationScope.js.map +0 -1
- package/dist/production/components/FormHasOneRelationScope.cjs +0 -15
- package/dist/production/components/FormHasOneRelationScope.cjs.map +0 -1
- package/dist/production/components/FormHasOneRelationScope.js +0 -15
- package/dist/production/components/FormHasOneRelationScope.js.map +0 -1
- package/dist/production/components/FormInput.cjs.map +0 -1
- package/dist/production/components/FormInput.js.map +0 -1
- package/dist/production/components/FormLabel.cjs.map +0 -1
- package/dist/production/components/FormLabel.js.map +0 -1
- package/dist/production/components/FormRadioInput.cjs.map +0 -1
- package/dist/production/components/FormRadioInput.js.map +0 -1
- package/dist/production/contexts.cjs +0 -14
- package/dist/production/contexts.cjs.map +0 -1
- package/dist/production/contexts.js +0 -14
- package/dist/production/contexts.js.map +0 -1
- package/dist/production/hooks/useFormInputValidationHandler.cjs.map +0 -1
- package/dist/production/hooks/useFormInputValidationHandler.js.map +0 -1
- package/dist/production/internal/useFormInputHandler.cjs.map +0 -1
- package/dist/production/internal/useFormInputHandler.js.map +0 -1
- package/src/contexts.ts +0 -12
- /package/dist/development/{internal → src/internal}/useFormInputHandler.cjs +0 -0
- /package/dist/development/{internal → src/internal}/useFormInputHandler.js +0 -0
- /package/dist/production/{internal → src/internal}/useFormInputHandler.cjs +0 -0
- /package/dist/production/{internal → src/internal}/useFormInputHandler.js +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FormHasOneRelationScope.cjs","sources":["../../../../../packages/react-form/src/components/FormHasOneRelationScope.tsx"],"sourcesContent":["import * as React from 'react'\nimport { HasOneRelationMarker, SugaredRelativeSingleEntity, useEntity } from '@contember/react-binding'\nimport { FormFieldStateProvider } from './FormFieldStateProvider'\nimport { useMemo } from 'react'\n\nexport type FormHasOneRelationScopeProps = {\n\tfield: SugaredRelativeSingleEntity['field']\n\tchildren: React.ReactNode\n\trequired?: boolean\n}\n\nexport const FormHasOneRelationScope = ({ field, children, required }: FormHasOneRelationScopeProps) => {\n\tconst entityRelation = useEntity({ field })\n\tconst marker = entityRelation.getMarker() as HasOneRelationMarker\n\tconst fieldName = marker.parameters.field\n\tconst entityName = entityRelation.getParent()!.name\n\tconst fieldInfo = useMemo(() => ({ entityName, fieldName }), [entityName, fieldName])\n\treturn (\n\t\t<FormFieldStateProvider\n\t\t\terrors={entityRelation.errors?.errors}\n\t\t\tdirty={entityRelation.hasUnpersistedChanges}\n\t\t\trequired={required}\n\t\t\tfield={fieldInfo}\n\t\t>\n\t\t\t{children}\n\t\t</FormFieldStateProvider>\n\t)\n}\n"],"names":["useEntity","useMemo","jsxDEV","FormFieldStateProvider","this"],"mappings":";;;;;;AAWO,MAAM,0BAA0B,CAAC,EAAE,OAAO,UAAU,eAA6C;AACvG,QAAM,iBAAiBA,aAAAA,UAAU,EAAE,OAAO;AACpC,QAAA,SAAS,eAAe,UAAU;AAClC,QAAA,YAAY,OAAO,WAAW;AAC9B,QAAA,aAAa,eAAe,UAAA,EAAa;AACzC,QAAA,YAAYC,cAAQ,OAAO,EAAE,YAAY,cAAc,CAAC,YAAY,SAAS,CAAC;AAEnF,SAAAC,8BAAA;AAAA,IAACC,uBAAA;AAAA,IAAA;AAAA,MACA,QAAQ,eAAe,QAAQ;AAAA,MAC/B,OAAO,eAAe;AAAA,MACtB;AAAA,MACA,OAAO;AAAA,MAEN;AAAA,IAAA;AAAA,IANF;AAAA,IAAA;AAAA,IAAA;AAAA,MAAA,UAAA;AAAA,MAAA,YAAA;AAAA,MAAA,cAAA;AAAA,IAAA;AAAA,IAAAC;AAAAA,EAOA;AAEF;;"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { jsxDEV } from "react/jsx-dev-runtime";
|
|
2
|
+
import { useEntity } from "@contember/react-binding";
|
|
3
|
+
import { FormFieldStateProvider } from "./FormFieldStateProvider.js";
|
|
4
|
+
import { useMemo } from "react";
|
|
5
|
+
const FormHasOneRelationScope = ({ field, children, required }) => {
|
|
6
|
+
const entityRelation = useEntity({ field });
|
|
7
|
+
const marker = entityRelation.getMarker();
|
|
8
|
+
const fieldName = marker.parameters.field;
|
|
9
|
+
const entityName = entityRelation.getParent().name;
|
|
10
|
+
const fieldInfo = useMemo(() => ({ entityName, fieldName }), [entityName, fieldName]);
|
|
11
|
+
return /* @__PURE__ */ jsxDEV(
|
|
12
|
+
FormFieldStateProvider,
|
|
13
|
+
{
|
|
14
|
+
errors: entityRelation.errors?.errors,
|
|
15
|
+
dirty: entityRelation.hasUnpersistedChanges,
|
|
16
|
+
required,
|
|
17
|
+
field: fieldInfo,
|
|
18
|
+
children
|
|
19
|
+
},
|
|
20
|
+
void 0,
|
|
21
|
+
false,
|
|
22
|
+
{
|
|
23
|
+
fileName: "/home/runner/work/contember/contember/packages/react-form/src/components/FormHasOneRelationScope.tsx",
|
|
24
|
+
lineNumber: 19,
|
|
25
|
+
columnNumber: 3
|
|
26
|
+
},
|
|
27
|
+
void 0
|
|
28
|
+
);
|
|
29
|
+
};
|
|
30
|
+
export {
|
|
31
|
+
FormHasOneRelationScope
|
|
32
|
+
};
|
|
33
|
+
//# sourceMappingURL=FormHasOneRelationScope.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FormHasOneRelationScope.js","sources":["../../../../../packages/react-form/src/components/FormHasOneRelationScope.tsx"],"sourcesContent":["import * as React from 'react'\nimport { HasOneRelationMarker, SugaredRelativeSingleEntity, useEntity } from '@contember/react-binding'\nimport { FormFieldStateProvider } from './FormFieldStateProvider'\nimport { useMemo } from 'react'\n\nexport type FormHasOneRelationScopeProps = {\n\tfield: SugaredRelativeSingleEntity['field']\n\tchildren: React.ReactNode\n\trequired?: boolean\n}\n\nexport const FormHasOneRelationScope = ({ field, children, required }: FormHasOneRelationScopeProps) => {\n\tconst entityRelation = useEntity({ field })\n\tconst marker = entityRelation.getMarker() as HasOneRelationMarker\n\tconst fieldName = marker.parameters.field\n\tconst entityName = entityRelation.getParent()!.name\n\tconst fieldInfo = useMemo(() => ({ entityName, fieldName }), [entityName, fieldName])\n\treturn (\n\t\t<FormFieldStateProvider\n\t\t\terrors={entityRelation.errors?.errors}\n\t\t\tdirty={entityRelation.hasUnpersistedChanges}\n\t\t\trequired={required}\n\t\t\tfield={fieldInfo}\n\t\t>\n\t\t\t{children}\n\t\t</FormFieldStateProvider>\n\t)\n}\n"],"names":["this"],"mappings":";;;;AAWO,MAAM,0BAA0B,CAAC,EAAE,OAAO,UAAU,eAA6C;AACvG,QAAM,iBAAiB,UAAU,EAAE,OAAO;AACpC,QAAA,SAAS,eAAe,UAAU;AAClC,QAAA,YAAY,OAAO,WAAW;AAC9B,QAAA,aAAa,eAAe,UAAA,EAAa;AACzC,QAAA,YAAY,QAAQ,OAAO,EAAE,YAAY,cAAc,CAAC,YAAY,SAAS,CAAC;AAEnF,SAAA;AAAA,IAAC;AAAA,IAAA;AAAA,MACA,QAAQ,eAAe,QAAQ;AAAA,MAC/B,OAAO,eAAe;AAAA,MACtB;AAAA,MACA,OAAO;AAAA,MAEN;AAAA,IAAA;AAAA,IANF;AAAA,IAAA;AAAA,IAAA;AAAA,MAAA,UAAA;AAAA,MAAA,YAAA;AAAA,MAAA,cAAA;AAAA,IAAA;AAAA,IAAAA;AAAAA,EAOA;AAEF;"}
|
|
@@ -10,9 +10,12 @@ const contexts = require("../contexts.cjs");
|
|
|
10
10
|
const useFormInputValidationHandler = require("../hooks/useFormInputValidationHandler.cjs");
|
|
11
11
|
const SlotInput = reactSlot.Slot;
|
|
12
12
|
const FormInput = reactBinding.Component(({ field, isNonbearing, defaultValue, formatValue: formatValueIn, parseValue: parseValueIn, ...props }) => {
|
|
13
|
-
const id = contexts.useFormFieldId();
|
|
14
13
|
const accessor = reactBinding.useField(field);
|
|
15
|
-
const
|
|
14
|
+
const formState = contexts.useFormFieldState();
|
|
15
|
+
const id = formState?.htmlId;
|
|
16
|
+
const hasErrors = (formState?.errors.length ?? accessor.errors?.errors?.length ?? 0) > 0;
|
|
17
|
+
const dirty = formState?.dirty ?? accessor.hasUnpersistedChanges;
|
|
18
|
+
const required = formState?.required ?? !accessor.schema.nullable;
|
|
16
19
|
const { parseValue, formatValue, defaultInputProps } = useFormInputHandler.useFormInputHandler(accessor, { formatValue: formatValueIn, parseValue: parseValueIn });
|
|
17
20
|
const accessorGetter = accessor.getAccessor;
|
|
18
21
|
const { ref, onFocus, onBlur } = useFormInputValidationHandler.useFormInputValidationHandler(accessor);
|
|
@@ -21,7 +24,9 @@ const FormInput = reactBinding.Component(({ field, isNonbearing, defaultValue, f
|
|
|
21
24
|
{
|
|
22
25
|
ref,
|
|
23
26
|
value: formatValue(accessor.value),
|
|
24
|
-
"data-invalid": utilities.dataAttribute(
|
|
27
|
+
"data-invalid": utilities.dataAttribute(hasErrors),
|
|
28
|
+
"data-dirty": utilities.dataAttribute(dirty),
|
|
29
|
+
"data-required": utilities.dataAttribute(required),
|
|
25
30
|
onFocus,
|
|
26
31
|
onBlur,
|
|
27
32
|
onChange: React.useCallback((e) => {
|
|
@@ -29,22 +34,22 @@ const FormInput = reactBinding.Component(({ field, isNonbearing, defaultValue, f
|
|
|
29
34
|
}, [accessorGetter, parseValue]),
|
|
30
35
|
...defaultInputProps,
|
|
31
36
|
id: id ? `${id}-input` : void 0,
|
|
32
|
-
required
|
|
37
|
+
required,
|
|
33
38
|
...props
|
|
34
39
|
},
|
|
35
40
|
void 0,
|
|
36
41
|
false,
|
|
37
42
|
{
|
|
38
|
-
fileName: "/
|
|
39
|
-
lineNumber:
|
|
43
|
+
fileName: "/home/runner/work/contember/contember/packages/react-form/src/components/FormInput.tsx",
|
|
44
|
+
lineNumber: 37,
|
|
40
45
|
columnNumber: 3
|
|
41
46
|
},
|
|
42
47
|
void 0
|
|
43
48
|
);
|
|
44
49
|
}, ({ field, isNonbearing, defaultValue }) => {
|
|
45
50
|
return /* @__PURE__ */ jsxDevRuntime.jsxDEV(reactBinding.Field, { field, isNonbearing, defaultValue }, void 0, false, {
|
|
46
|
-
fileName: "/
|
|
47
|
-
lineNumber:
|
|
51
|
+
fileName: "/home/runner/work/contember/contember/packages/react-form/src/components/FormInput.tsx",
|
|
52
|
+
lineNumber: 55,
|
|
48
53
|
columnNumber: 9
|
|
49
54
|
}, void 0);
|
|
50
55
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FormInput.cjs","sources":["../../../../../packages/react-form/src/components/FormInput.tsx"],"sourcesContent":["import * as React from 'react'\nimport { ChangeEventHandler, ComponentType, useCallback } from 'react'\nimport { Slot } from '@radix-ui/react-slot'\nimport { Component, Field, OptionallyVariableFieldValue, SugaredRelativeSingleField, useField } from '@contember/react-binding'\nimport { dataAttribute } from '@contember/utilities'\nimport { useFormInputHandler } from '../internal/useFormInputHandler'\nimport { useFormFieldState } from '../contexts'\nimport { useFormInputValidationHandler } from '../hooks/useFormInputValidationHandler'\nimport { FormInputHandler } from '../types'\n\ntype InputProps = React.JSX.IntrinsicElements['input']\nconst SlotInput = Slot as ComponentType<InputProps>\n\nexport interface FormInputProps {\n\tfield: SugaredRelativeSingleField['field']\n\tisNonbearing?: boolean\n\tdefaultValue?: OptionallyVariableFieldValue\n\tchildren: React.ReactElement\n\tformatValue?: FormInputHandler['formatValue']\n\tparseValue?: FormInputHandler['parseValue']\n}\n\nexport const FormInput = Component<FormInputProps>(({ field, isNonbearing, defaultValue, formatValue: formatValueIn, parseValue: parseValueIn, ...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\tconst required = formState?.required ?? !accessor.schema.nullable\n\n\tconst { parseValue, formatValue, defaultInputProps } = useFormInputHandler(accessor, { formatValue: formatValueIn, parseValue: parseValueIn })\n\tconst accessorGetter = accessor.getAccessor\n\tconst { ref, onFocus, onBlur } = useFormInputValidationHandler(accessor)\n\n\treturn (\n\t\t<SlotInput\n\t\t\tref={ref}\n\t\t\tvalue={formatValue(accessor.value)}\n\t\t\tdata-invalid={dataAttribute(hasErrors)}\n\t\t\tdata-dirty={dataAttribute(dirty)}\n\t\t\tdata-required={dataAttribute(required)}\n\t\t\tonFocus={onFocus}\n\t\t\tonBlur={onBlur}\n\t\t\tonChange={useCallback<ChangeEventHandler<HTMLInputElement>>(e => {\n\t\t\t\taccessorGetter().updateValue(parseValue(e.target.value))\n\t\t\t}, [accessorGetter, parseValue])}\n\t\t\t{...defaultInputProps}\n\t\t\tid={id ? `${id}-input` : undefined}\n\t\t\trequired={required}\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","useFormInputHandler","useFormInputValidationHandler","jsxDEV","dataAttribute","useCallback","this","Field"],"mappings":";;;;;;;;;;AAWA,MAAM,YAAYA,UAAA;AAWX,MAAM,YAAYC,aAAA,UAA0B,CAAC,EAAE,OAAO,cAAc,cAAc,aAAa,eAAe,YAAY,cAAc,GAAG,YAAY;AACvJ,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;AAC3C,QAAM,WAAW,WAAW,YAAY,CAAC,SAAS,OAAO;AAEzD,QAAM,EAAE,YAAY,aAAa,kBAAsB,IAAAC,oBAAA,oBAAoB,UAAU,EAAE,aAAa,eAAe,YAAY,aAAA,CAAc;AAC7I,QAAM,iBAAiB,SAAS;AAChC,QAAM,EAAE,KAAK,SAAS,OAAO,IAAIC,8BAAAA,8BAA8B,QAAQ;AAGtE,SAAAC,8BAAA;AAAA,IAAC;AAAA,IAAA;AAAA,MACA;AAAA,MACA,OAAO,YAAY,SAAS,KAAK;AAAA,MACjC,gBAAcC,wBAAc,SAAS;AAAA,MACrC,cAAYA,wBAAc,KAAK;AAAA,MAC/B,iBAAeA,wBAAc,QAAQ;AAAA,MACrC;AAAA,MACA;AAAA,MACA,UAAUC,MAAAA,YAAkD,CAAK,MAAA;AAChE,uBAAA,EAAiB,YAAY,WAAW,EAAE,OAAO,KAAK,CAAC;AAAA,MAAA,GACrD,CAAC,gBAAgB,UAAU,CAAC;AAAA,MAC9B,GAAG;AAAA,MACJ,IAAI,KAAK,GAAG,EAAE,WAAW;AAAA,MACzB;AAAA,MACC,GAAG;AAAA,IAAA;AAAA,IAdL;AAAA,IAAA;AAAA,IAAA;AAAA,MAAA,UAAA;AAAA,MAAA,YAAA;AAAA,MAAA,cAAA;AAAA,IAAA;AAAA,IAAAC;AAAAA,EAeA;AAEF,GAAG,CAAC,EAAE,OAAO,cAAc,mBAAmB;AAC7C,SAAQH,8BAAA,OAAAI,aAAA,OAAA,EAAM,OAAc,cAA4B,aAAjD,GAAA,QAAA,OAAA;AAAA,IAAA,UAAA;AAAA,IAAA,YAAA;AAAA,IAAA,cAAA;AAAA,EAA6E,GAAAD,MAAA;AACrF,CAAC;;"}
|
|
@@ -4,13 +4,16 @@ import { Slot } from "@radix-ui/react-slot";
|
|
|
4
4
|
import { Component, useField, Field } from "@contember/react-binding";
|
|
5
5
|
import { dataAttribute } from "@contember/utilities";
|
|
6
6
|
import { useFormInputHandler } from "../internal/useFormInputHandler.js";
|
|
7
|
-
import {
|
|
7
|
+
import { useFormFieldState } from "../contexts.js";
|
|
8
8
|
import { useFormInputValidationHandler } from "../hooks/useFormInputValidationHandler.js";
|
|
9
9
|
const SlotInput = Slot;
|
|
10
10
|
const FormInput = Component(({ field, isNonbearing, defaultValue, formatValue: formatValueIn, parseValue: parseValueIn, ...props }) => {
|
|
11
|
-
const id = useFormFieldId();
|
|
12
11
|
const accessor = useField(field);
|
|
13
|
-
const
|
|
12
|
+
const formState = useFormFieldState();
|
|
13
|
+
const id = formState?.htmlId;
|
|
14
|
+
const hasErrors = (formState?.errors.length ?? accessor.errors?.errors?.length ?? 0) > 0;
|
|
15
|
+
const dirty = formState?.dirty ?? accessor.hasUnpersistedChanges;
|
|
16
|
+
const required = formState?.required ?? !accessor.schema.nullable;
|
|
14
17
|
const { parseValue, formatValue, defaultInputProps } = useFormInputHandler(accessor, { formatValue: formatValueIn, parseValue: parseValueIn });
|
|
15
18
|
const accessorGetter = accessor.getAccessor;
|
|
16
19
|
const { ref, onFocus, onBlur } = useFormInputValidationHandler(accessor);
|
|
@@ -19,7 +22,9 @@ const FormInput = Component(({ field, isNonbearing, defaultValue, formatValue: f
|
|
|
19
22
|
{
|
|
20
23
|
ref,
|
|
21
24
|
value: formatValue(accessor.value),
|
|
22
|
-
"data-invalid": dataAttribute(
|
|
25
|
+
"data-invalid": dataAttribute(hasErrors),
|
|
26
|
+
"data-dirty": dataAttribute(dirty),
|
|
27
|
+
"data-required": dataAttribute(required),
|
|
23
28
|
onFocus,
|
|
24
29
|
onBlur,
|
|
25
30
|
onChange: useCallback((e) => {
|
|
@@ -27,22 +32,22 @@ const FormInput = Component(({ field, isNonbearing, defaultValue, formatValue: f
|
|
|
27
32
|
}, [accessorGetter, parseValue]),
|
|
28
33
|
...defaultInputProps,
|
|
29
34
|
id: id ? `${id}-input` : void 0,
|
|
30
|
-
required
|
|
35
|
+
required,
|
|
31
36
|
...props
|
|
32
37
|
},
|
|
33
38
|
void 0,
|
|
34
39
|
false,
|
|
35
40
|
{
|
|
36
|
-
fileName: "/
|
|
37
|
-
lineNumber:
|
|
41
|
+
fileName: "/home/runner/work/contember/contember/packages/react-form/src/components/FormInput.tsx",
|
|
42
|
+
lineNumber: 37,
|
|
38
43
|
columnNumber: 3
|
|
39
44
|
},
|
|
40
45
|
void 0
|
|
41
46
|
);
|
|
42
47
|
}, ({ field, isNonbearing, defaultValue }) => {
|
|
43
48
|
return /* @__PURE__ */ jsxDEV(Field, { field, isNonbearing, defaultValue }, void 0, false, {
|
|
44
|
-
fileName: "/
|
|
45
|
-
lineNumber:
|
|
49
|
+
fileName: "/home/runner/work/contember/contember/packages/react-form/src/components/FormInput.tsx",
|
|
50
|
+
lineNumber: 55,
|
|
46
51
|
columnNumber: 9
|
|
47
52
|
}, void 0);
|
|
48
53
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FormInput.js","sources":["../../../../../packages/react-form/src/components/FormInput.tsx"],"sourcesContent":["import * as React from 'react'\nimport { ChangeEventHandler, ComponentType, useCallback } from 'react'\nimport { Slot } from '@radix-ui/react-slot'\nimport { Component, Field, OptionallyVariableFieldValue, SugaredRelativeSingleField, useField } from '@contember/react-binding'\nimport { dataAttribute } from '@contember/utilities'\nimport { useFormInputHandler } from '../internal/useFormInputHandler'\nimport { useFormFieldState } from '../contexts'\nimport { useFormInputValidationHandler } from '../hooks/useFormInputValidationHandler'\nimport { FormInputHandler } from '../types'\n\ntype InputProps = React.JSX.IntrinsicElements['input']\nconst SlotInput = Slot as ComponentType<InputProps>\n\nexport interface FormInputProps {\n\tfield: SugaredRelativeSingleField['field']\n\tisNonbearing?: boolean\n\tdefaultValue?: OptionallyVariableFieldValue\n\tchildren: React.ReactElement\n\tformatValue?: FormInputHandler['formatValue']\n\tparseValue?: FormInputHandler['parseValue']\n}\n\nexport const FormInput = Component<FormInputProps>(({ field, isNonbearing, defaultValue, formatValue: formatValueIn, parseValue: parseValueIn, ...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\tconst required = formState?.required ?? !accessor.schema.nullable\n\n\tconst { parseValue, formatValue, defaultInputProps } = useFormInputHandler(accessor, { formatValue: formatValueIn, parseValue: parseValueIn })\n\tconst accessorGetter = accessor.getAccessor\n\tconst { ref, onFocus, onBlur } = useFormInputValidationHandler(accessor)\n\n\treturn (\n\t\t<SlotInput\n\t\t\tref={ref}\n\t\t\tvalue={formatValue(accessor.value)}\n\t\t\tdata-invalid={dataAttribute(hasErrors)}\n\t\t\tdata-dirty={dataAttribute(dirty)}\n\t\t\tdata-required={dataAttribute(required)}\n\t\t\tonFocus={onFocus}\n\t\t\tonBlur={onBlur}\n\t\t\tonChange={useCallback<ChangeEventHandler<HTMLInputElement>>(e => {\n\t\t\t\taccessorGetter().updateValue(parseValue(e.target.value))\n\t\t\t}, [accessorGetter, parseValue])}\n\t\t\t{...defaultInputProps}\n\t\t\tid={id ? `${id}-input` : undefined}\n\t\t\trequired={required}\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":["this"],"mappings":";;;;;;;;AAWA,MAAM,YAAY;AAWX,MAAM,YAAY,UAA0B,CAAC,EAAE,OAAO,cAAc,cAAc,aAAa,eAAe,YAAY,cAAc,GAAG,YAAY;AACvJ,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;AAC3C,QAAM,WAAW,WAAW,YAAY,CAAC,SAAS,OAAO;AAEzD,QAAM,EAAE,YAAY,aAAa,kBAAsB,IAAA,oBAAoB,UAAU,EAAE,aAAa,eAAe,YAAY,aAAA,CAAc;AAC7I,QAAM,iBAAiB,SAAS;AAChC,QAAM,EAAE,KAAK,SAAS,OAAO,IAAI,8BAA8B,QAAQ;AAGtE,SAAA;AAAA,IAAC;AAAA,IAAA;AAAA,MACA;AAAA,MACA,OAAO,YAAY,SAAS,KAAK;AAAA,MACjC,gBAAc,cAAc,SAAS;AAAA,MACrC,cAAY,cAAc,KAAK;AAAA,MAC/B,iBAAe,cAAc,QAAQ;AAAA,MACrC;AAAA,MACA;AAAA,MACA,UAAU,YAAkD,CAAK,MAAA;AAChE,uBAAA,EAAiB,YAAY,WAAW,EAAE,OAAO,KAAK,CAAC;AAAA,MAAA,GACrD,CAAC,gBAAgB,UAAU,CAAC;AAAA,MAC9B,GAAG;AAAA,MACJ,IAAI,KAAK,GAAG,EAAE,WAAW;AAAA,MACzB;AAAA,MACC,GAAG;AAAA,IAAA;AAAA,IAdL;AAAA,IAAA;AAAA,IAAA;AAAA,MAAA,UAAA;AAAA,MAAA,YAAA;AAAA,MAAA,cAAA;AAAA,IAAA;AAAA,IAAAA;AAAAA,EAeA;AAEF,GAAG,CAAC,EAAE,OAAO,cAAc,mBAAmB;AAC7C,SAAQ,uBAAA,OAAA,EAAM,OAAc,cAA4B,aAAjD,GAAA,QAAA,OAAA;AAAA,IAAA,UAAA;AAAA,IAAA,YAAA;AAAA,IAAA,cAAA;AAAA,EAA6E,GAAAA,MAAA;AACrF,CAAC;"}
|
|
@@ -6,22 +6,24 @@ const utilities = require("@contember/utilities");
|
|
|
6
6
|
const contexts = require("../contexts.cjs");
|
|
7
7
|
const reactBinding = require("@contember/react-binding");
|
|
8
8
|
const FormLabel = (props) => {
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
throw new reactBinding.BindingError("FormLabel must be used inside a FormField");
|
|
9
|
+
const formState = contexts.useFormFieldState();
|
|
10
|
+
if (!formState) {
|
|
11
|
+
throw new reactBinding.BindingError("FormError must be used inside a FormField");
|
|
13
12
|
}
|
|
13
|
+
const { errors, htmlId, dirty, required } = formState;
|
|
14
14
|
return /* @__PURE__ */ jsxDevRuntime.jsxDEV(
|
|
15
15
|
reactSlot.Slot,
|
|
16
16
|
{
|
|
17
17
|
"data-invalid": utilities.dataAttribute(errors.length > 0),
|
|
18
|
-
|
|
18
|
+
"data-dirty": utilities.dataAttribute(dirty),
|
|
19
|
+
"data-required": utilities.dataAttribute(required),
|
|
20
|
+
...htmlId ? { htmlFor: `${htmlId}-input` } : {},
|
|
19
21
|
...props
|
|
20
22
|
},
|
|
21
23
|
void 0,
|
|
22
24
|
false,
|
|
23
25
|
{
|
|
24
|
-
fileName: "/
|
|
26
|
+
fileName: "/home/runner/work/contember/contember/packages/react-form/src/components/FormLabel.tsx",
|
|
25
27
|
lineNumber: 16,
|
|
26
28
|
columnNumber: 3
|
|
27
29
|
},
|
|
@@ -0,0 +1 @@
|
|
|
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","jsxDEV","Slot","dataAttribute","this"],"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,8BAAA;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,IALL;AAAA,IAAA;AAAA,IAAA;AAAA,MAAA,UAAA;AAAA,MAAA,YAAA;AAAA,MAAA,cAAA;AAAA,IAAA;AAAA,IAAAC;AAAAA,EAMA;AAEF;;"}
|
|
@@ -1,25 +1,27 @@
|
|
|
1
1
|
import { jsxDEV } from "react/jsx-dev-runtime";
|
|
2
2
|
import { Slot } from "@radix-ui/react-slot";
|
|
3
3
|
import { dataAttribute } from "@contember/utilities";
|
|
4
|
-
import {
|
|
4
|
+
import { useFormFieldState } from "../contexts.js";
|
|
5
5
|
import { BindingError } from "@contember/react-binding";
|
|
6
6
|
const FormLabel = (props) => {
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
throw new BindingError("FormLabel must be used inside a FormField");
|
|
7
|
+
const formState = useFormFieldState();
|
|
8
|
+
if (!formState) {
|
|
9
|
+
throw new BindingError("FormError must be used inside a FormField");
|
|
11
10
|
}
|
|
11
|
+
const { errors, htmlId, dirty, required } = formState;
|
|
12
12
|
return /* @__PURE__ */ jsxDEV(
|
|
13
13
|
Slot,
|
|
14
14
|
{
|
|
15
15
|
"data-invalid": dataAttribute(errors.length > 0),
|
|
16
|
-
|
|
16
|
+
"data-dirty": dataAttribute(dirty),
|
|
17
|
+
"data-required": dataAttribute(required),
|
|
18
|
+
...htmlId ? { htmlFor: `${htmlId}-input` } : {},
|
|
17
19
|
...props
|
|
18
20
|
},
|
|
19
21
|
void 0,
|
|
20
22
|
false,
|
|
21
23
|
{
|
|
22
|
-
fileName: "/
|
|
24
|
+
fileName: "/home/runner/work/contember/contember/packages/react-form/src/components/FormLabel.tsx",
|
|
23
25
|
lineNumber: 16,
|
|
24
26
|
columnNumber: 3
|
|
25
27
|
},
|
|
@@ -0,0 +1 @@
|
|
|
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":["this"],"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,IALL;AAAA,IAAA;AAAA,IAAA;AAAA,MAAA,UAAA;AAAA,MAAA,YAAA;AAAA,MAAA,cAAA;AAAA,IAAA;AAAA,IAAAA;AAAAA,EAMA;AAEF;"}
|
|
@@ -2,48 +2,52 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const jsxDevRuntime = require("react/jsx-dev-runtime");
|
|
4
4
|
const reactBinding = require("@contember/react-binding");
|
|
5
|
-
const
|
|
5
|
+
const utilities = require("@contember/utilities");
|
|
6
6
|
const reactSlot = require("@radix-ui/react-slot");
|
|
7
|
+
const React = require("react");
|
|
7
8
|
const contexts = require("../contexts.cjs");
|
|
8
|
-
const utilities = require("@contember/utilities");
|
|
9
9
|
const useFormInputValidationHandler = require("../hooks/useFormInputValidationHandler.cjs");
|
|
10
10
|
const SlotInput = reactSlot.Slot;
|
|
11
11
|
const FormRadioInput = reactBinding.Component(({ field, value, defaultValue, isNonbearing, ...props }) => {
|
|
12
|
-
const id = contexts.useFormFieldId();
|
|
13
12
|
const accessor = reactBinding.useField(field);
|
|
14
|
-
const
|
|
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;
|
|
15
17
|
const accessorGetter = accessor.getAccessor;
|
|
16
18
|
const { ref, onFocus, onBlur } = useFormInputValidationHandler.useFormInputValidationHandler(accessor);
|
|
19
|
+
const handleChange = React.useCallback((e) => {
|
|
20
|
+
accessorGetter().updateValue(value);
|
|
21
|
+
}, [accessorGetter, value]);
|
|
17
22
|
return /* @__PURE__ */ jsxDevRuntime.jsxDEV(
|
|
18
23
|
SlotInput,
|
|
19
24
|
{
|
|
20
25
|
ref,
|
|
21
26
|
type: "radio",
|
|
22
|
-
value,
|
|
27
|
+
value: typeof value === "string" ? value : void 0,
|
|
23
28
|
checked: accessor.value === value,
|
|
24
|
-
"data-invalid": utilities.dataAttribute(
|
|
25
|
-
|
|
26
|
-
|
|
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,
|
|
27
33
|
onFocus,
|
|
28
34
|
onBlur,
|
|
29
|
-
onChange:
|
|
30
|
-
accessorGetter().updateValue(value);
|
|
31
|
-
}, [accessorGetter, value]),
|
|
35
|
+
onChange: handleChange,
|
|
32
36
|
...props
|
|
33
37
|
},
|
|
34
38
|
void 0,
|
|
35
39
|
false,
|
|
36
40
|
{
|
|
37
|
-
fileName: "/
|
|
38
|
-
lineNumber:
|
|
41
|
+
fileName: "/home/runner/work/contember/contember/packages/react-form/src/components/FormRadioInput.tsx",
|
|
42
|
+
lineNumber: 35,
|
|
39
43
|
columnNumber: 3
|
|
40
44
|
},
|
|
41
45
|
void 0
|
|
42
46
|
);
|
|
43
47
|
}, ({ field, isNonbearing, defaultValue }) => {
|
|
44
48
|
return /* @__PURE__ */ jsxDevRuntime.jsxDEV(reactBinding.Field, { field, isNonbearing, defaultValue }, void 0, false, {
|
|
45
|
-
fileName: "/
|
|
46
|
-
lineNumber:
|
|
49
|
+
fileName: "/home/runner/work/contember/contember/packages/react-form/src/components/FormRadioInput.tsx",
|
|
50
|
+
lineNumber: 51,
|
|
47
51
|
columnNumber: 9
|
|
48
52
|
}, void 0);
|
|
49
53
|
});
|
|
@@ -0,0 +1 @@
|
|
|
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","jsxDEV","dataAttribute","this","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,8BAAA;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,IAZL;AAAA,IAAA;AAAA,IAAA;AAAA,MAAA,UAAA;AAAA,MAAA,YAAA;AAAA,MAAA,cAAA;AAAA,IAAA;AAAA,IAAAC;AAAAA,EAaA;AAEF,GAAG,CAAC,EAAE,OAAO,cAAc,mBAAmB;AAC7C,SAAQF,8BAAA,OAAAG,aAAA,OAAA,EAAM,OAAc,cAA4B,aAAjD,GAAA,QAAA,OAAA;AAAA,IAAA,UAAA;AAAA,IAAA,YAAA;AAAA,IAAA,cAAA;AAAA,EAA4E,GAAAD,MAAA;AACpF,CAAC;;"}
|
|
@@ -1,47 +1,51 @@
|
|
|
1
1
|
import { jsxDEV } from "react/jsx-dev-runtime";
|
|
2
2
|
import { Component, useField, Field } from "@contember/react-binding";
|
|
3
|
-
import { useCallback } from "react";
|
|
4
|
-
import { Slot } from "@radix-ui/react-slot";
|
|
5
|
-
import { useFormFieldId, useFormError } from "../contexts.js";
|
|
6
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
7
|
import { useFormInputValidationHandler } from "../hooks/useFormInputValidationHandler.js";
|
|
8
8
|
const SlotInput = Slot;
|
|
9
9
|
const FormRadioInput = Component(({ field, value, defaultValue, isNonbearing, ...props }) => {
|
|
10
|
-
const id = useFormFieldId();
|
|
11
10
|
const accessor = useField(field);
|
|
12
|
-
const
|
|
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;
|
|
13
15
|
const accessorGetter = accessor.getAccessor;
|
|
14
16
|
const { ref, onFocus, onBlur } = useFormInputValidationHandler(accessor);
|
|
17
|
+
const handleChange = useCallback((e) => {
|
|
18
|
+
accessorGetter().updateValue(value);
|
|
19
|
+
}, [accessorGetter, value]);
|
|
15
20
|
return /* @__PURE__ */ jsxDEV(
|
|
16
21
|
SlotInput,
|
|
17
22
|
{
|
|
18
23
|
ref,
|
|
19
24
|
type: "radio",
|
|
20
|
-
value,
|
|
25
|
+
value: typeof value === "string" ? value : void 0,
|
|
21
26
|
checked: accessor.value === value,
|
|
22
|
-
"data-invalid": dataAttribute(
|
|
23
|
-
|
|
24
|
-
|
|
27
|
+
"data-invalid": dataAttribute(hasErrors),
|
|
28
|
+
"data-dirty": dataAttribute(dirty),
|
|
29
|
+
name: id ? id + "-input" : void 0,
|
|
30
|
+
id: id ? `${id}-input` : void 0,
|
|
25
31
|
onFocus,
|
|
26
32
|
onBlur,
|
|
27
|
-
onChange:
|
|
28
|
-
accessorGetter().updateValue(value);
|
|
29
|
-
}, [accessorGetter, value]),
|
|
33
|
+
onChange: handleChange,
|
|
30
34
|
...props
|
|
31
35
|
},
|
|
32
36
|
void 0,
|
|
33
37
|
false,
|
|
34
38
|
{
|
|
35
|
-
fileName: "/
|
|
36
|
-
lineNumber:
|
|
39
|
+
fileName: "/home/runner/work/contember/contember/packages/react-form/src/components/FormRadioInput.tsx",
|
|
40
|
+
lineNumber: 35,
|
|
37
41
|
columnNumber: 3
|
|
38
42
|
},
|
|
39
43
|
void 0
|
|
40
44
|
);
|
|
41
45
|
}, ({ field, isNonbearing, defaultValue }) => {
|
|
42
46
|
return /* @__PURE__ */ jsxDEV(Field, { field, isNonbearing, defaultValue }, void 0, false, {
|
|
43
|
-
fileName: "/
|
|
44
|
-
lineNumber:
|
|
47
|
+
fileName: "/home/runner/work/contember/contember/packages/react-form/src/components/FormRadioInput.tsx",
|
|
48
|
+
lineNumber: 51,
|
|
45
49
|
columnNumber: 9
|
|
46
50
|
}, void 0);
|
|
47
51
|
});
|
|
@@ -0,0 +1 @@
|
|
|
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":["this"],"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,IAZL;AAAA,IAAA;AAAA,IAAA;AAAA,MAAA,UAAA;AAAA,MAAA,YAAA;AAAA,MAAA,cAAA;AAAA,IAAA;AAAA,IAAAA;AAAAA,EAaA;AAEF,GAAG,CAAC,EAAE,OAAO,cAAc,mBAAmB;AAC7C,SAAQ,uBAAA,OAAA,EAAM,OAAc,cAA4B,aAAjD,GAAA,QAAA,OAAA;AAAA,IAAA,UAAA;AAAA,IAAA,YAAA;AAAA,IAAA,cAAA;AAAA,EAA4E,GAAAA,MAAA;AACpF,CAAC;"}
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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;;;;;"}
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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;"}
|
|
@@ -38,8 +38,11 @@ const useFormInputValidationHandler = (field) => {
|
|
|
38
38
|
const message = valid ? void 0 : input?.validationMessage;
|
|
39
39
|
const previousMessage = validationMessage.current;
|
|
40
40
|
validationMessage.current = message;
|
|
41
|
+
const accessor = accessorGetter();
|
|
41
42
|
if (!message) {
|
|
42
|
-
|
|
43
|
+
if (previousMessage) {
|
|
44
|
+
clearSpecificError(accessor, previousMessage);
|
|
45
|
+
}
|
|
43
46
|
return;
|
|
44
47
|
}
|
|
45
48
|
if (message === previousMessage) {
|
|
@@ -49,8 +52,8 @@ const useFormInputValidationHandler = (field) => {
|
|
|
49
52
|
return;
|
|
50
53
|
}
|
|
51
54
|
if (!focus || !!previousMessage) {
|
|
52
|
-
|
|
53
|
-
|
|
55
|
+
accessor.clearErrors();
|
|
56
|
+
accessor.addError(message);
|
|
54
57
|
}
|
|
55
58
|
});
|
|
56
59
|
return {
|
|
@@ -67,5 +70,11 @@ const useFormInputValidationHandler = (field) => {
|
|
|
67
70
|
}, [accessorGetter])
|
|
68
71
|
};
|
|
69
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
|
+
};
|
|
70
79
|
exports.useFormInputValidationHandler = useFormInputValidationHandler;
|
|
71
80
|
//# sourceMappingURL=useFormInputValidationHandler.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
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","useEntityBeforePersist","useCallback","useEffect"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAIa,MAAA,gCAAgC,CAAC,UAA8B;AAC3E,QAAM,iBAAiB,MAAM;AAC7B,QAAM,oBAAoBA,MAAAA,OAAe;AACzC,QAAM,CAAC,OAAO,QAAQ,IAAIC,iBAAM,SAAS,KAAK;AACxC,QAAA,WAAWA,iBAAM,OAAyB,IAAI;AACpDC,eAAA,uBAAuBC,kBAAY,MAAM;AACxC,QAAI,kBAAkB,SAAS;AACf,uBAAE,SAAS,kBAAkB,OAAO;AAAA,IAAA;AAAA,EACpD,GACE,CAAC,cAAc,CAAC,CAAC;AAEpBC,QAAAA,UAAU,MAAM;AACX,QAAA,CAAC,SAAS,SAAS;AACtB;AAAA,IAAA;AAGD,UAAM,QAAQ,SAAS;AACjB,UAAA,QAAQ,MAAM,UAAU;AAExB,UAAA,UAAU,QAAQ,SAAY,OAAO;AAC3C,UAAM,kBAAkB,kBAAkB;AAC1C,sBAAkB,UAAU;AAE5B,UAAM,WAAW,eAAe;AAEhC,QAAI,CAAC,SAAS;AACb,UAAI,iBAAiB;AACpB,2BAAmB,UAAU,eAAe;AAAA,MAAA;AAE7C;AAAA,IAAA;AAED,QAAI,YAAY,iBAAiB;AAChC;AAAA,IAAA;AAGG,QAAA,CAAC,MAAM,WAAW;AACrB;AAAA,IAAA;AAKD,QAAI,CAAC,SAAS,CAAC,CAAC,iBAAiB;AAChC,eAAS,YAAY;AACrB,eAAS,SAAS,OAAO;AAAA,IAAA;AAAA,EAC1B,CACA;AAEM,SAAA;AAAA,IACN,KAAK;AAAA,IACL,SAASD,kBAAY,MAAM;AAC1B,eAAS,IAAI;AAAA,IACd,GAAG,EAAE;AAAA,IACL,QAAQA,kBAAY,MAAM;AACzB,eAAS,KAAK;AACd,qBAAA,EAAiB,YAAY;AAC7B,UAAI,kBAAkB,SAAS;AACf,yBAAE,SAAS,kBAAkB,OAAO;AAAA,MAAA;AAAA,IAErD,GAAG,CAAC,cAAc,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;;"}
|
|
@@ -20,8 +20,11 @@ const useFormInputValidationHandler = (field) => {
|
|
|
20
20
|
const message = valid ? void 0 : input?.validationMessage;
|
|
21
21
|
const previousMessage = validationMessage.current;
|
|
22
22
|
validationMessage.current = message;
|
|
23
|
+
const accessor = accessorGetter();
|
|
23
24
|
if (!message) {
|
|
24
|
-
|
|
25
|
+
if (previousMessage) {
|
|
26
|
+
clearSpecificError(accessor, previousMessage);
|
|
27
|
+
}
|
|
25
28
|
return;
|
|
26
29
|
}
|
|
27
30
|
if (message === previousMessage) {
|
|
@@ -31,8 +34,8 @@ const useFormInputValidationHandler = (field) => {
|
|
|
31
34
|
return;
|
|
32
35
|
}
|
|
33
36
|
if (!focus || !!previousMessage) {
|
|
34
|
-
|
|
35
|
-
|
|
37
|
+
accessor.clearErrors();
|
|
38
|
+
accessor.addError(message);
|
|
36
39
|
}
|
|
37
40
|
});
|
|
38
41
|
return {
|
|
@@ -49,6 +52,12 @@ const useFormInputValidationHandler = (field) => {
|
|
|
49
52
|
}, [accessorGetter])
|
|
50
53
|
};
|
|
51
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
|
+
};
|
|
52
61
|
export {
|
|
53
62
|
useFormInputValidationHandler
|
|
54
63
|
};
|
|
@@ -0,0 +1 @@
|
|
|
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;AAEpB,YAAU,MAAM;AACX,QAAA,CAAC,SAAS,SAAS;AACtB;AAAA,IAAA;AAGD,UAAM,QAAQ,SAAS;AACjB,UAAA,QAAQ,MAAM,UAAU;AAExB,UAAA,UAAU,QAAQ,SAAY,OAAO;AAC3C,UAAM,kBAAkB,kBAAkB;AAC1C,sBAAkB,UAAU;AAE5B,UAAM,WAAW,eAAe;AAEhC,QAAI,CAAC,SAAS;AACb,UAAI,iBAAiB;AACpB,2BAAmB,UAAU,eAAe;AAAA,MAAA;AAE7C;AAAA,IAAA;AAED,QAAI,YAAY,iBAAiB;AAChC;AAAA,IAAA;AAGG,QAAA,CAAC,MAAM,WAAW;AACrB;AAAA,IAAA;AAKD,QAAI,CAAC,SAAS,CAAC,CAAC,iBAAiB;AAChC,eAAS,YAAY;AACrB,eAAS,SAAS,OAAO;AAAA,IAAA;AAAA,EAC1B,CACA;AAEM,SAAA;AAAA,IACN,KAAK;AAAA,IACL,SAAS,YAAY,MAAM;AAC1B,eAAS,IAAI;AAAA,IACd,GAAG,EAAE;AAAA,IACL,QAAQ,YAAY,MAAM;AACzB,eAAS,KAAK;AACd,qBAAA,EAAiB,YAAY;AAC7B,UAAI,kBAAkB,SAAS;AACf,yBAAE,SAAS,kBAAkB,OAAO;AAAA,MAAA;AAAA,IAErD,GAAG,CAAC,cAAc,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;"}
|
|
@@ -0,0 +1 @@
|
|
|
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\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: () => ({\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 = parseFloat(value)\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\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\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":"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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFormInputHandler.js","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\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: () => ({\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 = parseFloat(value)\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\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\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":[],"mappings":"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"}
|