Feat: Render MessageForm with shadcn-ui. #3221 (#5596)

### What problem does this PR solve?

Feat: Render MessageForm with shadcn-ui. #3221

### Type of change


- [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
balibabu
2025-03-04 15:47:05 +08:00
committed by GitHub
parent 9816b868f9
commit f256e1a59a
4 changed files with 120 additions and 101 deletions

View File

@ -1,85 +1,80 @@
import { useTranslate } from '@/hooks/common-hooks';
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';
import { Button, Form, Input } from 'antd';
import { IOperatorForm } from '../../interface';
import { Button } from '@/components/ui/button';
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from '@/components/ui/form';
import { Textarea } from '@/components/ui/textarea';
import { PlusCircle, Trash2 } from 'lucide-react';
import { useFieldArray } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { INextOperatorForm } from '../../interface';
import styles from './index.less';
const formItemLayout = {
labelCol: {
sm: { span: 6 },
},
wrapperCol: {
sm: { span: 18 },
},
};
const formItemLayoutWithOutLabel = {
wrapperCol: {
sm: { span: 18, offset: 6 },
},
};
const MessageForm = ({ onValuesChange, form }: IOperatorForm) => {
const { t } = useTranslate('flow');
const MessageForm = ({ form }: INextOperatorForm) => {
const { t } = useTranslation();
const { fields, append, remove } = useFieldArray({
name: 'messages',
control: form.control,
});
return (
<Form
name="basic"
{...formItemLayoutWithOutLabel}
onValuesChange={onValuesChange}
autoComplete="off"
form={form}
>
<Form.List name="messages">
{(fields, { add, remove }, {}) => (
<>
<Form {...form}>
<form
className="space-y-6"
onSubmit={(e) => {
e.preventDefault();
}}
>
<FormItem>
<FormLabel>{t('flow.msg')}</FormLabel>
<div className="space-y-4">
{fields.map((field, index) => (
<Form.Item
{...(index === 0 ? formItemLayout : formItemLayoutWithOutLabel)}
label={index === 0 ? t('msg') : ''}
required={false}
key={field.key}
>
<Form.Item
{...field}
validateTrigger={['onChange', 'onBlur']}
rules={[
{
required: true,
whitespace: true,
message: t('messageMsg'),
},
]}
noStyle
>
<Input.TextArea
rows={4}
placeholder={t('messagePlaceholder')}
style={{ width: '80%' }}
/>
</Form.Item>
{fields.length > 1 ? (
<MinusCircleOutlined
className={styles.dynamicDeleteButton}
onClick={() => remove(field.name)}
/>
) : null}
</Form.Item>
<div key={field.id} className="flex items-start gap-2">
<FormField
control={form.control}
name={`messages.${index}`}
render={({ field }) => (
<FormItem className="flex-1">
<FormControl>
<Textarea
{...field}
placeholder={t('flow.messagePlaceholder')}
rows={5}
/>
</FormControl>
</FormItem>
)}
/>
{fields.length > 1 && (
<Button
variant="ghost"
size="icon"
type="button"
onClick={() => remove(index)}
className="cursor-pointer text-colors-text-functional-danger"
>
<Trash2 className="h-4 w-4" />
</Button>
)}
</div>
))}
<Form.Item>
<Button
type="dashed"
onClick={() => add()}
style={{ width: '80%' }}
icon={<PlusOutlined />}
>
{t('addMessage')}
</Button>
</Form.Item>
</>
)}
</Form.List>
<Button
type="button"
variant="outline"
onClick={() => append(' ')} // "" will cause the inability to add, refer to: https://github.com/orgs/react-hook-form/discussions/8485#discussioncomment-2961861
className="w-full mt-4"
>
<PlusCircle className="mr-2 h-4 w-4" />
{t('flow.addMessage')}
</Button>
</div>
<FormMessage />
</FormItem>
</form>
</Form>
);
};