@hitachivantara/uikit-react-core
Version:
Core React components for the NEXT Design System.
67 lines (66 loc) • 2.22 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
const Context = require("../../Context.cjs");
const index = require("../../utils/index.cjs");
const Dropdown = require("../../../Dropdown/Dropdown.cjs");
const Attribute = ({
id,
attribute,
disabled,
isInvalid
}) => {
const { dispatchAction, attributes, operators, labels, readOnly } = Context.useQueryBuilderContext();
const values = React.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__ */ jsxRuntime.jsx(
Dropdown.HvDropdown,
{
singleSelectionToggle: false,
label: labels.rule.attribute.label,
placeholder: labels.rule.attribute.placeholder,
values,
disabled,
readOnly,
status: isInvalid ? "invalid" : "valid",
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: index.isBigList(values),
...index.isBigList(values) && { virtualized: true, height: 300 }
}
);
};
React.memo(Attribute);
exports.Attribute = Attribute;