-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathFileField.tsx
More file actions
71 lines (68 loc) · 1.7 KB
/
FileField.tsx
File metadata and controls
71 lines (68 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* Copyright Oxide Computer Company
*/
import {
useController,
type Control,
type FieldPath,
type FieldPathValue,
type FieldValues,
type Validate,
} from 'react-hook-form'
import { FieldLabel } from '~/ui/lib/FieldLabel'
import { FileInput } from '~/ui/lib/FileInput'
import { TextInputHint } from '~/ui/lib/TextInput'
import { ErrorMessage } from './ErrorMessage'
export function FileField<
TFieldValues extends FieldValues,
TName extends FieldPath<TFieldValues>,
>({
id,
name,
label,
control,
required = false,
accept,
description,
disabled,
validate,
}: {
id: string
name: TName
label: string
tooltipText?: string
control: Control<TFieldValues>
required?: boolean
accept?: string
description?: string | React.ReactNode
disabled?: boolean
validate?: Validate<FieldPathValue<TFieldValues, TName>, TFieldValues>
}) {
const {
field: { value: _, ...rest },
fieldState: { error },
} = useController({ name, control, rules: { required, validate } })
return (
<div className="max-w-lg">
<div className="mb-2">
<FieldLabel id={`${id}-label`} htmlFor={id} optional={!required}>
{label}
</FieldLabel>
{description && <TextInputHint id={`${id}-help-text`}>{description}</TextInputHint>}
</div>
<FileInput
id={id}
className="mt-2"
accept={accept}
disabled={disabled}
{...rest}
error={!!error}
/>
<ErrorMessage error={error} label={label} />
</div>
)
}