feat: add valueChecked prop to Field and TypeTag components for improved tag management

This commit is contained in:
faisolavolut 2025-04-10 11:36:28 +07:00
parent f7a79188e0
commit 637556e335
2 changed files with 88 additions and 76 deletions

View File

@ -74,6 +74,7 @@ export interface FieldProps {
isDebounce?: boolean; isDebounce?: boolean;
data?: any; data?: any;
mode?: string; mode?: string;
valueChecked?: string[];
} }
export const Field: React.FC<FieldProps> = ({ export const Field: React.FC<FieldProps> = ({
fm, fm,
@ -109,6 +110,7 @@ export const Field: React.FC<FieldProps> = ({
description, description,
styleField, styleField,
isDebounce = false, isDebounce = false,
valueChecked,
mode, mode,
}) => { }) => {
let result = null; let result = null;
@ -469,6 +471,7 @@ export const Field: React.FC<FieldProps> = ({
fm={fm} fm={fm}
fields={initField} fields={initField}
name={name} name={name}
valueChecked={valueChecked}
disabled={is_disable} disabled={is_disable}
className={className} className={className}
onChange={onChange} onChange={onChange}

View File

@ -2,7 +2,7 @@ import { cva } from "class-variance-authority";
import { useRef, useState, useEffect } from "react"; import { useRef, useState, useEffect } from "react";
import { Checkbox } from "../../ui/checkbox"; import { Checkbox } from "../../ui/checkbox";
import { X } from "lucide-react"; import { X } from "lucide-react";
import { IoIosRadioButtonOff } from "react-icons/io"; import { IoIosRadioButtonOff, IoIosRadioButtonOn } from "react-icons/io";
export const TypeTag: React.FC<any> = ({ export const TypeTag: React.FC<any> = ({
name, name,
@ -15,6 +15,7 @@ export const TypeTag: React.FC<any> = ({
onChange, onChange,
styleField, styleField,
mode, mode,
valueChecked,
}) => { }) => {
const [tags, setTags] = useState<string[]>(fm.data?.[name] || []); const [tags, setTags] = useState<string[]>(fm.data?.[name] || []);
const [inputValue, setInputValue] = useState(""); const [inputValue, setInputValue] = useState("");
@ -98,7 +99,11 @@ export const TypeTag: React.FC<any> = ({
disabled && !tags?.length ? "h-9" : "" disabled && !tags?.length ? "h-9" : ""
)} )}
> >
{tags.map((tag, index) => ( {tags.map((tag, index) => {
const checked = valueChecked?.length
? isChecked(tag, valueChecked)
: false;
return (
<div <div
key={index} key={index}
className={cx( className={cx(
@ -114,14 +119,12 @@ export const TypeTag: React.FC<any> = ({
<> <>
<Checkbox <Checkbox
className="border border-primary" className="border border-primary"
checked={false} checked={checked}
onClick={(e) => {}} onClick={(e) => {}}
/>{" "} />{" "}
</> </>
) : styleField === "radio" ? ( ) : styleField === "radio" ? (
<> <>{checked ? <IoIosRadioButtonOn /> : <IoIosRadioButtonOff />}</>
<IoIosRadioButtonOff />{" "}
</>
) : styleField === "order" ? ( ) : styleField === "order" ? (
<> <>
{index + 1} {index + 1}
@ -174,7 +177,8 @@ export const TypeTag: React.FC<any> = ({
</button> </button>
)} )}
</div> </div>
))} );
})}
{!disabled && ( {!disabled && (
<input <input
type="text" type="text"
@ -188,3 +192,8 @@ export const TypeTag: React.FC<any> = ({
</div> </div>
); );
}; };
const isChecked = (value: string, valueChecked: string[]) => {
const findCheck = valueChecked?.find((item) => item === value);
return findCheck ? true : false;
};