Skip to content

Commit d2e0fff

Browse files
nolotzerikras
authored andcommitted
adds checkbox & radio input support (#9)
1 parent 97b14c4 commit d2e0fff

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/useField.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ export const all = fieldSubscriptionItems.reduce((result, key) => {
66
return result
77
}, {})
88

9+
const eventValue = event => {
10+
if (!event.target) {
11+
return event
12+
} else if (['checkbox', 'radio'].includes(event.target.type)) {
13+
return event.target.checked
14+
}
15+
16+
return event.target.value
17+
}
18+
919
const useField = (name, form, subscription) => {
1020
const autoFocus = useRef(false)
1121
const [state, setState] = useState({})
@@ -31,8 +41,7 @@ const useField = (name, form, subscription) => {
3141
name,
3242
value: value || '',
3343
onBlur: () => state.blur(),
34-
onChange: event =>
35-
state.change(event.target ? event.target.value : event),
44+
onChange: event => state.change(eventValue(event)),
3645
onFocus: () => {
3746
if (state.focus) {
3847
state.focus()

0 commit comments

Comments
 (0)