CreateRole.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { makeStyles, Theme } from '@material-ui/core';
  2. import { FC, useMemo, useState } from 'react';
  3. import { useTranslation } from 'react-i18next';
  4. import DialogTemplate from '@/components/customDialog/DialogTemplate';
  5. import CustomInput from '@/components/customInput/CustomInput';
  6. import { ITextfieldConfig } from '@/components/customInput/Types';
  7. import { useFormValidation } from '@/hooks/Form';
  8. import { formatForm } from '@/utils/Form';
  9. import { CreateRoleProps, CreateRoleParams } from './Types';
  10. const useStyles = makeStyles((theme: Theme) => ({
  11. input: {
  12. margin: theme.spacing(3, 0, 0.5),
  13. },
  14. }));
  15. const CreateRole: FC<CreateRoleProps> = ({ handleCreate, handleClose }) => {
  16. const { t: commonTrans } = useTranslation();
  17. const { t: userTrans } = useTranslation('user');
  18. const { t: btnTrans } = useTranslation('btn');
  19. const { t: warningTrans } = useTranslation('warning');
  20. const attuTrans = commonTrans('attu');
  21. const [form, setForm] = useState<CreateRoleParams>({
  22. roleName: '',
  23. });
  24. const checkedForm = useMemo(() => {
  25. return formatForm(form);
  26. }, [form]);
  27. const { validation, checkIsValid, disabled } = useFormValidation(checkedForm);
  28. const classes = useStyles();
  29. const handleInputChange = (key: 'roleName' | 'password', value: string) => {
  30. setForm(v => ({ ...v, [key]: value }));
  31. };
  32. const createConfigs: ITextfieldConfig[] = [
  33. {
  34. label: userTrans('role'),
  35. key: 'roleName',
  36. onChange: (value: string) => handleInputChange('roleName', value),
  37. variant: 'filled',
  38. className: classes.input,
  39. placeholder: userTrans('role'),
  40. fullWidth: true,
  41. validations: [
  42. {
  43. rule: 'require',
  44. errorText: warningTrans('required', {
  45. name: userTrans('role'),
  46. }),
  47. },
  48. ],
  49. defaultValue: form.roleName,
  50. },
  51. ];
  52. const handleCreateRole = () => {
  53. handleCreate(form);
  54. };
  55. return (
  56. <DialogTemplate
  57. title={userTrans('createRoleTitle')}
  58. handleClose={handleClose}
  59. confirmLabel={btnTrans('create')}
  60. handleConfirm={handleCreateRole}
  61. confirmDisabled={disabled}
  62. >
  63. <>
  64. {createConfigs.map(v => (
  65. <CustomInput
  66. type="text"
  67. textConfig={v}
  68. checkValid={checkIsValid}
  69. validInfo={validation}
  70. key={v.label}
  71. />
  72. ))}
  73. </>
  74. </DialogTemplate>
  75. );
  76. };
  77. export default CreateRole;