Skip to content

Commit d9f0675

Browse files
committed
removed use single field option
1 parent b3c2847 commit d9f0675

File tree

5 files changed

+15
-24
lines changed

5 files changed

+15
-24
lines changed

fast-context-generic-extended/src/App.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import MainPage from "./pages/MainPage";
33

44
export const {
55
FastContextProvider:AppFastContextProvider,
6-
useFastContextField:useAppFastContextField,
76
useFastContextFields:useAppFastContextFields
87
} = createFastContext({
98
first: "" as string,

fast-context-generic-extended/src/app/createFastContext.tsx

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,29 +64,20 @@ export default function createFastContext<FastContext>(initialState: FastContext
6464
return [state, fastContext.set];
6565
}
6666

67-
function useFastContextField<SelectorOutput>(
68-
field: string
69-
): [SelectorOutput, (value: SelectorOutput) => void] {
70-
const [fieldValue, setter] = useFastContext((fc) => (fc as Record<string, SelectorOutput>)[field]);
71-
const setField = (value: any) => setter({ [field]: value } as Partial<FastContext>);
72-
return [fieldValue, setField as (value: SelectorOutput) => void];
73-
}
74-
7567
function useFastContextFields<SelectorOutput>(
7668
fieldNames: string[]
7769
): { [key: string]: { get: SelectorOutput, set: (value: any) => void } } {
78-
const valuesAndSetters: { [key: string]: { get: SelectorOutput, set: (value: any) => void } } = {};
70+
const gettersAndSetters: { [key: string]: { get: SelectorOutput, set: (value: any) => void } } = {};
7971
for (const fieldName of fieldNames) {
80-
const [fieldValue, setter] = useFastContextField(fieldName);
81-
valuesAndSetters[fieldName] = { get: fieldValue as SelectorOutput, set: setter };
72+
const [getter, setter] = useFastContext((fc) => (fc as Record<string, SelectorOutput>)[fieldName]);
73+
gettersAndSetters[fieldName] = { get: getter, set: (value: any) => setter({ [fieldName]: value } as Partial<FastContext>) };
8274
}
83-
return valuesAndSetters;
84-
75+
76+
return gettersAndSetters;
8577
}
8678

8779
return {
8880
FastContextProvider,
89-
useFastContextField,
9081
useFastContextFields,
9182
};
9283
}

fast-context-generic-extended/src/components/Display.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useAppFastContextField } from "../App";
1+
import { useAppFastContextFields } from "../App";
22

33
type Props = {
44
fieldName?: string;
@@ -15,13 +15,13 @@ export function PropDrivenDisplay({label, value}: Readonly<Props>) {
1515
);
1616
};
1717

18-
export function SelfDrivenDisplay({ fieldName, label }: Readonly<Props>) {
18+
export function SelfDrivenDisplay({ fieldName = "", label }: Readonly<Props>) {
1919
console.log(`Self Driven ${label} display rendering`)
20-
const [value] = useAppFastContextField(fieldName as string);
20+
const value = useAppFastContextFields([fieldName]);
2121
return (
2222
<div className="value">
2323
{label ? <label>{label} : </label> : null}
24-
<input value={value as string} readOnly style={{backgroundColor: "#eee", cursor: 'auto', border: 0}}/>
24+
<input value={value[fieldName].get as string} readOnly style={{backgroundColor: "#eee", cursor: 'auto', border: 0}}/>
2525
</div>
2626
);
2727
};

fast-context-generic-extended/src/components/FormContainer.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { FormDrivenTextInput, SelfDrivenTextInput } from "./TextInput";
44
export function PropDrivenFormContainer() {
55
console.log(`Prop Driven Form Rendering`)
66
const fields = useAppFastContextFields(['first', 'last']);
7+
console.log(`fields:`, fields)
78
return (
89
<div className="container">
910
<h4>'Prop Driven' Input Form (Form AND children re-render on field changes)</h4>

fast-context-generic-extended/src/components/TextInput.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useAppFastContextField } from "../App";
1+
import { useAppFastContextFields } from "../App";
22

33
type Props = {
44
fieldName?: string;
@@ -20,15 +20,15 @@ export function FormDrivenTextInput( { label = '', value, onChange = (v) => {}}:
2020
};
2121

2222

23-
export function SelfDrivenTextInput( { fieldName, label }: Readonly<Props> ) {
23+
export function SelfDrivenTextInput( { fieldName = "", label }: Readonly<Props> ) {
2424
console.log(`Self Driven ${label} input rendering`)
25-
const [value, setValue] = useAppFastContextField(fieldName as string);
25+
const field = useAppFastContextFields([fieldName]);
2626
return (
2727
<div className="field">
2828
{label ? <label>{label} : </label> : null}
2929
<input
30-
value={value as string}
31-
onChange={(e) => setValue(e.target.value)}
30+
value={field[fieldName].get as string}
31+
onChange={(e) => field[fieldName].set(e.target.value)}
3232
/>
3333
</div>
3434
);

0 commit comments

Comments
 (0)