diff --git a/web/app/components/base/form/components/field/select.tsx b/web/app/components/base/form/components/field/select.tsx index 60f9f21943..289fceb928 100644 --- a/web/app/components/base/form/components/field/select.tsx +++ b/web/app/components/base/form/components/field/select.tsx @@ -1,18 +1,13 @@ import cn from '@/utils/classnames' import { useFieldContext } from '../..' -import type { PureSelectProps } from '../../../select/pure' +import type { Option, PureSelectProps } from '../../../select/pure' import PureSelect from '../../../select/pure' import Label from '../label' import { useCallback } from 'react' -type SelectOption = { - value: string - label: string -} - type SelectFieldProps = { label: string - options: SelectOption[] + options: Option[] onChange?: (value: string) => void isRequired?: boolean showOptional?: boolean diff --git a/web/app/components/base/form/components/form/actions.tsx b/web/app/components/base/form/components/form/actions.tsx new file mode 100644 index 0000000000..4e22350c90 --- /dev/null +++ b/web/app/components/base/form/components/form/actions.tsx @@ -0,0 +1,39 @@ +import { useStore } from '@tanstack/react-form' +import type { FormType } from '../..' +import { useFormContext } from '../..' +import Button from '../../../button' +import { useTranslation } from 'react-i18next' + +type ActionsProps = { + CustomActions?: (form: FormType) => React.ReactNode +} + +const Actions = ({ + CustomActions, +}: ActionsProps) => { + const { t } = useTranslation() + const form = useFormContext() + + const [isSubmitting, canSubmit] = useStore(form.store, state => [ + state.isSubmitting, + state.canSubmit, + ]) + + if (CustomActions) + return CustomActions(form) + + return ( +
+ +
+ ) +} + +export default Actions diff --git a/web/app/components/base/form/components/form/submit-button.tsx b/web/app/components/base/form/components/form/submit-button.tsx deleted file mode 100644 index 494d19b843..0000000000 --- a/web/app/components/base/form/components/form/submit-button.tsx +++ /dev/null @@ -1,25 +0,0 @@ -import { useStore } from '@tanstack/react-form' -import { useFormContext } from '../..' -import Button, { type ButtonProps } from '../../../button' - -type SubmitButtonProps = Omit - -const SubmitButton = ({ ...buttonProps }: SubmitButtonProps) => { - const form = useFormContext() - - const [isSubmitting, canSubmit] = useStore(form.store, state => [ - state.isSubmitting, - state.canSubmit, - ]) - - return ( - - - {t('common.operation.save')} - + diff --git a/web/app/components/base/form/index.tsx b/web/app/components/base/form/index.tsx index 2ce8014cc0..22c3da9fef 100644 --- a/web/app/components/base/form/index.tsx +++ b/web/app/components/base/form/index.tsx @@ -5,7 +5,9 @@ import CheckboxField from './components/field/checkbox' import SelectField from './components/field/select' import CustomSelectField from './components/field/custom-select' import OptionsField from './components/field/options' -import SubmitButton from './components/form/submit-button' +import Actions from './components/form/actions' + +export type FormType = ReturnType export const { fieldContext, useFieldContext, formContext, useFormContext } = createFormHookContexts() @@ -20,7 +22,7 @@ export const { useAppForm, withForm } = createFormHook({ OptionsField, }, formComponents: { - SubmitButton, + Actions, }, fieldContext, formContext, diff --git a/web/app/components/base/select/pure.tsx b/web/app/components/base/select/pure.tsx index 05d0e864fc..083c018551 100644 --- a/web/app/components/base/select/pure.tsx +++ b/web/app/components/base/select/pure.tsx @@ -17,7 +17,7 @@ import type { } from '@/app/components/base/portal-to-follow-elem' import cn from '@/utils/classnames' -type Option = { +export type Option = { label: string value: string } diff --git a/web/app/dev-preview/page.tsx b/web/app/dev-preview/page.tsx index c8d7f8c241..4849e29478 100644 --- a/web/app/dev-preview/page.tsx +++ b/web/app/dev-preview/page.tsx @@ -1,19 +1,62 @@ 'use client' - -import InputFieldForm from '../components/base/form/form-scenarios/input-field' -// import DemoForm from '../components/base/form/form-scenarios/demo' +import InputFieldForm from '../components/base/form/form-scenarios/base' +import { BaseVarType } from '../components/base/form/form-scenarios/base/types' export default function Page() { return (
-
+
{ console.log('cancel') }} - onSubmit={value => console.log('submit', value)} + initialData={{ + type: 'option_1', + variable: 'test', + label: 'Test', + required: true, + }} + configurations={[ + { + type: BaseVarType.textInput, + variable: 'variable', + label: 'Variable', + required: true, + showConditions: [{ + variable: 'type', + value: 'option_1', + }], + }, + { + type: BaseVarType.numberInput, + variable: 'max_length', + label: 'Max Length', + required: true, + showConditions: [], + max: 100, + min: 1, + }, + { + type: BaseVarType.checkbox, + variable: 'required', + label: 'Required', + required: true, + showConditions: [], + }, + { + type: BaseVarType.select, + variable: 'type', + label: 'Type', + required: true, + showConditions: [], + options: [ + { label: 'Option 1', value: 'option_1' }, + { label: 'Option 2', value: 'option_2' }, + { label: 'Option 3', value: 'option_3' }, + ], + }, + ]} + onSubmit={(value) => { + console.log('onSubmit', value) + }} /> - {/* */}
)