Skip to content

Commit

Permalink
feature: review form manager typing
Browse files Browse the repository at this point in the history
  • Loading branch information
wilcorrea committed Apr 18, 2024
1 parent 24a14b2 commit bcb374e
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 28 deletions.
2 changes: 1 addition & 1 deletion frontend/src/Domain/Contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export enum DriverType {

export type Driver = {
type: DriverType
config: Data
config: Record<string, string>
}

export type DriverResolver = Record<DriverType, Record<string, (config: Data) => unknown>>
4 changes: 2 additions & 2 deletions frontend/view/components/form/FormInput.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { FormFieldProps } from './index.ts'
import { useFormComponent } from './hooks/useFormComponent.ts'

type FormInputProps = FormFieldProps & { type?: string }
type FormInputProps<T> = FormFieldProps<T> & { type?: string }

export function FormInput (props: FormInputProps) {
export function FormInput<T> (props: FormInputProps<T>) {
const {
fieldId,
fieldName,
Expand Down
2 changes: 1 addition & 1 deletion frontend/view/components/form/FormPassword.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FormFieldProps } from './index.ts'
import { FormInput } from './FormInput.tsx'

export function FormPassword (props: FormFieldProps) {
export function FormPassword (props: FormFieldProps<string>) {
return FormInput({ ...props, type: 'password' })
}
4 changes: 2 additions & 2 deletions frontend/view/components/form/FormSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { useFormComponent } from './hooks/useFormComponent.ts'

type OptionValue = string | number

type Option<T extends OptionValue> = {
export type Option<T extends OptionValue> = {
value: T
label: string
}

export type FormSelectProps<T extends OptionValue> = FormFieldProps & {
export type FormSelectProps<T extends OptionValue> = FormFieldProps<T> & {
options: Option<T>[]
}

Expand Down
2 changes: 1 addition & 1 deletion frontend/view/components/form/FormText.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FormFieldProps } from './index.ts'
import { FormInput } from './FormInput.tsx'

export function FormText (props: FormFieldProps) {
export function FormText (props: FormFieldProps<string>) {
return FormInput({ ...props, type: 'text' })
}
4 changes: 2 additions & 2 deletions frontend/view/components/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ export type FormValueWatchCallback = (current: unknown, previous: unknown) => vo

export type FormValueWatch = (fieldName: string, callback: FormValueWatchCallback) => void

export type FormFieldProps = {
export type FormFieldProps<T> = {
id?: string
name: string
label: string
value?: unknown
value?: T
description?: string
placeholder?: string
update?: FormValueUpdate
Expand Down
41 changes: 22 additions & 19 deletions frontend/view/pages/dashboard/DashboardSettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import UserConfigRepository from '../../../src/Domain/Admin/UserConfigRepository
import { Form, FormSelect, FormText, useFormValue } from '../../components/form'
import { Case, Switch } from '../../components/general'
import { useApp, useI18n } from '../../hooks'
import { Option } from '../../components/form/FormSelect.tsx'

export function DashboardSettingsPage () {
const $t = useI18n('pages.dashboard.settings')
Expand Down Expand Up @@ -35,6 +36,25 @@ export function DashboardSettingsPage () {
return $t('error')
}

const options: Option<string>[] = [
{
value: DriverType.memory,
label: $t('fields.type.drivers.memory')
},
{
value: DriverType.json,
label: $t('fields.type.drivers.json')
},
{
value: DriverType.http,
label: $t('fields.type.drivers.http')
},
{
value: DriverType.supabase,
label: $t('fields.type.drivers.supabase')
},
]

return (
<div className="DashboardSettingsForm">
<h4>{$t('title')}</h4>
Expand All @@ -44,30 +64,13 @@ export function DashboardSettingsPage () {
onResolve={onResolve}
onReject={onReject}
fields={<>
<FormSelect
<FormSelect<string>
name="type"
value={value.type}
update={update}
label={$t('fields.type.label')}
description={$t('fields.type.details')}
options={[
{
value: DriverType.memory,
label: $t('fields.type.drivers.memory')
},
{
value: DriverType.json,
label: $t('fields.type.drivers.json')
},
{
value: DriverType.http,
label: $t('fields.type.drivers.http')
},
{
value: DriverType.supabase,
label: $t('fields.type.drivers.supabase')
},
]}
options={options}
/>
{/* config */}
<div className="form-control">
Expand Down

0 comments on commit bcb374e

Please sign in to comment.