-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
128 additions
and
353 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,20 +1,89 @@ | ||
import React, { createContext, useState } from 'react' | ||
import { createFormStore, FormProps, FormStore } from './formStore' | ||
import { SectionDescriptor, SectionedFormDescriptor } from './formDescriptor' | ||
Check failure on line 2 in src/lib/form/sectionedForm/SectionedFormContext.tsx GitHub Actions / build
|
||
|
||
export const SectionedFormContext = createContext<FormStore | null>(null) | ||
/* Some of the types in this file may look complex. | ||
However they are here to help type-safety and autocommpletion for consumers. | ||
The only thing consumers need to do is pass the type of the formdescriptor to use the context. | ||
useSectionedFormDescriptor<typeof FormDescriptor>() | ||
This helps usage in specific form components. | ||
*/ | ||
|
||
export const SectionedFormProvider = ({ | ||
type AllFieldNames<T extends SectionedFormDescriptor> = | ||
T['sections'][number]['fields'][number]['name'] | ||
|
||
/* Helper to avoid returning undefined from a map when we know we have the value from the type. | ||
And conversely - add undefined to TType if we dont have a specifc type for T*/ | ||
type EnforceIfInferrable< | ||
T extends SectionedFormDescriptor, | ||
TType | ||
> = T extends SectionedFormDescriptor<infer U> | ||
? unknown extends U | ||
? TType | undefined | ||
: TType | ||
: never | ||
|
||
function createContextValue<const T extends SectionedFormDescriptor>( | ||
descriptor: T | ||
) { | ||
const fieldLabels = Object.fromEntries( | ||
descriptor.sections.flatMap((section) => | ||
section.fields.map((f) => [f.name, f.label] as const) | ||
) | ||
) as Record<AllFieldNames<T>, EnforceIfInferrable<T, string>> | ||
|
||
const sectionMap = Object.fromEntries( | ||
descriptor.sections.map((s) => [s.name, s]) | ||
) as Record< | ||
T['sections'][number]['name'], | ||
EnforceIfInferrable<T, SectionDescriptor> | ||
> | ||
|
||
const sections: T['sections'] = descriptor.sections | ||
return { | ||
formName: descriptor.name, | ||
formLabel: descriptor.label, | ||
sections, | ||
getSection: (name: T['sections'][number]['name']) => sectionMap[name], | ||
getFieldLabel: (field: AllFieldNames<T>) => { | ||
return fieldLabels[field] | ||
}, | ||
} | ||
} | ||
type SectionFormContextValue<T extends SectionedFormDescriptor> = ReturnType< | ||
typeof createContextValue<T> | ||
> | ||
|
||
export const SectionedFormContext = createContext<ReturnType< | ||
typeof createContextValue | ||
> | null>(null) | ||
|
||
export const SectionedFormDescriptorProvider = < | ||
T extends SectionedFormDescriptor | ||
>({ | ||
children, | ||
initialValue, | ||
}: { | ||
initialValue: Partial<FormProps> | ||
initialValue: T | ||
children: React.ReactNode | ||
}) => { | ||
const [store] = useState(() => createFormStore(initialValue)) | ||
const [contextValue] = useState(() => createContextValue(initialValue)) | ||
|
||
return ( | ||
<SectionedFormContext.Provider value={store}> | ||
<SectionedFormContext.Provider value={contextValue}> | ||
{children} | ||
</SectionedFormContext.Provider> | ||
) | ||
} | ||
|
||
export const useSectionedFormDescriptor = < | ||
T extends SectionedFormDescriptor | ||
>() => { | ||
const context = React.useContext(SectionedFormContext) | ||
if (!context) { | ||
throw new Error( | ||
'useSectionedFormDescriptor must be used within a SectionedFormDescriptorProvider' | ||
) | ||
} | ||
return context as SectionFormContextValue<T> | ||
} |
90 changes: 0 additions & 90 deletions
90
src/lib/form/sectionedForm/SectionedFormDescriptorProvider.tsx
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
36 changes: 0 additions & 36 deletions
36
src/lib/form/sectionedForm/SectionedFormSectionContext.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.