@contember/react-form 2.0.0-alpha.8 → 2.0.0-beta.1

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.
Files changed (137) hide show
  1. package/dist/development/index.cjs +6 -2
  2. package/dist/development/index.cjs.map +1 -1
  3. package/dist/development/index.js +5 -1
  4. package/dist/development/index.js.map +1 -1
  5. package/dist/development/src/components/FormCheckbox.cjs +9 -6
  6. package/dist/development/src/components/FormCheckbox.cjs.map +1 -1
  7. package/dist/development/src/components/FormCheckbox.js +10 -7
  8. package/dist/development/src/components/FormCheckbox.js.map +1 -1
  9. package/dist/development/src/components/FormError.cjs +5 -5
  10. package/dist/development/src/components/FormError.cjs.map +1 -1
  11. package/dist/development/src/components/FormError.js +6 -6
  12. package/dist/development/src/components/FormError.js.map +1 -1
  13. package/dist/development/src/components/FormFieldScope.cjs +24 -14
  14. package/dist/development/src/components/FormFieldScope.cjs.map +1 -1
  15. package/dist/development/src/components/FormFieldScope.js +25 -15
  16. package/dist/development/src/components/FormFieldScope.js.map +1 -1
  17. package/dist/development/src/components/FormFieldStateProvider.cjs +56 -0
  18. package/dist/development/src/components/FormFieldStateProvider.cjs.map +1 -0
  19. package/dist/development/src/components/FormFieldStateProvider.js +56 -0
  20. package/dist/development/src/components/FormFieldStateProvider.js.map +1 -0
  21. package/dist/development/src/components/FormHasManyRelationScope.cjs +25 -15
  22. package/dist/development/src/components/FormHasManyRelationScope.cjs.map +1 -1
  23. package/dist/development/src/components/FormHasManyRelationScope.js +26 -16
  24. package/dist/development/src/components/FormHasManyRelationScope.js.map +1 -1
  25. package/dist/development/src/components/FormHasOneRelationScope.cjs +25 -15
  26. package/dist/development/src/components/FormHasOneRelationScope.cjs.map +1 -1
  27. package/dist/development/src/components/FormHasOneRelationScope.js +25 -15
  28. package/dist/development/src/components/FormHasOneRelationScope.js.map +1 -1
  29. package/dist/development/src/components/FormInput.cjs +11 -6
  30. package/dist/development/src/components/FormInput.cjs.map +1 -1
  31. package/dist/development/src/components/FormInput.js +12 -7
  32. package/dist/development/src/components/FormInput.js.map +1 -1
  33. package/dist/development/src/components/FormLabel.cjs +7 -5
  34. package/dist/development/src/components/FormLabel.cjs.map +1 -1
  35. package/dist/development/src/components/FormLabel.js +8 -6
  36. package/dist/development/src/components/FormLabel.js.map +1 -1
  37. package/dist/development/src/components/FormRadioInput.cjs +17 -13
  38. package/dist/development/src/components/FormRadioInput.cjs.map +1 -1
  39. package/dist/development/src/components/FormRadioInput.js +18 -14
  40. package/dist/development/src/components/FormRadioInput.js.map +1 -1
  41. package/dist/development/src/contexts.cjs +7 -8
  42. package/dist/development/src/contexts.cjs.map +1 -1
  43. package/dist/development/src/contexts.js +8 -9
  44. package/dist/development/src/contexts.js.map +1 -1
  45. package/dist/development/src/hooks/useFormInputValidationHandler.cjs +12 -3
  46. package/dist/development/src/hooks/useFormInputValidationHandler.cjs.map +1 -1
  47. package/dist/development/src/hooks/useFormInputValidationHandler.js +12 -3
  48. package/dist/development/src/hooks/useFormInputValidationHandler.js.map +1 -1
  49. package/dist/development/src/internal/useFormInputHandler.cjs.map +1 -1
  50. package/dist/development/src/internal/useFormInputHandler.js.map +1 -1
  51. package/dist/production/index.cjs +6 -2
  52. package/dist/production/index.cjs.map +1 -1
  53. package/dist/production/index.js +5 -1
  54. package/dist/production/index.js.map +1 -1
  55. package/dist/production/src/components/FormCheckbox.cjs +7 -4
  56. package/dist/production/src/components/FormCheckbox.cjs.map +1 -1
  57. package/dist/production/src/components/FormCheckbox.js +8 -5
  58. package/dist/production/src/components/FormCheckbox.js.map +1 -1
  59. package/dist/production/src/components/FormError.cjs +5 -5
  60. package/dist/production/src/components/FormError.cjs.map +1 -1
  61. package/dist/production/src/components/FormError.js +6 -6
  62. package/dist/production/src/components/FormError.js.map +1 -1
  63. package/dist/production/src/components/FormFieldScope.cjs +16 -6
  64. package/dist/production/src/components/FormFieldScope.cjs.map +1 -1
  65. package/dist/production/src/components/FormFieldScope.js +17 -7
  66. package/dist/production/src/components/FormFieldScope.js.map +1 -1
  67. package/dist/production/src/components/FormFieldStateProvider.cjs +34 -0
  68. package/dist/production/src/components/FormFieldStateProvider.cjs.map +1 -0
  69. package/dist/production/src/components/FormFieldStateProvider.js +34 -0
  70. package/dist/production/src/components/FormFieldStateProvider.js.map +1 -0
  71. package/dist/production/src/components/FormHasManyRelationScope.cjs +17 -7
  72. package/dist/production/src/components/FormHasManyRelationScope.cjs.map +1 -1
  73. package/dist/production/src/components/FormHasManyRelationScope.js +18 -8
  74. package/dist/production/src/components/FormHasManyRelationScope.js.map +1 -1
  75. package/dist/production/src/components/FormHasOneRelationScope.cjs +17 -7
  76. package/dist/production/src/components/FormHasOneRelationScope.cjs.map +1 -1
  77. package/dist/production/src/components/FormHasOneRelationScope.js +17 -7
  78. package/dist/production/src/components/FormHasOneRelationScope.js.map +1 -1
  79. package/dist/production/src/components/FormInput.cjs +9 -4
  80. package/dist/production/src/components/FormInput.cjs.map +1 -1
  81. package/dist/production/src/components/FormInput.js +10 -5
  82. package/dist/production/src/components/FormInput.js.map +1 -1
  83. package/dist/production/src/components/FormLabel.cjs +7 -5
  84. package/dist/production/src/components/FormLabel.cjs.map +1 -1
  85. package/dist/production/src/components/FormLabel.js +8 -6
  86. package/dist/production/src/components/FormLabel.js.map +1 -1
  87. package/dist/production/src/components/FormRadioInput.cjs +15 -11
  88. package/dist/production/src/components/FormRadioInput.cjs.map +1 -1
  89. package/dist/production/src/components/FormRadioInput.js +16 -12
  90. package/dist/production/src/components/FormRadioInput.js.map +1 -1
  91. package/dist/production/src/contexts.cjs +7 -8
  92. package/dist/production/src/contexts.cjs.map +1 -1
  93. package/dist/production/src/contexts.js +8 -9
  94. package/dist/production/src/contexts.js.map +1 -1
  95. package/dist/production/src/hooks/useFormInputValidationHandler.cjs +12 -3
  96. package/dist/production/src/hooks/useFormInputValidationHandler.cjs.map +1 -1
  97. package/dist/production/src/hooks/useFormInputValidationHandler.js +12 -3
  98. package/dist/production/src/hooks/useFormInputValidationHandler.js.map +1 -1
  99. package/dist/production/src/internal/useFormInputHandler.cjs.map +1 -1
  100. package/dist/production/src/internal/useFormInputHandler.js.map +1 -1
  101. package/dist/types/components/FormCheckbox.d.ts.map +1 -1
  102. package/dist/types/components/FormFieldScope.d.ts +2 -1
  103. package/dist/types/components/FormFieldScope.d.ts.map +1 -1
  104. package/dist/types/components/FormFieldStateProvider.d.ts +36 -0
  105. package/dist/types/components/FormFieldStateProvider.d.ts.map +1 -0
  106. package/dist/types/components/FormHasManyRelationScope.d.ts +5 -1
  107. package/dist/types/components/FormHasManyRelationScope.d.ts.map +1 -1
  108. package/dist/types/components/FormHasOneRelationScope.d.ts +2 -1
  109. package/dist/types/components/FormHasOneRelationScope.d.ts.map +1 -1
  110. package/dist/types/components/FormInput.d.ts.map +1 -1
  111. package/dist/types/components/FormLabel.d.ts.map +1 -1
  112. package/dist/types/components/FormRadioInput.d.ts +1 -1
  113. package/dist/types/components/FormRadioInput.d.ts.map +1 -1
  114. package/dist/types/components/index.d.ts +1 -0
  115. package/dist/types/components/index.d.ts.map +1 -1
  116. package/dist/types/contexts.d.ts +10 -6
  117. package/dist/types/contexts.d.ts.map +1 -1
  118. package/dist/types/hooks/useFormInputValidationHandler.d.ts.map +1 -1
  119. package/dist/types/tsconfig.tsbuildinfo +1 -1
  120. package/dist/types/types/index.d.ts +12 -0
  121. package/dist/types/types/index.d.ts.map +1 -1
  122. package/package.json +7 -6
  123. package/src/components/FormCheckbox.tsx +17 -6
  124. package/src/components/FormError.tsx +6 -6
  125. package/src/components/FormFieldScope.tsx +17 -12
  126. package/src/components/FormFieldStateProvider.tsx +71 -0
  127. package/src/components/FormHasManyRelationScope.tsx +22 -14
  128. package/src/components/FormHasOneRelationScope.tsx +17 -13
  129. package/src/components/FormInput.tsx +12 -5
  130. package/src/components/FormLabel.tsx +8 -6
  131. package/src/components/FormRadioInput.tsx +22 -15
  132. package/src/components/index.ts +1 -0
  133. package/src/contexts.tsx +18 -0
  134. package/src/hooks/useFormInputValidationHandler.tsx +13 -3
  135. package/src/tsconfig.json +5 -2
  136. package/src/types/index.ts +13 -0
  137. package/src/contexts.ts +0 -12
