@springmicro/forms 0.1.3
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 +18 -0
- package/README.md +11 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +24760 -0
- package/dist/index.umd.cjs +105 -0
- package/package.json +46 -0
- package/src/fields/ArrayField.tsx +875 -0
- package/src/fields/BooleanField.tsx +110 -0
- package/src/fields/MultiSchemaField.tsx +236 -0
- package/src/fields/NullField.tsx +22 -0
- package/src/fields/NumberField.tsx +87 -0
- package/src/fields/ObjectField.tsx +338 -0
- package/src/fields/SchemaField.tsx +402 -0
- package/src/fields/StringField.tsx +67 -0
- package/src/fields/index.ts +24 -0
- package/src/index.tsx +17 -0
- package/src/interfaces/MessagesProps.interface.ts +5 -0
- package/src/interfaces/Option.interface.ts +4 -0
- package/src/styles/select.styles.ts +28 -0
- package/src/templates/ArrayFieldDescriptionTemplate.tsx +42 -0
- package/src/templates/ArrayFieldItemTemplate.tsx +78 -0
- package/src/templates/ArrayFieldTemplate.tsx +90 -0
- package/src/templates/ArrayFieldTitleTemplate.tsx +44 -0
- package/src/templates/BaseInputTemplate.tsx +94 -0
- package/src/templates/ButtonTemplates/AddButton.tsx +29 -0
- package/src/templates/ButtonTemplates/IconButton.tsx +49 -0
- package/src/templates/ButtonTemplates/SubmitButton.tsx +29 -0
- package/src/templates/ButtonTemplates/index.ts +16 -0
- package/src/templates/DescriptionField.tsx +29 -0
- package/src/templates/ErrorList.tsx +25 -0
- package/src/templates/FieldTemplate/FieldTemplate.tsx +39 -0
- package/src/templates/FieldTemplate/Label.tsx +29 -0
- package/src/templates/FieldTemplate/WrapIfAdditional.tsx +85 -0
- package/src/templates/FieldTemplate/index.ts +3 -0
- package/src/templates/ObjectFieldTemplate.tsx +79 -0
- package/src/templates/TitleField.tsx +20 -0
- package/src/templates/UnsupportedField.tsx +29 -0
- package/src/templates/index.ts +32 -0
- package/src/types/Message.type.ts +6 -0
- package/src/types/RawMessage.type.ts +15 -0
- package/src/utils/processSelectValue.ts +50 -0
- package/src/widgets/AltDateTimeWidget.tsx +17 -0
- package/src/widgets/AltDateWidget.tsx +216 -0
- package/src/widgets/CheckboxWidget.tsx +80 -0
- package/src/widgets/CheckboxesWidget.tsx +74 -0
- package/src/widgets/ColorWidget.tsx +26 -0
- package/src/widgets/DateTimeWidget.tsx +28 -0
- package/src/widgets/DateWidget.tsx +36 -0
- package/src/widgets/EmailWidget.tsx +19 -0
- package/src/widgets/FileWidget.tsx +144 -0
- package/src/widgets/HiddenWidget.tsx +22 -0
- package/src/widgets/PasswordWidget.tsx +20 -0
- package/src/widgets/RadioWidget.tsx +87 -0
- package/src/widgets/RangeWidget.tsx +24 -0
- package/src/widgets/SelectWidget.tsx +99 -0
- package/src/widgets/TextWidget.tsx +19 -0
- package/src/widgets/TextareaWidget.tsx +64 -0
- package/src/widgets/URLWidget.tsx +19 -0
- package/src/widgets/UpDownWidget.tsx +20 -0
- package/src/widgets/index.ts +43 -0
- package/tsconfig.json +24 -0
- package/tsconfig.node.json +10 -0
- package/vite.config.ts +25 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type {
|
|
3
|
+
FieldProps,
|
|
4
|
+
EnumOptionsType,
|
|
5
|
+
RJSFSchema,
|
|
6
|
+
GenericObjectType,
|
|
7
|
+
} from "@rjsf/utils";
|
|
8
|
+
import { getWidget, getUiOptions, optionsList } from "@rjsf/utils";
|
|
9
|
+
import isObject from "lodash/isObject";
|
|
10
|
+
|
|
11
|
+
/** The `BooleanField` component is used to render a field in the schema is boolean. It constructs `enumOptions` for the
|
|
12
|
+
* two boolean values based on the various alternatives in the schema.
|
|
13
|
+
*
|
|
14
|
+
* @param props - The `FieldProps` for this template
|
|
15
|
+
*/
|
|
16
|
+
function BooleanField<T = any, F extends GenericObjectType = any>(
|
|
17
|
+
props: FieldProps<T, F>
|
|
18
|
+
) {
|
|
19
|
+
const {
|
|
20
|
+
schema,
|
|
21
|
+
name,
|
|
22
|
+
uiSchema,
|
|
23
|
+
idSchema,
|
|
24
|
+
formData,
|
|
25
|
+
registry,
|
|
26
|
+
required,
|
|
27
|
+
disabled,
|
|
28
|
+
readonly,
|
|
29
|
+
autofocus,
|
|
30
|
+
onChange,
|
|
31
|
+
onFocus,
|
|
32
|
+
onBlur,
|
|
33
|
+
rawErrors,
|
|
34
|
+
} = props;
|
|
35
|
+
const { title } = schema;
|
|
36
|
+
const { widgets, formContext } = registry;
|
|
37
|
+
const { widget = "checkbox", ...options } = getUiOptions<T, F>(uiSchema);
|
|
38
|
+
const Widget = getWidget(schema, widget, widgets);
|
|
39
|
+
|
|
40
|
+
let enumOptions: EnumOptionsType<any>[] | undefined;
|
|
41
|
+
|
|
42
|
+
if (Array.isArray(schema.oneOf)) {
|
|
43
|
+
enumOptions = optionsList({
|
|
44
|
+
oneOf: schema.oneOf
|
|
45
|
+
.map((option) => {
|
|
46
|
+
if (isObject(option)) {
|
|
47
|
+
return {
|
|
48
|
+
...option,
|
|
49
|
+
// @ts-ignore
|
|
50
|
+
title: option.title || (option.const === true ? "Yes" : "No"),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
return undefined;
|
|
54
|
+
})
|
|
55
|
+
.filter((o) => o) as any[], // cast away the error that typescript can't grok is fixed
|
|
56
|
+
}) as EnumOptionsType<any>[] | undefined;
|
|
57
|
+
} else {
|
|
58
|
+
// We deprecated enumNames in v5. It's intentionally omitted from RSJFSchema type, so we need to cast here.
|
|
59
|
+
const schemaWithEnumNames = schema as RJSFSchema & { enumNames?: string[] };
|
|
60
|
+
// @ts-ignore
|
|
61
|
+
schema.enum = schema.enum ?? [true, false];
|
|
62
|
+
if (
|
|
63
|
+
!schemaWithEnumNames.enumNames &&
|
|
64
|
+
schema.enum &&
|
|
65
|
+
schema.enum.length === 2 &&
|
|
66
|
+
schema.enum.every((v: any) => typeof v === "boolean")
|
|
67
|
+
) {
|
|
68
|
+
enumOptions = [
|
|
69
|
+
{
|
|
70
|
+
value: schema.enum[0],
|
|
71
|
+
label: schema.enum[0] ? "Yes" : "No",
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
value: schema.enum[1],
|
|
75
|
+
label: schema.enum[1] ? "Yes" : "No",
|
|
76
|
+
},
|
|
77
|
+
];
|
|
78
|
+
} else {
|
|
79
|
+
enumOptions = optionsList({
|
|
80
|
+
enum: schema.enum,
|
|
81
|
+
// NOTE: enumNames is deprecated, but still supported for now.
|
|
82
|
+
enumNames: schemaWithEnumNames.enumNames,
|
|
83
|
+
}) as EnumOptionsType<any>[] | undefined;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return (
|
|
88
|
+
<Widget
|
|
89
|
+
name={name}
|
|
90
|
+
options={{ ...options, enumOptions }}
|
|
91
|
+
schema={schema}
|
|
92
|
+
uiSchema={uiSchema}
|
|
93
|
+
id={idSchema && idSchema.$id}
|
|
94
|
+
onChange={onChange}
|
|
95
|
+
onFocus={onFocus}
|
|
96
|
+
onBlur={onBlur}
|
|
97
|
+
label={title === undefined ? name : title}
|
|
98
|
+
value={formData}
|
|
99
|
+
required={required}
|
|
100
|
+
disabled={disabled}
|
|
101
|
+
readonly={readonly}
|
|
102
|
+
registry={registry}
|
|
103
|
+
formContext={formContext}
|
|
104
|
+
autofocus={autofocus}
|
|
105
|
+
rawErrors={rawErrors}
|
|
106
|
+
/>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export default BooleanField;
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import React, { Component } from "react";
|
|
2
|
+
import type { FieldProps, RJSFSchema, GenericObjectType } from "@rjsf/utils";
|
|
3
|
+
import { getUiOptions, getWidget, guessType, deepEquals } from "@rjsf/utils";
|
|
4
|
+
import unset from "lodash/unset";
|
|
5
|
+
|
|
6
|
+
/** Type used for the state of the `AnyOfField` component */
|
|
7
|
+
type AnyOfFieldState = {
|
|
8
|
+
/** The currently selected option */
|
|
9
|
+
selectedOption: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/** The `AnyOfField` component is used to render a field in the schema that is an `anyOf`, `allOf` or `oneOf`. It tracks
|
|
13
|
+
* the currently selected option and cleans up any irrelevant data in `formData`.
|
|
14
|
+
*
|
|
15
|
+
* @param props - The `FieldProps` for this template
|
|
16
|
+
*/
|
|
17
|
+
class AnyOfField<T = any, F extends GenericObjectType = any> extends Component<
|
|
18
|
+
FieldProps<T, F>
|
|
19
|
+
// AnyOfFieldState
|
|
20
|
+
> {
|
|
21
|
+
/** Constructs an `AnyOfField` with the given `props` to initialize the initially selected option in state
|
|
22
|
+
*
|
|
23
|
+
* @param props - The `FieldProps` for this template
|
|
24
|
+
*/
|
|
25
|
+
constructor(props: FieldProps<T, F>) {
|
|
26
|
+
super(props);
|
|
27
|
+
|
|
28
|
+
const { formData, options } = this.props;
|
|
29
|
+
|
|
30
|
+
this.state = {
|
|
31
|
+
selectedOption: this.getMatchingOption(
|
|
32
|
+
0,
|
|
33
|
+
formData as unknown as T,
|
|
34
|
+
options
|
|
35
|
+
),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** React lifecycle methos that is called when the props and/or state for this component is updated. It recomputes the
|
|
40
|
+
* currently selected option based on the overall `formData`
|
|
41
|
+
*
|
|
42
|
+
* @param prevProps - The previous `FieldProps` for this template
|
|
43
|
+
* @param prevState - The previous `AnyOfFieldState` for this template
|
|
44
|
+
*/
|
|
45
|
+
componentDidUpdate(
|
|
46
|
+
prevProps: Readonly<FieldProps<T, F>>,
|
|
47
|
+
prevState: Readonly<AnyOfFieldState>
|
|
48
|
+
) {
|
|
49
|
+
const { formData, options, idSchema } = this.props;
|
|
50
|
+
// @ts-ignore
|
|
51
|
+
const { selectedOption } = this.state;
|
|
52
|
+
if (
|
|
53
|
+
!deepEquals(formData, prevProps.formData) &&
|
|
54
|
+
idSchema.$id === prevProps.idSchema.$id
|
|
55
|
+
) {
|
|
56
|
+
const matchingOption = this.getMatchingOption(
|
|
57
|
+
selectedOption,
|
|
58
|
+
// @ts-ignore
|
|
59
|
+
formData,
|
|
60
|
+
options
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
if (!prevState || matchingOption === selectedOption) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
this.setState({
|
|
68
|
+
selectedOption: matchingOption,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Determines the best matching option for the given `formData` and `options`.
|
|
74
|
+
*
|
|
75
|
+
* @param formData - The new formData
|
|
76
|
+
* @param options - The list of options to choose from
|
|
77
|
+
* @return - The index of the `option` that best matches the `formData`
|
|
78
|
+
*/
|
|
79
|
+
getMatchingOption(
|
|
80
|
+
selectedOption: number,
|
|
81
|
+
formData: T,
|
|
82
|
+
options: RJSFSchema[]
|
|
83
|
+
) {
|
|
84
|
+
const { schemaUtils } = this.props.registry;
|
|
85
|
+
// @ts-ignore
|
|
86
|
+
const option = schemaUtils.getMatchingOption(formData, options);
|
|
87
|
+
if (option !== 0) {
|
|
88
|
+
return option;
|
|
89
|
+
}
|
|
90
|
+
// If the form data matches none of the options, use the currently selected
|
|
91
|
+
// option, assuming it's available; otherwise use the first option
|
|
92
|
+
return selectedOption || 0;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Callback handler to remember what the currently selected option is. In addition to that the `formData` is updated
|
|
96
|
+
* to remove properties that are not part of the newly selected option schema, and then the updated data is passed to
|
|
97
|
+
* the `onChange` handler.
|
|
98
|
+
*
|
|
99
|
+
* @param option -
|
|
100
|
+
*/
|
|
101
|
+
onOptionChange = (option: any) => {
|
|
102
|
+
const selectedOption = parseInt(option, 10);
|
|
103
|
+
const { formData, onChange, options, registry } = this.props;
|
|
104
|
+
const { schemaUtils } = registry;
|
|
105
|
+
const newOption = schemaUtils.retrieveSchema(
|
|
106
|
+
options[selectedOption],
|
|
107
|
+
formData
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
// If the new option is of type object and the current data is an object,
|
|
111
|
+
// discard properties added using the old option.
|
|
112
|
+
let newFormData: T | undefined = undefined;
|
|
113
|
+
if (
|
|
114
|
+
guessType(formData) === "object" &&
|
|
115
|
+
(newOption.type === "object" || newOption.properties)
|
|
116
|
+
) {
|
|
117
|
+
newFormData = Object.assign({}, formData);
|
|
118
|
+
|
|
119
|
+
const optionsToDiscard = options.slice();
|
|
120
|
+
optionsToDiscard.splice(selectedOption, 1);
|
|
121
|
+
|
|
122
|
+
// Discard any data added using other options
|
|
123
|
+
for (const option of optionsToDiscard) {
|
|
124
|
+
if (option.properties) {
|
|
125
|
+
for (const key in option.properties) {
|
|
126
|
+
// @ts-ignore
|
|
127
|
+
if (key in newFormData) {
|
|
128
|
+
unset(newFormData, key);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
// Call getDefaultFormState to make sure defaults are populated on change.
|
|
135
|
+
onChange(
|
|
136
|
+
schemaUtils.getDefaultFormState(options[selectedOption], newFormData) as T
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
this.setState({
|
|
140
|
+
selectedOption: parseInt(option, 10),
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/** Renders the `AnyOfField` selector along with a `SchemaField` for the value of the `formData`
|
|
145
|
+
*/
|
|
146
|
+
render() {
|
|
147
|
+
const {
|
|
148
|
+
name,
|
|
149
|
+
baseType,
|
|
150
|
+
disabled = false,
|
|
151
|
+
readonly = false,
|
|
152
|
+
hideError = false,
|
|
153
|
+
errorSchema = {},
|
|
154
|
+
formData,
|
|
155
|
+
formContext,
|
|
156
|
+
idPrefix,
|
|
157
|
+
idSeparator,
|
|
158
|
+
idSchema,
|
|
159
|
+
onBlur,
|
|
160
|
+
onChange,
|
|
161
|
+
onFocus,
|
|
162
|
+
options,
|
|
163
|
+
registry,
|
|
164
|
+
uiSchema,
|
|
165
|
+
schema,
|
|
166
|
+
} = this.props;
|
|
167
|
+
|
|
168
|
+
const { widgets, fields } = registry;
|
|
169
|
+
const { SchemaField: _SchemaField } = fields;
|
|
170
|
+
// @ts-ignore
|
|
171
|
+
const { selectedOption } = this.state;
|
|
172
|
+
const { widget = "select", ...uiOptions } = getUiOptions<T, F>(uiSchema);
|
|
173
|
+
const Widget = getWidget<T, F>({ type: "number" }, widget, widgets);
|
|
174
|
+
|
|
175
|
+
const option = options[selectedOption] || null;
|
|
176
|
+
let optionSchema;
|
|
177
|
+
|
|
178
|
+
if (option) {
|
|
179
|
+
// If the subschema doesn't declare a type, infer the type from the
|
|
180
|
+
// parent schema
|
|
181
|
+
optionSchema = option.type
|
|
182
|
+
? option
|
|
183
|
+
: Object.assign({}, option, { type: baseType });
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const enumOptions = options.map((option: RJSFSchema, index: number) => ({
|
|
187
|
+
label: option.title || `Option ${index + 1}`,
|
|
188
|
+
value: index,
|
|
189
|
+
}));
|
|
190
|
+
|
|
191
|
+
return (
|
|
192
|
+
<div className="panel panel-default panel-body">
|
|
193
|
+
<div className="form-group">
|
|
194
|
+
<Widget
|
|
195
|
+
id={`${idSchema.$id}${
|
|
196
|
+
schema.oneOf ? "__oneof_select" : "__anyof_select"
|
|
197
|
+
}`}
|
|
198
|
+
// @ts-ignore
|
|
199
|
+
schema={{ type: "number", default: 0 }}
|
|
200
|
+
onChange={this.onOptionChange}
|
|
201
|
+
onBlur={onBlur}
|
|
202
|
+
onFocus={onFocus}
|
|
203
|
+
value={selectedOption}
|
|
204
|
+
options={{ enumOptions }}
|
|
205
|
+
registry={registry}
|
|
206
|
+
formContext={formContext}
|
|
207
|
+
{...uiOptions}
|
|
208
|
+
label=""
|
|
209
|
+
/>
|
|
210
|
+
</div>
|
|
211
|
+
{option !== null && (
|
|
212
|
+
<_SchemaField
|
|
213
|
+
name={name}
|
|
214
|
+
schema={optionSchema}
|
|
215
|
+
uiSchema={uiSchema}
|
|
216
|
+
errorSchema={errorSchema}
|
|
217
|
+
idSchema={idSchema}
|
|
218
|
+
idPrefix={idPrefix}
|
|
219
|
+
idSeparator={idSeparator}
|
|
220
|
+
formData={formData}
|
|
221
|
+
formContext={formContext}
|
|
222
|
+
onChange={onChange}
|
|
223
|
+
onBlur={onBlur}
|
|
224
|
+
onFocus={onFocus}
|
|
225
|
+
registry={registry}
|
|
226
|
+
disabled={disabled}
|
|
227
|
+
readonly={readonly}
|
|
228
|
+
hideError={hideError}
|
|
229
|
+
/>
|
|
230
|
+
)}
|
|
231
|
+
</div>
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export default AnyOfField;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
import type { FieldProps, GenericObjectType } from "@rjsf/utils";
|
|
3
|
+
|
|
4
|
+
/** The `NullField` component is used to render a field in the schema is null. It also ensures that the `formData` is
|
|
5
|
+
* also set to null if it has no value.
|
|
6
|
+
*
|
|
7
|
+
* @param props - The `FieldProps` for this template
|
|
8
|
+
*/
|
|
9
|
+
function NullField<T = any, F extends GenericObjectType = any>(
|
|
10
|
+
props: FieldProps<T, F>
|
|
11
|
+
) {
|
|
12
|
+
const { formData, onChange } = props;
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
if (formData === undefined) {
|
|
15
|
+
onChange(null as unknown as T);
|
|
16
|
+
}
|
|
17
|
+
}, [formData, onChange]);
|
|
18
|
+
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default NullField;
|
|
@@ -0,0 +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;
|