UNPKG

@navikt/ds-react

Version:

React components from the Norwegian Labour and Welfare Administration.

78 lines 6.39 kB
/* eslint-disable jsx-a11y/no-static-element-interactions */ 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; }; /* eslint-disable jsx-a11y/click-events-have-key-events */ import React, { forwardRef } from "react"; import { CircleSlashIcon, CloudUpIcon } from "@navikt/aksel-icons"; import { Button } from "../../../../button/index.js"; import { BodyShort, ErrorMessage, Label } from "../../../../typography/index.js"; import { omit, useId } from "../../../../utils-external/index.js"; import { cl, composeEventHandlers } from "../../../../utils/helpers/index.js"; import { useI18n } from "../../../../utils/i18n/i18n.hooks.js"; import { useFormField } from "../../../useFormField.js"; import { useFileUploadTranslation } from "../../FileUpload.context.js"; import { useFileUpload } from "../../useFileUpload.js"; import { useDropzone } from "./useDropzone.js"; const Dropzone = forwardRef((props, ref) => { const { onSelect, error, label, description, className, multiple = true, accept, validator, maxSizeInBytes, fileLimit, icon: DropzoneIcon = CloudUpIcon, disabled, translations, onClick, id: buttonId, "data-color": color } = props, rest = __rest(props, ["onSelect", "error", "label", "description", "className", "multiple", "accept", "validator", "maxSizeInBytes", "fileLimit", "icon", "disabled", "translations", "onClick", "id", "data-color"]); const context = useFileUploadTranslation(false); const translate = useI18n("FileUpload", { dropzone: translations }, context === null || context === void 0 ? void 0 : context.translations); const fileLimitReached = fileLimit && (fileLimit === null || fileLimit === void 0 ? void 0 : fileLimit.current) >= (fileLimit === null || fileLimit === void 0 ? void 0 : fileLimit.max) && (fileLimit === null || fileLimit === void 0 ? void 0 : fileLimit.max) > 0; const _disabled = disabled !== null && disabled !== void 0 ? disabled : fileLimitReached; const { inputProps, errorId, showErrorMsg, hasError, inputDescriptionId } = useFormField(Object.assign(Object.assign({}, omit(props, ["id"])), { disabled: _disabled }), "fileUpload"); const { id: inputId, "aria-describedby": ariaDescribedby } = inputProps, inputPropsRest = __rest(inputProps, ["id", "aria-describedby"]); const labelId = useId(); const { upload, onChange, inputRef, mergedRef } = useFileUpload({ ref, onSelect, validator, accept, maxSizeInBytes, disabled: inputProps.disabled, }); const dropzoneCtx = useDropzone({ upload, disabled: inputProps.disabled, }); return (React.createElement("div", { className: cl("aksel-form-field", "aksel-dropzone", className, { "aksel-dropzone--error": hasError, "aksel-dropzone--dragging": dropzoneCtx.isDraggingOver, "aksel-dropzone--disabled": inputProps.disabled, }) }, React.createElement(Label, { htmlFor: inputId, id: labelId, className: "aksel-form-field__label" }, label), !!description && (React.createElement(BodyShort, { id: inputDescriptionId, className: "aksel-form-field__description", as: "div" }, description)), React.createElement("div", { className: "aksel-dropzone__area", onDragEnter: dropzoneCtx.onDragEnter, onDragOver: dropzoneCtx.onDragOver, onDragLeave: dropzoneCtx.onDragLeave, onDrop: dropzoneCtx.onDrop, onClick: composeEventHandlers(onClick, () => { var _a; return (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.click(); }), "data-disabled": inputProps.disabled, "data-color": color }, !inputProps.disabled && (React.createElement(React.Fragment, null, React.createElement("div", { className: "aksel-dropzone__area-icon" }, React.createElement(DropzoneIcon, { fontSize: "1.5rem", "aria-hidden": true })), React.createElement("div", { className: "aksel-dropzone__area-release" }, React.createElement("div", { className: "aksel-dropzone__area-release__icon" }, React.createElement(DropzoneIcon, { "aria-hidden": true })), React.createElement("span", { "aria-hidden": !dropzoneCtx.isDraggingOver, className: "aksel-dropzone__area-release__text" }, translate("dropzone.drop"))), React.createElement("div", { "aria-hidden": true }, React.createElement(BodyShort, { as: "div", spacing: true }, multiple ? translate("dropzone.dragAndDropMultiple") : translate("dropzone.dragAndDrop")), React.createElement(BodyShort, { as: "div" }, translate("dropzone.or"))), React.createElement(Button, Object.assign({}, omit(rest, ["errorId"]), inputPropsRest, { id: buttonId, "aria-describedby": cl(labelId, ariaDescribedby), className: "aksel-dropzone__area-button", type: "button", variant: "secondary" }), multiple ? translate("dropzone.buttonMultiple") : translate("dropzone.button")))), inputProps.disabled && (React.createElement("div", { className: "aksel-dropzone__area-disabled" }, React.createElement(CircleSlashIcon, { "aria-hidden": true, fontSize: "1.75rem" }), React.createElement(BodyShort, { as: "div" }, fileLimitReached ? translate("dropzone.disabledFilelimit") : translate("dropzone.disabled")))), React.createElement("input", { id: inputId, type: "file", style: { display: "none" }, multiple: multiple, accept: accept, onChange: onChange, ref: mergedRef, disabled: inputProps.disabled })), React.createElement("div", { className: "aksel-form-field__error", id: errorId, "aria-relevant": "additions removals", "aria-live": "polite" }, showErrorMsg && React.createElement(ErrorMessage, { showIcon: true }, error)))); }); export default Dropzone; //# sourceMappingURL=Dropzone.js.map