@@ -1,14 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const reactUtils = require("@contember/react-utils");
4
- const FormFieldIdContext_ = reactUtils.createContext("FormFieldIdContext", void 0);
5
- const FormFieldIdContext = FormFieldIdContext_[0];
6
- const useFormFieldId = FormFieldIdContext_[1];
7
- const FormErrorContext_ = reactUtils.createContext("FormErrorContext", void 0);
8
- const FormErrorContext = FormErrorContext_[0];
9
- const useFormError = FormErrorContext_[1];
10
- exports.FormErrorContext = FormErrorContext;
11
- exports.FormFieldIdContext = FormFieldIdContext;
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;
12
10
  exports.useFormError = useFormError;
13
11
  exports.useFormFieldId = useFormFieldId;
12
+ exports.useFormFieldState = useFormFieldState;
14
13
  //# sourceMappingURL=contexts.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"contexts.cjs","sources":["../../../../packages/react-form/src/contexts.ts"],"sourcesContent":["import { createContext } from '@contember/react-utils'\nimport { ErrorAccessor } from '@contember/react-binding'\n\nconst FormFieldIdContext_ = createContext<string | undefined>('FormFieldIdContext', undefined)\n/** @internal */\nexport const FormFieldIdContext = FormFieldIdContext_[0]\nexport const useFormFieldId = FormFieldIdContext_[1]\n\nconst FormErrorContext_ = createContext<ErrorAccessor.Error[] | undefined>('FormErrorContext', undefined)\n/** @internal */\nexport const FormErrorContext = FormErrorContext_[0]\nexport const useFormError = FormErrorContext_[1]\n"],"names":["createContext"],"mappings":";;;AAGA,MAAM,sBAAsBA,WAAkC,cAAA,sBAAsB,MAAS;AAEhF,MAAA,qBAAqB,oBAAoB,CAAC;AAC1C,MAAA,iBAAiB,oBAAoB,CAAC;AAEnD,MAAM,oBAAoBA,WAAiD,cAAA,oBAAoB,MAAS;AAE3F,MAAA,mBAAmB,kBAAkB,CAAC;AACtC,MAAA,eAAe,kBAAkB,CAAC;;;;;"}
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,14 +1,13 @@
1
1
  import { createContext } from "@contember/react-utils";
2
- const FormFieldIdContext_ = createContext("FormFieldIdContext", void 0);
3
- const FormFieldIdContext = FormFieldIdContext_[0];
4
- const useFormFieldId = FormFieldIdContext_[1];
5
- const FormErrorContext_ = createContext("FormErrorContext", void 0);
6
- const FormErrorContext = FormErrorContext_[0];
7
- const useFormError = FormErrorContext_[1];
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;
8
7
  export {
9
- FormErrorContext,
10
- FormFieldIdContext,
8
+ FormFieldStateContext,
11
9
  useFormError,
12
- useFormFieldId
10
+ useFormFieldId,
11
+ useFormFieldState
13
12
  };
14
13
  //# sourceMappingURL=contexts.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"contexts.js","sources":["../../../../packages/react-form/src/contexts.ts"],"sourcesContent":["import { createContext } from '@contember/react-utils'\nimport { ErrorAccessor } from '@contember/react-binding'\n\nconst FormFieldIdContext_ = createContext<string | undefined>('FormFieldIdContext', undefined)\n/** @internal */\nexport const FormFieldIdContext = FormFieldIdContext_[0]\nexport const useFormFieldId = FormFieldIdContext_[1]\n\nconst FormErrorContext_ = createContext<ErrorAccessor.Error[] | undefined>('FormErrorContext', undefined)\n/** @internal */\nexport const FormErrorContext = FormErrorContext_[0]\nexport const useFormError = FormErrorContext_[1]\n"],"names":[],"mappings":";AAGA,MAAM,sBAAsB,cAAkC,sBAAsB,MAAS;AAEhF,MAAA,qBAAqB,oBAAoB,CAAC;AAC1C,MAAA,iBAAiB,oBAAoB,CAAC;AAEnD,MAAM,oBAAoB,cAAiD,oBAAoB,MAAS;AAE3F,MAAA,mBAAmB,kBAAkB,CAAC;AACtC,MAAA,eAAe,kBAAkB,CAAC;"}
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
- accessorGetter().clearErrors();
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
- accessorGetter().clearErrors();
53
- accessorGetter().addError(message);
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
@@ -1 +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\t// if there is no message, we want to clear the error\n\t\tif (!message) {\n\t\t\taccessorGetter().clearErrors()\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\taccessorGetter().clearErrors()\n\t\t\taccessorGetter().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"],"names":["useRef","React","useEntityBeforePersist","useCallback","useEffect"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAIa,MAAA,gCAAgC,CAAC,UAA8B;AAC3E,QAAM,iBAAiB,MAAM;AAC7B,QAAM,oBAAoBA,MAAAA;AAC1B,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,IACpD;AAAA,EAAA,GACE,CAAC,cAAc,CAAC,CAAC;AAEpBC,QAAAA,UAAU,MAAM;AACX,QAAA,CAAC,SAAS,SAAS;AACtB;AAAA,IACD;AAEA,UAAM,QAAQ,SAAS;AACjB,UAAA,QAAQ,MAAM,UAAU;AAExB,UAAA,UAAU,QAAQ,SAAY,OAAO;AAC3C,UAAM,kBAAkB,kBAAkB;AAC1C,sBAAkB,UAAU;AAG5B,QAAI,CAAC,SAAS;AACb,qBAAA,EAAiB;AACjB;AAAA,IACD;AACA,QAAI,YAAY,iBAAiB;AAChC;AAAA,IACD;AAEI,QAAA,CAAC,MAAM,WAAW;AACrB;AAAA,IACD;AAIA,QAAI,CAAC,SAAS,CAAC,CAAC,iBAAiB;AAChC,qBAAA,EAAiB;AACF,qBAAA,EAAE,SAAS,OAAO;AAAA,IAClC;AAAA,EAAA,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;AACjB,UAAI,kBAAkB,SAAS;AACf,yBAAE,SAAS,kBAAkB,OAAO;AAAA,MACpD;AAAA,IAAA,GACE,CAAC,cAAc,CAAC;AAAA,EAAA;AAErB;;"}
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,IAAI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,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
- accessorGetter().clearErrors();
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
- accessorGetter().clearErrors();
35
- accessorGetter().addError(message);
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
  };
