@adaptabletools/adaptable
Version:
Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
74 lines (73 loc) • 3.62 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import FormLayout, { FormRow } from '../../../../components/FormLayout';
import SimpleButton from '../../../../components/SimpleButton';
import { useAdaptable } from '../../../AdaptableContext';
import { PredicateEditor } from '../../PredicateEditor/PredicateEditor';
import { Box, Flex } from '../../../../components/Flex';
import { SingleSelect } from '../../../../components/NewSelect';
export const EntityRulePredicateEditor = (props) => {
const { api } = useAdaptable();
const { predicate } = props;
const allColumns = React.useMemo(() => {
return api.columnApi.getColumns().map((column) => ({
label: column.friendlyName,
value: column.columnId,
}));
}, []);
const handleClear = () => {
const newPredicates = props.data.Rule?.Predicates?.filter((prevPredicate) => prevPredicate !== predicate);
props.onChange({
...props.data,
Rule: {
Predicates: newPredicates,
},
});
};
const handlePredicateChange = (newPredicateDef) => {
let newPredicates = [];
if (predicate) {
newPredicates = props.data.Rule?.Predicates?.map((prevPredicate) => {
if (prevPredicate === predicate) {
return newPredicateDef;
}
return prevPredicate;
});
}
else {
newPredicates = [newPredicateDef];
}
props.onChange({
...props.data,
Rule: {
Predicates: newPredicates,
},
});
};
const predicateDefs = React.useMemo(() => {
let preparedPredicates = [];
if (props.getPredicateDefsForColId && props?.predicate?.ColumnId) {
preparedPredicates = props.getPredicateDefsForColId(props.predicate.ColumnId);
}
else {
preparedPredicates = props.predicateDefs;
}
return preparedPredicates;
}, [props.predicate]);
const columnIdPredicateScopeEnabled = Boolean(props.columnPredicateEnabled);
const selectedColumn = allColumns.find((col) => col.value === predicate?.ColumnId);
const columnOptions = allColumns.map((col) => ({
label: col.label,
value: col.value,
}));
let predicateColumnId = null;
if (predicate?.ColumnId && predicate.ColumnId !== '') {
predicateColumnId = predicate.ColumnId;
}
else if ('ColumnIds' in props.data.Scope && props.data.Scope?.ColumnIds?.[0]) {
predicateColumnId = props.data.Scope?.ColumnIds?.[0];
}
return (_jsxs(Flex, { className: "ab-EntityRulePredicateEditor twa:p-3 twa:mb-3", children: [_jsx(Box, { className: "twa:flex-1", children: _jsxs(FormLayout, { children: [columnIdPredicateScopeEnabled && (_jsx(FormRow, { label: "Column", children: _jsx(SingleSelect, { className: "twa:w-full", placeholder: "Select Column", items: columnOptions, value: selectedColumn?.value, onValueChange: (value) => {
handlePredicateChange({ PredicateId: null, ColumnId: value, Inputs: [] });
} }) })), _jsx(FormRow, { label: "Predicate", children: _jsx(PredicateEditor, { columnId: predicateColumnId, predicateDefs: predicateDefs, predicate: predicate, onChange: handlePredicateChange, onClear: handleClear }) })] }) }), _jsx(Box, { children: _jsx(SimpleButton, { disabled: !predicate, className: "twa:ml-2 twa:h-input", onClick: handleClear, icon: "delete" }) })] }));
};