UNPKG

@mantine/core

Version:

React components library focused on usability, accessibility and developer experience

1 lines 4.09 kB
{"version":3,"file":"FileButton.cjs","names":["genericFactory","useProps"],"sources":["../../../src/components/FileButton/FileButton.tsx"],"sourcesContent":["import React, { useRef } from 'react';\nimport { assignRef, useMergedRef } from '@mantine/hooks';\nimport { Factory, genericFactory, useProps } from '../../core';\n\nexport interface FileButtonProps<Multiple extends boolean = false> {\n ref?: React.Ref<HTMLInputElement>;\n\n /** Called when files are picked */\n onChange: (payload: Multiple extends true ? File[] : File | null) => void;\n\n /** Function that receives button props and returns react node that should be rendered */\n children: (props: { onClick: () => void }) => React.ReactNode;\n\n /** If set, user can pick more than one file */\n multiple?: Multiple;\n\n /** File input accept attribute, for example, `\"image/png,image/jpeg\"` */\n accept?: string;\n\n /** Input name attribute */\n name?: string;\n\n /** Input form attribute */\n form?: string;\n\n /** Reference of the function that should be called when value changes to null or empty array */\n resetRef?: React.Ref<() => void>;\n\n /** Disables file picker */\n disabled?: boolean;\n\n /** 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. */\n capture?: boolean | 'user' | 'environment';\n\n /** Passes down props to the input element used to capture files */\n inputProps?: React.ComponentProps<'input'>;\n}\n\nexport type FileButtonFactory = Factory<{\n props: FileButtonProps;\n signature: <Multiple extends boolean = false>(\n props: FileButtonProps<Multiple>\n ) => React.JSX.Element;\n}>;\n\nexport const FileButton = genericFactory<FileButtonFactory>((props) => {\n const {\n onChange,\n children,\n multiple,\n accept,\n name,\n form,\n resetRef,\n disabled,\n capture,\n inputProps,\n ref,\n ...others\n } = useProps('FileButton', null, props);\n\n const inputRef = useRef<HTMLInputElement>(null);\n\n const onClick = () => {\n !disabled && inputRef.current?.click();\n };\n\n const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {\n if (event.currentTarget.files === null) {\n return onChange(multiple ? ([] as any) : null);\n }\n\n if (multiple) {\n onChange(Array.from(event.currentTarget.files) as any);\n } else {\n onChange((event.currentTarget.files[0] as any) || null);\n }\n };\n\n const reset = () => {\n if (inputRef.current) {\n inputRef.current.value = '';\n }\n };\n\n assignRef(resetRef, reset);\n\n return (\n <>\n <input\n style={{ display: 'none' }}\n type=\"file\"\n accept={accept}\n multiple={multiple}\n onChange={handleChange}\n ref={useMergedRef(ref, inputRef)}\n name={name}\n form={form}\n capture={capture}\n {...inputProps}\n />\n\n {children({ onClick, ...others })}\n </>\n );\n});\n\nFileButton.displayName = '@mantine/core/FileButton';\n\nexport namespace FileButton {\n export type Props<Multiple extends boolean = false> = FileButtonProps<Multiple>;\n export type Factory = FileButtonFactory;\n}\n"],"mappings":";;;;;;;;;AA6CA,MAAa,aAAaA,gBAAAA,gBAAmC,UAAU;CACrE,MAAM,EACJ,UACA,UACA,UACA,QACA,MACA,MACA,UACA,UACA,SACA,YACA,KACA,GAAG,WACDC,kBAAAA,SAAS,cAAc,MAAM,KAAK;CAEtC,MAAM,YAAA,GAAA,MAAA,QAAoC,IAAI;CAE9C,MAAM,gBAAgB;EACpB,CAAC,YAAY,SAAS,SAAS,MAAM;CACvC;CAEA,MAAM,gBAAgB,UAA+C;EACnE,IAAI,MAAM,cAAc,UAAU,MAChC,OAAO,SAAS,WAAY,CAAC,IAAY,IAAI;EAG/C,IAAI,UACF,SAAS,MAAM,KAAK,MAAM,cAAc,KAAK,CAAQ;OAErD,SAAU,MAAM,cAAc,MAAM,MAAc,IAAI;CAE1D;CAEA,MAAM,cAAc;EAClB,IAAI,SAAS,SACX,SAAS,QAAQ,QAAQ;CAE7B;CAEA,CAAA,GAAA,eAAA,WAAU,UAAU,KAAK;CAEzB,OACE,iBAAA,GAAA,kBAAA,MAAA,kBAAA,UAAA,EAAA,UAAA,CACE,iBAAA,GAAA,kBAAA,KAAC,SAAD;EACE,OAAO,EAAE,SAAS,OAAO;EACzB,MAAK;EACG;EACE;EACV,UAAU;EACV,MAAA,GAAA,eAAA,cAAkB,KAAK,QAAQ;EACzB;EACA;EACG;EACT,GAAI;CACL,CAAA,GAEA,SAAS;EAAE;EAAS,GAAG;CAAO,CAAC,CAChC,EAAA,CAAA;AAEN,CAAC;AAED,WAAW,cAAc"}