--- title: FileInput description: "`FileInput` is a component used for users to select files." links: - style: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/components/file-input/file-input.style.ts - source: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/components/file-input - storybook: https://yamada-ui.github.io/yamada-ui?path=/story/components-fileinput--basic --- ```tsx ``` ## Usage ```tsx import { FileInput } from "@yamada-ui/react" ``` ```tsx import { FileInput } from "@/components/ui" ``` ```tsx import { FileInput } from "@workspaces/ui" ``` ```tsx ``` ### Change Variant ```tsx {(variant, index) => ( )} ``` ### Change Size ```tsx {(size, index) => ( )} ``` ### Change Color Scheme ```tsx {(colorScheme, index) => ( )} ``` ### Allow Multiple Selection To allow multiple selection, set `multiple` to `true`. ```tsx ``` ### Specify File Extensions To specify file extensions, set `accept` to a string(type). ```tsx ``` ### Customize Separator To customize the separator, set `separator` to a string. ```tsx ``` ### Use Custom Component To use a custom component, set `component` to a `ReactNode`. `component` provides `value`(`File`) and `index`. ```tsx {name}} gapY="xs" multiple placeholder="Please upload file" /> ``` ### Format File Names To format file names, use `format`. `format` provides `File`. ```tsx name.substring(0, name.indexOf("."))} multiple placeholder="Please upload file" /> ``` ### Customize Children To customize children, use `children`. `children` provides `File` as an array. ```tsx {(files) => Selected: {files?.length ?? 0}} ``` ### Add Addon To add an addon, wrap `FileInput` with `InputGroup.Root` and use `InputGroup.Addon`. ```tsx ``` ### Add Element To add an element, wrap `FileInput` with `InputGroup.Root` and use `InputGroup.Element`. ```tsx ``` ### Disable To disable, set `disabled` to `true`. ```tsx ``` ### Read-Only To make it read-only, set `readOnly` to `true`. ```tsx ``` ### Invalid To make the input invalid, set `invalid` to `true`. ```tsx ``` ### Change Border Color To change the border color, set a color to `focusBorderColor` or `errorBorderColor`. ```tsx ``` ### Reset To reset the value, set `resetRef` to `ref`. A callback function will be attached to the set `ref`, execute it. ```tsx const [value, onChange] = useState(undefined) const resetRef = useRef<() => void>(null) const onReset = () => { onChange(undefined) resetRef.current?.() } return ( files: {value?.length ?? 0} {value?.length ? (
) : null}
) ``` ## Uses Components & Hooks ## Used By Components & Hooks ## Props | Prop | Default | Type | Description | | ------------------ | ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------ | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `size` | `"md"` | `"2xl" \| "lg" \| "md" \| "sm" \| "xl" \| "xs"` | The size of the component. | | `variant` | `"outline"` | `"filled" \| "flushed" \| "outline" \| "plain"` | The variant of the component. | | `children` | - | `(files: File[] \| undefined) => ReactNode` | A callback that returns a React node. | | `component` | - | `(props: { index: number; value: File }) => ReactNode` | The component that displays uploaded files. | | `defaultValue` | - | `File[]` | The initial value of the file input. | | `errorBorderColor` | - | `"-moz-initial" \| "AccentColor" \| "AccentColorText" \| "ActiveBorder" \| "ActiveCaption" \| "ActiveText" \| "aliceblue" \| "amber.100" \| "amber.200" \| "amber.300" ...` | The border color when the input is invalid. | | `focusBorderColor` | - | `"-moz-initial" \| "AccentColor" \| "AccentColorText" \| "ActiveBorder" \| "ActiveCaption" \| "ActiveText" \| "aliceblue" \| "amber.100" \| "amber.200" \| "amber.300" ...` | The border color when the input is focused. | | `format` | - | `(value: File, index: number) => string` | A callback that formats the name of the uploaded file. | | `invalid` | `false` | `boolean` | If `true`, the field will be invalid. | | `onChange` | - | `(files: File[] \| undefined) => void` | Function to be called when a file change event occurs. | | `resetRef` | - | `RefObject<(() => void) \| null>` | Ref to a reset function. | | `separator` | `","` | `string` | The string to separate uploaded files. | | `value` | - | `File[]` | The value of the file input. | ## Accessibility If you are not using `Field.Root`, set `aria-label` or `aria-labelledby` to `FileInput`. ```tsx ``` ```tsx Upload file ``` ### Keyboard Navigation | Key | Description | State | | ---------------- | ------------------------------ | ----- | | `Tab` | Focuses the element. | - | | `Space`, `Enter` | Activates the focused element. | - | ### ARIA Roles and Attributes | Component | Roles and Attributes | Usage | | ----------- | -------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `FileInput` | `role="button"` | Indicates that this is a button. | | | `aria-disabled` | Sets to `"true"` if `disabled` or `readOnly` is set. | | | `aria-labelledby` | If `FileInput` is within a `Field.Root` and `Field.Root` has a `label`, sets its `id`. | | | `aria-describedby` | If `FileInput` is within a `Field.Root` and `Field.Root` has an `errorMessage`, `helperMessage`, `Field.ErrorMessage`, or `Field.HelperMessage`, sets its `id`. | | | `aria-invalid` | Sets to `"true"` if `invalid` is set. | | | `aria-required` | Sets to `"true"` if `required` is set. | | `input` | `aria-disabled` | Sets to `"true"` if `disabled` or `readOnly` is set. | | | `aria-describedby` | If `FileInput` is within a `Field.Root` and `Field.Root` has an `errorMessage`, `helperMessage`, `Field.ErrorMessage`, or `Field.HelperMessage`, sets its `id`. | | | `aria-hidden` | Excludes the element from the accessibility tree. | | | `aria-invalid` | Sets to `"true"` if `invalid` is set. | | | `aria-required` | Sets to `"true"` if `required` is set. |