accounts-list.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { useTranslation } from 'react-i18next';
  2. import { Button, Form, Input, Space } from 'antd';
  3. import { MinusOutlined, PlusOutlined } from '@ant-design/icons';
  4. import { useFieldArray, useFormContext } from 'react-hook-form';
  5. import { RandomUtil } from '@/utils';
  6. import { InputAddon } from '@/components/ui';
  7. import { FormField } from '@/components/form/rhf';
  8. export default function AccountsList() {
  9. const { t } = useTranslation();
  10. const { control } = useFormContext();
  11. const { fields, append, remove } = useFieldArray({ control, name: 'settings.accounts' });
  12. return (
  13. <>
  14. <Form.Item label={t('pages.inbounds.form.accounts')}>
  15. <Button
  16. size="small"
  17. onClick={() => append({
  18. user: RandomUtil.randomLowerAndNum(8),
  19. pass: RandomUtil.randomLowerAndNum(12),
  20. })}
  21. >
  22. <PlusOutlined /> {t('add')}
  23. </Button>
  24. </Form.Item>
  25. {fields.length > 0 && (
  26. <Form.Item wrapperCol={{ span: 24 }}>
  27. {fields.map((field, idx) => (
  28. <Space.Compact key={field.id} className="mb-8" block>
  29. <InputAddon>{String(idx + 1)}</InputAddon>
  30. <FormField name={['settings', 'accounts', idx, 'user']} noStyle>
  31. <Input placeholder={t('username')} />
  32. </FormField>
  33. <FormField name={['settings', 'accounts', idx, 'pass']} noStyle>
  34. <Input placeholder={t('password')} />
  35. </FormField>
  36. <Button aria-label={t('remove')} onClick={() => remove(idx)}>
  37. <MinusOutlined />
  38. </Button>
  39. </Space.Compact>
  40. ))}
  41. </Form.Item>
  42. )}
  43. </>
  44. );
  45. }