diff --git a/components/form/Field.tsx b/components/form/Field.tsx index b290d5a..1d6e041 100644 --- a/components/form/Field.tsx +++ b/components/form/Field.tsx @@ -74,6 +74,7 @@ export interface FieldProps { isDebounce?: boolean; data?: any; mode?: string; + valueChecked?: string[]; } export const Field: React.FC = ({ fm, @@ -109,6 +110,7 @@ export const Field: React.FC = ({ description, styleField, isDebounce = false, + valueChecked, mode, }) => { let result = null; @@ -469,6 +471,7 @@ export const Field: React.FC = ({ fm={fm} fields={initField} name={name} + valueChecked={valueChecked} disabled={is_disable} className={className} onChange={onChange} diff --git a/components/form/field/TypeTag.tsx b/components/form/field/TypeTag.tsx index 904ea6d..946d910 100644 --- a/components/form/field/TypeTag.tsx +++ b/components/form/field/TypeTag.tsx @@ -2,7 +2,7 @@ import { cva } from "class-variance-authority"; import { useRef, useState, useEffect } from "react"; import { Checkbox } from "../../ui/checkbox"; import { X } from "lucide-react"; -import { IoIosRadioButtonOff } from "react-icons/io"; +import { IoIosRadioButtonOff, IoIosRadioButtonOn } from "react-icons/io"; export const TypeTag: React.FC = ({ name, @@ -15,6 +15,7 @@ export const TypeTag: React.FC = ({ onChange, styleField, mode, + valueChecked, }) => { const [tags, setTags] = useState(fm.data?.[name] || []); const [inputValue, setInputValue] = useState(""); @@ -98,83 +99,86 @@ export const TypeTag: React.FC = ({ disabled && !tags?.length ? "h-9" : "" )} > - {tags.map((tag, index) => ( -
- {styleField === "checkbox" ? ( - <> - {}} - />{" "} - - ) : styleField === "radio" ? ( - <> - {" "} - - ) : styleField === "order" ? ( - <> - {index + 1} - {". "} - - ) : ( - <> - )} - {disabled ? ( -
{tag}
- ) : ( -
{ - if (el) tagRefs.current[index] = el; - }} - className={cx( - "px-3 py-1 pr-0 flex-grow focus:shadow-none focus:ring-0 focus:border-none focus:outline-none", - editingIndex !== index && "cursor-pointer" - )} - contentEditable={editingIndex === index} - suppressContentEditableWarning - onBlur={() => handleSaveEdit(index)} - onKeyDown={(e) => { - if (disabled) return; - if (e.key === "Enter") { - e.preventDefault(); - e.stopPropagation(); - handleSaveEdit(index); + {tags.map((tag, index) => { + const checked = valueChecked?.length + ? isChecked(tag, valueChecked) + : false; + return ( +
+ {styleField === "checkbox" ? ( + <> + {}} + />{" "} + + ) : styleField === "radio" ? ( + <>{checked ? : } + ) : styleField === "order" ? ( + <> + {index + 1} + {". "} + + ) : ( + <> + )} + {disabled ? ( +
{tag}
+ ) : ( +
{ + if (el) tagRefs.current[index] = el; + }} + className={cx( + "px-3 py-1 pr-0 flex-grow focus:shadow-none focus:ring-0 focus:border-none focus:outline-none", + editingIndex !== index && "cursor-pointer" + )} + contentEditable={editingIndex === index} + suppressContentEditableWarning + onBlur={() => handleSaveEdit(index)} + onKeyDown={(e) => { + if (disabled) return; + if (e.key === "Enter") { + e.preventDefault(); + e.stopPropagation(); + handleSaveEdit(index); + } + if (e.key === "Escape") { + setEditingIndex(null); + } + }} + onClick={() => handleFocusTag(index)} + onInput={(e) => + setTempValue((e.target as HTMLDivElement).innerText) } - if (e.key === "Escape") { - setEditingIndex(null); - } - }} - onClick={() => handleFocusTag(index)} - onInput={(e) => - setTempValue((e.target as HTMLDivElement).innerText) - } - > - {tag} -
- )} + > + {tag} +
+ )} - {!disabled && ( - - )} -
- ))} + {!disabled && ( + + )} +
+ ); + })} {!disabled && ( = ({ ); }; + +const isChecked = (value: string, valueChecked: string[]) => { + const findCheck = valueChecked?.find((item) => item === value); + return findCheck ? true : false; +};