mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 15:59:52 +03:00
replace class components (HOCs) with React Hooks
This commit is contained in:
33
interface/src/utils/binding.ts
Normal file
33
interface/src/utils/binding.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
type UpdateEntity<S> = (state: (prevState: Readonly<S>) => S) => void;
|
||||
|
||||
export const extractEventValue = (
|
||||
event: React.ChangeEvent<HTMLInputElement>
|
||||
) => {
|
||||
switch (event.target.type) {
|
||||
case 'number':
|
||||
return event.target.valueAsNumber;
|
||||
case 'checkbox':
|
||||
return event.target.checked;
|
||||
default:
|
||||
return event.target.value;
|
||||
}
|
||||
};
|
||||
|
||||
export const updateValue = <S>(updateEntity: UpdateEntity<S>) => (
|
||||
event: React.ChangeEvent<HTMLInputElement>
|
||||
) => {
|
||||
updateEntity((prevState) => ({
|
||||
...prevState,
|
||||
[event.target.name]: extractEventValue(event)
|
||||
}));
|
||||
};
|
||||
|
||||
export const updateBooleanValue = <S>(updateEntity: UpdateEntity<S>) => (
|
||||
name: string,
|
||||
value?: boolean
|
||||
) => {
|
||||
updateEntity((prevState) => ({
|
||||
...prevState,
|
||||
[name]: value
|
||||
}));
|
||||
};
|
||||
Reference in New Issue
Block a user