Skip to content

Commit ddb7b71

Browse files
committed
fix(forms): check if value is null before rendering input controls
1 parent a4d22a4 commit ddb7b71

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

demo/FormExamples.jsx

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export function FormExamples() {
2121
switchField2: true,
2222
checkboxField2: true,
2323
radioField2: 'b',
24+
numberField: null,
2425
dateField: new Date().toISOString(),
2526
}}
2627
onChange={console.info}

src/forms/helpers/useFormControl.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useContext } from 'react';
22
import { FormContext } from './form-helpers';
33
import { toDatetimeLocal, fromDatetimeLocal } from '../../utils/formatters';
4+
import { isNull } from '../../utils/types';
45

56
export function useFormControl(name, type) {
67
const formState = useContext(FormContext);
@@ -40,7 +41,7 @@ function getEmptyValue(type) {
4041
}
4142

4243
function encode(value, type) {
43-
if (typeof value === 'undefined') {
44+
if (typeof value === 'undefined' || isNull(value)) {
4445
return getEmptyValue(type);
4546
}
4647

src/utils/types.js

+4
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ export function isString(value) {
1717
export function isDate(value) {
1818
return Object.prototype.toString.call(value) === '[object Date]';
1919
}
20+
21+
export function isNull(value) {
22+
return value === null;
23+
}

0 commit comments

Comments
 (0)