@springmicro/forms 0.6.4 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/.eslintrc.cjs +22 -22
  2. package/README.md +11 -11
  3. package/dist/index.d.ts +0 -0
  4. package/dist/index.js +0 -0
  5. package/dist/index.umd.cjs +0 -0
  6. package/package.json +3 -3
  7. package/src/builder/bottom-drawer.tsx +429 -429
  8. package/src/builder/form-builder.tsx +256 -256
  9. package/src/builder/modal.tsx +39 -39
  10. package/src/builder/nodes/node-base.tsx +94 -94
  11. package/src/builder/nodes/node-child-helpers.tsx +273 -273
  12. package/src/builder/nodes/node-parent.tsx +187 -187
  13. package/src/builder/nodes/node-types/array-node.tsx +134 -134
  14. package/src/builder/nodes/node-types/date-node.tsx +60 -60
  15. package/src/builder/nodes/node-types/file-node.tsx +67 -67
  16. package/src/builder/nodes/node-types/integer-node.tsx +60 -60
  17. package/src/builder/nodes/node-types/object-node.tsx +67 -67
  18. package/src/builder/nodes/node-types/text-node.tsx +66 -66
  19. package/src/fields/ArrayField.tsx +875 -875
  20. package/src/fields/BooleanField.tsx +110 -110
  21. package/src/fields/MultiSchemaField.tsx +236 -236
  22. package/src/fields/NullField.tsx +22 -22
  23. package/src/fields/NumberField.tsx +87 -87
  24. package/src/fields/ObjectField.tsx +338 -338
  25. package/src/fields/SchemaField.tsx +402 -402
  26. package/src/fields/StringField.tsx +67 -67
  27. package/src/fields/index.ts +24 -24
  28. package/src/index.tsx +26 -26
  29. package/src/interfaces/MessagesProps.interface.ts +5 -5
  30. package/src/interfaces/Option.interface.ts +4 -4
  31. package/src/styles/select.styles.ts +28 -28
  32. package/src/templates/ArrayFieldDescriptionTemplate.tsx +42 -42
  33. package/src/templates/ArrayFieldItemTemplate.tsx +78 -78
  34. package/src/templates/ArrayFieldTemplate.tsx +90 -90
  35. package/src/templates/ArrayFieldTitleTemplate.tsx +44 -44
  36. package/src/templates/BaseInputTemplate.tsx +94 -94
  37. package/src/templates/ButtonTemplates/AddButton.tsx +29 -29
  38. package/src/templates/ButtonTemplates/IconButton.tsx +49 -49
  39. package/src/templates/ButtonTemplates/SubmitButton.tsx +29 -29
  40. package/src/templates/ButtonTemplates/index.ts +16 -16
  41. package/src/templates/DescriptionField.tsx +29 -29
  42. package/src/templates/ErrorList.tsx +25 -25
  43. package/src/templates/FieldTemplate/FieldTemplate.tsx +39 -39
  44. package/src/templates/FieldTemplate/Label.tsx +29 -29
  45. package/src/templates/FieldTemplate/WrapIfAdditional.tsx +85 -85
  46. package/src/templates/FieldTemplate/index.ts +3 -3
  47. package/src/templates/ObjectFieldTemplate.tsx +79 -79
  48. package/src/templates/TitleField.tsx +20 -20
  49. package/src/templates/UnsupportedField.tsx +29 -29
  50. package/src/templates/index.ts +32 -32
  51. package/src/types/Message.type.ts +6 -6
  52. package/src/types/RawMessage.type.ts +15 -15
  53. package/src/types/form-builder.ts +135 -135
  54. package/src/types/utils.type.ts +1 -1
  55. package/src/utils/form-builder.ts +424 -424
  56. package/src/utils/processSelectValue.ts +50 -50
  57. package/src/widgets/AltDateTimeWidget.tsx +17 -17
  58. package/src/widgets/AltDateWidget.tsx +216 -216
  59. package/src/widgets/CheckboxWidget.tsx +80 -80
  60. package/src/widgets/CheckboxesWidget.tsx +74 -74
  61. package/src/widgets/ColorWidget.tsx +26 -26
  62. package/src/widgets/DateTimeWidget.tsx +28 -28
  63. package/src/widgets/DateWidget.tsx +36 -36
  64. package/src/widgets/EmailWidget.tsx +19 -19
  65. package/src/widgets/FileWidget.tsx +144 -144
  66. package/src/widgets/HiddenWidget.tsx +22 -22
  67. package/src/widgets/PasswordWidget.tsx +20 -20
  68. package/src/widgets/RadioWidget.tsx +87 -87
  69. package/src/widgets/RangeWidget.tsx +24 -24
  70. package/src/widgets/SelectWidget.tsx +99 -99
  71. package/src/widgets/TextWidget.tsx +19 -19
  72. package/src/widgets/TextareaWidget.tsx +64 -64
  73. package/src/widgets/URLWidget.tsx +19 -19
  74. package/src/widgets/UpDownWidget.tsx +20 -20
  75. package/src/widgets/index.ts +43 -43
  76. package/tsconfig.json +24 -24
  77. package/tsconfig.node.json +10 -10
  78. package/vite.config.ts +25 -25
