diff --git a/components/form/Field.tsx b/components/form/Field.tsx index f40ea98..82cb548 100644 --- a/components/form/Field.tsx +++ b/components/form/Field.tsx @@ -70,6 +70,7 @@ export interface FieldProps { autoRefresh?: boolean; forceDisabled?: boolean; description?: string | (() => any); + styleField?: string | null; } export const Field: React.FC = ({ fm, @@ -103,6 +104,7 @@ export const Field: React.FC = ({ autoRefresh = false, forceDisabled, description, + styleField, }) => { let result = null; const field = useLocal({ @@ -458,6 +460,7 @@ export const Field: React.FC = ({ ) : ["tag"].includes(type) ? ( <> = ({ name, @@ -9,6 +13,7 @@ export const TypeTag: React.FC = ({ type, field, onChange, + styleField, }) => { const [tags, setTags] = useState(fm.data?.[name] || []); const [inputValue, setInputValue] = useState(""); @@ -16,8 +21,15 @@ export const TypeTag: React.FC = ({ const [tempValue, setTempValue] = useState(""); // Nilai sementara untuk pengeditan const tagRefs = useRef<(HTMLDivElement | null)[]>([]); const val = fm?.data?.[name]; + + useEffect(() => { + if (editingIndex !== null && tagRefs.current[editingIndex]) { + tagRefs.current[editingIndex]?.focus(); + } + }, [editingIndex]); + const handleSaveEdit = (index: number) => { - if (!disabled) return; + if (disabled) return; const updatedTags = [...tags]; updatedTags[index] = tempValue.trim(); // Update nilai tag setTags(updatedTags); @@ -30,6 +42,7 @@ export const TypeTag: React.FC = ({ onChange(tags); } }; + const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter") { e.preventDefault(); @@ -50,44 +63,88 @@ export const TypeTag: React.FC = ({ setTags(tags.slice(0, -1)); } }; + const handleFocusTag = (index: number) => { if (disabled) return; setEditingIndex(index); // Masuk ke mode edit setTempValue(tags[index]); // Isi nilai sementara dengan nilai tag - setTimeout(() => { - tagRefs.current[index]?.focus(); // Fokus pada elemen yang diedit - }, 0); }; + const removeTag = (index: number) => { if (disabled) return; setTags(tags.filter((_, i) => i !== index)); }; + const buttonVariants = cva( + "flex flex-row items-center rounded-full text-sm p-1", + { + variants: { + variant: { + default: "bg-blue-100 text-blue-800 m-1", + moe: "", + }, + }, + } + ); + const stylingGroup = ["checkbox", "radio", "order"]; return (
{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" + editingIndex !== index && "cursor-pointer" )} contentEditable={editingIndex === index} suppressContentEditableWarning onBlur={() => handleSaveEdit(index)} onKeyDown={(e) => { - if (!disabled) return; + if (disabled) return; if (e.key === "Enter") { e.preventDefault(); e.stopPropagation(); @@ -97,9 +154,7 @@ export const TypeTag: React.FC = ({ setEditingIndex(null); } }} - onClick={() => { - handleFocusTag(index); - }} + onClick={() => handleFocusTag(index)} onInput={(e) => setTempValue((e.target as HTMLDivElement).innerText) } @@ -112,9 +167,9 @@ export const TypeTag: React.FC = ({ )}