Leave Yamada UI a star

Star
Yamada UIYamada UIv1.5.3

FileInput

FileInput is a component used for users to select files.

Source@yamada-ui/file-input

Import

import { FileInput } from "@yamada-ui/react"
Copied!

Usage

Editable example

<FileInput placeholder="basic" />
Copied!

Or, you can customize using children. children provides an array of File.

Editable example

<FileInput placeholder="multiple" multiple>
  {(files) => <Text>Selected: {files ? files.length : 0}</Text>}
</FileInput>
Copied!

Change Variant

Editable example

<VStack>
  <FileInput variant="outline" placeholder="outline" />
  <FileInput variant="filled" placeholder="filled" />
  <FileInput variant="flushed" placeholder="flushed" />
  <FileInput variant="unstyled" placeholder="unstyled" />
</VStack>
Copied!

Change Size

Editable example

<VStack>
  <FileInput placeholder="extra small size" size="xs" />
  <FileInput placeholder="small size" size="sm" />
  <FileInput placeholder="medium size" size="md" />
  <FileInput placeholder="large size" size="lg" />
</VStack>
Copied!

Allow Multiple Selection

To allow multiple selection, set multiple to true.

Editable example

<FileInput placeholder="multiple" multiple />
Copied!

Specify Extensions

To specify extensions, set accept to a string (type, type).

Editable example

<FileInput placeholder="only png, jpeg" accept="image/png,image/jpeg" />
Copied!

Customize Separator

To customize the separator, set separator to a string.

Editable example

<FileInput placeholder="multiple" multiple separator=";" />
Copied!

Use Custom Component

To use a custom component, set component to a ReactElement. component provides value (File) and index.

Editable example

<FileInput
  placeholder="multiple"
  multiple
  component={({ value: { name } }) => <Tag>{name}</Tag>}
/>
Copied!

Format File Names

To format file names, use format. format provides File.

Editable example

<FileInput
  placeholder="multiple"
  multiple
  format={({ name }) => `${name.substring(0, name.indexOf("."))}`}
/>
Copied!

Change Border Color

To change the border color, set focusBorderColor or errorBorderColor to a color.

Editable example

<VStack>
  <FileInput focusBorderColor="green.500" placeholder="custom border color" />
  <FileInput
    isInvalid
    errorBorderColor="orange.500"
    placeholder="custom border color"
  />
</VStack>
Copied!

Disable

To disable, set isDisabled to true.

Editable example

<VStack>
  <FileInput isDisabled variant="outline" placeholder="outline" />
  <FileInput isDisabled variant="filled" placeholder="filled" />
  <FileInput isDisabled variant="flushed" placeholder="flushed" />
  <FileInput isDisabled variant="unstyled" placeholder="unstyled" />

  <FormControl isDisabled label="Upload file">
    <FileInput type="email" placeholder="your file" />
  </FormControl>
</VStack>
Copied!

Make Read-Only

To make read-only, set isReadOnly to true.

Editable example

<VStack>
  <FileInput isReadOnly variant="outline" placeholder="outline" />
  <FileInput isReadOnly variant="filled" placeholder="filled" />
  <FileInput isReadOnly variant="flushed" placeholder="flushed" />
  <FileInput isReadOnly variant="unstyled" placeholder="unstyled" />

  <FormControl isReadOnly label="Upload file">
    <FileInput type="email" placeholder="your file" />
  </FormControl>
</VStack>
Copied!

Make Input Invalid

To make the input invalid, set isInvalid to true.

Editable example

<VStack>
  <FileInput isInvalid variant="outline" placeholder="outline" />
  <FileInput isInvalid variant="filled" placeholder="filled" />
  <FileInput isInvalid variant="flushed" placeholder="flushed" />
  <FileInput isInvalid variant="unstyled" placeholder="unstyled" />

  <FormControl isInvalid label="Upload file" errorMessage="File is required.">
    <FileInput type="email" placeholder="your file" />
  </FormControl>
</VStack>
Copied!

Add Addon

To add an addon, wrap Input with InputGroup and use InputLeftAddon or InputRightAddon.

Editable example

<InputGroup>
  <InputLeftAddon>
    <File />
  </InputLeftAddon>
  <FileInput type="tel" placeholder="Please upload file" />
</InputGroup>
Copied!

Add Element

To add an element, wrap Input with InputGroup and use InputLeftElement or InputRightElement.

Editable example

<InputGroup>
  <InputLeftElement>
    <File color="gray.500" />
  </InputLeftElement>
  <FileInput type="email" placeholder="Please upload file" />
</InputGroup>
Copied!

Reset Value

To reset the value, set resetRef to a ref. A callback function is attached to the set ref, execute it.

Editable example

const [value, onChange] = useState<File[] | null>(null)
const resetRef = useRef<() => void>(null)

const onReset = () => {
  onChange(null)

  if (resetRef.current) resetRef.current()
}

return (
  <VStack>
    <Text>files: {value ? value.length : 0}</Text>

    <InputGroup>
      <FileInput
        multiple
        value={value}
        onChange={onChange}
        resetRef={resetRef}
      />

      {value && value.length ? (
        <InputRightElement isClickable onClick={onReset}>
          <X color="gray.500" />
        </InputRightElement>
      ) : null}
    </InputGroup>
  </VStack>
)
Copied!

Control

Editable example

const [value, onChange] = useState<File[] | null>(null)

return (
  <VStack>
    <Text>files: {value ? value.length : 0}</Text>

    <FileInput value={value} onChange={onChange} multiple />
  </VStack>
)
Copied!

Use React Hook Form

Editable example

type Data = { fileInput: File[] | null }

const resetRef = useRef<() => void>(null)
const {
  control,
  handleSubmit,
  watch,
  setValue,
  formState: { errors },
} = useForm<Data>()

const onReset = () => {
  setValue("fileInput", null)

  if (resetRef.current) resetRef.current()
}

const onSubmit: SubmitHandler<Data> = (data) => console.log("submit:", data)

console.log("watch:", watch())

return (
  <VStack as="form" onSubmit={handleSubmit(onSubmit)}>
    <FormControl
      isInvalid={!!errors.fileInput}
      label="Files"
      errorMessage={errors.fileInput ? errors.fileInput.message : undefined}
    >
      <Controller
        name="fileInput"
        control={control}
        rules={{ required: { value: true, message: "This is required." } }}
        render={({ field }) => (
          <InputGroup>
            <FileInput multiple {...field} resetRef={resetRef} />

            {field.value && field.value.length ? (
              <InputRightElement isClickable onClick={onReset}>
                <X color="gray.500" />
              </InputRightElement>
            ) : null}
          </InputGroup>
        )}
      />
    </FormControl>

    <Button type="submit" alignSelf="flex-end">
      Submit
    </Button>
  </VStack>
)
Copied!

Edit this page on GitHub

PreviousColorPickerNextFileButton