UNPKG

@activecollab/components

Version:

ActiveCollab Components

148 lines 4.82 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose"; const _excluded = ["size", "disabled", "value", "onChange", "scheme"]; import React, { forwardRef, useCallback, useEffect, useMemo, useRef, useState } from "react"; import { Input } from "./Input"; import { InputSelect } from "./InputSelect"; import { StyledInputSelectTrigger } from "./Styles"; import { useForkRef } from "../../utils"; import { sanitizeAndTrim } from "../../utils/stringUtils"; const defaultOptions = [{ id: "https://", name: "https://" }, { id: "http://", name: "http://" }]; export const InputUrl = /*#__PURE__*/forwardRef((_ref, ref) => { let _ref$size = _ref.size, size = _ref$size === void 0 ? "regular" : _ref$size, disabled = _ref.disabled, _ref$value = _ref.value, defaultValue = _ref$value === void 0 ? "" : _ref$value, onChange = _ref.onChange, scheme = _ref.scheme, rest = _objectWithoutPropertiesLoose(_ref, _excluded); const inputRef = useRef(null); const handleRef = useForkRef(ref, inputRef); const options = useMemo(() => { if (scheme && scheme.length > 0) { if (Array.isArray(scheme)) { return scheme.map(i => ({ id: i, name: i })); } else { return [{ id: scheme, name: scheme }]; } } return defaultOptions; }, [scheme]); const _useState = useState(options[0].id), prefix = _useState[0], setPrefix = _useState[1]; const _useState2 = useState(""), value = _useState2[0], setValue = _useState2[1]; const separatePrefixFromValue = fullValue => { let newPrefix = prefix; let newValue = fullValue; options.forEach(option => { if (fullValue.startsWith(option.id)) { newPrefix = option.id; newValue = fullValue.replace(option.id, ""); } }); setValue(newValue); return { newPrefix, newValue }; }; useEffect(() => { const _separatePrefixFromVa = separatePrefixFromValue(defaultValue), newPrefix = _separatePrefixFromVa.newPrefix, newValue = _separatePrefixFromVa.newValue; setPrefix(newPrefix); setValue(newValue); // eslint-disable-next-line }, [defaultValue]); const handleChange = useCallback(e => { const inputValue = e.target.value; if (!inputValue.startsWith(prefix)) { const _separatePrefixFromVa2 = separatePrefixFromValue(inputValue), newPrefix = _separatePrefixFromVa2.newPrefix, newValue = _separatePrefixFromVa2.newValue; setPrefix(newPrefix); setValue(newValue); typeof onChange === "function" && onChange(newPrefix + newValue); } else { const newValue = inputValue.replace(prefix, ""); setValue(newValue); typeof onChange === "function" && onChange(prefix + newValue); } }, // eslint-disable-next-line [onChange, prefix]); const handleSelectChange = useCallback(val => { setPrefix(val); setTimeout(() => { var _inputRef$current; (_inputRef$current = inputRef.current) == null || _inputRef$current.focus(); }, 200); typeof onChange === "function" && onChange(val + value); }, [onChange, value]); const handlePaste = e => { e.preventDefault(); const paste = sanitizeAndTrim(e.clipboardData.getData("text")); const _separatePrefixFromVa3 = separatePrefixFromValue(paste), newPrefix = _separatePrefixFromVa3.newPrefix, newValue = _separatePrefixFromVa3.newValue; if (newPrefix !== prefix) { setPrefix(newPrefix); } setValue(newValue); if (typeof onChange === "function") { onChange(newPrefix + newValue); } }; const startAdornmentComponent = useMemo(() => { if (options.length > 1) { return /*#__PURE__*/React.createElement(InputSelect, { key: prefix, size: size, onChange: handleSelectChange, options: options, selected: prefix, disabled: disabled }); } else { return /*#__PURE__*/React.createElement(StyledInputSelectTrigger, { key: prefix, $disabled: disabled, $size: size }, prefix); } }, [disabled, handleSelectChange, options, prefix, size]); useEffect(() => { if (options.length > 0 && !defaultValue) { setPrefix(options[0].id); } }, [defaultValue, options]); return /*#__PURE__*/React.createElement(Input, _extends({}, rest, { ref: handleRef, size: size, value: value, disabled: disabled, type: "text", onPaste: handlePaste, onChange: handleChange, "data-testid": "InputUrl", startAdornment: startAdornmentComponent })); }); InputUrl.displayName = "InputUrl"; //# sourceMappingURL=InputUrl.js.map