@springmicro/forms 0.7.0 → 0.7.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.
- package/.eslintrc.cjs +22 -22
- package/README.md +11 -11
- package/dist/index.d.ts +0 -0
- package/dist/index.js +0 -0
- package/dist/index.umd.cjs +0 -0
- package/package.json +3 -3
- package/src/builder/bottom-drawer.tsx +429 -429
- package/src/builder/form-builder.tsx +256 -256
- package/src/builder/modal.tsx +39 -39
- package/src/builder/nodes/node-base.tsx +94 -94
- package/src/builder/nodes/node-child-helpers.tsx +273 -273
- package/src/builder/nodes/node-parent.tsx +187 -187
- package/src/builder/nodes/node-types/array-node.tsx +134 -134
- package/src/builder/nodes/node-types/date-node.tsx +60 -60
- package/src/builder/nodes/node-types/file-node.tsx +67 -67
- package/src/builder/nodes/node-types/integer-node.tsx +60 -60
- package/src/builder/nodes/node-types/object-node.tsx +67 -67
- package/src/builder/nodes/node-types/text-node.tsx +66 -66
- package/src/fields/ArrayField.tsx +875 -875
- package/src/fields/BooleanField.tsx +110 -110
- package/src/fields/MultiSchemaField.tsx +236 -236
- package/src/fields/NullField.tsx +22 -22
- package/src/fields/NumberField.tsx +87 -87
- package/src/fields/ObjectField.tsx +338 -338
- package/src/fields/SchemaField.tsx +402 -402
- package/src/fields/StringField.tsx +67 -67
- package/src/fields/index.ts +24 -24
- package/src/index.tsx +26 -26
- package/src/interfaces/MessagesProps.interface.ts +5 -5
- package/src/interfaces/Option.interface.ts +4 -4
- package/src/styles/select.styles.ts +28 -28
- package/src/templates/ArrayFieldDescriptionTemplate.tsx +42 -42
- package/src/templates/ArrayFieldItemTemplate.tsx +78 -78
- package/src/templates/ArrayFieldTemplate.tsx +90 -90
- package/src/templates/ArrayFieldTitleTemplate.tsx +44 -44
- package/src/templates/BaseInputTemplate.tsx +94 -94
- package/src/templates/ButtonTemplates/AddButton.tsx +29 -29
- package/src/templates/ButtonTemplates/IconButton.tsx +49 -49
- package/src/templates/ButtonTemplates/SubmitButton.tsx +29 -29
- package/src/templates/ButtonTemplates/index.ts +16 -16
- package/src/templates/DescriptionField.tsx +29 -29
- package/src/templates/ErrorList.tsx +25 -25
- package/src/templates/FieldTemplate/FieldTemplate.tsx +39 -39
- package/src/templates/FieldTemplate/Label.tsx +29 -29
- package/src/templates/FieldTemplate/WrapIfAdditional.tsx +85 -85
- package/src/templates/FieldTemplate/index.ts +3 -3
- package/src/templates/ObjectFieldTemplate.tsx +79 -79
- package/src/templates/TitleField.tsx +20 -20
- package/src/templates/UnsupportedField.tsx +29 -29
- package/src/templates/index.ts +32 -32
- package/src/types/Message.type.ts +6 -6
- package/src/types/RawMessage.type.ts +15 -15
- package/src/types/form-builder.ts +135 -135
- package/src/types/utils.type.ts +1 -1
- package/src/utils/form-builder.ts +424 -424
- package/src/utils/processSelectValue.ts +50 -50
- package/src/widgets/AltDateTimeWidget.tsx +17 -17
- package/src/widgets/AltDateWidget.tsx +216 -216
- package/src/widgets/CheckboxWidget.tsx +80 -80
- package/src/widgets/CheckboxesWidget.tsx +74 -74
- package/src/widgets/ColorWidget.tsx +26 -26
- package/src/widgets/DateTimeWidget.tsx +28 -28
- package/src/widgets/DateWidget.tsx +36 -36
- package/src/widgets/EmailWidget.tsx +19 -19
- package/src/widgets/FileWidget.tsx +144 -144
- package/src/widgets/HiddenWidget.tsx +22 -22
- package/src/widgets/PasswordWidget.tsx +20 -20
- package/src/widgets/RadioWidget.tsx +87 -87
- package/src/widgets/RangeWidget.tsx +24 -24
- package/src/widgets/SelectWidget.tsx +99 -99
- package/src/widgets/TextWidget.tsx +19 -19
- package/src/widgets/TextareaWidget.tsx +64 -64
- package/src/widgets/URLWidget.tsx +19 -19
- package/src/widgets/UpDownWidget.tsx +20 -20
- package/src/widgets/index.ts +43 -43
- package/tsconfig.json +24 -24
- package/tsconfig.node.json +10 -10
- 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;
|