import React, { ChangeEvent, forwardRef, useEffect, useState } from "react"; import Input, { InputProps } from "../input"; import { isHexColor } from "../../utils/colors"; import { ControlSize } from "../../common/controls.type"; import Icon from "../icon"; import ColorPicker, { ColorPickerProps } from "../color-picker"; import IconButton from "../icon-button"; import CloseIcon from "../../icons/close-icon"; import Button, { ButtonProps } from "../button"; interface InputColorPickerPreviewProps { color?: string; onClick?: () => void; } const InputColorPickerPreview = ({ color, onClick, }: InputColorPickerPreviewProps) => { return (
); }; interface Props extends Omit { onChange: (value?: string) => void; pickButton?: ButtonProps; modal?: ColorPickerProps["modal"]; } const InputColorPicker = (props: Props) => { const { value, onChange, pickButton, modal } = props; const [inputValue, setInputValue] = useState(value); useEffect(() => { setInputValue(value); }, [value]); const handleInputChange = (e: ChangeEvent) => { const newValue = e.target.value; setInputValue(newValue); }; const handleKeyUp = (e: React.KeyboardEvent) => { const isEnter = e.key == "Enter"; if (isEnter) { onSubmit(); } }; const onSubmit = () => { if (!inputValue || !value) { handleOnChange(); return; } if (isHexColor(inputValue)) { handleOnChange(inputValue); } else { handleOnChange(value); } }; const handleOnChange = (value?: string) => { if (!value) { onChange(""); setInputValue(""); return; } onChange(value); setInputValue(value); }; const handleBlur = (e: React.FocusEvent) => { if (props.onBlur) { props.onBlur(e); } onSubmit(); }; return ( {({ pickColor, clearColor, color, hasColor }) => hasColor ? ( } value={inputValue} onChange={handleInputChange} onKeyUp={handleKeyUp} onBlur={handleBlur} htmlSize={10} maxLength={9} suffix={ hasColor ? ( } /> ) : undefined } /> ) : (