Skip to content

Commit 8d7b56e

Browse files
committed
feat(forms): include value parsing from type prop
1 parent dc0432f commit 8d7b56e

File tree

3 files changed

+52
-27
lines changed

3 files changed

+52
-27
lines changed

demo/FormExamples.jsx

+7-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function FormExamples() {
3636
onCancel={() => console.log('onCancel')}
3737
customValidation={true}
3838
validations={{
39-
textField3: [
39+
numberField: [
4040
{
4141
message: 'Must be filled if textField is not empty',
4242
validate(value, formData) {
@@ -46,9 +46,9 @@ export function FormExamples() {
4646
],
4747
autocompleteField1: [
4848
{
49-
message: 'Must be filled if textField3 is empty',
49+
message: 'Must be filled if numberField is empty',
5050
validate(value, formData) {
51-
return formData.textField3 || value;
51+
return formData.numberField || value;
5252
},
5353
},
5454
],
@@ -102,7 +102,10 @@ export function FormExamples() {
102102
<FormGroupInput name="textField2" label="Text field 2" required placeholder="Fill some value" />
103103
</div>
104104
<div className="col">
105-
<FormGroupInput name="textField3" label="Text field 3" type="number" />
105+
<FormGroupInput name="numberField" label="Number field" type="number" />
106+
</div>
107+
<div className="col">
108+
<FormGroupInput name="dateField" label="Date field " type="datetime-local" />
106109
</div>
107110
</div>
108111

src/forms/FormInput.jsx

+10-10
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ import PropTypes from 'prop-types';
33
import { useFormControl } from './helpers/useFormControl';
44

55
export function FormInput({ id, type, name, placeholder, required, minLength, maxLength, min, max, pattern, step }) {
6-
const { getValue, handleOnChange, register } = useFormControl(name);
6+
const { getValue, handleOnChange, register } = useFormControl(name, type);
77
const registerRef = useCallback(register, []);
88

9-
return (
10-
<input
11-
{...{ required, name, id, placeholder, type, minLength, maxLength, min, max, pattern, step }}
12-
className="form-control"
13-
onChange={handleOnChange}
14-
value={getValue()}
15-
ref={registerRef}
16-
/>
17-
);
9+
const attrs = { required, name, id, placeholder, type, minLength, maxLength, min, max, pattern, step };
10+
11+
if (type === 'datetime-local') {
12+
attrs.defaultValue = getValue();
13+
} else {
14+
attrs.value = getValue();
15+
}
16+
17+
return <input {...attrs} className="form-control" onChange={handleOnChange} ref={registerRef} />;
1818
}
1919

2020
FormInput.defaultProps = {

src/forms/helpers/useFormControl.js

+35-13
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,10 @@ export function useFormControl(name, type) {
99
}
1010

1111
return {
12-
getValue: () => formState.getValue(name) || getEmptyValue(type),
12+
getValue: () => encode(formState.getValue(name) || getEmptyValue(type), type),
1313
setValue,
1414
handleOnChange: ({ target }) => {
15-
let value = target.type === 'checkbox' ? target.checked : target.value;
16-
17-
if (target.type === 'select-one') {
18-
if (value && ['{', '['].includes(value[0])) {
19-
try {
20-
value = JSON.parse(value);
21-
} catch (error) {
22-
// eslint-disable-next-line no-console
23-
console.error(error);
24-
}
25-
}
26-
}
15+
const value = getTargetValue(target);
2716

2817
setValue(value);
2918
},
@@ -48,3 +37,36 @@ function getEmptyValue(type) {
4837
return '';
4938
}
5039
}
40+
41+
function encode(value, type) {
42+
if (type === 'datetime-local') {
43+
return value && value.toISOString().slice(0, 16);
44+
}
45+
46+
return value;
47+
}
48+
49+
function getTargetValue(target) {
50+
let value = target.type === 'checkbox' ? target.checked : target.value;
51+
52+
if (target.type === 'number') {
53+
value = target.valueAsNumber;
54+
}
55+
56+
if (target.type === 'datetime-local') {
57+
value = new Date(Date.parse(target.value));
58+
}
59+
60+
if (target.type === 'select-one') {
61+
if (value && ['{', '['].includes(value[0])) {
62+
try {
63+
value = JSON.parse(value);
64+
} catch (error) {
65+
// eslint-disable-next-line no-console
66+
console.error(error);
67+
}
68+
}
69+
}
70+
71+
return value;
72+
}

0 commit comments

Comments
 (0)