@adaptabletools/adaptable
Version:
Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
68 lines (67 loc) • 4.69 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import * as React from 'react';
import HelpBlock from '../../../../components/HelpBlock';
import { useAdaptable } from '../../../AdaptableContext';
import { ButtonInfo } from '../../Buttons/ButtonInfo';
import { PredicateDocsLink } from '../../../../Utilities/Constants/DocumentationLinkConstants';
import { ButtonNew } from '../../Buttons/ButtonNew';
import { Tag } from '../../../../components/Tag';
import { EntityRulePredicateEditor } from './EntityRulePredicateEditor';
import Radio from '../../../../components/Radio';
import { getScopeViewItems } from '../../../../Utilities/getScopeViewItems';
import { Box, Flex } from '../../../../components/Flex';
export const EntityRulePredicatesEditor = (props) => {
const { api } = useAdaptable();
const showDocumentationLinks = api.internalApi.isDocumentationLinksDisplayed();
const columnPredicateEnabled = props.data.Rule.Predicates.some((predicate) => predicate.ColumnId !== undefined);
const defaultPredicateDefs = props.predicateDefs.filter((predicateDef) => {
return props.data.Rule?.Predicates?.every((rulePrediate) => rulePrediate.PredicateId !== predicateDef.id);
});
const handlePredicateScopeTypeChange = (columnPredicateEnabled) => {
if (columnPredicateEnabled) {
props.onChange({
...props.data,
Rule: {
Predicates: [
{
PredicateId: null,
ColumnId: '',
},
],
},
});
}
else {
props.onChange({
...props.data,
Rule: {
Predicates: [],
},
});
}
};
const handleAddNewPredicate = () => {
props.onChange({
...props.data,
Rule: {
Predicates: [
...(props.data.Rule?.Predicates || []),
{
PredicateId: defaultPredicateDefs[0].id,
Inputs: [],
},
],
},
});
};
const scopeString = getScopeViewItems(props.data.Scope, api).values.join(', ');
const enablePredicateColumnId = props.enablePredicateColumnId ?? true;
return (_jsxs(_Fragment, { children: [_jsxs(Flex, { justifyContent: "space-between", className: "twa:mb-2", children: [_jsx(Box, { className: "twa:text-2 twa:mb-2", children: props.descriptions.selectPredicate }), _jsx(ButtonNew, { disabled: !props.data.Rule?.Predicates?.length, onClick: handleAddNewPredicate, children: "Add Predicate" })] }), enablePredicateColumnId && (_jsxs(Flex, { className: "ab-EntityRulePredicateEditor-ScopeTypeSelector twa:p-2 twa:mb-2", flexDirection: "column", children: [_jsx(Box, { className: "twa:mb-2", children: "Create Predicate Rule(s) using" }), _jsxs(Radio, { "data-name": "entity-scope", onClick: () => handlePredicateScopeTypeChange(false), className: "twa:mr-3", checked: !columnPredicateEnabled, children: ["Active Column: ", _jsx(Tag, { className: "twa:mx-1", children: scopeString || 'Not Defined' }), ' '] }), _jsx(Radio, { "data-name": "column-scope", onClick: () => handlePredicateScopeTypeChange(true), checked: columnPredicateEnabled, children: "Selected Columns (overriding Active Column)" })] })), (props.data.Rule?.Predicates?.length ? props.data.Rule.Predicates : [null]).map((predicate, index) => {
const currentPredicatDef = props.predicateDefs.find((pd) => pd.id === predicate?.PredicateId);
let editorPredicateDefs = defaultPredicateDefs;
if (currentPredicatDef) {
editorPredicateDefs = [currentPredicatDef, ...defaultPredicateDefs];
}
return (_jsxs(React.Fragment, { children: [index > 0 && (_jsx(Box, { children: _jsx(Tag, { className: "twa:mb-3", children: "AND" }) })), _jsx(EntityRulePredicateEditor, { data: props.data, predicate: predicate, onChange: props.onChange, predicateDefs: editorPredicateDefs, getPredicateDefsForColId: props.getPredicateDefsForColId, columnPredicateEnabled: columnPredicateEnabled })] }, `${index}-${predicate?.PredicateId}`));
}), showDocumentationLinks && (_jsxs(HelpBlock, { "data-name": "query-documentation", className: "twa:mt-3 twa:mb-2 twa:p-0 twa:text-3", children: [_jsx(ButtonInfo, { className: "twa:mr-2", onClick: () => window.open(PredicateDocsLink, '_blank') }), "See Predicate documentation for more details and examples"] }))] }));
};