import { useCallback, useState } from "react"; import { Popover } from "@mantine/core"; import { Choice, SelectTypeOptions } from "@/ee/base/types/base.types"; import { choiceColor } from "@/ee/base/components/cells/choice-color"; import { ChoicePicker } from "@/ee/base/components/cells/choice-picker"; import { FieldProps, FieldShell } from "./detail-field"; import classes from "@/ee/base/styles/row-detail-modal.module.css"; import cellClasses from "@/ee/base/styles/cells.module.css"; export function FieldChoice({ property, value, readOnly, onChange }: FieldProps) { const [opened, setOpened] = useState(false); const multiple = property.type === "multiSelect"; const choices = (property.typeOptions as SelectTypeOptions | undefined)?.choices ?? []; const selectedIds = multiple ? Array.isArray(value) ? (value as string[]) : [] : typeof value === "string" ? [value] : []; const selectedChoices = choices.filter((c) => selectedIds.includes(c.id)); const handleToggle = useCallback( (choice: Choice) => { if (multiple) { const next = selectedIds.includes(choice.id) ? selectedIds.filter((id) => id !== choice.id) : [...selectedIds, choice.id]; onChange(next.length > 0 ? next : null); } else { onChange(choice.id === selectedIds[0] ? null : choice.id); setOpened(false); } }, [multiple, selectedIds, onChange], ); const chips = selectedChoices.map((choice) => ( {choice.name} )); if (readOnly) { return (
{chips}
); } return ( setOpened((o) => !o)} onKeyDown={(e) => { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); setOpened((o) => !o); } }} >
{chips}
{opened && ( setOpened(false)} /> )}
); }