UNPKG

@activecollab/components

Version:

ActiveCollab Components

146 lines 4.26 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"; const defaultOptions = [{ id: "https://", name: "https://" }, { id: "http://", name: "http://" }]; export const InputUrl = /*#__PURE__*/forwardRef((_ref, ref) => { let { size = "regular", disabled, value: defaultValue = "", onChange, scheme } = _ref, 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 [prefix, setPrefix] = useState(options[0].id); const [value, setValue] = useState(""); 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 { newPrefix, newValue } = separatePrefixFromValue(defaultValue); setPrefix(newPrefix); setValue(newValue); // eslint-disable-next-line }, [defaultValue]); const handleChange = useCallback(e => { const inputValue = e.target.value; if (!inputValue.startsWith(prefix)) { const { newPrefix, newValue } = separatePrefixFromValue(inputValue); 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 = e.clipboardData.getData("text"); const { newPrefix, newValue } = separatePrefixFromValue(paste); 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