-
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.
refactor: improve dynamic import ux and typesafety for form element I…
…nputComponents (#704) * refactor: make loading component config more efficient and slightly easier to read * refactor: almost make the form config components more typesafe * fix: simplify the types and remove all type errors * Discard changes to core/lib/types.ts * Discard changes to core/app/components/FormBuilder/ElementPanel/index.tsx
- Loading branch information
Showing
12 changed files
with
69 additions
and
83 deletions.
There are no files selected for viewing
5 changes: 3 additions & 2 deletions
5
core/app/components/FormBuilder/ElementPanel/ComponentConfig/Checkbox.tsx
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
5 changes: 3 additions & 2 deletions
5
core/app/components/FormBuilder/ElementPanel/ComponentConfig/ConfidenceInterval.tsx
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
5 changes: 3 additions & 2 deletions
5
core/app/components/FormBuilder/ElementPanel/ComponentConfig/DatePicker.tsx
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
5 changes: 3 additions & 2 deletions
5
core/app/components/FormBuilder/ElementPanel/ComponentConfig/FileUpload.tsx
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
5 changes: 3 additions & 2 deletions
5
core/app/components/FormBuilder/ElementPanel/ComponentConfig/MemberSelect.tsx
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
5 changes: 3 additions & 2 deletions
5
core/app/components/FormBuilder/ElementPanel/ComponentConfig/TextArea.tsx
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
5 changes: 3 additions & 2 deletions
5
core/app/components/FormBuilder/ElementPanel/ComponentConfig/TextInput.tsx
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
83 changes: 31 additions & 52 deletions
83
core/app/components/FormBuilder/ElementPanel/ComponentConfig/index.tsx
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,61 +1,40 @@ | ||
"use client"; | ||
|
||
import type { ComponentType } from "react"; | ||
import type React from "react"; | ||
|
||
import dynamic from "next/dynamic"; | ||
|
||
import { InputComponent } from "db/public"; | ||
import { Skeleton } from "ui/skeleton"; | ||
|
||
import type { ComponentConfigFormProps, InnerFormProps } from "./types.ts"; | ||
|
||
export const ComponentConfig = ({ component, ...props }: ComponentConfigFormProps) => { | ||
let ConfigForm: ComponentType<InnerFormProps>; | ||
switch (component) { | ||
case InputComponent.checkbox: | ||
ConfigForm = dynamic(() => import("./Checkbox.tsx"), { | ||
ssr: false, | ||
loading: () => <Skeleton className="h-full w-full" />, | ||
}); | ||
break; | ||
case InputComponent.confidenceInterval: | ||
ConfigForm = dynamic(() => import("./ConfidenceInterval.tsx"), { | ||
ssr: false, | ||
loading: () => <Skeleton className="h-full w-full" />, | ||
}); | ||
break; | ||
case InputComponent.datePicker: | ||
ConfigForm = dynamic(() => import("./DatePicker.tsx"), { | ||
ssr: false, | ||
loading: () => <Skeleton className="h-full w-full" />, | ||
}); | ||
break; | ||
case InputComponent.fileUpload: | ||
ConfigForm = dynamic(() => import("./FileUpload.tsx"), { | ||
ssr: false, | ||
loading: () => <Skeleton className="h-full w-full" />, | ||
}); | ||
break; | ||
case InputComponent.memberSelect: | ||
ConfigForm = dynamic(() => import("./MemberSelect.tsx"), { | ||
ssr: false, | ||
loading: () => <Skeleton className="h-full w-full" />, | ||
}); | ||
break; | ||
case InputComponent.textArea: | ||
ConfigForm = dynamic(() => import("./TextArea.tsx"), { | ||
ssr: false, | ||
loading: () => <Skeleton className="h-full w-full" />, | ||
}); | ||
break; | ||
case InputComponent.textInput: | ||
ConfigForm = dynamic(() => import("./TextInput.tsx"), { | ||
ssr: false, | ||
loading: () => <Skeleton className="h-full w-full" />, | ||
}); | ||
break; | ||
default: | ||
return null; | ||
} | ||
return <ConfigForm {...props} />; | ||
import type { ComponentConfigFormProps } from "./types.ts"; | ||
|
||
const toDynamic = (path: string) => | ||
// this dynamic import path needs to provide enough information for webpack/turbopack | ||
// to be able to find it. The relative path and the extension are enough, but something like | ||
// `import(path)` will not work. | ||
dynamic(() => import(`./${path}.tsx`), { | ||
ssr: false, | ||
loading: () => <Skeleton className="h-full w-full" />, | ||
}); | ||
|
||
const InputCompomentMap = { | ||
[InputComponent.checkbox]: toDynamic("Checkbox"), | ||
[InputComponent.confidenceInterval]: toDynamic("ConfidenceInterval"), | ||
[InputComponent.datePicker]: toDynamic("DatePicker"), | ||
[InputComponent.fileUpload]: toDynamic("FileUpload"), | ||
[InputComponent.memberSelect]: toDynamic("MemberSelect"), | ||
[InputComponent.textArea]: toDynamic("TextArea"), | ||
[InputComponent.textInput]: toDynamic("TextInput"), | ||
}; | ||
|
||
export const ComponentConfig = <I extends InputComponent>(props: ComponentConfigFormProps<I>) => { | ||
// ideally the compenent would be selected through some (generic) function, but for `dynamic` | ||
// to work properly the components need to be defined already outside of the react tree, | ||
// hence the map and the type cast | ||
const ConfigComponent = InputCompomentMap[props.component] as React.FC< | ||
ComponentConfigFormProps<I> | ||
>; | ||
|
||
return <ConfigComponent {...props} />; | ||
}; |
19 changes: 10 additions & 9 deletions
19
core/app/components/FormBuilder/ElementPanel/ComponentConfig/types.ts
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,17 +1,18 @@ | ||
import type { CoreSchemaType } from "@prisma/client"; | ||
import type { Static } from "@sinclair/typebox"; | ||
import type { UseFormReturn } from "react-hook-form"; | ||
import type { componentConfigSchemas } from "schemas"; | ||
|
||
import type { CoreSchemaType, InputComponent } from "db/public"; | ||
import type { InputComponent } from "db/public"; | ||
|
||
export type ComponentConfigFormProps = { | ||
form: UseFormReturn<ConfigFormData>; | ||
export type ComponentConfigFormProps<I extends InputComponent> = { | ||
form: UseFormReturn<ConfigFormData<I>>; | ||
schemaName: CoreSchemaType; | ||
component: InputComponent; | ||
component: I; | ||
}; | ||
|
||
export type InnerFormProps = Omit<ComponentConfigFormProps, "component">; | ||
|
||
export type ConfigFormData = { | ||
export type ConfigFormData<I extends InputComponent> = { | ||
required: boolean | null; | ||
component: InputComponent; | ||
config: any; | ||
component: I; | ||
config: Static<(typeof componentConfigSchemas)[I]>; | ||
}; |
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 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