@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
97 lines (93 loc) • 2.61 kB
JavaScript
"use client";
import { visuallyHiddenAttributes } from "../../utils/dom.js";
import { assignRef, mergeRefs } from "../../utils/ref.js";
import { utils_exports } from "../../utils/index.js";
import { useControllableState } from "../../hooks/use-controllable-state/index.js";
import { useFieldProps } from "../field/use-field-props.js";
import { useClickable } from "../../hooks/use-clickable/index.js";
import { useCallback, useRef } from "react";
//#region src/components/file-input/use-file-input.ts
const useFileInput = (props) => {
const { props: { id, ref, form, name, accept, defaultValue, disabled, multiple, readOnly, required, resetRef, value, onChange: onChangeProp, onClick: onClickProp,...rest }, ariaProps, dataProps, eventProps } = useFieldProps(props);
const interactive = !(readOnly || disabled);
const inputRef = useRef(null);
const [values, setValues] = useControllableState({
defaultValue,
value,
onChange: onChangeProp
});
const count = values?.length ?? 0;
const onClick = useCallback(() => {
if (!interactive) return;
inputRef.current?.click();
}, [interactive]);
const onReset = useCallback(() => {
if (inputRef.current) inputRef.current.value = "";
setValues(void 0);
}, [setValues]);
const onChange = useCallback((ev) => {
const files = !(0, utils_exports.isNull)(ev.currentTarget.files) ? Array.from(ev.currentTarget.files) : void 0;
setValues(files?.length ? files : void 0);
}, [setValues]);
const clickableProps = useClickable({
...dataProps,
...eventProps,
...rest,
disabled,
focusOnClick: interactive,
onClick: (0, utils_exports.handlerAll)(onClickProp, onClick)
});
assignRef(resetRef, onReset);
const getInputProps = useCallback((props$1 = {}) => ({
...visuallyHiddenAttributes,
...ariaProps,
...dataProps,
id,
form,
type: "file",
name,
accept,
disabled,
multiple,
readOnly,
required,
...props$1,
ref: mergeRefs(inputRef, props$1.ref, ref),
onChange: (0, utils_exports.handlerAll)(props$1.onChange, onChange)
}), [
required,
ariaProps,
dataProps,
id,
form,
name,
accept,
disabled,
multiple,
readOnly,
ref,
onChange
]);
return {
disabled,
interactive,
readOnly,
required,
values,
clickableProps,
getFieldProps: useCallback((props$1 = {}) => ({
"data-placeholder": (0, utils_exports.dataAttr)(!count),
...clickableProps,
tabIndex: interactive ? 0 : clickableProps.tabIndex,
...props$1
}), [
clickableProps,
count,
interactive
]),
getInputProps
};
};
//#endregion
export { useFileInput };
//# sourceMappingURL=use-file-input.js.map