Skip to content

Commit

Permalink
feat: makes display function returns a generic and makes it more lax …
Browse files Browse the repository at this point in the history
…by defaulting it to string|null

feat: makes react-hook-form adapter return more lax by allowing it to return JSX.Element | null
  • Loading branch information
edusig committed May 12, 2023
1 parent c62445b commit 31280b3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
6 changes: 4 additions & 2 deletions src/adapters/react-hook-form/react-hook-form-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ReactHookFormItemProps<
Resources extends APIResources,
FieldKinds extends PropertyKey,
FieldObjectKinds extends PropertyKey,
FormResult extends JSX.Element | null = JSX.Element | null,
FormType extends FieldValues = FieldValues,
> {
name?: string;
Expand All @@ -18,13 +19,14 @@ export interface ReactHookFormItemProps<
componentProps?: Record<string, unknown>;
formItemProps?: Partial<ControllerProps>;
FormControlWrapper?: FC<FormControlWrapperProps>;
manager: ResourceManager<Resources, FieldKinds, FieldObjectKinds, JSX.Element, any, any>;
manager: ResourceManager<Resources, FieldKinds, FieldObjectKinds, FormResult, any, any>;
}

export const ReactHookFormItem = <
Resources extends APIResources,
FieldKinds extends PropertyKey,
FieldObjectKinds extends PropertyKey,
FormResult extends JSX.Element | null = JSX.Element | null,
FormType extends FieldValues = FieldValues,
>({
control,
Expand All @@ -34,7 +36,7 @@ export const ReactHookFormItem = <
FormControlWrapper,
name,
manager,
}: ReactHookFormItemProps<Resources, FieldKinds, FieldObjectKinds, FormType>) => {
}: ReactHookFormItemProps<Resources, FieldKinds, FieldObjectKinds, FormResult, FormType>) => {
const defaultValue = useMemo(() => (isAPIField(field) ? field.defaultValue : undefined), [field]);
return (
<Controller
Expand Down
6 changes: 4 additions & 2 deletions src/adapters/react/react-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ export interface ReactFieldProps<
Resources extends APIResources,
FieldKinds extends PropertyKey,
FieldObjectKinds extends PropertyKey,
FormResult = JSX.Element | null,
> {
field: APIFieldUnion;
manager: ResourceManager<Resources, FieldKinds, FieldObjectKinds, JSX.Element, any, any>;
manager: ResourceManager<Resources, FieldKinds, FieldObjectKinds, FormResult, any, any>;
componentProps?: any;
}

export const ReactField = <
Resources extends APIResources,
FieldKinds extends PropertyKey,
FieldObjectKinds extends PropertyKey,
FormResult = JSX.Element | null,
>({
field,
componentProps,
manager,
}: ReactFieldProps<Resources, FieldKinds, FieldObjectKinds>) => {
}: ReactFieldProps<Resources, FieldKinds, FieldObjectKinds, FormResult>) => {
const formField = useMemo(
() => manager.getFieldFormField(field, componentProps),
[field, manager],
Expand Down
52 changes: 39 additions & 13 deletions src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import {
FieldValidation,
} from './resource.interface';

export interface KindManager<Kind extends APIFieldUnion, FormResult, ValidationResult> {
display(field: Kind, value: any): string;
export interface KindManager<
Kind extends APIFieldUnion,
FormResult,
ValidationResult,
DisplayResult = string | null,
> {
display(field: Kind, value: any): DisplayResult;
validation(field: Kind, validation?: FieldValidation): ValidationResult;
formField(field: Kind, props?: any): FormResult;
}
Expand All @@ -18,14 +23,23 @@ export interface ManagerOptions<
FormResult,
ValidationResult,
ValidationSchema,
DisplayResult = string | null,
> {
fieldManagerByKind: Record<FieldKinds, KindManager<APIField, FormResult, ValidationResult>>;
fieldManagerByKind: Record<
FieldKinds,
KindManager<APIField, FormResult, ValidationResult, DisplayResult>
>;
fieldObjectManagerByKind: Record<
FieldObjectKinds,
KindManager<APIFieldObject, FormResult, ValidationResult>
KindManager<APIFieldObject, FormResult, ValidationResult, DisplayResult>
>;
defaultFieldManager: KindManager<APIField, FormResult, ValidationResult, DisplayResult>;
defaultFieldObjectManager: KindManager<
APIFieldObject,
FormResult,
ValidationResult,
DisplayResult
>;
defaultFieldManager: KindManager<APIField, FormResult, ValidationResult>;
defaultFieldObjectManager: KindManager<APIFieldObject, FormResult, ValidationResult>;
validationSchemaBuilder: (
fieldValidationTuple: [APIField, ValidationResult][],
) => ValidationSchema;
Expand All @@ -38,15 +52,24 @@ export class ResourceManager<
FormResult,
ValidationResult,
ValidationSchema,
DisplayResult = string | null,
> {
resources: Resources;
fieldManagerByKind: Record<FieldKinds, KindManager<APIField, FormResult, ValidationResult>>;
fieldManagerByKind: Record<
FieldKinds,
KindManager<APIField, FormResult, ValidationResult, DisplayResult>
>;
fieldObjectManagerByKind: Record<
FieldObjectKinds,
KindManager<APIFieldObject, FormResult, ValidationResult>
KindManager<APIFieldObject, FormResult, ValidationResult, DisplayResult>
>;
defaultFieldManager: KindManager<APIField, FormResult, ValidationResult, DisplayResult>;
defaultFieldObjectManager: KindManager<
APIFieldObject,
FormResult,
ValidationResult,
DisplayResult
>;
defaultFieldManager: KindManager<APIField, FormResult, ValidationResult>;
defaultFieldObjectManager: KindManager<APIFieldObject, FormResult, ValidationResult>;
validationSchemaBuilder: (
fieldValidationTuple: [APIField, ValidationResult][],
) => ValidationSchema;
Expand All @@ -58,7 +81,8 @@ export class ResourceManager<
FieldObjectKinds,
FormResult,
ValidationResult,
ValidationSchema
ValidationSchema,
DisplayResult
>,
) {
this.resources = resources;
Expand All @@ -68,7 +92,9 @@ export class ResourceManager<
this.fieldObjectManagerByKind = options.fieldObjectManagerByKind;
}

getManager(field: APIFieldUnion): KindManager<APIFieldUnion, FormResult, ValidationResult> {
getManager(
field: APIFieldUnion,
): KindManager<APIFieldUnion, FormResult, ValidationResult, DisplayResult> {
if ('kind' in field) {
if (field.kind in this.fieldManagerByKind) {
return this.fieldManagerByKind[field.kind as FieldKinds];
Expand All @@ -81,7 +107,7 @@ export class ResourceManager<
return this.defaultFieldObjectManager;
}

getFieldDisplay(field: APIFieldUnion, value?: FieldValidation): string {
getFieldDisplay(field: APIFieldUnion, value?: FieldValidation): DisplayResult {
return this.getManager(field).display(field, value);
}

Expand Down

0 comments on commit 31280b3

Please sign in to comment.