UNPKG

@amaui/ui-react

Version:
206 lines (205 loc) 11 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = __importDefault(require("react")); const utils_1 = require("@amaui/utils"); const style_react_1 = require("@amaui/style-react"); const IconMaterialCloudUploadW100_1 = __importDefault(require("@amaui/icons-material-rounded-react/IconMaterialCloudUploadW100")); const FileChoose_1 = __importDefault(require("../FileChoose")); const Surface_1 = __importDefault(require("../Surface")); const Type_1 = __importDefault(require("../Type")); const Line_1 = __importDefault(require("../Line")); const Interaction_1 = __importDefault(require("../Interaction")); const utils_2 = require("../utils"); const useStyle = (0, style_react_1.style)(theme => ({ root: {}, wrapperText: { width: '100%', height: '100%', userSelect: 'none', pointerEvents: 'none' }, wrapper: { width: '100%' }, label: { position: 'relative', '&.amaui-Surface-root': { width: '100%', height: '174px', outlineStyle: 'dashed', outlineWidth: '3px', borderRadius: theme.methods.shape.radius.value('lg', 'px'), cursor: 'pointer' } }, disabled: { opacity: '0.4', cursor: 'default', pointerEvents: 'none' } }), { name: 'amaui-DropZone' }); const DropZone = react_1.default.forwardRef((props_, ref) => { const theme = (0, style_react_1.useAmauiTheme)(); const props = react_1.default.useMemo(() => { var _a, _b, _c, _d, _e, _f, _g, _h; return (Object.assign(Object.assign(Object.assign({}, (_d = (_c = (_b = (_a = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _a === void 0 ? void 0 : _a.elements) === null || _b === void 0 ? void 0 : _b.all) === null || _c === void 0 ? void 0 : _c.props) === null || _d === void 0 ? void 0 : _d.default), (_h = (_g = (_f = (_e = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _e === void 0 ? void 0 : _e.elements) === null || _f === void 0 ? void 0 : _f.amauiDropZone) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]); const Line = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Line) || Line_1.default; }, [theme]); const FileChoose = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.FileChoose) || FileChoose_1.default; }, [theme]); const Surface = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Surface) || Surface_1.default; }, [theme]); const Type = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Type) || Type_1.default; }, [theme]); const Interaction = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Interaction) || Interaction_1.default; }, [theme]); const { tonal = true, color = 'default', version, start, files, multiple, max, allowedTypes, valueDefault, value: value_, onChange: onChange_, IconStart = IconMaterialCloudUploadW100_1.default, onFocus: onFocus_, onBlur: onBlur_, onDrop: onDrop_, onDragOver: onDragOver_, onDragEnter: onDragEnter_, onDragLeave: onDragLeave_, disabled, children, className } = props, other = __rest(props, ["tonal", "color", "version", "start", "files", "multiple", "max", "allowedTypes", "valueDefault", "value", "onChange", "IconStart", "onFocus", "onBlur", "onDrop", "onDragOver", "onDragEnter", "onDragLeave", "disabled", "children", "className"]); const { classes } = useStyle(); const [init, setInit] = react_1.default.useState(false); const [focus, setFocus] = react_1.default.useState(false); const [dragOver, setDragOver] = react_1.default.useState(false); const [value, setValue] = react_1.default.useState(valueDefault !== undefined ? valueDefault : value_); const refs = { root: react_1.default.useRef(undefined), value: react_1.default.useRef(undefined), label: react_1.default.useRef(undefined), input: react_1.default.useRef(undefined), focus: react_1.default.useRef(undefined), allowedTypes: react_1.default.useRef(undefined), max: react_1.default.useRef(undefined) }; refs.value.current = value; refs.focus.current = focus; refs.max.current = max; refs.allowedTypes.current = allowedTypes; react_1.default.useEffect(() => { var _a; const method = (event) => { switch (event.key) { case 'Enter': if (refs.focus.current) refs.input.current.click(); break; default: break; } }; const rootDocument = (0, utils_1.isEnvironment)('browser') ? (((_a = refs.root.current) === null || _a === void 0 ? void 0 : _a.ownerDocument) || window.document) : undefined; rootDocument.addEventListener('keydown', method); setInit(true); return () => { rootDocument.removeEventListener('keydown', method); }; }, []); react_1.default.useEffect(() => { if (init) { if (value_ !== refs.value.current) setValue(value_); } }, [value_]); const onChange = react_1.default.useCallback((valueNew_) => { let valueNew = valueNew_; // Allowed types if ((0, utils_1.is)('array', refs.allowedTypes.current)) valueNew = valueNew.map((item) => refs.allowedTypes.current.includes(item.name)); // Max if (refs.max.current) valueNew = valueNew.slice(0, refs.max.current); if (!props.hasOwnProperty('value')) setValue(valueNew); if ((0, utils_1.is)('function', onChange_)) onChange_(valueNew); }, []); const onFocus = react_1.default.useCallback((event) => { setFocus(true); if ((0, utils_1.is)('function', onFocus_)) onFocus_(event); }, [onFocus_]); const onBlur = react_1.default.useCallback((event) => { setFocus(false); if ((0, utils_1.is)('function', onBlur_)) onBlur_(event); }, [onBlur_]); const onDrop = react_1.default.useCallback((event) => { event.preventDefault(); setDragOver(false); const valueNew = Array.from(event.dataTransfer.files); onChange(valueNew); if ((0, utils_1.is)('function', onDrop_)) onDrop_(event); }, []); const onDragEnter = react_1.default.useCallback((event) => { event.preventDefault(); setDragOver(true); if ((0, utils_1.is)('function', onDragEnter_)) onDragEnter_(event); }, []); const onDragLeave = react_1.default.useCallback((event) => { event.preventDefault(); setDragOver(false); if ((0, utils_1.is)('function', onDragLeave_)) onDragLeave_(event); }, []); const onDragOver = react_1.default.useCallback((event) => { event.preventDefault(); if ((0, utils_1.is)('function', onDragOver_)) onDragOver_(event); }, []); other.onDrop = onDrop; other.onDragEnter = onDragEnter; other.onDragLeave = onDragLeave; other.onDragOver = onDragOver; const custom = !!children; let WrapperProps = {}; let ComponentProps = {}; if (files) { WrapperProps = Object.assign(Object.assign({}, WrapperProps), other); } else { ComponentProps = Object.assign(Object.assign({}, ComponentProps), other); } return ((0, jsx_runtime_1.jsxs)(FileChoose, Object.assign({ ref: item => { if (ref) { if ((0, utils_1.is)('function', ref)) ref(item); else ref.current = item; } refs.root.current = item; }, inputRef: refs.input, valueDefault: valueDefault, value: value, tonal: tonal, color: color, version: 'text', files: files, multiple: multiple, max: max, allowedTypes: allowedTypes, start: undefined, onChange: onChange, Component: Surface, className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('DropZone', theme) && [ 'amaui-DropZone-root' ], className, classes.root, disabled && classes.disabled ]), ComponentProps: Object.assign(Object.assign({ tabIndex: 0, onFocus, onBlur }, ComponentProps), { className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('DropZone', theme) && [ 'amaui-DropZone-label' ], ComponentProps === null || ComponentProps === void 0 ? void 0 : ComponentProps.className, !custom && classes.label ]) }), WrapperProps: Object.assign(Object.assign({}, WrapperProps), { className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('DropZone', theme) && [ 'amaui-DropZone-wrapper' ], WrapperProps === null || WrapperProps === void 0 ? void 0 : WrapperProps.className, classes.wrapper ]) }) }, { children: [(0, jsx_runtime_1.jsx)(Interaction, { pulse: focus || dragOver }), children, !custom && ((0, jsx_runtime_1.jsxs)(Line, Object.assign({ direction: 'row', align: 'center', justify: 'center', className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('DropZone', theme) && [ 'amaui-DropZone-wrapper-text' ], classes.wrapperText ]) }, { children: [(0, jsx_runtime_1.jsx)(IconStart, {}), (0, jsx_runtime_1.jsx)(Type, Object.assign({ version: 't1' }, { children: "Drop files here" }))] })))] }))); }); DropZone.displayName = 'amaui-DropZone'; exports.default = DropZone;