UNPKG

@kiwicom/orbit-components

Version:

Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.

196 lines (187 loc) 6.05 kB
import * as React from "react"; import styled, { css } from "styled-components"; import defaultTheme from "../defaultTheme"; import Button from "../Button"; import ButtonLink from "../ButtonLink"; import FormLabel from "../FormLabel"; import ErrorFormTooltip from "../ErrorFormTooltip"; import Attachment from "../icons/Attachment"; import CloseCircle from "../icons/CloseCircle"; import { rtlSpacing } from "../utils/rtl"; import getSpacingToken from "../common/getSpacingToken"; import getFieldDataState from "../common/getFieldDataState"; import formElementFocus from "../InputField/helpers/formElementFocus"; import useErrorTooltip from "../ErrorFormTooltip/hooks/useErrorTooltip"; import mq from "../utils/mediaQuery"; const Field = styled.label.withConfig({ displayName: "InputFile__Field", componentId: "sc-10wwda7-0" })(["", ""], ({ theme, $width }) => css(["font-family:", ";display:block;position:relative;width:", ";margin-bottom:", ";"], theme.orbit.fontFamily, $width, getSpacingToken)); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 Field.defaultProps = { theme: defaultTheme }; const FakeInput = styled(({ children, className }) => /*#__PURE__*/React.createElement("div", { className: className }, children)).withConfig({ displayName: "InputFile__FakeInput", componentId: "sc-10wwda7-1" })(["box-sizing:border-box;display:flex;align-items:center;padding:", ";height:", ";box-shadow:inset 0 0 0 ", ";background-color:", ";transition:box-shadow ", " ease-in-out;border-radius:6px;", ";&:hover{box-shadow:inset 0 0 0 ", ";}"], ({ theme }) => rtlSpacing(theme.orbit.paddingInputFile), ({ theme }) => theme.orbit.heightInputNormal, ({ theme, error }) => `${theme.orbit.borderWidthInput} ${error ? theme.orbit.borderColorInputError : theme.orbit.borderColorInput}`, ({ theme }) => theme.backgroundInput, ({ theme }) => theme.orbit.durationFast, mq.tablet(css(["border-radius:", ";"], ({ theme }) => theme.orbit.borderRadiusNormal)), ({ theme, error }) => `${theme.orbit.borderWidthInput} ${error ? theme.orbit.paletteRedNormalHover : theme.orbit.borderColorInputHover}`); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 FakeInput.defaultProps = { theme: defaultTheme }; const Input = styled.input.withConfig({ displayName: "InputFile__Input", componentId: "sc-10wwda7-2" })(["opacity:0;position:absolute;height:0;&:focus ~ ", "{", "}"], FakeInput, formElementFocus); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 Input.defaultProps = { theme: defaultTheme }; const getFileInputColor = ({ error, fileName }, theme) => { if (error) { return theme.orbit.paletteRedNormal; } if (fileName) { return theme.orbit.colorTextInput; } return theme.orbit.paletteInkLight; }; const StyledFileInput = styled.div.withConfig({ displayName: "InputFile__StyledFileInput", componentId: "sc-10wwda7-3" })(["font-family:", ";color:", ";width:100%;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;padding:", ";"], ({ theme }) => theme.orbit.fontFamily, ({ error, fileName, theme }) => getFileInputColor({ error, fileName }, theme), ({ theme }) => rtlSpacing(`0 0 0 ${theme.orbit.spaceSmall}`)); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 StyledFileInput.defaultProps = { theme: defaultTheme }; const InputFile = /*#__PURE__*/React.forwardRef((props, ref) => { const { placeholder = "No file selected", buttonLabel = "Select file", required, onRemoveFile, dataTest, id, helpClosable = true, spaceAfter, width = "100%", help, error, onFocus, onBlur, name, label, onChange, allowedFileTypes, tabIndex, fileName, insideInputGroup } = props; const { tooltipShown, tooltipShownHover, setTooltipShown, setTooltipShownHover, labelRef, iconRef, handleFocus } = useErrorTooltip({ onFocus }); const shown = tooltipShown || tooltipShownHover; return /*#__PURE__*/React.createElement(Field, { spaceAfter: spaceAfter, ref: label ? null : labelRef, $width: width }, /*#__PURE__*/React.createElement(Input, { "data-test": dataTest, id: id, "aria-required": required, "data-state": insideInputGroup && typeof error === "undefined" ? undefined : getFieldDataState(!!props.error), type: "file", name: name, error: error, onChange: onChange, onFocus: handleFocus, onBlur: onBlur, accept: allowedFileTypes, tabIndex: tabIndex }), label && /*#__PURE__*/React.createElement(FormLabel, { filled: !!fileName, error: !!error, help: !!help, labelRef: labelRef, iconRef: iconRef, onMouseEnter: () => setTooltipShownHover(true), onMouseLeave: () => setTooltipShownHover(false) }, label), /*#__PURE__*/React.createElement(FakeInput, { error: error }, /*#__PURE__*/React.createElement(Button, { type: "secondary", size: "small", iconLeft: /*#__PURE__*/React.createElement(Attachment, null), asComponent: "div", role: "button" }, buttonLabel), /*#__PURE__*/React.createElement(StyledFileInput, { fileName: fileName, error: error, ref: ref }, fileName || placeholder), fileName && /*#__PURE__*/React.createElement(ButtonLink, { type: "primary", compact: true, iconLeft: /*#__PURE__*/React.createElement(CloseCircle, { ariaLabel: "remove", color: "secondary" }), onClick: ev => { ev.preventDefault(); if (onRemoveFile) { onRemoveFile(); } } })), !insideInputGroup && /*#__PURE__*/React.createElement(ErrorFormTooltip, { help: help, error: error, helpClosable: helpClosable, inputSize: "normal", referenceElement: label ? iconRef : labelRef, shown: shown, onShown: setTooltipShown })); }); InputFile.displayName = "InputFile"; export default InputFile;