UNPKG

@hitachivantara/uikit-react-core

Version:
60 lines (59 loc) 1.89 kB
import { HvDropdown } from "../../Dropdown/Dropdown.js"; import { useQueryBuilderContext } from "../Context.js"; import { isBigList } from "../utils/index.js"; import { useMemo } from "react"; import { jsx } from "react/jsx-runtime"; //#region src/QueryBuilder/Rule/Attribute.tsx var Attribute = ({ id, attribute, disabled, isInvalid }) => { const { dispatchAction, attributes, operators, labels, readOnly } = useQueryBuilderContext(); const values = useMemo(() => { if (!attributes) return []; return Object.keys(attributes).map((key) => ({ id: key, label: attributes[key].label, selected: key === attribute })); }, [attributes, attribute]); const currentType = attribute != null && attributes ? attributes[attribute]?.type : null; return /* @__PURE__ */ jsx(HvDropdown, { singleSelectionToggle: false, label: labels.rule.attribute.label, placeholder: labels.rule.attribute.placeholder, values, disabled, readOnly, status: isInvalid ? "invalid" : "valid", style: { gridArea: "attribute" }, statusMessage: labels.rule.attribute.exists, onChange: (selected) => { if (selected) { const attributeId = selected.id; const type = attributes && attributeId && attributes[attributeId]?.type; const typeOperators = type ? operators[type] : void 0; let operator; if (currentType === type) operator = void 0; else if (typeOperators?.length === 1) operator = typeOperators[0].operator; else operator = null; const value = type === "boolean" ? true : void 0; dispatchAction({ type: "set-attribute", id, attribute: attributeId?.toString(), operator, value }); } else dispatchAction({ type: "set-attribute", id, attribute: null }); }, showSearch: isBigList(values), ...isBigList(values) && { virtualized: true, height: 300 } }); }; //#endregion export { Attribute };