@@ -1 +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\t// if there is no message, we want to clear the error\n\t\tif (!message) {\n\t\t\taccessorGetter().clearErrors()\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\taccessorGetter().clearErrors()\n\t\t\taccessorGetter().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"],"names":[],"mappings":";;;AAIa,MAAA,gCAAgC,CAAC,UAA8B;AAC3E,QAAM,iBAAiB,MAAM;AAC7B,QAAM,oBAAoB;AAC1B,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,IACpD;AAAA,EAAA,GACE,CAAC,cAAc,CAAC,CAAC;AAEpB,YAAU,MAAM;AACX,QAAA,CAAC,SAAS,SAAS;AACtB;AAAA,IACD;AAEA,UAAM,QAAQ,SAAS;AACjB,UAAA,QAAQ,MAAM,UAAU;AAExB,UAAA,UAAU,QAAQ,SAAY,OAAO;AAC3C,UAAM,kBAAkB,kBAAkB;AAC1C,sBAAkB,UAAU;AAG5B,QAAI,CAAC,SAAS;AACb,qBAAA,EAAiB;AACjB;AAAA,IACD;AACA,QAAI,YAAY,iBAAiB;AAChC;AAAA,IACD;AAEI,QAAA,CAAC,MAAM,WAAW;AACrB;AAAA,IACD;AAIA,QAAI,CAAC,SAAS,CAAC,CAAC,iBAAiB;AAChC,qBAAA,EAAiB;AACF,qBAAA,EAAE,SAAS,OAAO;AAAA,IAClC;AAAA,EAAA,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;AACjB,UAAI,kBAAkB,SAAS;AACf,yBAAE,SAAS,kBAAkB,OAAO;AAAA,MACpD;AAAA,IAAA,GACE,CAAC,cAAc,CAAC;AAAA,EAAA;AAErB;"}
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;"}
@@ -1 +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":";;;AAIO,MAAM,sBAAsB,CAAC,OAAsB,EAAE,aAAa,iBAA8D;AACtI,SAAOA,cAAQ,MAAM;AACpB,UAAM,SAAS,MAAM;AACrB,UAAM,aAAa,OAAO;AACpB,UAAA,iBAA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MAAM,IAAI,KAAK,QAAA,CAAS,IACxB,MAAM,IAAI,KAAK,SAAS,CAAC,IACzB,MAAM,IAAI,KAAK,WAAA,CAAY,IAC3B,MAAM,IAAI,KAAK,YAAY;AAC7B;;"}
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\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,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 +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":";AAIO,MAAM,sBAAsB,CAAC,OAAsB,EAAE,aAAa,iBAA8D;AACtI,SAAO,QAAQ,MAAM;AACpB,UAAM,SAAS,MAAM;AACrB,UAAM,aAAa,OAAO;AACpB,UAAA,iBAAiB,2BAA2B,UAAmC,KAAK;AAC1F,UAAM,UAAU,eAAe,QAAQ,MAAM,WAAW;AACjD,WAAA;AAAA,MACN,mBAAmB,QAAQ;AAAA,MAC3B,aAAa,eAAe,QAAQ;AAAA,MACpC,YAAY,cAAc,QAAQ;AAAA,IAAA;AAAA,EACnC,GACE,CAAC,MAAM,aAAa,MAAM,QAAQ,aAAa,UAAU,CAAC;AAC9D;AAIA,MAAM,wBAAkD,CAAC,QAAQ,WAAW;AAAA,EAC3E,YAAY,CAAC,UAAkB;AAC9B,QAAI,UAAU,MAAM,OAAO,YAAY,MAAM,EAAE,kBAAkB,MAAM;AAC/D,aAAA;AAAA,IACR;AACO,WAAA;AAAA,EACR;AAAA,EACA,aAAa,CAAC,UAAyB,SAAS;AACjD;AAEA,MAAM,6BAAkG;AAAA,EACvG,QAAQ;AAAA,EACR,SAAS,OAAO;AAAA,IACf,YAAY,CAAC,UAAkB;AAC9B,UAAI,UAAU,IA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M,KAAK;AACxB,aAAA,MAAM,MAAM,IAAI,OAAQ,IAAI,KAAK,MAAM,EAAG;IAClD;AAAA,IACA,aAAa,CAAC,UAAyB;AACtC,YAAM,SAAS,QAAQ,KAAK,MAAM,KAAK,IAAI;AACpC,aAAA,CAAC,UAAU,MAAM,MAAM,IAAI,KAAK,YAAY,IAAI,KAAK,MAAM,CAAC;AAAA,IACpE;AAAA,IACA,mBAAmB;AAAA,MAClB,MAAM;AAAA,IACP;AAAA,EAAA;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;AACT,WAAA,IAAI,OAAO,KAAK,IAAI,GAAG,SAAS,IAAI,MAAM,CAAC,IAAI;AAAA,EAAA;AAGvD,SAAO,IAAI,KAAK,YAAY,GAAG,CAAC,IAC/B,MAAM,IAAI,KAAK,SAAa,IAAA,CAAC,IAC7B,MAAM,IAAI,KAAK,QAAA,CAAS,IACxB,MAAM,IAAI,KAAK,SAAS,CAAC,IACzB,MAAM,IAAI,KAAK,WAAA,CAAY,IAC3B,MAAM,IAAI,KAAK,YAAY;AAC7B;"}
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":";AAIO,MAAM,sBAAsB,CAAC,OAAsB,EAAE,aAAa,iBAA8D;AACtI,SAAO,QAAQ,MAAM;AACpB,UAAM,SAAS,MAAM;AACrB,UAAM,aAAa,OAAO;AACpB,UAAA,iBAAiB,2BAA2B,UAAmC,KAAK;AAC1F,UAAM,UAAU,eAAe,QAAQ,MAAM,WAAW;AACjD,WAAA;AAAA,MACN,mBAAmB,QAAQ;AAAA,MAC3B,aAAa,eAAe,QAAQ;AAAA,MACpC,YAAY,cAAc,QAAQ;AAAA,IACnC;AAAA,EAAA,GACE,CAAC,MAAM,aAAa,MAAM,QAAQ,aAAa,UAAU,CAAC;AAC9D;AAIA,MAAM,wBAAkD,CAAC,QAAQ,WAAW;AAAA,EA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,CAAC,UAAU,MAAM,MAAM,IAAI,KAAK,IAAI,KAAK,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,EAAE,CAAC;AAAA,IACnF;AAAA,IACA,mBAAmB;AAAA,MAClB,MAAM;AAAA,IAAA;AAAA,EACP;AAAA,EAED,UAAU,OAAO;AAAA,IAChB,YAAY,CAAC,UAAkB;AAC9B,UAAI,UAAU,IAAI;AACV,eAAA;AAAA,MAAA;AAGF,YAAA,SAAS,KAAK,MAAM,KAAK;AACxB,aAAA,MAAM,MAAM,IAAI,OAAQ,IAAI,KAAK,MAAM,EAAG,YAAY;AAAA,IAC9D;AAAA,IACA,aAAa,CAAC,UAAyB;AACtC,YAAM,SAAS,QAAQ,KAAK,MAAM,KAAK,IAAI;AACpC,aAAA,CAAC,UAAU,MAAM,MAAM,IAAI,KAAK,YAAY,IAAI,KAAK,MAAM,CAAC;AAAA,IACpE;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;"}
@@ -4,19 +4,23 @@ const contexts = require("./src/contexts.cjs");
4
4
  const FormError = require("./src/components/FormError.cjs");
5
5
  const FormCheckbox = require("./src/components/FormCheckbox.cjs");
6
6
  const FormFieldScope = require("./src/components/FormFieldScope.cjs");
7
+ const FormFieldStateProvider = require("./src/components/FormFieldStateProvider.cjs");
7
8
  const FormHasOneRelationScope = require("./src/components/FormHasOneRelationScope.cjs");
8
9
  const FormHasManyRelationScope = require("./src/components/FormHasManyRelationScope.cjs");
9
10
  const FormInput = require("./src/components/FormInput.cjs");
10
11
  const FormLabel = require("./src/components/FormLabel.cjs");
11
12
  const FormRadioInput = require("./src/components/FormRadioInput.cjs");
12
13
  const useFormInputValidationHandler = require("./src/hooks/useFormInputValidationHandler.cjs");
13
- exports.FormErrorContext = contexts.FormErrorContext;
14
- exports.FormFieldIdContext = contexts.FormFieldIdContext;
14
+ exports.FormFieldStateContext = contexts.FormFieldStateContext;
15
15
  exports.useFormError = contexts.useFormError;
16
16
  exports.useFormFieldId = contexts.useFormFieldId;
17
+ exports.useFormFieldState = contexts.useFormFieldState;
17
18
  exports.FormError = FormError.FormError;
18
19
  exports.FormCheckbox = FormCheckbox.FormCheckbox;
19
20
  exports.FormFieldScope = FormFieldScope.FormFieldScope;
21
+ exports.FormErrorContext = FormFieldStateProvider.FormErrorContext;
22
+ exports.FormFieldIdContext = FormFieldStateProvider.FormFieldIdContext;
23
+ exports.FormFieldStateProvider = FormFieldStateProvider.FormFieldStateProvider;
20
24
  exports.FormHasOneRelationScope = FormHasOneRelationScope.FormHasOneRelationScope;
21
25
  exports.FormHasManyRelationScope = FormHasManyRelationScope.FormHasManyRelationScope;
22
26
  exports.FormInput = FormInput.FormInput;
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,7 +1,8 @@
1
- import { FormErrorContext, FormFieldIdContext, useFormError, useFormFieldId } from "./src/contexts.js";
1
+ import { FormFieldStateContext, useFormError, useFormFieldId, useFormFieldState } from "./src/contexts.js";
2
2
  import { FormError } from "./src/components/FormError.js";
3
3
  import { FormCheckbox } from "./src/components/FormCheckbox.js";
4
4
  import { FormFieldScope } from "./src/components/FormFieldScope.js";
5
+ import { FormErrorContext, FormFieldIdContext, FormFieldStateProvider } from "./src/components/FormFieldStateProvider.js";
5
6
  import { FormHasOneRelationScope } from "./src/components/FormHasOneRelationScope.js";
6
7
  import { FormHasManyRelationScope } from "./src/components/FormHasManyRelationScope.js";
7
8
  import { FormInput } from "./src/components/FormInput.js";