@@ -1,87 +1,87 @@
1
- import React, { useState, useCallback } from "react";
2
- import type { FieldProps, GenericObjectType } from "@rjsf/utils";
3
- import { asNumber } from "@rjsf/utils";
4
-
5
- // Matches a string that ends in a . character, optionally followed by a sequence of
6
- // digits followed by any number of 0 characters up until the end of the line.
7
- // Ensuring that there is at least one prefixed character is important so that
8
- // you don't incorrectly match against "0".
9
- const trailingCharMatcherWithPrefix = /\.([0-9]*0)*$/;
10
-
11
- // This is used for trimming the trailing 0 and . characters without affecting
12
- // the rest of the string. Its possible to use one RegEx with groups for this
13
- // functionality, but it is fairly complex compared to simply defining two
14
- // different matchers.
15
- const trailingCharMatcher = /[0.]0*$/;
16
-
17
- /**
18
- * The NumberField class has some special handling for dealing with trailing
19
- * decimal points and/or zeroes. This logic is designed to allow trailing values
20
- * to be visible in the input element, but not be represented in the
21
- * corresponding form data.
22
- *
23
- * The algorithm is as follows:
24
- *
25
- * 1. When the input value changes the value is cached in the component state
26
- *
27
- * 2. The value is then normalized, removing trailing decimal points and zeros,
28
- * then passed to the "onChange" callback
29
- *
30
- * 3. When the component is rendered, the formData value is checked against the
31
- * value cached in the state. If it matches the cached value, the cached
32
- * value is passed to the input instead of the formData value
33
- */
34
- function NumberField<T = any, F extends GenericObjectType = any>(
35
- props: FieldProps<T, F>
36
- ) {
37
- const { registry, onChange, formData, value: initialValue } = props;
38
- const [lastValue, setLastValue] = useState(initialValue);
39
- const { StringField } = registry.fields;
40
-
41
- let value = formData;
42
-
43
- /** Handle the change from the `StringField` to properly convert to a number
44
- *
45
- * @param value - The current value for the change occurring
46
- */
47
- const handleChange = useCallback(
48
- (value: FieldProps<T, F>["value"]) => {
49
- // Cache the original value in component state
50
- setLastValue(value);
51
-
52
- // Normalize decimals that don't start with a zero character in advance so
53
- // that the rest of the normalization logic is simpler
54
- if (`${value}`.charAt(0) === ".") {
55
- value = `0${value}`;
56
- }
57
-
58
- // Check that the value is a string (this can happen if the widget used is a
59
- // <select>, due to an enum declaration etc) then, if the value ends in a
60
- // trailing decimal point or multiple zeroes, strip the trailing values
61
- const processed =
62
- typeof value === "string" && value.match(trailingCharMatcherWithPrefix)
63
- ? asNumber(value.replace(trailingCharMatcher, ""))
64
- : asNumber(value);
65
-
66
- onChange(processed as unknown as T);
67
- },
68
- [onChange]
69
- );
70
-
71
- if (typeof lastValue === "string" && typeof value === "number") {
72
- // Construct a regular expression that checks for a string that consists
73
- // of the formData value suffixed with zero or one '.' characters and zero
74
- // or more '0' characters
75
- const re = new RegExp(`${value}`.replace(".", "\\.") + "\\.?0*$");
76
-
77
- // If the cached "lastValue" is a match, use that instead of the formData
78
- // value to prevent the input value from changing in the UI
79
- if (lastValue.match(re)) {
80
- value = lastValue as unknown as T;
81
- }
82
- }
83
-
84
- return <StringField {...props} formData={value} onChange={handleChange} />;
85
- }
86
-
87
- export default NumberField;
1
+ import React, { useState, useCallback } from "react";
2
+ import type { FieldProps, GenericObjectType } from "@rjsf/utils";
3
+ import { asNumber } from "@rjsf/utils";
4
+
5
+ // Matches a string that ends in a . character, optionally followed by a sequence of
6
+ // digits followed by any number of 0 characters up until the end of the line.
7
+ // Ensuring that there is at least one prefixed character is important so that
8
+ // you don't incorrectly match against "0".
9
+ const trailingCharMatcherWithPrefix = /\.([0-9]*0)*$/;
10
+
11
+ // This is used for trimming the trailing 0 and . characters without affecting
12
+ // the rest of the string. Its possible to use one RegEx with groups for this
13
+ // functionality, but it is fairly complex compared to simply defining two
14
+ // different matchers.
15
+ const trailingCharMatcher = /[0.]0*$/;
16
+
17
+ /**
18
+ * The NumberField class has some special handling for dealing with trailing
19
+ * decimal points and/or zeroes. This logic is designed to allow trailing values
20
+ * to be visible in the input element, but not be represented in the
21
+ * corresponding form data.
22
+ *
23
+ * The algorithm is as follows:
24
+ *
25
+ * 1. When the input value changes the value is cached in the component state
26
+ *
27
+ * 2. The value is then normalized, removing trailing decimal points and zeros,
28
+ * then passed to the "onChange" callback
29
+ *
30
+ * 3. When the component is rendered, the formData value is checked against the
31
+ * value cached in the state. If it matches the cached value, the cached
32
+ * value is passed to the input instead of the formData value
33
+ */
34
+ function NumberField<T = any, F extends GenericObjectType = any>(
35
+ props: FieldProps<T, F>
36
+ ) {
37
+ const { registry, onChange, formData, value: initialValue } = props;
38
+ const [lastValue, setLastValue] = useState(initialValue);
39
+ const { StringField } = registry.fields;
40
+
41
+ let value = formData;
42
+
43
+ /** Handle the change from the `StringField` to properly convert to a number
44
+ *
45
+ * @param value - The current value for the change occurring
46
+ */
47
+ const handleChange = useCallback(
48
+ (value: FieldProps<T, F>["value"]) => {
49
+ // Cache the original value in component state
50
+ setLastValue(value);
51
+
52
+ // Normalize decimals that don't start with a zero character in advance so
53
+ // that the rest of the normalization logic is simpler
54
+ if (`${value}`.charAt(0) === ".") {
55
+ value = `0${value}`;
56
+ }
57
+
58
+ // Check that the value is a string (this can happen if the widget used is a
59
+ // <select>, due to an enum declaration etc) then, if the value ends in a
60
+ // trailing decimal point or multiple zeroes, strip the trailing values
61
+ const processed =
62
+ typeof value === "string" && value.match(trailingCharMatcherWithPrefix)
63
+ ? asNumber(value.replace(trailingCharMatcher, ""))
64
+ : asNumber(value);
65
+
66
+ onChange(processed as unknown as T);
67
+ },
68
+ [onChange]
69
+ );
70
+
71
+ if (typeof lastValue === "string" && typeof value === "number") {
72
+ // Construct a regular expression that checks for a string that consists
73
+ // of the formData value suffixed with zero or one '.' characters and zero
74
+ // or more '0' characters
75
+ const re = new RegExp(`${value}`.replace(".", "\\.") + "\\.?0*$");
76
+
77
+ // If the cached "lastValue" is a match, use that instead of the formData
78
+ // value to prevent the input value from changing in the UI
79
+ if (lastValue.match(re)) {
80
+ value = lastValue as unknown as T;
81
+ }
82
+ }
83
+
84
+ return <StringField {...props} formData={value} onChange={handleChange} />;
85
+ }
86
+
87
+ export default NumberField;