Skip to content

Commit a731477

Browse files
committed
fix: handle 0 as a valid initial value
1 parent 974baf2 commit a731477

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

demo/Form2Examples.jsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ export function Form2Examples() {
77
<div>
88
Alternative Form implementation
99
<Form2
10-
initialValues={{ attrA: 'ABC', Obj: { x: 'X' }, arr: [1, 2, 3], arrObj: [{ o: 1 }, { o: 2 }, { o: 3 }] }}
11-
onSubmit={console.info.bind(console, 'onSubmit')}
12-
onChange={console.info.bind(console, 'onChange')}
10+
initialValues={{ attrA: 'ABC', Obj: { x: 'X', z: 0 }, arr: [1, 2, 3], arrObj: [{ o: 1 }, { o: 2 }, { o: 3 }] }}
11+
onSubmit={(data) => console.log('onSubmit', data)}
12+
onChange={(data) => console.log('onChange', data)}
1313
transform={(formData) => {
1414
console.log('transform', formData);
1515

@@ -42,6 +42,9 @@ export function Form2Examples() {
4242
<div className="col">
4343
<FormInput2 name="Obj.y" />
4444
</div>
45+
<div className="col">
46+
<FormInput2 name="Obj.z" type="number" step="0.1" />
47+
</div>
4548
</div>
4649
</div>
4750
<div className="form-group">

src/forms2/helpers/useFormControl.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isFunction } from 'js-var-type';
1+
import { isDefined, isFunction } from 'js-var-type';
22
import { useCallback, useContext, useEffect, useState } from 'react';
33
import { decode, getTargetValue } from '../../forms/helpers/form-helpers';
44
import { FormContext } from './useFormHelper';
@@ -17,7 +17,7 @@ export function useFormControl2(name, type) {
1717

1818
formHelper.notify(name, nextValue);
1919

20-
return nextValue;
20+
return isDefined(nextValue) ? nextValue : '';
2121
});
2222
},
2323
[formHelper, name]

src/forms2/helpers/useFormHelper.jsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useRef } from 'react';
22
import { debounce } from 'lodash-es';
3+
import { isDefined } from 'js-var-type';
34
import { flattenObject, getValueByPath, setValueByPath } from '../../utils/getters-setters';
45

56
export const FormContext = React.createContext(null);
@@ -15,7 +16,7 @@ export class FormHelper {
1516
register(name, formControl) {
1617
const value = getValueByPath(this.formData, name);
1718

18-
if (value) {
19+
if (isDefined(value)) {
1920
formControl.setValue(value);
2021
}
2122

0 commit comments

Comments
 (0)