@danderson00/react-forms 0.0.48 → 0.0.50
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/README.md +203 -203
- package/TODO.md +2 -2
- package/dist/ErrorMessage.js +2 -4
- package/dist/ErrorMessage.js.map +1 -1
- package/dist/Form.js +18 -19
- package/dist/Form.js.map +1 -1
- package/dist/Group.js +9 -10
- package/dist/Group.js.map +1 -1
- package/dist/index.js +21 -33
- package/dist/index.js.map +1 -1
- package/dist/injectCss.js.map +1 -1
- package/dist/input.js +26 -21
- package/dist/input.js.map +1 -1
- package/dist/inputTypes.js +7 -9
- package/dist/inputTypes.js.map +1 -1
- package/dist/styles.js +1 -1
- package/dist/styles.js.map +1 -1
- package/dist/submit.js +15 -17
- package/dist/submit.js.map +1 -1
- package/dist/throttle.js +1 -2
- package/dist/throttle.js.map +1 -1
- package/dist/useForm.js +5 -5
- package/dist/useForm.js.map +1 -1
- package/dist/useFormValues.js +1 -2
- package/dist/useFormValues.js.map +1 -1
- package/dist/usePrevious.js.map +1 -1
- package/dist/useUpdate.js.map +1 -1
- package/package.json +59 -59
- package/src/ErrorMessage.js +11 -11
- package/src/Form.js +123 -123
- package/src/Group.js +14 -14
- package/src/index.js +40 -40
- package/src/injectCss.js +15 -15
- package/src/input.js +186 -174
- package/src/inputTypes.js +63 -63
- package/src/styles.js +20 -20
- package/src/submit.js +29 -29
- package/src/throttle.js +10 -10
- package/src/useForm.js +15 -10
- package/src/useFormValues.js +4 -4
- package/src/usePrevious.js +8 -8
- package/src/useUpdate.js +11 -11
package/src/input.js
CHANGED
|
@@ -1,175 +1,187 @@
|
|
|
1
|
-
import { useCallback, useState, useEffect } from 'react'
|
|
2
|
-
import { get } from 'object-path-immutable'
|
|
3
|
-
import usePrevious from './usePrevious'
|
|
4
|
-
import useUpdate from './useUpdate'
|
|
5
|
-
import types from './inputTypes'
|
|
6
|
-
import createThrottled from './throttle'
|
|
7
|
-
|
|
8
|
-
const nextElementId = (id => () => `react-forms-${++id}`)(0)
|
|
9
|
-
|
|
10
|
-
export default (render, options = {}) => function Input(props) {
|
|
11
|
-
if (!props.name) throw new Error("You must provide a name prop to form components")
|
|
12
|
-
if (!props.setFieldValue) throw new Error("Input components must be contained within a Form component")
|
|
13
|
-
|
|
14
|
-
const [fieldValidated, setFieldValidated] = useState(false)
|
|
15
|
-
const [error, setError] = useState(false)
|
|
16
|
-
const [
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
//
|
|
32
|
-
//
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
//
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
//
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
//
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const
|
|
81
|
-
const {
|
|
82
|
-
const {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
//
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
if(
|
|
114
|
-
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
1
|
+
import { useCallback, useState, useEffect } from 'react'
|
|
2
|
+
import { get } from 'object-path-immutable'
|
|
3
|
+
import usePrevious from './usePrevious'
|
|
4
|
+
import useUpdate from './useUpdate'
|
|
5
|
+
import types from './inputTypes'
|
|
6
|
+
import createThrottled from './throttle'
|
|
7
|
+
|
|
8
|
+
const nextElementId = (id => () => `react-forms-${++id}`)(0)
|
|
9
|
+
|
|
10
|
+
export default (render, options = {}) => function Input(props) {
|
|
11
|
+
if (!props.name) throw new Error("You must provide a name prop to form components")
|
|
12
|
+
if (!props.setFieldValue) throw new Error("Input components must be contained within a Form component")
|
|
13
|
+
|
|
14
|
+
const [fieldValidated, setFieldValidated] = useState(false)
|
|
15
|
+
const [error, setError] = useState(false)
|
|
16
|
+
const [active, setActive] = useState(false)
|
|
17
|
+
const [id] = useState(props.id || nextElementId())
|
|
18
|
+
const previousValueProp = usePrevious(props.value)
|
|
19
|
+
|
|
20
|
+
// this is currently broken :-\
|
|
21
|
+
const { groups = [], throttle, onSubmit } = props
|
|
22
|
+
const name = groups.map(group => group + '.') + props.name
|
|
23
|
+
const submitHandler = (throttle && onSubmit) ? createThrottled(onSubmit, isNaN(throttle) ? 200 : throttle) : onSubmit
|
|
24
|
+
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
const { getFieldValue, setFieldValue, registerFieldValidator } = props
|
|
27
|
+
const { setInitialValue, applyValueTransforms } = types(props, options)
|
|
28
|
+
|
|
29
|
+
// don't override values that were set from passing a values prop to the form
|
|
30
|
+
if(getFieldValue(name) === undefined) {
|
|
31
|
+
// TODO: change setter to a getter and return a private `UNSET` symbol to indicate not to call the field setter
|
|
32
|
+
// this allows us to set the default value with useState above and not have an initial render cycle without the default value
|
|
33
|
+
// BUT... to get the default value of a select list, we need to render it first? dilemma....
|
|
34
|
+
|
|
35
|
+
setInitialValue({
|
|
36
|
+
// pass a setter function - radio buttons do not always want to set a value!
|
|
37
|
+
set: value => setFieldValue(name, applyValueTransforms(value)),
|
|
38
|
+
element: document.querySelector(`#${id}`)
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
registerFieldValidator(updateValidationState)
|
|
42
|
+
|
|
43
|
+
// TODO: need to unregister in case of dynamic form elements
|
|
44
|
+
}, [])
|
|
45
|
+
|
|
46
|
+
// ensure form values are updated to reflect passed `value` prop
|
|
47
|
+
// this probably introduces some redundancy with the onChange handler below
|
|
48
|
+
useUpdate(() => {
|
|
49
|
+
const { value, setFieldValue } = props
|
|
50
|
+
const { applyValueTransforms } = types(props, options)
|
|
51
|
+
|
|
52
|
+
if(value !== previousValueProp) {
|
|
53
|
+
setFieldValue(name, applyValueTransforms(value))
|
|
54
|
+
}
|
|
55
|
+
}, [props])
|
|
56
|
+
|
|
57
|
+
// when clicking submit, getFieldValue(s) return undefined due to an issue with closures I can't figure out
|
|
58
|
+
// the form component passes in its copy of field values as the oldValues parameter for this case
|
|
59
|
+
const updateValidationState = useCallback((newValue, oldValues) => {
|
|
60
|
+
const { getValidationMessage, getFieldValue, getFieldValues } = props
|
|
61
|
+
const element = document.querySelector(`#${id}`)
|
|
62
|
+
if(element) {
|
|
63
|
+
const validationMessage = getValidationMessage && getValidationMessage(
|
|
64
|
+
newValue === undefined ? (getFieldValue(name) || get(oldValues, name)) : newValue,
|
|
65
|
+
oldValues || getFieldValues()
|
|
66
|
+
)
|
|
67
|
+
element.setCustomValidity?.(validationMessage || "")
|
|
68
|
+
|
|
69
|
+
setFieldValidated(true)
|
|
70
|
+
// this is a little hacky, but the simplest way I could think of to determine the
|
|
71
|
+
// validity of the actual input element. :invalid is also not supported by enzyme
|
|
72
|
+
const invalid = !!validationMessage || Boolean(document.querySelector(`#${id}:invalid`))
|
|
73
|
+
setError(invalid)
|
|
74
|
+
props.onValidated && props.onValidated(!invalid)
|
|
75
|
+
}
|
|
76
|
+
}, [props])
|
|
77
|
+
|
|
78
|
+
// TODO: refactor bubbling to prop pattern
|
|
79
|
+
const onChange = useCallback((...args) => {
|
|
80
|
+
const [event] = args
|
|
81
|
+
const { setFieldValue, onChange, submitOnChange } = props
|
|
82
|
+
const { valueFromEvent } = options
|
|
83
|
+
const { getOutputValue, applyValueTransforms } = types(props, options)
|
|
84
|
+
|
|
85
|
+
const elementValue = valueFromEvent
|
|
86
|
+
? valueFromEvent.apply(null, args)
|
|
87
|
+
: getOutputValue({ event })
|
|
88
|
+
|
|
89
|
+
setFieldValue(name, applyValueTransforms(elementValue))
|
|
90
|
+
|
|
91
|
+
if(event && event.target && event.target.checkValidity) {
|
|
92
|
+
event.target.checkValidity()
|
|
93
|
+
updateValidationState(applyValueTransforms(elementValue))
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if(onChange) {
|
|
97
|
+
onChange.apply(event.target, args)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if(submitOnChange && submitHandler) {
|
|
101
|
+
// the setFieldValue above sets state on the Form component, used in the onSubmit handler
|
|
102
|
+
// setting state requires an additional render cycle - we need to pass in the new value so it's set correctly
|
|
103
|
+
submitHandler(undefined, { additionalValues: { [name]: applyValueTransforms(elementValue) } })
|
|
104
|
+
}
|
|
105
|
+
}, [props.setFieldValue, props.onChange, props.submitOnChange, props.setFieldValue, updateValidationState])
|
|
106
|
+
|
|
107
|
+
const onBlur = (...args) => {
|
|
108
|
+
const { submitOnBlur, onSubmit, onBlur } = props
|
|
109
|
+
const [event] = args
|
|
110
|
+
|
|
111
|
+
setActive(false)
|
|
112
|
+
|
|
113
|
+
if(submitOnBlur) {
|
|
114
|
+
onSubmit()
|
|
115
|
+
}
|
|
116
|
+
if(onBlur) {
|
|
117
|
+
onBlur.apply(event.target, args)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const onFocus = (...args) => {
|
|
122
|
+
const { selectOnFocus, onFocus } = props
|
|
123
|
+
const [event] = args
|
|
124
|
+
const element = event.target
|
|
125
|
+
|
|
126
|
+
setActive(true)
|
|
127
|
+
|
|
128
|
+
const selectText = () => {
|
|
129
|
+
element.selectionStart = 0
|
|
130
|
+
element.selectionEnd = element.value.length
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if(selectOnFocus) {
|
|
134
|
+
selectText()
|
|
135
|
+
setTimeout(selectText)
|
|
136
|
+
}
|
|
137
|
+
if(onFocus) {
|
|
138
|
+
onFocus.apply(element, args)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const onKeyDown = (...args) => {
|
|
143
|
+
const { onKeyDown } = props
|
|
144
|
+
const [event] = args
|
|
145
|
+
const element = event.target
|
|
146
|
+
|
|
147
|
+
if(props.numeric && !/[0-9-]/.exec(event.key) && ![null, 0, 8, 9, 13, 27, 35, 36, 37, 39, 46].includes(event.keyCode)) {
|
|
148
|
+
event.preventDefault()
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if(onKeyDown) {
|
|
152
|
+
onKeyDown.apply(element, args)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/* render */ {
|
|
157
|
+
const { passErrorProp } = options
|
|
158
|
+
const { getValueProps } = types(props, options)
|
|
159
|
+
|
|
160
|
+
const {
|
|
161
|
+
type, value, checked, defaultValue, numeric, getValidationMessage,
|
|
162
|
+
onSubmit, registerFieldValidator, setFieldValue, getFieldValue, getFieldValues,
|
|
163
|
+
submitOnChange, submitOnBlur, selectOnFocus, throttle, valid, onValidated,
|
|
164
|
+
...passThroughProps
|
|
165
|
+
} = props
|
|
166
|
+
const valueProps = getValueProps({ currentValue: getFieldValue(name) })
|
|
167
|
+
const numericProps = (numeric && { type: 'number' })
|
|
168
|
+
const typeProps = (type && { type })
|
|
169
|
+
const errorProps = (passErrorProp && { error })
|
|
170
|
+
const className = (
|
|
171
|
+
(
|
|
172
|
+
(fieldValidated ? 'validated ' : '')
|
|
173
|
+
+ (active ? 'active ' : '')
|
|
174
|
+
+ (props.className || '')
|
|
175
|
+
)
|
|
176
|
+
|| undefined
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
const finalProps = {
|
|
180
|
+
id, name, className,
|
|
181
|
+
...passThroughProps, ...valueProps, ...numericProps, ...typeProps, ...errorProps,
|
|
182
|
+
onChange, onFocus, onBlur, onKeyDown
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return render(finalProps)
|
|
186
|
+
}
|
|
175
187
|
}
|
package/src/inputTypes.js
CHANGED
|
@@ -1,63 +1,63 @@
|
|
|
1
|
-
const resolveValue = (value, args) => typeof value === 'function' ? value.apply(null, args) : value
|
|
2
|
-
|
|
3
|
-
export default (props, options) => {
|
|
4
|
-
const { type, checked, value, defaultValue } = props
|
|
5
|
-
|
|
6
|
-
const applyValueTransforms = value => ((props.type === 'number' || props.numeric) && !isNaN(+value) && value !== '') ? +value : value
|
|
7
|
-
|
|
8
|
-
const defaultType = {
|
|
9
|
-
setInitialValue: ({ set }) => {
|
|
10
|
-
if(value !== undefined) {
|
|
11
|
-
set(value)
|
|
12
|
-
} else if (defaultValue !== undefined) {
|
|
13
|
-
set(defaultValue)
|
|
14
|
-
} else if (options.defaultValue !== undefined) {
|
|
15
|
-
set(resolveValue(options.defaultValue, [props]))
|
|
16
|
-
} else {
|
|
17
|
-
set(undefined)
|
|
18
|
-
}
|
|
19
|
-
},
|
|
20
|
-
getOutputValue: ({ event }) => {
|
|
21
|
-
if (event.target && event.target.value !== undefined) {
|
|
22
|
-
return event.target.value
|
|
23
|
-
} else if (event.value !== undefined) {
|
|
24
|
-
return event.value
|
|
25
|
-
} else {
|
|
26
|
-
throw new Error("Unable to determine input value from event")
|
|
27
|
-
}
|
|
28
|
-
},
|
|
29
|
-
getValueProps: ({ currentValue }) => ({
|
|
30
|
-
value: currentValue === undefined ? '' : currentValue
|
|
31
|
-
})
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const checkboxValue = (checked, value) => (value === undefined
|
|
35
|
-
? Boolean(checked)
|
|
36
|
-
: checked ? value : undefined
|
|
37
|
-
)
|
|
38
|
-
|
|
39
|
-
const types = {
|
|
40
|
-
text: defaultType,
|
|
41
|
-
radio: {
|
|
42
|
-
setInitialValue: ({ set }) => checked && set(value),
|
|
43
|
-
getOutputValue: () => value,
|
|
44
|
-
getValueProps: ({ currentValue }) => ({ checked: applyValueTransforms(value) === currentValue })
|
|
45
|
-
},
|
|
46
|
-
checkbox: {
|
|
47
|
-
setInitialValue: ({ set }) => set(checkboxValue(checked, value)),
|
|
48
|
-
getOutputValue: ({ event }) => checkboxValue(event.target.checked, value),
|
|
49
|
-
getValueProps: ({ currentValue }) => ({ checked: Boolean(currentValue) })
|
|
50
|
-
},
|
|
51
|
-
select: {
|
|
52
|
-
...defaultType,
|
|
53
|
-
setInitialValue: ({ set, element }) => {
|
|
54
|
-
set(value || defaultValue ||
|
|
55
|
-
(props.options && props.options.length > 0 && props.options[0]) || // this probably should come out - is it the right behavior for all browsers? it's here to keep 1 unit test happy?
|
|
56
|
-
element && element.value || '' // this *should* work in all browsers (except jsdom)...
|
|
57
|
-
)
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return { ...(types[options.type || type] || types.text), applyValueTransforms }
|
|
63
|
-
}
|
|
1
|
+
const resolveValue = (value, args) => typeof value === 'function' ? value.apply(null, args) : value
|
|
2
|
+
|
|
3
|
+
export default (props, options) => {
|
|
4
|
+
const { type, checked, value, defaultValue } = props
|
|
5
|
+
|
|
6
|
+
const applyValueTransforms = value => ((props.type === 'number' || props.numeric) && !isNaN(+value) && value !== '') ? +value : value
|
|
7
|
+
|
|
8
|
+
const defaultType = {
|
|
9
|
+
setInitialValue: ({ set }) => {
|
|
10
|
+
if(value !== undefined) {
|
|
11
|
+
set(value)
|
|
12
|
+
} else if (defaultValue !== undefined) {
|
|
13
|
+
set(defaultValue)
|
|
14
|
+
} else if (options.defaultValue !== undefined) {
|
|
15
|
+
set(resolveValue(options.defaultValue, [props]))
|
|
16
|
+
} else {
|
|
17
|
+
set(undefined)
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
getOutputValue: ({ event }) => {
|
|
21
|
+
if (event.target && event.target.value !== undefined) {
|
|
22
|
+
return event.target.value
|
|
23
|
+
} else if (event.value !== undefined) {
|
|
24
|
+
return event.value
|
|
25
|
+
} else {
|
|
26
|
+
throw new Error("Unable to determine input value from event")
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
getValueProps: ({ currentValue }) => ({
|
|
30
|
+
value: currentValue === undefined ? '' : currentValue
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const checkboxValue = (checked, value) => (value === undefined
|
|
35
|
+
? Boolean(checked)
|
|
36
|
+
: checked ? value : undefined
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
const types = {
|
|
40
|
+
text: defaultType,
|
|
41
|
+
radio: {
|
|
42
|
+
setInitialValue: ({ set }) => checked && set(value),
|
|
43
|
+
getOutputValue: () => value,
|
|
44
|
+
getValueProps: ({ currentValue }) => ({ checked: applyValueTransforms(value) === currentValue })
|
|
45
|
+
},
|
|
46
|
+
checkbox: {
|
|
47
|
+
setInitialValue: ({ set }) => set(checkboxValue(checked, value)),
|
|
48
|
+
getOutputValue: ({ event }) => checkboxValue(event.target.checked, value),
|
|
49
|
+
getValueProps: ({ currentValue }) => ({ checked: Boolean(currentValue) })
|
|
50
|
+
},
|
|
51
|
+
select: {
|
|
52
|
+
...defaultType,
|
|
53
|
+
setInitialValue: ({ set, element }) => {
|
|
54
|
+
set(value || defaultValue ||
|
|
55
|
+
(props.options && props.options.length > 0 && props.options[0]) || // this probably should come out - is it the right behavior for all browsers? it's here to keep 1 unit test happy?
|
|
56
|
+
element && element.value || '' // this *should* work in all browsers (except jsdom)...
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return { ...(types[options.type || type] || types.text), applyValueTransforms }
|
|
63
|
+
}
|
package/src/styles.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import injectCss from './injectCss'
|
|
2
|
-
|
|
3
|
-
injectCss('react-forms-styles', `
|
|
4
|
-
.react-forms {
|
|
5
|
-
display: flex;
|
|
6
|
-
flex-direction: column;
|
|
7
|
-
align-items: flex-start;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
.react-forms-row {
|
|
11
|
-
flex-direction: row;
|
|
12
|
-
align-items: flex-start;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
.react-forms-error {
|
|
16
|
-
color: #f44336;
|
|
17
|
-
}
|
|
18
|
-
`)
|
|
19
|
-
|
|
20
|
-
|
|
1
|
+
import injectCss from './injectCss'
|
|
2
|
+
|
|
3
|
+
injectCss('react-forms-styles', `
|
|
4
|
+
.react-forms {
|
|
5
|
+
display: flex;
|
|
6
|
+
flex-direction: column;
|
|
7
|
+
align-items: flex-start;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
.react-forms-row {
|
|
11
|
+
flex-direction: row;
|
|
12
|
+
align-items: flex-start;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
.react-forms-error {
|
|
16
|
+
color: #f44336;
|
|
17
|
+
}
|
|
18
|
+
`)
|
|
19
|
+
|
|
20
|
+
|
package/src/submit.js
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import React, { useState } from 'react'
|
|
2
|
-
|
|
3
|
-
export default render => function Submit(props) {
|
|
4
|
-
if(!props.onSubmit) throw new Error('Button components must be contained within a Form component')
|
|
5
|
-
|
|
6
|
-
const {
|
|
7
|
-
onSubmit, registerFieldValidator, setFieldValue, getFieldValue, getFieldValues,
|
|
8
|
-
valid, validate, disableInvalid, values, ...propsToPass
|
|
9
|
-
} = props
|
|
10
|
-
|
|
11
|
-
// TODO: set executing when form is triggered by enter key
|
|
12
|
-
const [executing, setExecuting] = useState(false)
|
|
13
|
-
|
|
14
|
-
const onClick = e => {
|
|
15
|
-
const result = onSubmit(e, { validate, additionalValues: values })
|
|
16
|
-
if(result && result.finally && typeof result.finally === 'function') {
|
|
17
|
-
setExecuting(true)
|
|
18
|
-
result.finally(() => setExecuting(false))
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const finalProps = {
|
|
23
|
-
...propsToPass,
|
|
24
|
-
onClick,
|
|
25
|
-
disabled: props.disabled || executing || (disableInvalid && !valid)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return render(finalProps)
|
|
29
|
-
}
|
|
1
|
+
import React, { useState } from 'react'
|
|
2
|
+
|
|
3
|
+
export default render => function Submit(props) {
|
|
4
|
+
if(!props.onSubmit) throw new Error('Button components must be contained within a Form component')
|
|
5
|
+
|
|
6
|
+
const {
|
|
7
|
+
onSubmit, registerFieldValidator, setFieldValue, getFieldValue, getFieldValues,
|
|
8
|
+
valid, validate, disableInvalid, values, ...propsToPass
|
|
9
|
+
} = props
|
|
10
|
+
|
|
11
|
+
// TODO: set executing when form is triggered by enter key
|
|
12
|
+
const [executing, setExecuting] = useState(false)
|
|
13
|
+
|
|
14
|
+
const onClick = e => {
|
|
15
|
+
const result = onSubmit(e, { validate, additionalValues: values })
|
|
16
|
+
if(result && result.finally && typeof result.finally === 'function') {
|
|
17
|
+
setExecuting(true)
|
|
18
|
+
result.finally(() => setExecuting(false))
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const finalProps = {
|
|
23
|
+
...propsToPass,
|
|
24
|
+
onClick,
|
|
25
|
+
disabled: props.disabled || executing || (disableInvalid && !valid)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return render(finalProps)
|
|
29
|
+
}
|
package/src/throttle.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export default (target, delay = 200) => {
|
|
2
|
-
let timeout
|
|
3
|
-
|
|
4
|
-
return (...args) => {
|
|
5
|
-
clearTimeout(timeout)
|
|
6
|
-
timeout = setTimeout(
|
|
7
|
-
() => target(...args),
|
|
8
|
-
delay
|
|
9
|
-
)
|
|
10
|
-
}
|
|
1
|
+
export default (target, delay = 200) => {
|
|
2
|
+
let timeout
|
|
3
|
+
|
|
4
|
+
return (...args) => {
|
|
5
|
+
clearTimeout(timeout)
|
|
6
|
+
timeout = setTimeout(
|
|
7
|
+
() => target(...args),
|
|
8
|
+
delay
|
|
9
|
+
)
|
|
10
|
+
}
|
|
11
11
|
}
|
package/src/useForm.js
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
import { useContext } from 'react'
|
|
2
|
-
|
|
3
|
-
export default context => function useForm() {
|
|
4
|
-
const { onSubmit, getFieldValues, setFieldValue } = useContext(context)
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import { useCallback, useContext } from 'react'
|
|
2
|
+
|
|
3
|
+
export default context => function useForm() {
|
|
4
|
+
const { onSubmit, getFieldValues, setFieldValue } = useContext(context)
|
|
5
|
+
|
|
6
|
+
const submit = useCallback(
|
|
7
|
+
options => onSubmit(undefined, options),
|
|
8
|
+
[onSubmit]
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
return {
|
|
12
|
+
submit,
|
|
13
|
+
values: () => getFieldValues(),
|
|
14
|
+
setFieldValue
|
|
15
|
+
}
|
|
11
16
|
}
|