{"version":3,"file":"FileButton.cjs","sources":["../../../src/components/FileButton/FileButton.tsx"],"sourcesContent":["import { forwardRef, useRef } from 'react';\nimport { assignRef, useMergedRef } from '@raikou/hooks';\nimport { useProps } from '../../core';\n\nexport interface FileButtonProps<Multiple extends boolean = false> {\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  /** Determines whether 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.ForwardedRef<() => 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.ComponentPropsWithoutRef<'input'>;\n}\n\nconst defaultProps: Partial<FileButtonProps> = {\n  multiple: false,\n};\n\ntype FileButtonComponent = (<Multiple extends boolean = false>(\n  props: FileButtonProps<Multiple>\n) => React.ReactElement) & { displayName?: string };\n\nexport const FileButton: FileButtonComponent = forwardRef<HTMLInputElement, FileButtonProps>(\n  (props, ref) => {\n    const {\n      onChange,\n      children,\n      multiple,\n      accept,\n      name,\n      form,\n      resetRef,\n      disabled,\n      capture,\n      inputProps,\n      ...others\n    } = useProps('FileButton', defaultProps, props);\n\n    const inputRef = useRef<HTMLInputElement>();\n\n    const onClick = () => {\n      !disabled && inputRef.current?.click();\n    };\n\n    const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {\n      if (multiple) {\n        onChange(Array.from(event.currentTarget!.files!) as any);\n      } else {\n        onChange(event.currentTarget!.files![0] || 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        {children({ onClick, ...others })}\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    );\n  }\n) as any;\n\nFileButton.displayName = '@raikou/core/FileButton';\n"],"names":["forwardRef","useProps","useRef","assignRef","jsxs","Fragment","jsx","useMergedRef"],"mappings":";;;;;;;;;;;AAoCA,MAAM,YAAyC,GAAA;AAAA,EAC7C,QAAU,EAAA;AACZ,CAAA;AAMO,MAAM,UAAkC,GAAAA,gBAAA;AAAA,EAC7C,CAAC,OAAO,GAAQ,KAAA;AACd,IAAM,MAAA;AAAA,MACJ,QAAA;AAAA,MACA,QAAA;AAAA,MACA,QAAA;AAAA,MACA,MAAA;AAAA,MACA,IAAA;AAAA,MACA,IAAA;AAAA,MACA,QAAA;AAAA,MACA,QAAA;AAAA,MACA,OAAA;AAAA,MACA,UAAA;AAAA,MACA,GAAG;AAAA,KACD,GAAAC,iBAAA,CAAS,YAAc,EAAA,YAAA,EAAc,KAAK,CAAA;AAE9C,IAAA,MAAM,WAAWC,YAAyB,EAAA;AAE1C,IAAA,MAAM,UAAU,MAAM;AACpB,MAAC,CAAA,QAAA,IAAY,QAAS,CAAA,OAAA,EAAS,KAAM,EAAA;AAAA,KACvC;AAEA,IAAM,MAAA,YAAA,GAAe,CAAC,KAA+C,KAAA;AACnE,MAAA,IAAI,QAAU,EAAA;AACZ,QAAA,QAAA,CAAS,KAAM,CAAA,IAAA,CAAK,KAAM,CAAA,aAAA,CAAe,KAAM,CAAQ,CAAA;AAAA,OAClD,MAAA;AACL,QAAA,QAAA,CAAS,KAAM,CAAA,aAAA,CAAe,KAAO,CAAA,CAAC,KAAK,IAAI,CAAA;AAAA;AACjD,KACF;AAEA,IAAA,MAAM,QAAQ,M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