cleanup code a bit more
This commit is contained in:
parent
07b51dd2f2
commit
1f72542234
25 changed files with 146 additions and 122 deletions
|
|
@ -3,16 +3,16 @@ import { Dispatch, SetStateAction, useCallback, useMemo } from "react";
|
|||
export function makePartialSetter<T, K extends keyof T>(fullSetter: Dispatch<SetStateAction<T>>, key: K): Dispatch<SetStateAction<T[typeof key]>> {
|
||||
return (newValueOrCallback: T[K] | ((newValue: T[K]) => T[K])) => {
|
||||
fullSetter(oldFullValue => {
|
||||
if (typeof newValueOrCallback === 'function') {
|
||||
const newValue = (typeof newValueOrCallback === 'function') ? (newValueOrCallback as (newValue: T[K]) => T[K])(oldFullValue[key] as T[K]) : newValueOrCallback as T[K];
|
||||
if (newValue === oldFullValue[key]) {
|
||||
return oldFullValue;
|
||||
}
|
||||
else {
|
||||
return {
|
||||
...oldFullValue,
|
||||
[key]: (newValueOrCallback as (newValue: T[K]) => T[K])(oldFullValue[key] as T[K]),
|
||||
[key]: newValue,
|
||||
}
|
||||
}
|
||||
return {
|
||||
...oldFullValue,
|
||||
[key]: newValueOrCallback as T[K],
|
||||
}
|
||||
})
|
||||
};
|
||||
}
|
||||
|
|
@ -21,16 +21,16 @@ export type Setters<T extends {[key: string]: any}> = {
|
|||
[K in keyof T as `set${Capitalize<Extract<K, string>>}`]: Dispatch<SetStateAction<T[K]>>;
|
||||
}
|
||||
|
||||
export function makeIndividualSetters<T extends {[key: string]: any}>(
|
||||
export function makeAllSetters<T extends {[key: string]: any}>(
|
||||
fullSetter: Dispatch<SetStateAction<T>>,
|
||||
keys: (keyof T)[],
|
||||
): Setters<T> {
|
||||
// @ts-ignore
|
||||
return useMemo(() =>
|
||||
return useMemo(() => {
|
||||
console.log('creating setters for App');
|
||||
// @ts-ignore
|
||||
Object.fromEntries(keys.map((key: string) => {
|
||||
return Object.fromEntries(keys.map((key: string) => {
|
||||
return [`set${key.charAt(0).toUpperCase()}${key.slice(1)}`, makePartialSetter(fullSetter, key)];
|
||||
})),
|
||||
[fullSetter]
|
||||
);
|
||||
}));
|
||||
}, [fullSetter]);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue