-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement uncontrolled form components
A few versions ago we introduced compatibility with the native `form` element. This means that behind the scenes we render hidden inputs that are kept in sync which allows you to submit your normal form and get data via `new FormData(event.currentTarget)`. Before this change every form related component (Switch, RadioGroup, Listbox and Combobox) always had to be passed a `value` and an `onChange` regardless of this change. This change will allow you to not even use the `value` and the `onChange` at all and keep it completely uncontrolled. This has some changes: - `value` is made optional - `onChange` is made optional (but will still be called if passed regardless of being controlled or uncontrolled) - `defaultValue` got added so that you can still pre-fill your values with known values. - `value` render prop got exposed so that you can still use this while rendering. This should also make it completely compatible with tools like Remix without wiring up your own state.
- Loading branch information
1 parent
6d13e79
commit bbc7b6d
Showing
5 changed files
with
88 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useState } from 'react' | ||
import { useEvent } from './use-event' | ||
|
||
export function useControllable<T>( | ||
controlledValue: T | undefined, | ||
onChange?: (value: T) => void, | ||
defaultValue?: T | ||
) { | ||
let [internalValue, setInternalValue] = useState(defaultValue) | ||
let isControlled = controlledValue !== undefined | ||
|
||
return [ | ||
(isControlled ? controlledValue : internalValue)!, | ||
useEvent((value) => { | ||
if (isControlled) { | ||
return onChange?.(value) | ||
} else { | ||
setInternalValue(value) | ||
return onChange?.(value) | ||
} | ||
}), | ||
] as const | ||
} |