UNPKG

@adaptabletools/adaptable

Version:

Powerful data-agnostic HTML5 AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements

147 lines (146 loc) 7.9 kB
import * as React from 'react'; import { Box, Flex } from 'rebass'; import SimpleButton from '../../../components/SimpleButton'; import { ColumnFilterInputList } from './components/ColumnFilterInputList'; import { usePredicateDef } from './hooks'; import Radio from '../../../components/Radio'; import { useAdaptable } from '../../AdaptableContext'; import { Select } from '../../../components/Select'; const ColumnFilterPredicateDropdown = (props) => { const predicateDef = usePredicateDef(props.predicate?.operator, props.predicateDefs); const options = props.predicateDefs.map((predicateDef) => { return { label: (React.createElement(Box, { display: 'flex', alignItems: "center" }, React.createElement(SimpleButton, { variant: "raised", mr: 2, tone: "accent" }, predicateDef.icon), predicateDef.label)), value: predicateDef.operator, }; }); const operator = props.predicate?.operator; const isAndOr = operator === 'AND' || operator === 'OR'; return (React.createElement(Box, { display: 'flex', alignItems: "center", style: { //@ts-ignore ignore '--ab-cmp-input__background': 'var(--ab-color-primary)', } }, React.createElement(Select, { style: predicateDef ? { minWidth: '10rem', } : null // for AND and OR don't apply a min width , onChange: (value) => { props.onPredicateChange({ operator: value, args: [], }); }, options: options, value: isAndOr ? 'Add Condition' : props.predicate?.operator }))); }; const ColumnFilterEditor = (props) => { return (React.createElement(Box, { mb: 3 }, React.createElement(Flex, { mb: 2 }, React.createElement(ColumnFilterPredicateDropdown, { columnId: props.columnId, disabled: props.disabled, predicate: props.predicate, predicateDefs: props.predicateDefs, onPredicateChange: props.onPredicateChange }), React.createElement(Flex, { alignItems: "center", ml: 2 }, React.createElement(SimpleButton, { disabled: props.deleteDisabled, onClick: props.onDelete, variant: "text", icon: "delete" }))), React.createElement(ColumnFilterInputList, { columnId: props.columnId, disabled: props.disabled, predicate: props.predicate, onPredicateChange: props.onPredicateChange, predicateDefs: props.predicateDefs }))); }; const AndOrInput = (props) => { return ( // <Select // value={props.operator} // onChange={props.onChange} // options={[ // { label: 'AND', value: 'AND' }, // { label: 'OR', value: 'OR' }, // ]} // /> (React.createElement( Flex, { flex: 1 }, React.createElement(Radio, { mr: 2, onClick: () => props.onChange('AND'), checked: props.operator === 'AND' }, "AND"), React.createElement(Radio, { mr: 2, onClick: () => props.onChange('OR'), checked: props.operator === 'OR' }, "OR") )) ); }; export const ColumnFilterComponent = (props) => { const adaptable = useAdaptable(); const autoApplyColumnFilter = adaptable.adaptableOptions.filterOptions.columnFilterOptions?.autoApplyColumnFilter ?? true; const [predicateNotYetApplied, setPredicateNotYetApplied] = React.useState(props.predicate); const applyFilter = () => { props.onPredicateChange(currentPredicateRef.current); setPredicateNotYetApplied(undefined); }; const onPredicateChange = (predicate) => { // even if autoApplyColumnFilter is false, when we explicitly clear the filter // we want to apply the filter immediately if (autoApplyColumnFilter || !predicate) { props.onPredicateChange(predicate); } else { setPredicateNotYetApplied(predicate); } }; const onNewPredicate = (predicateDef) => { const currentPredicate = currentPredicateRef.current; const newPredicate = { ...currentPredicate, // @ts-ignore args: [...currentPredicate.args, { operator: predicateDef.operator, args: [] }], }; onPredicateChange(newPredicate); }; const onCombineChange = (operator) => { const newPredicate = { ...currentPredicateRef.current, args: currentPredicateRef.current.args || [], operator, }; onPredicateChange(newPredicate); }; const currentPredicate = autoApplyColumnFilter ? props.predicate : predicateNotYetApplied ?? props.predicate; const currentPredicateRef = React.useRef(currentPredicate); //#ref-predicate currentPredicateRef.current = currentPredicate; // Not sure how to check this but would be nice... const isLastPredicateValid = true; const filterPredicateDropdown = (React.createElement(ColumnFilterPredicateDropdown, { columnId: props.columnId, disabled: props.disabled, predicate: currentPredicate, predicateDefs: props.predicateDefs, onPredicateChange: (predicate) => { const predicateDef = props.predicateDefs.find((predicateDef) => predicateDef.operator === predicate.operator); onNewPredicate(predicateDef); }, targetProps: { tone: 'neutral', variant: 'raised', children: 'Add Condition', } })); return (React.createElement(React.Fragment, null, React.createElement(Flex, { m: 2 }, React.createElement(AndOrInput, { onChange: onCombineChange, operator: currentPredicate.operator }), React.createElement(SimpleButton, { ml: 2, onClick: () => onPredicateChange(null) }, "Clear All")), React.createElement(Flex, { flexDirection: "column", className: "ab-ColumnFilter", flex: 1, minHeight: 0, ...props.wrapperProps }, React.createElement(Box, { flex: 1, style: { overflow: 'auto' } }, currentPredicate.args.map((predicate, index) => { return (React.createElement(ColumnFilterEditor, { deleteDisabled: currentPredicate.args.length < 2, onDelete: () => { const currentPredicate = currentPredicateRef.current; const newArgs = [...currentPredicate.args]; newArgs.splice(index, 1); onPredicateChange({ operator: currentPredicate.operator, args: newArgs, }); }, key: index, columnId: props.columnId, disabled: props.disabled, predicate: predicate, predicateDefs: props.predicateDefs, onPredicateChange: (predicate) => { // #ref-predicate - for whatever reason // the `currentPredicate` here would be one from an old render // if we weren't using a ref. we're not using memo/usecallback here, // so I don't understand why this is happening const currentPredicate = currentPredicateRef.current; const newArgs = [...currentPredicate.args]; newArgs[index] = predicate; onPredicateChange({ operator: currentPredicate.operator, args: newArgs, }); } })); }), isLastPredicateValid && filterPredicateDropdown), !autoApplyColumnFilter ? (React.createElement(Box, { pt: 2 }, React.createElement(SimpleButton, { tone: "accent", variant: "raised", onClick: applyFilter }, "Apply Filter"))) : null))); };