@wordpress/components
Version:
UI components for WordPress.
79 lines (76 loc) • 2.09 kB
JavaScript
/**
* WordPress dependencies
*/
import { useRef } from '@wordpress/element';
/**
* Internal dependencies
*/
import Button from '../button';
import { maybeWarnDeprecated36pxSize } from '../utils/deprecated-36px-size';
/**
* FormFileUpload allows users to select files from their local device.
*
* ```jsx
* import { FormFileUpload } from '@wordpress/components';
*
* const MyFormFileUpload = () => (
* <FormFileUpload
* __next40pxDefaultSize
* accept="image/*"
* onChange={ ( event ) => console.log( event.currentTarget.files ) }
* >
* Upload
* </FormFileUpload>
* );
* ```
*/
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export function FormFileUpload({
accept,
children,
multiple = false,
onChange,
onClick,
render,
...props
}) {
const ref = useRef(null);
const openFileDialog = () => {
ref.current?.click();
};
if (!render) {
maybeWarnDeprecated36pxSize({
componentName: 'FormFileUpload',
__next40pxDefaultSize: props.__next40pxDefaultSize,
// @ts-expect-error - We don't "officially" support all Button props but this likely happens.
size: props.size
});
}
const ui = render ? render({
openFileDialog
}) : /*#__PURE__*/_jsx(Button, {
onClick: openFileDialog,
...props,
children: children
});
// iOS browsers may not reliably handle 'audio/*' in the accept attribute.
// Adding explicit audio MIME types improves compatibility across all devices.
const compatAccept = accept?.includes('audio/*') ? `${accept}, audio/mp3, audio/x-m4a, audio/x-m4b, audio/x-m4p, audio/x-wav, audio/webm` : accept;
return /*#__PURE__*/_jsxs("div", {
className: "components-form-file-upload",
children: [ui, /*#__PURE__*/_jsx("input", {
type: "file",
ref: ref,
multiple: multiple,
style: {
display: 'none'
},
accept: compatAccept,
onChange: onChange,
onClick: onClick,
"data-testid": "form-file-upload-input"
})]
});
}
export default FormFileUpload;
//# sourceMappingURL=index.js.map