Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

1.7 KB
Raw Blame
import { FileButton as MantineFileButton } from "@mantine/core";
import type { ForwardedRef, InputHTMLAttributes, ReactNode } from "react";
import { Button } from "@editor/ui/Button";
import type { ButtonProps } from "@editor/ui/Button";

type TriggerButtonProps = Omit<
  ButtonProps,
  "onClick" | "onChange" | "children"
>;

export interface FilePickerProps<
  Multiple extends boolean = false,
> extends TriggerButtonProps {
  /** Called with the picked file(s); null when the dialog is dismissed. */
  onChange: (payload: Multiple extends true ? File[] : File | null) => void;
  accept?: string;
  multiple?: Multiple;
  /** Ref to a function that clears the current selection (Mantine resetRef). */
  resetRef?: ForwardedRef<() => void>;
  name?: string;
  capture?: boolean | "user" | "environment";
  /** Props forwarded to the hidden <input> element (e.g. { id: 'my-input' } for test selectors). */
  inputProps?: InputHTMLAttributes<HTMLInputElement>;
  /** Trigger label. */
  children?: ReactNode;
}

/** File-picker button: a shared Button trigger over a hidden file input (Mantine FileButton). */
export function FilePicker<Multiple extends boolean = false>({
  onChange,
  accept,
  multiple,
  resetRef,
  name,
  capture,
  disabled,
  inputProps,
  children,
  ...buttonProps
}: FilePickerProps<Multiple>) {
  return (
    <MantineFileButton<Multiple>
      onChange={onChange}
      accept={accept}
      multiple={multiple}
      resetRef={resetRef}
      name={name}
      capture={capture}
      disabled={disabled}
      inputProps={inputProps}
    >
      {(props) => (
        <Button {...buttonProps} {...props} disabled={disabled}>
          {children}
        </Button>
      )}
    </MantineFileButton>
  );
}