source: src/components/hook-form/rhf-upload.tsx@ 5d6f37a

main
Last change on this file since 5d6f37a was 5d6f37a, checked in by Naum Shapkarovski <naumshapkarovski@…>, 7 weeks ago

add customer

  • Property mode set to 100644
File size: 2.5 KB
Line 
1import { useFormContext, Controller } from 'react-hook-form';
2// @mui
3import FormHelperText from '@mui/material/FormHelperText';
4//
5import { UploadAvatar, Upload, UploadBox, UploadProps } from '../upload';
6
7// ----------------------------------------------------------------------
8
9interface Props extends Omit<UploadProps, 'file'> {
10 name: string;
11 multiple?: boolean;
12}
13
14// ----------------------------------------------------------------------
15
16export function RHFUploadAvatar({ name, ...other }: Props) {
17 const { control } = useFormContext();
18
19 return (
20 <Controller
21 name={name}
22 control={control}
23 render={({ field, fieldState: { error } }) => (
24 <div>
25 <UploadAvatar error={!!error} file={field.value} {...other} />
26
27 {!!error && (
28 <FormHelperText error sx={{ px: 2, textAlign: 'center' }}>
29 {error.message}
30 </FormHelperText>
31 )}
32 </div>
33 )}
34 />
35 );
36}
37
38// ----------------------------------------------------------------------
39
40export function RHFUploadBox({ name, ...other }: Props) {
41 const { control } = useFormContext();
42
43 return (
44 <Controller
45 name={name}
46 control={control}
47 render={({ field, fieldState: { error } }) => (
48 <UploadBox files={field.value} error={!!error} {...other} />
49 )}
50 />
51 );
52}
53
54// ----------------------------------------------------------------------
55
56export function RHFUpload({ name, multiple, helperText, ...other }: Props) {
57 const { control } = useFormContext();
58
59 return (
60 <Controller
61 name={name}
62 control={control}
63 render={({ field, fieldState: { error } }) =>
64 multiple ? (
65 <Upload
66 multiple
67 accept={{ 'image/*': [] }}
68 files={field.value}
69 error={!!error}
70 helperText={
71 (!!error || helperText) && (
72 <FormHelperText error={!!error} sx={{ px: 2 }}>
73 {error ? error?.message : helperText}
74 </FormHelperText>
75 )
76 }
77 {...other}
78 />
79 ) : (
80 <Upload
81 accept={{ 'image/*': [] }}
82 file={field.value}
83 error={!!error}
84 helperText={
85 (!!error || helperText) && (
86 <FormHelperText error={!!error} sx={{ px: 2 }}>
87 {error ? error?.message : helperText}
88 </FormHelperText>
89 )
90 }
91 {...other}
92 />
93 )
94 }
95 />
96 );
97}
Note: See TracBrowser for help on using the repository browser.