JGuang

FileButton

FileButton · Mantine Core

Preview
npm install @mantine/core@9.7.1 @mantine/hooks@9.7.1

FileButton

Install: npm install @mantine/core@9.7.1 @mantine/hooks@9.7.1

Version and compatibility

Pinned Mantine Core 9.7.1 requires React and React DOM ^19.2.0 and matching @mantine/hooks@9.7.1, from the installed npm package's peerDependencies. Do not force it into React 18 or another incompatible version. Explain upgrade impact and agree on a plan with the project owner; do not hide conflicts with --force/--legacy-peer-deps. The native Provider enables theme.respectReducedMotion and uses forceColorScheme for light/dark.

Use case

Open a native file picker from a custom button.

Native props used by this example

These props are extracted from the local typechecked JSX and grouped by their owning native component. This is not an exhaustive prop list; see pinned source signatures and inheritance below.

Component propExplanation
FileButton.onChangeNative value/event callback; preserve its actual signature.
FileButton.acceptNative file picker type hint; not server-side file validation.

Interaction and state checks

Current scenarios: Default. Check actual behavior after selecting supported disabled/error/read-only scenarios; components without those native states do not receive invented universal props. Verify keyboard focus and exits, then connect changed values through the native callback signature.

Business integration boundary

Render the provided button props so the hidden input is connected. File selection and upload are separate; cancel is not successful selection.

Host setup

Install pinned @mantine/core and @mantine/hooks, import @mantine/core/styles.css at the entry and use MantineProvider. The example's theme/variant are JGuang-authored host props, not universal native component APIs; forceColorScheme synchronizes the app theme. Native implementation source is a reading reference: use npm exports rather than copying internal engines. This batch covers Core, not dates/charts/forms extension packages. Detailed upstream prose remains English.

Runnable usage

import { useState } from 'react';
import * as M from '@mantine/core';
import '@mantine/core/styles.css';


function ComponentPreview({ variant = 'Default' }: { variant?: string }) {
const [fileName, setFileName] = useState<string | null>(null);
return <M.FileButton onChange={(file) => setFileName(file?.name ?? null)} accept="image/*">{(props) => <M.Button {...props}>{fileName ?? "Choose a file / 选择文件"}</M.Button>}</M.FileButton>;
}
export default function Example({ variant = 'Default', theme = 'light' }: { variant?: string; theme?: 'light' | 'dark' }) {
return <M.MantineProvider forceColorScheme={theme} theme={{ respectReducedMotion: true }}><ComponentPreview variant={variant} /></M.MantineProvider>;
}

Preview states

Default

The example uses the native Mantine API. For supported input demos, change the local variant to Disabled, Invalid or Read only; these are example labels, not new component props. Keep the documented data, controlled values, stores and compound children when adapting the example.

Usage

Multiple files

Set the multiple prop to allow picking multiple files:

Reset file

resetRef should be used to fix the issue with stale value on the hidden input element as input type file cannot be controlled. Call resetRef when the user selection is cleared:

Source-backed parameter reference

Implementation signatures and types are generated from the pinned local source. Optional props, native inherited types, union branches and never exclusions are preserved. A forwardRef implementation's ref is a React reference, not an additional business prop.

FileButton(props)
export interface FileButtonProps<Multiple extends boolean = false> {
  ref?: React.Ref<HTMLInputElement>;

  /** Called when files are picked */
  onChange: (payload: Multiple extends true ? File[] : File | null) => void;

  /** Function that receives button props and returns react node that should be rendered */
  children: (props: { onClick: () => void }) => React.ReactNode;

  /** If set, user can pick more than one file */
  multiple?: Multiple;

  /** File input accept attribute, for example, `"image/png,image/jpeg"` */
  accept?: string;

  /** Input name attribute */
  name?: string;

  /** Input form attribute */
  form?: string;

  /** Reference of the function that should be called when value changes to null or empty array */
  resetRef?: React.Ref<() => void>;

  /** Disables file picker */
  disabled?: boolean;

  /** Specifies that, optionally, a new file should be captured, and which device should be used to capture that new media of a type defined by the accept attribute. */
  capture?: boolean | 'user' | 'environment';

  /** Passes down props to the input element used to capture files */
  inputProps?: React.ComponentProps<'input'>;
}

export type FileButtonFactory = Factory<{
  props: FileButtonProps;
  signature: <Multiple extends boolean = false>(
    props: FileButtonProps<Multiple>
  ) => React.JSX.Element;
}>;

Native and external types

External types remain defined by React or native dependencies, not manually expanded into guessed props. The installed version's TypeScript definitions are authoritative.

Factory<{
  props: FileButtonProps;
  signature: <Multiple extends boolean = false>(
    props: FileButtonProps<Multiple>
  ) => React.JSX.Element;
}>
React.Ref<HTMLInputElement>
HTMLInputElement
Multiple
File
React.ReactNode
React.Ref<() => void>
React.ComponentProps<'input'>
React.JSX.Element