@adaptabletools/adaptable
Version:
Powerful data-agnostic HTML5 AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
89 lines (88 loc) • 4.95 kB
JavaScript
import * as React from 'react';
import { Box, Flex, Text } from 'rebass';
import HelpBlock from '../../../../components/HelpBlock';
import { useAdaptable } from '../../../AdaptableContext';
import { ButtonInfo } from '../../Buttons/ButtonInfo';
import { PredicateDocsLink } from '../../../../Utilities/Constants/DocumentationLinkConstants';
import SimpleButton from '../../../../components/SimpleButton';
import { Tag } from '../../../../components/Tag';
import { EntityRulePredicateEditor } from './EntityRulePredicateEditor';
import Radio from '../../../../components/Radio';
import { getScopeViewItems } from '../../../../Utilities/getScopeViewItems';
export const EntityRulePredicatesEditor = (props) => {
const { api } = useAdaptable();
const showDocumentationLinks = api.internalApi.isDocumentationLinksDisplayed();
const columnPredicateEnabled = props.data.Rule.Predicates.some((predicate) => predicate.ColumnId !== undefined);
// filter out predicates that are already in use
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 (React.createElement(React.Fragment, null,
React.createElement(Flex, { mb: 2, justifyContent: "space-between" },
React.createElement(Text, { fontSize: 2, mb: 2 }, props.descriptions.selectPredicate),
React.createElement(SimpleButton, { disabled: !props.data.Rule?.Predicates?.length, onClick: handleAddNewPredicate, icon: "plus", variant: "raised" }, "Add Predicate")),
enablePredicateColumnId && (React.createElement(Flex, { className: "ab-EntityRulePredicateEditor-ScopeTypeSelector", p: 2, mb: 2, flexDirection: "column" },
React.createElement(Text, { mb: 2 }, "Create Predicate Rule(s) using"),
React.createElement(Radio, { "data-name": "entity-scope", onClick: () => handlePredicateScopeTypeChange(false), mr: 3, checked: !columnPredicateEnabled },
"Active Column:",
' ',
React.createElement(Tag, { ml: 1, mr: 1 }, scopeString || 'Not Defined'),
' '),
React.createElement(Radio, { "data-name": "column-scope", onClick: () => handlePredicateScopeTypeChange(true), checked: columnPredicateEnabled }, "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 (React.createElement(React.Fragment, { key: `${index}-${predicate?.PredicateId}` },
index > 0 && (React.createElement(Box, null,
React.createElement(Tag, { mb: 3 }, "AND"))),
React.createElement(EntityRulePredicateEditor, { data: props.data, predicate: predicate, onChange: props.onChange, predicateDefs: editorPredicateDefs, getPredicateDefsForColId: props.getPredicateDefsForColId, columnPredicateEnabled: columnPredicateEnabled })));
}),
showDocumentationLinks && (React.createElement(HelpBlock, { "data-name": "query-documentation", mt: 3, mb: 2, style: {
fontSize: 'var(--ab-font-size-3)',
padding: 0,
} },
React.createElement(ButtonInfo, { mr: 2, onClick: () => window.open(PredicateDocsLink, '_blank') }),
"See Predicate documentation for more details and examples"))));
};