Leave Yamada UI a star

Star
Yamada UIYamada UIv1.7.2

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>
  <For each={["outline", "filled", "flushed", "unstyled"]}>
    {(variant, index) => (
      <FileInput key={index} variant={variant} placeholder={variant} />
    )}
  </For>
</VStack>
Copied!

Change Size

Editable example

<VStack>
  <For
    each={[
      {
        placeholder: "extra small",
        size: "xs",
      },
      {
        placeholder: "small",
        size: "sm",
      },
      {
        placeholder: "medium",
        size: "md",
      },
      {
        placeholder: "medium",
        size: "lg",
      },
    ]}
  >
    {({ placeholder, size }, index) => (
      <FileInput key={index} placeholder={`${placeholder} size`} size={size} />
    )}
  </For>
</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>
  <For each={["outline", "filled", "flushed", "unstyled"]}>
    {(variant, index) => (
      <FileInput
        key={index}
        isDisabled
        variant={variant}
        placeholder={variant}
      />
    )}
  </For>

  <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>
  <For each={["outline", "filled", "flushed", "unstyled"]}>
    {(variant, index) => (
      <FileInput
        key={index}
        isReadOnly
        variant={variant}
        placeholder={variant}
      />
    )}
  </For>

  <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>
  <For each={["outline", "filled", "flushed", "unstyled"]}>
    {(variant, index) => (
      <FileInput
        key={index}
        isInvalid
        variant={variant}
        placeholder={variant}
      />
    )}
  </For>

  <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>
    <FileIcon />
  </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>
    <FileIcon 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}>
          <XIcon 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}>
                <XIcon color="gray.500" />
              </InputRightElement>
            ) : null}
          </InputGroup>
        )}
      />
    </FormControl>

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

Edit this page on GitHub

PreviousColorPickerNextFileButton