@selfcommunity/react-ui
Version:
React UI Components to integrate a Community created with SelfCommunity Platform.
30 lines (29 loc) • 1.58 kB
JavaScript
import { __rest } from "tslib";
import { jsx as _jsx } from "react/jsx-runtime";
import { forwardRef, useCallback, useRef } from "react";
import { markAsUploadOptionsComponent, useUploadyContext } from "@rpldy/shared-ui";
const asUploadButton = (Component, InputProps) => {
const AsUploadButton = (props, ref) => {
const { showFileUpload, getInternalFileInput } = useUploadyContext();
const { id, className, text, children, extraProps, onClick } = props, uploadOptions = __rest(props, ["id", "className", "text", "children", "extraProps", "onClick"]);
//using ref so onButtonClick can stay memoized
const uploadOptionsRef = useRef();
uploadOptionsRef.current = uploadOptions;
const onButtonClick = useCallback((e) => {
const input = getInternalFileInput();
input.current.accept = InputProps.accept;
if (InputProps.capture) {
input.current.capture = InputProps.capture;
}
else {
input.current.removeAttribute('capture');
}
showFileUpload(uploadOptionsRef.current);
onClick === null || onClick === void 0 ? void 0 : onClick(e);
}, [getInternalFileInput, showFileUpload, uploadOptionsRef, onClick]);
return _jsx(Component, Object.assign({ ref: ref, onClick: onButtonClick, id: id, className: className, children: children || text || "Upload" }, extraProps));
};
markAsUploadOptionsComponent(AsUploadButton);
return forwardRef(AsUploadButton);
};
export default asUploadButton;