From d9f067545107eeade87f5782a71a96bc5f3b87ac Mon Sep 17 00:00:00 2001 From: Craig Date: Sun, 28 Apr 2024 01:55:11 +0100 Subject: [PATCH] removed use single field option --- fast-context-generic-extended/src/App.tsx | 1 - .../src/app/createFastContext.tsx | 19 +++++-------------- .../src/components/Display.tsx | 8 ++++---- .../src/components/FormContainer.tsx | 1 + .../src/components/TextInput.tsx | 10 +++++----- 5 files changed, 15 insertions(+), 24 deletions(-) diff --git a/fast-context-generic-extended/src/App.tsx b/fast-context-generic-extended/src/App.tsx index 592315d..dd201d8 100644 --- a/fast-context-generic-extended/src/App.tsx +++ b/fast-context-generic-extended/src/App.tsx @@ -3,7 +3,6 @@ import MainPage from "./pages/MainPage"; export const { FastContextProvider:AppFastContextProvider, - useFastContextField:useAppFastContextField, useFastContextFields:useAppFastContextFields } = createFastContext({ first: "" as string, diff --git a/fast-context-generic-extended/src/app/createFastContext.tsx b/fast-context-generic-extended/src/app/createFastContext.tsx index af64590..321fb7c 100644 --- a/fast-context-generic-extended/src/app/createFastContext.tsx +++ b/fast-context-generic-extended/src/app/createFastContext.tsx @@ -64,29 +64,20 @@ export default function createFastContext(initialState: FastContext return [state, fastContext.set]; } - function useFastContextField( - field: string - ): [SelectorOutput, (value: SelectorOutput) => void] { - const [fieldValue, setter] = useFastContext((fc) => (fc as Record)[field]); - const setField = (value: any) => setter({ [field]: value } as Partial); - return [fieldValue, setField as (value: SelectorOutput) => void]; - } - function useFastContextFields( fieldNames: string[] ): { [key: string]: { get: SelectorOutput, set: (value: any) => void } } { - const valuesAndSetters: { [key: string]: { get: SelectorOutput, set: (value: any) => void } } = {}; + const gettersAndSetters: { [key: string]: { get: SelectorOutput, set: (value: any) => void } } = {}; for (const fieldName of fieldNames) { - const [fieldValue, setter] = useFastContextField(fieldName); - valuesAndSetters[fieldName] = { get: fieldValue as SelectorOutput, set: setter }; + const [getter, setter] = useFastContext((fc) => (fc as Record)[fieldName]); + gettersAndSetters[fieldName] = { get: getter, set: (value: any) => setter({ [fieldName]: value } as Partial) }; } - return valuesAndSetters; - + + return gettersAndSetters; } return { FastContextProvider, - useFastContextField, useFastContextFields, }; } diff --git a/fast-context-generic-extended/src/components/Display.tsx b/fast-context-generic-extended/src/components/Display.tsx index a5eb93f..a62f8ba 100644 --- a/fast-context-generic-extended/src/components/Display.tsx +++ b/fast-context-generic-extended/src/components/Display.tsx @@ -1,4 +1,4 @@ -import { useAppFastContextField } from "../App"; +import { useAppFastContextFields } from "../App"; type Props = { fieldName?: string; @@ -15,13 +15,13 @@ export function PropDrivenDisplay({label, value}: Readonly) { ); }; -export function SelfDrivenDisplay({ fieldName, label }: Readonly) { +export function SelfDrivenDisplay({ fieldName = "", label }: Readonly) { console.log(`Self Driven ${label} display rendering`) - const [value] = useAppFastContextField(fieldName as string); + const value = useAppFastContextFields([fieldName]); return (
{label ? : null} - +
); }; diff --git a/fast-context-generic-extended/src/components/FormContainer.tsx b/fast-context-generic-extended/src/components/FormContainer.tsx index 8f98523..a41392d 100644 --- a/fast-context-generic-extended/src/components/FormContainer.tsx +++ b/fast-context-generic-extended/src/components/FormContainer.tsx @@ -4,6 +4,7 @@ import { FormDrivenTextInput, SelfDrivenTextInput } from "./TextInput"; export function PropDrivenFormContainer() { console.log(`Prop Driven Form Rendering`) const fields = useAppFastContextFields(['first', 'last']); + console.log(`fields:`, fields) return (

'Prop Driven' Input Form (Form AND children re-render on field changes)

diff --git a/fast-context-generic-extended/src/components/TextInput.tsx b/fast-context-generic-extended/src/components/TextInput.tsx index 37b7b80..ecd1bd2 100644 --- a/fast-context-generic-extended/src/components/TextInput.tsx +++ b/fast-context-generic-extended/src/components/TextInput.tsx @@ -1,4 +1,4 @@ -import { useAppFastContextField } from "../App"; +import { useAppFastContextFields } from "../App"; type Props = { fieldName?: string; @@ -20,15 +20,15 @@ export function FormDrivenTextInput( { label = '', value, onChange = (v) => {}}: }; -export function SelfDrivenTextInput( { fieldName, label }: Readonly ) { +export function SelfDrivenTextInput( { fieldName = "", label }: Readonly ) { console.log(`Self Driven ${label} input rendering`) - const [value, setValue] = useAppFastContextField(fieldName as string); + const field = useAppFastContextFields([fieldName]); return (
{label ? : null} setValue(e.target.value)} + value={field[fieldName].get as string} + onChange={(e) => field[fieldName].set(e.target.value)} />
);