diff --git a/src/components/core/colorPicker/index.tsx b/src/components/core/colorPicker/index.tsx index 33529b9f..3ba52d1a 100644 --- a/src/components/core/colorPicker/index.tsx +++ b/src/components/core/colorPicker/index.tsx @@ -3,26 +3,38 @@ import { useState } from "react"; type ColorpickerTypes = { colors: { - color01: string; - color02: string; - color03: string; - color04: string; + [key: string]: string; }; isPalleteTouched: boolean; setColors: any; setTouched: any; }; + // eslint-disable-next-line react/prop-types function Colorpicker({ colors, setColors, setTouched, isPalleteTouched }: ColorpickerTypes) { const [selectedColorSection, setColorSection] = useState(null); + const [colorInputs, setColorInputs] = useState(colors); + const changeHandler = (color: string) => { if (!isPalleteTouched) { setTouched(true); } - if (typeof color === "string") - setColors({ ...colors, [`color0` + selectedColorSection]: color }); + if (typeof color === "string") { + const updatedColors = { ...colors, [`color0${selectedColorSection}`]: color }; + setColors(updatedColors); + setColorInputs(updatedColors); + } + }; + + const handleInputChange = (e: React.ChangeEvent, section: number) => { + const value = e.target.value; + setColorInputs({ ...colorInputs, [`color0${section}`]: value }); + if (/^#([0-9A-F]{3}){1,2}$/i.test(value)) { + changeHandler(value); + } }; + const showPicker = (id: number) => { if (id === selectedColorSection) { setColorSection(null); @@ -33,59 +45,46 @@ function Colorpicker({ colors, setColors, setTouched, isPalleteTouched }: Colorp return ( <> -
-
-
showPicker(1)} - className="cursor-pointer w-full rounded-tl-[0.8rem] rounded-bl-[0.8rem]" - style={{ - backgroundColor: (typeof colors.color01 === "string" && colors.color01) || "", - }} - >
-
showPicker(2)} - className="cursor-pointer w-full " - style={{ - backgroundColor: (typeof colors.color02 === "string" && colors.color02) || "", - }} - >
-
showPicker(3)} - className="cursor-pointer w-full" - style={{ - backgroundColor: (typeof colors.color03 === "string" && colors.color03) || "", - }} - >
-
showPicker(4)} - className="cursor-pointer w-full rounded-tr-[0.8rem] rounded-br-[0.8rem]" - style={{ - backgroundColor: (typeof colors.color04 === "string" && colors.color04) || "", - }} - >
+
+
+
+ {['color01', 'color02', 'color03', 'color04'].map((color, index) => ( +
showPicker(index + 1)} + onKeyUp={(e) => { if (e.key === 'Enter') showPicker(index + 1); }} + className={`cursor-pointer w-full ${index === 0 ? 'rounded-tl-[0.8rem] rounded-bl-[0.8rem]' : ''} ${index === 3 ? 'rounded-tr-[0.8rem] rounded-br-[0.8rem]' : ''}`} + style={{ + backgroundColor: colors[color], + }} + >
+ ))} +
+
+ {['color01', 'color02', 'color03', 'color04'].map((color, index) => ( + handleInputChange(e, index + 1)} + placeholder="#000000" + className="w-20 text-center mx-2.5" + /> + ))} +
{selectedColorSection && ( - <> - {/*
*/} -
- -
- +
+ +
)}
- {/*
+ {/*
-
-
-