Skip to content

Commit 85d65cd

Browse files
committed
feat(forms): allow data change monitoring events
1 parent 5311904 commit 85d65cd

File tree

5 files changed

+24
-11
lines changed

5 files changed

+24
-11
lines changed

demo/FormExamples.jsx

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export function FormExamples() {
1515
return (
1616
<Form
1717
initialValues={{ textField: 'abc' }}
18+
onChange={console.info}
1819
onSubmit={(formData, reset) => {
1920
console.log('onSubmit', formData);
2021
// return Promise.resolve();

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"eslint": "^6.8.0",
3737
"eslint-plugin-react": "^7.19.0",
3838
"jest": "^25.2.3",
39+
"lodash-es": "^4.17.15",
3940
"prettier": "^2.0.2",
4041
"react": "^16.13.1",
4142
"react-dom": "^16.13.1",

src/forms/Form.jsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ export function Form({
1212
initialValues,
1313
onCancel,
1414
onSubmit,
15+
onChange,
1516
submitLabel,
1617
validations,
1718
}) {
18-
const formState = useForm(initialValues, validations);
19+
const formState = useForm(initialValues, validations, onChange);
1920
const formRef = useRef(null);
2021

2122
function resetForm() {
@@ -72,12 +73,13 @@ Form.defaultProps = {
7273

7374
Form.propTypes = {
7475
cancelLabel: PropTypes.string,
75-
children: PropTypes.oneOfType([PropTypes.element, PropTypes.arrayOf(PropTypes.element)]),
76+
children: PropTypes.oneOfType([PropTypes.node, PropTypes.arrayOf(PropTypes.node)]),
7677
customValidation: PropTypes.bool,
77-
customActions: PropTypes.oneOfType([PropTypes.element, PropTypes.arrayOf(PropTypes.element)]),
78+
customActions: PropTypes.oneOfType([PropTypes.node, PropTypes.arrayOf(PropTypes.node)]),
7879
initialValues: PropTypes.object,
7980
onCancel: PropTypes.func,
8081
onSubmit: PropTypes.func.isRequired,
82+
onChange: PropTypes.func,
8183
submitLabel: PropTypes.string,
8284
validations: PropTypes.object,
8385
};

src/forms/FormAutocomplete.jsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ export function FormAutocomplete({
4242
ref={searchInputRef}
4343
className={`form-control ${isFocused ? '' : 'd-none'} ${controlFeedback}`}
4444
onChange={handleInputChange.bind(null, {
45-
update(_, value) {
46-
setSearchValue(value);
47-
onSearch(value);
45+
update(_, nextSearchValue) {
46+
setSearchValue(nextSearchValue);
47+
onSearch(nextSearchValue);
4848
open();
4949

50-
if (value) {
50+
if (nextSearchValue && value) {
5151
setValue(null);
5252
}
5353
},
@@ -56,7 +56,7 @@ export function FormAutocomplete({
5656
if (openOnFocus) {
5757
setTimeout(() => {
5858
open();
59-
}, 250);
59+
}, 100);
6060
}
6161
}}
6262
onBlur={() => {
@@ -110,7 +110,7 @@ export function FormAutocomplete({
110110
setTimeout(() => {
111111
setFocus(false);
112112
close();
113-
}, 250);
113+
}, 100);
114114
}}
115115
template={template}
116116
onTouchStart={() => setIgnoreBlur(true)}

src/forms/helpers/useForm.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
import { useState } from 'react';
1+
import { useState, useEffect, useCallback } from 'react';
22
import { useArrayValueMap } from '../../utils/useValueMap';
33
import { setValueByPath, deepClone, getValueByPath } from '../../utils/getters-setters';
44
import { validateFormElement } from './form-helpers';
5+
import { debounce } from 'lodash-es';
56

6-
export function useForm(initialState, validations) {
7+
export function useForm(initialState, validations, onChange) {
78
const [formState, setFormState] = useState(initialState);
89
const [submitAttempted, setSubmitAttempted] = useState(false);
910
const { getAllKeys: getElementNames, get: getElementRefs, push: registerElementRef } = useArrayValueMap();
1011

12+
if (onChange) {
13+
const _onChange = useCallback(debounce(onChange, 500), []);
14+
15+
useEffect(() => {
16+
_onChange(formState);
17+
}, [formState]);
18+
}
19+
1120
return {
1221
register(name, elementRef) {
1322
registerElementRef(name, elementRef);

0 commit comments

Comments
 (0)