@@ -14,6 +15,8 @@ export {
14
15
  FormErrorContext,
15
16
  FormFieldIdContext,
16
17
  FormFieldScope,
18
+ FormFieldStateContext,
19
+ FormFieldStateProvider,
17
20
  FormHasManyRelationScope,
18
21
  FormHasOneRelationScope,
19
22
  FormInput,
@@ -21,6 +24,7 @@ export {
21
24
  FormRadioInput,
22
25
  useFormError,
23
26
  useFormFieldId,
27
+ useFormFieldState,
24
28
  useFormInputValidationHandler
25
29
  };
26
30
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;"}
@@ -26,10 +26,12 @@ function _interopNamespaceDefault(e) {
26
26
  const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
27
27
  const SlotInput = reactSlot.Slot;
28
28
  const FormCheckbox = reactBinding.Component(({ field, isNonbearing, defaultValue, ...props }) => {
29
- const id = contexts.useFormFieldId();
30
29
  const accessor = reactBinding.useField(field);
30
+ const formState = contexts.useFormFieldState();
31
+ const id = formState?.htmlId;
32
+ const hasErrors = (formState?.errors.length ?? accessor.errors?.errors?.length ?? 0) > 0;
33
+ const dirty = formState?.dirty ?? accessor.hasUnpersistedChanges;
31
34
  const value = accessor.value;
32
- const errors = contexts.useFormError() ?? accessor.errors?.errors ?? [];
33
35
  const accessorGetter = accessor.getAccessor;
34
36
  const { ref, onFocus, onBlur } = useFormInputValidationHandler.useFormInputValidationHandler(accessor);
35
37
  const [checkboxRef, setCheckboxRef] = React__namespace.useState(null);
@@ -49,8 +51,9 @@ const FormCheckbox = reactBinding.Component(({ field, isNonbearing, defaultValue
49
51
  type: "checkbox",
50
52
  checked: accessor.value === true,
51
53
  "data-state": value === null ? "indeterminate" : value ? "checked" : "unchecked",
52
- "data-invalid": utilities.dataAttribute(errors.length > 0),
53
- id: `${id}-input`,
54
+ "data-invalid": utilities.dataAttribute(hasErrors),
55
+ "data-dirty": utilities.dataAttribute(dirty),
56
+ id: id ? `${id}-input` : void 0,
54
57
  onFocus,
55
58
  onBlur,
56
59
  onChange: React.useCallback((e) => {
@@ -1 +1 @@
1
- {"version":3,"file":"FormCheckbox.cjs","sources":["../../../../../packages/react-form/src/components/FormCheckbox.tsx"],"sourcesContent":["import * as React from 'react'\nimport { ChangeEventHandler, ReactElement, useCallback, useEffect } from 'react'\nimport { Component, Field, OptionallyVariableFieldValue, SugaredRelativeSingleField, TreeNodeEnvironmentFactory, useField } from '@contember/react-binding'\nimport { useFormError, useFormFieldId } from '../contexts'\nimport { dataAttribute } from '@contember/utilities'\nimport { useFormInputValidationHandler } from '../hooks'\nimport { Slot } from '@radix-ui/react-slot'\n\nconst SlotInput = Slot as React.ForwardRefExoticComponent<React.RefAttributes<HTMLInputElement> & React.InputHTMLAttributes<HTMLInputElement>>\n\nexport interface FormCheckboxProps {\n\tfield: SugaredRelativeSingleField['field']\n\tisNonbearing?: boolean\n\tdefaultValue?: OptionallyVariableFieldValue\n\tchildren: ReactElement<HTMLInputElement, 'input'>\n}\n\nexport const FormCheckbox = Component<FormCheckboxProps>(({ field, isNonbearing, defaultValue, ...props }) => {\n\tconst id = useFormFieldId()\n\tconst accessor = useField<boolean>(field)\n\tconst value = accessor.value\n\tconst errors = useFormError() ?? accessor.errors?.errors ?? []\n\tconst accessorGetter = accessor.getAccessor\n\tconst { ref, onFocus, onBlur } = useFormInputValidationHandler(accessor)\n\tconst [checkboxRef, setCheckboxRef] = React.useState<HTMLInputElement | null>(null)\n\n\tuseEffect(() => {\n\t\tif (!checkboxRef) {\n\t\t\treturn\n\t\t}\n\t\tcheckboxRef.indeterminate = value === null\n\t}, [checkboxRef, ref, value])\n\n\treturn (\n\t\t<SlotInput\n\t\t\tref={it => {\n\t\t\t\t(ref as any).current = checkboxRef\n\t\t\t\tsetCheckboxRef(it)\n\t\t\t}}\n\t\t\ttype=\"checkbox\"\n\t\t\tchecked={accessor.value === true}\n\t\t\tdata-state={value === null ? 'indeterminate' : (value ? 'checked' : 'unchecked')}\n\t\t\tdata-invalid={dataAttribute(errors.length > 0)}\n\t\t\tid={`${id}-input`}\n\t\t\tonFocus={onFocus}\n\t\t\tonBlur={onBlur}\n\t\t\tonChange={useCallback<ChangeEventHandler<HTMLInputElement>>(e => {\n\t\t\t\taccessorGetter().updateValue(e.target.checked)\n\t\t\t}, [accessorGetter])}\n\t\t\t{...props}\n\t\t/>\n\t)\n}, ({ field, isNonbearing, defaultValue }, env) => {\n\tconst node = TreeNodeEnvironmentFactory.createEnvironmentForField(env, { field }).getSubTreeNode()\n\tconst resolvedDefaultValue = defaultValue !== undefined ? defaultValue : (node.field.defaultValue ?? (node.field.nullable ? null : false))\n\n\treturn <Field field={field} isNonbearing={isNonbearing} defaultValue={resolvedDefaultValue} />\n})\n"],"names":["Slot","Component","useFormFieldId","useField","useFormError","useFormInputValidationHandler","React","useEffect","jsx","dataAttribute","useCallback","TreeNodeEnvironmentFactory","Field"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,MAAM,YAAYA,UAAAA;AASL,MAAA,eAAeC,uBAA6B,CAAC,EAAE,OAAO,cAAc,cAAc,GAAG,YAAY;AAC7G,QAAM,KAAKC,SAAAA;AACL,QAAA,WAAWC,sBAAkB,KAAK;AACxC,QAAM,QAAQ,SAAS;AACvB,QAAM,SAASC,SAAAA,aAAa,KAAK,SAAS,QAAQ,UAAU;AAC5D,QAAM,iBAAiB,SAAS;AAChC,QAAM,EAAE,KAAK,SAAS,OAAO,IAAIC,8BAAAA,8BAA8B,QAAQ;AACvE,QAAM,CAAC,aAAa,cAAc,IAAIC,iBAAM,SAAkC,IAAI;AAElFC,QAAAA,UAAU,MAAM;AACf,QAAI,CAAC,aAAa;AACjB;AAAA,IACD;AACA,gBAAY,gBAAgB,UAAU;AAAA,EACpC,GAAA,CAAC,aAAa,KAAK,KAAK,CAAC;AAG3B,SAAAC,2BAAA;AAAA,IAAC;AAAA,IAAA;AAAA,MACA,KAAK,CAAM,OAAA;AACT,YAAY,UAAU;AACvB,uBAAe,EAAE;AAAA,MAClB;AAAA,MACA,MAAK;AAAA,MACL,SAAS,SAAS,UAAU;AAAA,MAC5B,cAAY,UAAU,OAAO,kBAAmB,QAAQ,YAAY;AAAA,MACpE,gBAAcC,UAAA,cAAc,OAAO,SAAS,CAAC;AAAA,MAC7C,IAAI,GAAG,EAAE;AAAA,MACT;AAAA,MACA;AAAA,MACA,UAAUC,MAAAA,YAAkD,CAAK,MAAA;AAChE,uBAAiB,EAAA,YAAY,EAAE,OAAO,OAAO;AAAA,MAAA,GAC3C,CAAC,cAAc,CAAC;AAAA,MAClB,GAAG;AAAA,IAAA;AAAA,EAAA;AAGP,GAAG,CAAC,EAAE,OAAO,cAAc,aAAA,GAAgB,QAAQ;AAC5C,QAAA,OAAOC,wCAA2B,0BAA0B,KAAK,EAAE,MAAO,CAAA,EAAE;AAC5E,QAAA,uBAAuB,iBAAiB,SAAY,eAAgB,KAAK,MAAM,iBAAiB,KAAK,MAAM,WAAW,OAAO;AAEnI,SAAQH,2BAAAA,IAAAI,aAAAA,OAAA,EAAM,OAAc,cAA4B,cAAc,qBAAsB,CAAA;AAC7F,CAAC;;"}
1
+ {"version":3,"file":"FormCheckbox.cjs","sources":["../../../../../packages/react-form/src/components/FormCheckbox.tsx"],"sourcesContent":["import * as React from 'react'\nimport { ChangeEventHandler, ReactElement, useCallback, useEffect } from 'react'\nimport {\n\tComponent,\n\tField,\n\tOptionallyVariableFieldValue,\n\tSugaredRelativeSingleField,\n\tTreeNodeEnvironmentFactory,\n\tuseField,\n} from '@contember/react-binding'\nimport { useFormFieldState } from '../contexts'\nimport { dataAttribute } from '@contember/utilities'\nimport { useFormInputValidationHandler } from '../hooks'\nimport { Slot } from '@radix-ui/react-slot'\n\nconst SlotInput = Slot as React.ForwardRefExoticComponent<React.RefAttributes<HTMLInputElement> & React.InputHTMLAttributes<HTMLInputElement>>\n\nexport interface FormCheckboxProps {\n\tfield: SugaredRelativeSingleField['field']\n\tisNonbearing?: boolean\n\tdefaultValue?: OptionallyVariableFieldValue\n\tchildren: ReactElement<HTMLInputElement, 'input'>\n}\n\nexport const FormCheckbox = Component<FormCheckboxProps>(({ field, isNonbearing, defaultValue, ...props }) => {\n\tconst accessor = useField<boolean>(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 value = accessor.value\n\tconst accessorGetter = accessor.getAccessor\n\tconst { ref, onFocus, onBlur } = useFormInputValidationHandler(accessor)\n\tconst [checkboxRef, setCheckboxRef] = React.useState<HTMLInputElement | null>(null)\n\n\tuseEffect(() => {\n\t\tif (!checkboxRef) {\n\t\t\treturn\n\t\t}\n\t\tcheckboxRef.indeterminate = value === null\n\t}, [checkboxRef, ref, value])\n\n\treturn (\n\t\t<SlotInput\n\t\t\tref={it => {\n\t\t\t\t(ref as any).current = checkboxRef\n\t\t\t\tsetCheckboxRef(it)\n\t\t\t}}\n\t\t\ttype=\"checkbox\"\n\t\t\tchecked={accessor.value === true}\n\t\t\tdata-state={value === null ? 'indeterminate' : (value ? 'checked' : 'unchecked')}\n\t\t\tdata-invalid={dataAttribute(hasErrors)}\n\t\t\tdata-dirty={dataAttribute(dirty)}\n\t\t\tid={id ? `${id}-input` : undefined}\n\t\t\tonFocus={onFocus}\n\t\t\tonBlur={onBlur}\n\t\t\tonChange={useCallback<ChangeEventHandler<HTMLInputElement>>(e => {\n\t\t\t\taccessorGetter().updateValue(e.target.checked)\n\t\t\t}, [accessorGetter])}\n\t\t\t{...props}\n\t\t/>\n\t)\n}, ({ field, isNonbearing, defaultValue }, env) => {\n\tconst node = TreeNodeEnvironmentFactory.createEnvironmentForField(env, { field }).getSubTreeNode()\n\tconst resolvedDefaultValue = defaultValue !== undefined ? defaultValue : (node.field.defaultValue ?? (node.field.nullable ? null : false))\n\n\treturn <Field field={field} isNonbearing={isNonbearing} defaultValue={resolvedDefaultValue} />\n})\n"],"names":["Slot","Component","useField","useFormFieldState","useFormInputValidationHandler","React","useEffect","jsx","dataAttribute","useCallback","TreeNodeEnvironmentFactory","Field"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAeA,MAAM,YAAYA,UAAA;AASL,MAAA,eAAeC,uBAA6B,CAAC,EAAE,OAAO,cAAc,cAAc,GAAG,YAAY;AACvG,QAAA,WAAWC,sBAAkB,KAAK;AAExC,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,QAAQ,SAAS;AACvB,QAAM,iBAAiB,SAAS;AAChC,QAAM,EAAE,KAAK,SAAS,OAAO,IAAIC,8BAAAA,8BAA8B,QAAQ;AACvE,QAAM,CAAC,aAAa,cAAc,IAAIC,iBAAM,SAAkC,IAAI;AAElFC,QAAAA,UAAU,MAAM;AACf,QAAI,CAAC,aAAa;AACjB;AAAA,IAAA;AAED,gBAAY,gBAAgB,UAAU;AAAA,EACpC,GAAA,CAAC,aAAa,KAAK,KAAK,CAAC;AAG3B,SAAAC,2BAAA;AAAA,IAAC;AAAA,IAAA;AAAA,MACA,KAAK,CAAM,OAAA;AACT,YAAY,UAAU;AACvB,uBAAe,EAAE;AAAA,MAClB;AAAA,MACA,MAAK;AAAA,MACL,SAAS,SAAS,UAAU;AAAA,MAC5B,cAAY,UAAU,OAAO,kBAAmB,QAAQ,YAAY;AAAA,MACpE,gBAAcC,wBAAc,SAAS;AAAA,MACrC,cAAYA,wBAAc,KAAK;AAAA,MAC/B,IAAI,KAAK,GAAG,EAAE,WAAW;AAAA,MACzB;AAAA,MACA;AAAA,MACA,UAAUC,MAAAA,YAAkD,CAAK,MAAA;AAChE,uBAAiB,EAAA,YAAY,EAAE,OAAO,OAAO;AAAA,MAAA,GAC3C,CAAC,cAAc,CAAC;AAAA,MAClB,GAAG;AAAA,IAAA;AAAA,EACL;AAEF,GAAG,CAAC,EAAE,OAAO,cAAc,aAAA,GAAgB,QAAQ;AAC5C,QAAA,OAAOC,aAAAA,2BAA2B,0BAA0B,KAAK,EAAE,MAAM,CAAC,EAAE,eAAe;AAC3F,QAAA,uBAAuB,iBAAiB,SAAY,eAAgB,KAAK,MAAM,iBAAiB,KAAK,MAAM,WAAW,OAAO;AAEnI,SAAQH,2BAAAA,IAAAI,aAAAA,OAAA,EAAM,OAAc,cAA4B,cAAc,sBAAsB;AAC7F,CAAC;;"}
@@ -2,16 +2,18 @@ import { jsx } from "react/jsx-runtime";
2
2
  import * as React from "react";
3
3
  import { useEffect, useCallback } from "react";
4
4
  import { Component, useField, TreeNodeEnvironmentFactory, Field } from "@contember/react-binding";
5
- import { useFormFieldId, useFormError } from "../contexts.js";
5
+ import { useFormFieldState } from "../contexts.js";
6
6
  import { dataAttribute } from "@contember/utilities";
7
7
  import { Slot } from "@radix-ui/react-slot";
8
8
  import { useFormInputValidationHandler } from "../hooks/useFormInputValidationHandler.js";
9
9
  const SlotInput = Slot;
10
10
  const FormCheckbox = Component(({ field, isNonbearing, defaultValue, ...props }) => {
11
- const id = useFormFieldId();
12
11
  const accessor = useField(field);
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;
13
16
  const value = accessor.value;
14
- const errors = useFormError() ?? accessor.errors?.errors ?? [];
15
17
  const accessorGetter = accessor.getAccessor;
16
18
  const { ref, onFocus, onBlur } = useFormInputValidationHandler(accessor);
17
19
  const [checkboxRef, setCheckboxRef] = React.useState(null);
@@ -31,8 +33,9 @@ const FormCheckbox = Component(({ field, isNonbearing, defaultValue, ...props })
31
33
  type: "checkbox",
32
34
  checked: accessor.value === true,
33
35
  "data-state": value === null ? "indeterminate" : value ? "checked" : "unchecked",
34
- "data-invalid": dataAttribute(errors.length > 0),
35
- id: `${id}-input`,
36
+ "data-invalid": dataAttribute(hasErrors),
37
+ "data-dirty": dataAttribute(dirty),
38
+ id: id ? `${id}-input` : void 0,
36
39
  onFocus,
37
40
  onBlur,
38
41
  onChange: useCallback((e) => {
@@ -1 +1 @@
1
- {"version":3,"file":"FormCheckbox.js","sources":["../../../../../packages/react-form/src/components/FormCheckbox.tsx"],"sourcesContent":["import * as React from 'react'\nimport { ChangeEventHandler, ReactElement, useCallback, useEffect } from 'react'\nimport { Component, Field, OptionallyVariableFieldValue, SugaredRelativeSingleField, TreeNodeEnvironmentFactory, useField } from '@contember/react-binding'\nimport { useFormError, useFormFieldId } from '../contexts'\nimport { dataAttribute } from '@contember/utilities'\nimport { useFormInputValidationHandler } from '../hooks'\nimport { Slot } from '@radix-ui/react-slot'\n\nconst SlotInput = Slot as React.ForwardRefExoticComponent<React.RefAttributes<HTMLInputElement> & React.InputHTMLAttributes<HTMLInputElement>>\n\nexport interface FormCheckboxProps {\n\tfield: SugaredRelativeSingleField['field']\n\tisNonbearing?: boolean\n\tdefaultValue?: OptionallyVariableFieldValue\n\tchildren: ReactElement<HTMLInputElement, 'input'>\n}\n\nexport const FormCheckbox = Component<FormCheckboxProps>(({ field, isNonbearing, defaultValue, ...props }) => {\n\tconst id = useFormFieldId()\n\tconst accessor = useField<boolean>(field)\n\tconst value = accessor.value\n\tconst errors = useFormError() ?? accessor.errors?.errors ?? []\n\tconst accessorGetter = accessor.getAccessor\n\tconst { ref, onFocus, onBlur } = useFormInputValidationHandler(accessor)\n\tconst [checkboxRef, setCheckboxRef] = React.useState<HTMLInputElement | null>(null)\n\n\tuseEffect(() => {\n\t\tif (!checkboxRef) {\n\t\t\treturn\n\t\t}\n\t\tcheckboxRef.indeterminate = value === null\n\t}, [checkboxRef, ref, value])\n\n\treturn (\n\t\t<SlotInput\n\t\t\tref={it => {\n\t\t\t\t(ref as any).current = checkboxRef\n\t\t\t\tsetCheckboxRef(it)\n\t\t\t}}\n\t\t\ttype=\"checkbox\"\n\t\t\tchecked={accessor.value === true}\n\t\t\tdata-state={value === null ? 'indeterminate' : (value ? 'checked' : 'unchecked')}\n\t\t\tdata-invalid={dataAttribute(errors.length > 0)}\n\t\t\tid={`${id}-input`}\n\t\t\tonFocus={onFocus}\n\t\t\tonBlur={onBlur}\n\t\t\tonChange={useCallback<ChangeEventHandler<HTMLInputElement>>(e => {\n\t\t\t\taccessorGetter().updateValue(e.target.checked)\n\t\t\t}, [accessorGetter])}\n\t\t\t{...props}\n\t\t/>\n\t)\n}, ({ field, isNonbearing, defaultValue }, env) => {\n\tconst node = TreeNodeEnvironmentFactory.createEnvironmentForField(env, { field }).getSubTreeNode()\n\tconst resolvedDefaultValue = defaultValue !== undefined ? defaultValue : (node.field.defaultValue ?? (node.field.nullable ? null : false))\n\n\treturn <Field field={field} isNonbearing={isNonbearing} defaultValue={resolvedDefaultValue} />\n})\n"],"names":[],"mappings":";;;;;;;;AAQA,MAAM,YAAY;AASL,MAAA,eAAe,UAA6B,CAAC,EAAE,OAAO,cAAc,cAAc,GAAG,YAAY;AAC7G,QAAM,KAAK;AACL,QAAA,WAAW,SAAkB,KAAK;AACxC,QAAM,QAAQ,SAAS;AACvB,QAAM,SAAS,aAAa,KAAK,SAAS,QAAQ,UAAU;AAC5D,QAAM,iBAAiB,SAAS;AAChC,QAAM,EAAE,KAAK,SAAS,OAAO,IAAI,8BAA8B,QAAQ;AACvE,QAAM,CAAC,aAAa,cAAc,IAAI,MAAM,SAAkC,IAAI;AAElF,YAAU,MAAM;AACf,QAAI,CAAC,aAAa;AACjB;AAAA,IACD;AACA,gBAAY,gBAAgB,UAAU;AAAA,EACpC,GAAA,CAAC,aAAa,KAAK,KAAK,CAAC;AAG3B,SAAA;AAAA,IAAC;AAAA,IAAA;AAAA,MACA,KAAK,CAAM,OAAA;AACT,YAAY,UAAU;AACvB,uBAAe,EAAE;AAAA,MAClB;AAAA,MACA,MAAK;AAAA,MACL,SAAS,SAAS,UAAU;AAAA,MAC5B,cAAY,UAAU,OAAO,kBAAmB,QAAQ,YAAY;AAAA,MACpE,gBAAc,cAAc,OAAO,SAAS,CAAC;AAAA,MAC7C,IAAI,GAAG,EAAE;AAAA,MACT;AAAA,MACA;AAAA,MACA,UAAU,YAAkD,CAAK,MAAA;AAChE,uBAAiB,EAAA,YAAY,EAAE,OAAO,OAAO;AAAA,MAAA,GAC3C,CAAC,cAAc,CAAC;AAAA,MAClB,GAAG;AAAA,IAAA;AAAA,EAAA;AAGP,GAAG,CAAC,EAAE,OAAO,cAAc,aAAA,GAAgB,QAAQ;AAC5C,QAAA,OAAO,2BAA2B,0BAA0B,KAAK,EAAE,MAAO,CAAA,EAAE;AAC5E,QAAA,uBAAuB,iBAAiB,SAAY,eAAgB,KAAK,MAAM,iBAAiB,KAAK,MAAM,WAAW,OAAO;AAEnI,SAAQ,oBAAA,OAAA,EAAM,OAAc,cAA4B,cAAc,qBAAsB,CAAA;AAC7F,CAAC;"}
1
+ {"version":3,"file":"FormCheckbox.js","sources":["../../../../../packages/react-form/src/components/FormCheckbox.tsx"],"sourcesContent":["import * as React from 'react'\nimport { ChangeEventHandler, ReactElement, useCallback, useEffect } from 'react'\nimport {\n\tComponent,\n\tField,\n\tOptionallyVariableFieldValue,\n\tSugaredRelativeSingleField,\n\tTreeNodeEnvironmentFactory,\n\tuseField,\n} from '@contember/react-binding'\nimport { useFormFieldState } from '../contexts'\nimport { dataAttribute } from '@contember/utilities'\nimport { useFormInputValidationHandler } from '../hooks'\nimport { Slot } from '@radix-ui/react-slot'\n\nconst SlotInput = Slot as React.ForwardRefExoticComponent<React.RefAttributes<HTMLInputElement> & React.InputHTMLAttributes<HTMLInputElement>>\n\nexport interface FormCheckboxProps {\n\tfield: SugaredRelativeSingleField['field']\n\tisNonbearing?: boolean\n\tdefaultValue?: OptionallyVariableFieldValue\n\tchildren: ReactElement<HTMLInputElement, 'input'>\n}\n\nexport const FormCheckbox = Component<FormCheckboxProps>(({ field, isNonbearing, defaultValue, ...props }) => {\n\tconst accessor = useField<boolean>(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 value = accessor.value\n\tconst accessorGetter = accessor.getAccessor\n\tconst { ref, onFocus, onBlur } = useFormInputValidationHandler(accessor)\n\tconst [checkboxRef, setCheckboxRef] = React.useState<HTMLInputElement | null>(null)\n\n\tuseEffect(() => {\n\t\tif (!checkboxRef) {\n\t\t\treturn\n\t\t}\n\t\tcheckboxRef.indeterminate = value === null\n\t}, [checkboxRef, ref, value])\n\n\treturn (\n\t\t<SlotInput\n\t\t\tref={it => {\n\t\t\t\t(ref as any).current = checkboxRef\n\t\t\t\tsetCheckboxRef(it)\n\t\t\t}}\n\t\t\ttype=\"checkbox\"\n\t\t\tchecked={accessor.value === true}\n\t\t\tdata-state={value === null ? 'indeterminate' : (value ? 'checked' : 'unchecked')}\n\t\t\tdata-invalid={dataAttribute(hasErrors)}\n\t\t\tdata-dirty={dataAttribute(dirty)}\n\t\t\tid={id ? `${id}-input` : undefined}\n\t\t\tonFocus={onFocus}\n\t\t\tonBlur={onBlur}\n\t\t\tonChange={useCallback<ChangeEventHandler<HTMLInputElement>>(e => {\n\t\t\t\taccessorGetter().updateValue(e.target.checked)\n\t\t\t}, [accessorGetter])}\n\t\t\t{...props}\n\t\t/>\n\t)\n}, ({ field, isNonbearing, defaultValue }, env) => {\n\tconst node = TreeNodeEnvironmentFactory.createEnvironmentForField(env, { field }).getSubTreeNode()\n\tconst resolvedDefaultValue = defaultValue !== undefined ? defaultValue : (node.field.defaultValue ?? (node.field.nullable ? null : false))\n\n\treturn <Field field={field} isNonbearing={isNonbearing} defaultValue={resolvedDefaultValue} />\n})\n"],"names":[],"mappings":";;;;;;;;AAeA,MAAM,YAAY;AASL,MAAA,eAAe,UAA6B,CAAC,EAAE,OAAO,cAAc,cAAc,GAAG,YAAY;AACvG,QAAA,WAAW,SAAkB,KAAK;AAExC,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,QAAQ,SAAS;AACvB,QAAM,iBAAiB,SAAS;AAChC,QAAM,EAAE,KAAK,SAAS,OAAO,IAAI,8BAA8B,QAAQ;AACvE,QAAM,CAAC,aAAa,cAAc,IAAI,MAAM,SAAkC,IAAI;AAElF,YAAU,MAAM;AACf,QAAI,CAAC,aAAa;AACjB;AAAA,IAAA;AAED,gBAAY,gBAAgB,UAAU;AAAA,EACpC,GAAA,CAAC,aAAa,KAAK,KAAK,CAAC;AAG3B,SAAA;AAAA,IAAC;AAAA,IAAA;AAAA,MACA,KAAK,CAAM,OAAA;AACT,YAAY,UAAU;AACvB,uBAAe,EAAE;AAAA,MAClB;AAAA,MACA,MAAK;AAAA,MACL,SAAS,SAAS,UAAU;AAAA,MAC5B,cAAY,UAAU,OAAO,kBAAmB,QAAQ,YAAY;AAAA,MACpE,gBAAc,cAAc,SAAS;AAAA,MACrC,cAAY,cAAc,KAAK;AAAA,MAC/B,IAAI,KAAK,GAAG,EAAE,WAAW;AAAA,MACzB;AAAA,MACA;AAAA,MACA,UAAU,YAAkD,CAAK,MAAA;AAChE,uBAAiB,EAAA,YAAY,EAAE,OAAO,OAAO;AAAA,MAAA,GAC3C,CAAC,cAAc,CAAC;AAAA,MAClB,GAAG;AAAA,IAAA;AAAA,EACL;AAEF,GAAG,CAAC,EAAE,OAAO,cAAc,aAAA,GAAgB,QAAQ;AAC5C,QAAA,OAAO,2BAA2B,0BAA0B,KAAK,EAAE,MAAM,CAAC,EAAE,eAAe;AAC3F,QAAA,uBAAuB,iBAAiB,SAAY,eAAgB,KAAK,MAAM,iBAAiB,KAAK,MAAM,WAAW,OAAO;AAEnI,SAAQ,oBAAA,OAAA,EAAM,OAAc,cAA4B,cAAc,sBAAsB;AAC7F,CAAC;"}
@@ -4,16 +4,16 @@ const React = require("react");
4
4
  const contexts = require("../contexts.cjs");
5
5
  const reactBinding = require("@contember/react-binding");
6
6
  const FormError = ({ children, formatter }) => {
7
- const errors = contexts.useFormError();
8
- const id = contexts.useFormFieldId();
9
- const formatted = React.useMemo(() => formatter(errors ?? []), [errors, formatter]);
10
- if (errors === void 0 || id === void 0) {
7
+ const formState = contexts.useFormFieldState();
8
+ if (!formState) {
11
9
  throw new reactBinding.BindingError("FormError must be used inside a FormField");
12
10
  }
11
+ const { errors, htmlId } = formState;
12
+ const formatted = React.useMemo(() => formatter(errors ?? []).filter((it, index, arr) => arr.indexOf(it) === index), [errors, formatter]);
13
13
  return formatted.map((it, index) => {
14
14
  return React.cloneElement(children, {
15
15
  key: index,
16
- ...{ id: `${id}-error-${index}` },
16
+ ...htmlId ? { id: `${htmlId}-error-${index}` } : {},
17
17
  children: it
18
18
  });
19
19
  });
@@ -1 +1 @@
1
- {"version":3,"file":"FormError.cjs","sources":["../../../../../packages/react-form/src/components/FormError.tsx"],"sourcesContent":["import { cloneElement, ReactElement, ReactNode, useMemo } from 'react'\nimport { useFormError, useFormFieldId } from '../contexts'\nimport { BindingError, ErrorAccessor } from '@contember/react-binding'\n\nexport const FormError = ({ children, formatter }: {\n\tformatter: (errors: ErrorAccessor.Error[]) => ReactNode[]\n\tchildren: ReactElement\n}) => {\n\tconst errors = useFormError()\n\tconst id = useFormFieldId()\n\tconst formatted = useMemo(() => formatter(errors ?? []), [errors, formatter])\n\tif (errors === undefined || id === undefined) {\n\t\tthrow new BindingError('FormError must be used inside a FormField')\n\t}\n\treturn formatted.map((it, index) => {\n\t\treturn cloneElement(children, {\n\t\t\tkey: index,\n\t\t\t...{ id: `${id}-error-${index}` },\n\t\t\tchildren: it,\n\t\t})\n\t})\n}\n"],"names":["useFormError","useFormFieldId","useMemo","BindingError","cloneElement"],"mappings":";;;;;AAIO,MAAM,YAAY,CAAC,EAAE,UAAU,gBAGhC;AACL,QAAM,SAASA,SAAAA;AACf,QAAM,KAAKC,SAAAA;AACL,QAAA,YAAYC,cAAQ,MAAM,UAAU,UAAU,CAAE,CAAA,GAAG,CAAC,QAAQ,SAAS,CAAC;AACxE,MAAA,WAAW,UAAa,OAAO,QAAW;AACvC,UAAA,IAAIC,aAAAA,aAAa,2CAA2C;AAAA,EACnE;AACA,SAAO,UAAU,IAAI,CAAC,IAAI,UAAU;AACnC,WAAOC,MAAAA,aAAa,UAAU;AAAA,MAC7B,KAAK;AAAA,MACL,GAAG,EAAE,IAAI,GAAG,EAAE,UAAU,KAAK,GAAG;AAAA,MAChC,UAAU;AAAA,IAAA,CACV;AAAA,EAAA,CACD;AACF;;"}
1
+ {"version":3,"file":"FormError.cjs","sources":["../../../../../packages/react-form/src/components/FormError.tsx"],"sourcesContent":["import { cloneElement, ReactElement, ReactNode, useMemo } from 'react'\nimport { useFormFieldState } from '../contexts'\nimport { BindingError, ErrorAccessor } from '@contember/react-binding'\n\nexport const FormError = ({ children, formatter }: {\n\tformatter: (errors: ErrorAccessor.Error[]) => ReactNode[]\n\tchildren: 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 } = formState\n\tconst formatted = useMemo(() => formatter(errors ?? []).filter((it, index, arr) => arr.indexOf(it) === index), [errors, formatter])\n\treturn formatted.map((it, index) => {\n\t\treturn cloneElement(children, {\n\t\t\tkey: index,\n\t\t\t...(htmlId ? { id: `${htmlId}-error-${index}` } : {}),\n\t\t\tchildren: it,\n\t\t})\n\t})\n}\n"],"names":["useFormFieldState","BindingError","useMemo","cloneElement"],"mappings":";;;;;AAIO,MAAM,YAAY,CAAC,EAAE,UAAU,gBAGhC;AACL,QAAM,YAAYA,SAAAA,kBAAkB;AACpC,MAAI,CAAC,WAAW;AACT,UAAA,IAAIC,0BAAa,2CAA2C;AAAA,EAAA;AAE7D,QAAA,EAAE,QAAQ,OAAA,IAAW;AACrB,QAAA,YAAYC,cAAQ,MAAM,UAAU,UAAU,EAAE,EAAE,OAAO,CAAC,IAAI,OAAO,QAAQ,IAAI,QAAQ,EAAE,MAAM,KAAK,GAAG,CAAC,QAAQ,SAAS,CAAC;AAClI,SAAO,UAAU,IAAI,CAAC,IAAI,UAAU;AACnC,WAAOC,MAAAA,aAAa,UAAU;AAAA,MAC7B,KAAK;AAAA,MACL,GAAI,SAAS,EAAE,IAAI,GAAG,MAAM,UAAU,KAAK,GAAG,IAAI,CAAC;AAAA,MACnD,UAAU;AAAA,IAAA,CACV;AAAA,EAAA,CACD;AACF;;"}
@@ -1,17 +1,17 @@
1
1
  import { useMemo, cloneElement } from "react";
2
- import { useFormError, useFormFieldId } from "../contexts.js";
2
+ import { useFormFieldState } from "../contexts.js";
3
3
  import { BindingError } from "@contember/react-binding";
4
4
  const FormError = ({ children, formatter }) => {
5
- const errors = useFormError();
6
- const id = useFormFieldId();
7
- const formatted = useMemo(() => formatter(errors ?? []), [errors, formatter]);
8
- if (errors === void 0 || id === void 0) {
5
+ const formState = useFormFieldState();
6
+ if (!formState) {
9
7
  throw new BindingError("FormError must be used inside a FormField");
10
8
  }
9
+ const { errors, htmlId } = formState;
10
+ const formatted = useMemo(() => formatter(errors ?? []).filter((it, index, arr) => arr.indexOf(it) === index), [errors, formatter]);
11
11
  return formatted.map((it, index) => {
12
12
  return cloneElement(children, {
13
13
  key: index,
14
- ...{ id: `${id}-error-${index}` },
14
+ ...htmlId ? { id: `${htmlId}-error-${index}` } : {},
15
15
  children: it
16
16
  });
17
17
  });
@@ -1 +1 @@
1
- {"version":3,"file":"FormError.js","sources":["../../../../../packages/react-form/src/components/FormError.tsx"],"sourcesContent":["import { cloneElement, ReactElement, ReactNode, useMemo } from 'react'\nimport { useFormError, useFormFieldId } from '../contexts'\nimport { BindingError, ErrorAccessor } from '@contember/react-binding'\n\nexport const FormError = ({ children, formatter }: {\n\tformatter: (errors: ErrorAccessor.Error[]) => ReactNode[]\n\tchildren: ReactElement\n}) => {\n\tconst errors = useFormError()\n\tconst id = useFormFieldId()\n\tconst formatted = useMemo(() => formatter(errors ?? []), [errors, formatter])\n\tif (errors === undefined || id === undefined) {\n\t\tthrow new BindingError('FormError must be used inside a FormField')\n\t}\n\treturn formatted.map((it, index) => {\n\t\treturn cloneElement(children, {\n\t\t\tkey: index,\n\t\t\t...{ id: `${id}-error-${index}` },\n\t\t\tchildren: it,\n\t\t})\n\t})\n}\n"],"names":[],"mappings":";;;AAIO,MAAM,YAAY,CAAC,EAAE,UAAU,gBAGhC;AACL,QAAM,SAAS;AACf,QAAM,KAAK;AACL,QAAA,YAAY,QAAQ,MAAM,UAAU,UAAU,CAAE,CAAA,GAAG,CAAC,QAAQ,SAAS,CAAC;AACxE,MAAA,WAAW,UAAa,OAAO,QAAW;AACvC,UAAA,IAAI,aAAa,2CAA2C;AAAA,EACnE;AACA,SAAO,UAAU,IAAI,CAAC,IAAI,UAAU;AACnC,WAAO,aAAa,UAAU;AAAA,MAC7B,KAAK;AAAA,MACL,GAAG,EAAE,IAAI,GAAG,EAAE,UAAU,KAAK,GAAG;AAAA,MAChC,UAAU;AAAA,IAAA,CACV;AAAA,EAAA,CACD;AACF;"}
1
+ {"version":3,"file":"FormError.js","sources":["../../../../../packages/react-form/src/components/FormError.tsx"],"sourcesContent":["import { cloneElement, ReactElement, ReactNode, useMemo } from 'react'\nimport { useFormFieldState } from '../contexts'\nimport { BindingError, ErrorAccessor } from '@contember/react-binding'\n\nexport const FormError = ({ children, formatter }: {\n\tformatter: (errors: ErrorAccessor.Error[]) => ReactNode[]\n\tchildren: 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 } = formState\n\tconst formatted = useMemo(() => formatter(errors ?? []).filter((it, index, arr) => arr.indexOf(it) === index), [errors, formatter])\n\treturn formatted.map((it, index) => {\n\t\treturn cloneElement(children, {\n\t\t\tkey: index,\n\t\t\t...(htmlId ? { id: `${htmlId}-error-${index}` } : {}),\n\t\t\tchildren: it,\n\t\t})\n\t})\n}\n"],"names":[],"mappings":";;;AAIO,MAAM,YAAY,CAAC,EAAE,UAAU,gBAGhC;AACL,QAAM,YAAY,kBAAkB;AACpC,MAAI,CAAC,WAAW;AACT,UAAA,IAAI,aAAa,2CAA2C;AAAA,EAAA;AAE7D,QAAA,EAAE,QAAQ,OAAA,IAAW;AACrB,QAAA,YAAY,QAAQ,MAAM,UAAU,UAAU,EAAE,EAAE,OAAO,CAAC,IAAI,OAAO,QAAQ,IAAI,QAAQ,EAAE,MAAM,KAAK,GAAG,CAAC,QAAQ,SAAS,CAAC;AAClI,SAAO,UAAU,IAAI,CAAC,IAAI,UAAU;AACnC,WAAO,aAAa,UAAU;AAAA,MAC7B,KAAK;AAAA,MACL,GAAI,SAAS,EAAE,IAAI,GAAG,MAAM,UAAU,KAAK,GAAG,IAAI,CAAC;AAAA,MACnD,UAAU;AAAA,IAAA,CACV;AAAA,EAAA,CACD;AACF;"}
@@ -2,14 +2,24 @@
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const jsxRuntime = require("react/jsx-runtime");
4
4
  const React = require("react");
5
- const contexts = require("../contexts.cjs");
6
5
  const reactBinding = require("@contember/react-binding");
7
- const emptyArr = [];
8
- const FormFieldScope = ({ field, children }) => {
9
- const id = React.useId();
6
+ const FormFieldStateProvider = require("./FormFieldStateProvider.cjs");
7
+ const FormFieldScope = ({ field, children, required }) => {
10
8
  const fieldAccessor = reactBinding.useField({ field });
11
- const errors = fieldAccessor.errors?.errors;
12
- return /* @__PURE__ */ jsxRuntime.jsx(contexts.FormFieldIdContext.Provider, { value: id, children: /* @__PURE__ */ jsxRuntime.jsx(contexts.FormErrorContext.Provider, { value: errors ?? emptyArr, children }) });
9
+ const entityName = fieldAccessor.getParent().name;
10
+ const fieldName = fieldAccessor.fieldName;
11
+ const enumName = fieldAccessor.schema.enumName ?? void 0;
12
+ const fieldInfo = React.useMemo(() => ({ entityName, fieldName, enumName }), [entityName, fieldName, enumName]);
13
+ return /* @__PURE__ */ jsxRuntime.jsx(
14
+ FormFieldStateProvider.FormFieldStateProvider,
15
+ {
16
+ errors: fieldAccessor.errors?.errors,
17
+ required: required ?? !fieldAccessor.schema.nullable,
18
+ dirty: fieldAccessor.hasUnpersistedChanges,
19
+ field: fieldInfo,
20
+ children
21
+ }
22
+ );
13
23
  };
14
24
  exports.FormFieldScope = FormFieldScope;
15
25
  //# sourceMappingURL=FormFieldScope.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"FormFieldScope.cjs","sources":["../../../../../packages/react-form/src/components/FormFieldScope.tsx"],"sourcesContent":["import * as React from 'react'\nimport { useId } from 'react'\nimport { FormErrorContext, FormFieldIdContext } from '../contexts'\nimport { SugaredRelativeSingleField, useField } from '@contember/react-binding'\n\nconst emptyArr: [] = []\n\nexport type FormFieldScopeProps = {\n\tfield: SugaredRelativeSingleField['field']\n\tchildren: React.ReactNode\n}\n\nexport const FormFieldScope = ({ field, children }: FormFieldScopeProps) => {\n\tconst id = useId()\n\tconst fieldAccessor = useField({ field })\n\tconst errors = fieldAccessor.errors?.errors\n\treturn (\n\t\t<FormFieldIdContext.Provider value={id}>\n\t\t\t<FormErrorContext.Provider value={errors ?? emptyArr}>\n\t\t\t\t{children}\n\t\t\t</FormErrorContext.Provider>\n\t\t</FormFieldIdContext.Provider>\n\t)\n}\n"],"names":["useId","useField","jsx","FormFieldIdContext","FormErrorContext"],"mappings":";;;;;;AAKA,MAAM,WAAe,CAAA;AAOd,MAAM,iBAAiB,CAAC,EAAE,OAAO,eAAoC;AAC3E,QAAM,KAAKA,MAAAA;AACX,QAAM,gBAAgBC,aAAAA,SAAS,EAAE,MAAO,CAAA;AAClC,QAAA,SAAS,cAAc,QAAQ;AACrC,SACEC,2BAAAA,IAAAC,SAAAA,mBAAmB,UAAnB,EAA4B,OAAO,IACnC,UAAAD,2BAAA,IAACE,SAAiB,iBAAA,UAAjB,EAA0B,OAAO,UAAU,UAC1C,UACF,EACD,CAAA;AAEF;;"}
1
+ {"version":3,"file":"FormFieldScope.cjs","sources":["../../../../../packages/react-form/src/components/FormFieldScope.tsx"],"sourcesContent":["import * as React from 'react'\nimport { useMemo } from 'react'\nimport { SugaredRelativeSingleField, useField } from '@contember/react-binding'\nimport { FormFieldStateProvider } from './FormFieldStateProvider'\n\nexport type FormFieldScopeProps = {\n\tfield: SugaredRelativeSingleField['field']\n\tchildren: React.ReactNode\n\trequired?: boolean\n}\n\nexport const FormFieldScope = ({ field, children, required }: FormFieldScopeProps) => {\n\tconst fieldAccessor = useField({ field })\n\tconst entityName = fieldAccessor.getParent().name\n\tconst fieldName = fieldAccessor.fieldName\n\tconst enumName = fieldAccessor.schema.enumName ?? undefined\n\tconst fieldInfo = useMemo(() => ({ entityName, fieldName, enumName }), [entityName, fieldName, enumName])\n\n\treturn (\n\t\t<FormFieldStateProvider\n\t\t\terrors={fieldAccessor.errors?.errors}\n\t\t\trequired={required ?? !fieldAccessor.schema.nullable}\n\t\t\tdirty={fieldAccessor.hasUnpersistedChanges}\n\t\t\tfield={fieldInfo}\n\t\t>\n\t\t\t{children}\n\t\t</FormFieldStateProvider>\n\t)\n}\n"],"names":["useField","useMemo","jsx","FormFieldStateProvider"],"mappings":";;;;;;AAWO,MAAM,iBAAiB,CAAC,EAAE,OAAO,UAAU,eAAoC;AACrF,QAAM,gBAAgBA,aAAAA,SAAS,EAAE,OAAO;AAClC,QAAA,aAAa,cAAc,UAAA,EAAY;AAC7C,QAAM,YAAY,cAAc;AAC1B,QAAA,WAAW,cAAc,OAAO,YAAY;AAClD,QAAM,YAAYC,MAAAA,QAAQ,OAAO,EAAE,YAAY,WAAW,SAAS,IAAI,CAAC,YAAY,WAAW,QAAQ,CAAC;AAGvG,SAAAC,2BAAA;AAAA,IAACC,uBAAA;AAAA,IAAA;AAAA,MACA,QAAQ,cAAc,QAAQ;AAAA,MAC9B,UAAU,YAAY,CAAC,cAAc,OAAO;AAAA,MAC5C,OAAO,cAAc;AAAA,MACrB,OAAO;AAAA,MAEN;AAAA,IAAA;AAAA,EACF;AAEF;;"}
@@ -1,13 +1,23 @@
1
1
  import { jsx } from "react/jsx-runtime";
2
- import { useId } from "react";
3
- import { FormFieldIdContext, FormErrorContext } from "../contexts.js";
2
+ import { useMemo } from "react";
4
3
  import { useField } from "@contember/react-binding";
5
- const emptyArr = [];
6
- const FormFieldScope = ({ field, children }) => {
7
- const id = useId();
4
+ import { FormFieldStateProvider } from "./FormFieldStateProvider.js";
5
+ const FormFieldScope = ({ field, children, required }) => {
8
6
  const fieldAccessor = useField({ field });
9
- const errors = fieldAccessor.errors?.errors;
10
- return /* @__PURE__ */ jsx(FormFieldIdContext.Provider, { value: id, children: /* @__PURE__ */ jsx(FormErrorContext.Provider, { value: errors ?? emptyArr, children }) });
7
+ const entityName = fieldAccessor.getParent().name;
8
+ const fieldName = fieldAccessor.fieldName;
9
+ const enumName = fieldAccessor.schema.enumName ?? void 0;
10
+ const fieldInfo = useMemo(() => ({ entityName, fieldName, enumName }), [entityName, fieldName, enumName]);
11
+ return /* @__PURE__ */ jsx(
12
+ FormFieldStateProvider,
13
+ {
14
+ errors: fieldAccessor.errors?.errors,
15
+ required: required ?? !fieldAccessor.schema.nullable,
16
+ dirty: fieldAccessor.hasUnpersistedChanges,
17
+ field: fieldInfo,
18
+ children
19
+ }
20
+ );
11
21
  };
12
22
  export {
13
23
  FormFieldScope
@@ -1 +1 @@
1
- {"version":3,"file":"FormFieldScope.js","sources":["../../../../../packages/react-form/src/components/FormFieldScope.tsx"],"sourcesContent":["import * as React from 'react'\nimport { useId } from 'react'\nimport { FormErrorContext, FormFieldIdContext } from '../contexts'\nimport { SugaredRelativeSingleField, useField } from '@contember/react-binding'\n\nconst emptyArr: [] = []\n\nexport type FormFieldScopeProps = {\n\tfield: SugaredRelativeSingleField['field']\n\tchildren: React.ReactNode\n}\n\nexport const FormFieldScope = ({ field, children }: FormFieldScopeProps) => {\n\tconst id = useId()\n\tconst fieldAccessor = useField({ field })\n\tconst errors = fieldAccessor.errors?.errors\n\treturn (\n\t\t<FormFieldIdContext.Provider value={id}>\n\t\t\t<FormErrorContext.Provider value={errors ?? emptyArr}>\n\t\t\t\t{children}\n\t\t\t</FormErrorContext.Provider>\n\t\t</FormFieldIdContext.Provider>\n\t)\n}\n"],"names":[],"mappings":";;;;AAKA,MAAM,WAAe,CAAA;AAOd,MAAM,iBAAiB,CAAC,EAAE,OAAO,eAAoC;AAC3E,QAAM,KAAK;AACX,QAAM,gBAAgB,SAAS,EAAE,MAAO,CAAA;AAClC,QAAA,SAAS,cAAc,QAAQ;AACrC,SACE,oBAAA,mBAAmB,UAAnB,EAA4B,OAAO,IACnC,UAAA,oBAAC,iBAAiB,UAAjB,EAA0B,OAAO,UAAU,UAC1C,UACF,EACD,CAAA;AAEF;"}
1
+ {"version":3,"file":"FormFieldScope.js","sources":["../../../../../packages/react-form/src/components/FormFieldScope.tsx"],"sourcesContent":["import * as React from 'react'\nimport { useMemo } from 'react'\nimport { SugaredRelativeSingleField, useField } from '@contember/react-binding'\nimport { FormFieldStateProvider } from './FormFieldStateProvider'\n\nexport type FormFieldScopeProps = {\n\tfield: SugaredRelativeSingleField['field']\n\tchildren: React.ReactNode\n\trequired?: boolean\n}\n\nexport const FormFieldScope = ({ field, children, required }: FormFieldScopeProps) => {\n\tconst fieldAccessor = useField({ field })\n\tconst entityName = fieldAccessor.getParent().name\n\tconst fieldName = fieldAccessor.fieldName\n\tconst enumName = fieldAccessor.schema.enumName ?? undefined\n\tconst fieldInfo = useMemo(() => ({ entityName, fieldName, enumName }), [entityName, fieldName, enumName])\n\n\treturn (\n\t\t<FormFieldStateProvider\n\t\t\terrors={fieldAccessor.errors?.errors}\n\t\t\trequired={required ?? !fieldAccessor.schema.nullable}\n\t\t\tdirty={fieldAccessor.hasUnpersistedChanges}\n\t\t\tfield={fieldInfo}\n\t\t>\n\t\t\t{children}\n\t\t</FormFieldStateProvider>\n\t)\n}\n"],"names":[],"mappings":";;;;AAWO,MAAM,iBAAiB,CAAC,EAAE,OAAO,UAAU,eAAoC;AACrF,QAAM,gBAAgB,SAAS,EAAE,OAAO;AAClC,QAAA,aAAa,cAAc,UAAA,EAAY;AAC7C,QAAM,YAAY,cAAc;AAC1B,QAAA,WAAW,cAAc,OAAO,YAAY;AAClD,QAAM,YAAY,QAAQ,OAAO,EAAE,YAAY,WAAW,SAAS,IAAI,CAAC,YAAY,WAAW,QAAQ,CAAC;AAGvG,SAAA;AAAA,IAAC;AAAA,IAAA;AAAA,MACA,QAAQ,cAAc,QAAQ;AAAA,MAC9B,UAAU,YAAY,CAAC,cAAc,OAAO;AAAA,MAC5C,OAAO,cAAc;AAAA,MACrB,OAAO;AAAA,MAEN;AAAA,IAAA;AAAA,EACF;AAEF;"}