Skip to content

Commit

Permalink
Adding operator group headers
Browse files Browse the repository at this point in the history
  • Loading branch information
m-rgba committed Dec 23, 2024
1 parent 1b77d8f commit fad7bc2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {GridFilterItem} from '@mui/x-data-grid-pro';
import React, {useMemo} from 'react';

import {Button} from '../../../../Button';
import {FilterId, getFieldType, getOperatorOptions, isWeaveRef} from './common';
import {FilterId, getFieldType, getGroupedOperatorOptions, isWeaveRef} from './common';
import {SelectField, SelectFieldOption} from './SelectField';
import {SelectOperator} from './SelectOperator';
import {SelectValue} from './SelectValue';
Expand Down Expand Up @@ -36,7 +36,7 @@ export const FilterRow = ({
};

const operatorOptions = useMemo(
() => getOperatorOptions(item.field),
() => getGroupedOperatorOptions(item.field),
[item.field]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import {Select} from '@wandb/weave/components/Form/Select';
import React from 'react';

import {Tooltip} from '../../../../Tooltip';
import {SelectOperatorOption} from './common';
import {OperatorGroupedOption, SelectOperatorOption} from './common';
import {components, GroupHeadingProps} from 'react-select';

type SelectOperatorProps = {
options: SelectOperatorOption[];
options: OperatorGroupedOption[];
value: string;
onSelectOperator: (value: string) => void;
isDisabled?: boolean;
Expand All @@ -24,13 +25,19 @@ const OptionLabel = (props: SelectOperatorOption) => {
);
};

const GroupHeading = (props: GroupHeadingProps<SelectOperatorOption, false, OperatorGroupedOption>) => {
return <components.GroupHeading {...props} />;
};

export const SelectOperator = ({
options,
value,
onSelectOperator,
isDisabled,
}: SelectOperatorProps) => {
const selectedOption = options.find(o => o.value === value) ?? options[0];
// Find the operator from the grouped selection:
const flattenedOptions = options.flatMap(group => group.options);
const selectedOption = flattenedOptions.find(o => o.value === value) ?? flattenedOptions[0];

const onReactSelectChange = (option: SelectOperatorOption | null) => {
if (option) {
Expand All @@ -39,13 +46,14 @@ export const SelectOperator = ({
};

return (
<Select<SelectOperatorOption>
<Select<SelectOperatorOption, false, OperatorGroupedOption>
options={options}
value={selectedOption}
placeholder="Select operator"
onChange={onReactSelectChange}
formatOptionLabel={OptionLabel}
isDisabled={isDisabled}
components={{GroupHeading}}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,55 @@ export const upsertFilter = (
items,
};
};

export type OperatorGroupedOption = {
label: string;
options: SelectOperatorOption[];
};

/**
* Return grouped operators by type (string, number, bool, etc.).
* Customize the group labels & operators as needed.
*/
export function getGroupedOperatorOptions(field: string): OperatorGroupedOption[] {
const stringOperators: SelectOperatorOption[] = [
{value: '(string): contains', label: 'contains'},
{value: '(string): equals', label: 'equals'},
{value: '(string): in', label: 'in'},
];
const numberOperators: SelectOperatorOption[] = [
{value: '(number): =', label: '='},
{value: '(number): !=', label: '≠'},
{value: '(number): <', label: '<'},
{value: '(number): <=', label: '≤'},
{value: '(number): >', label: '>'},
{value: '(number): >=', label: '≥'},
];
const dateOperators: SelectOperatorOption[] = [
{value: '(date): after', label: 'after'},
{value: '(date): before', label: 'before'},
];
const boolOperators: SelectOperatorOption[] = [
{value: '(bool): is', label: 'is'},
];
const anyOperators: SelectOperatorOption[] = [
{value: '(any): isEmpty', label: 'is empty'},
{value: '(any): isNotEmpty', label: 'is not empty'},
];

// Select which groups/ops to show based on the field type
const fieldType = getFieldType(field);
if (fieldType === 'datetime') {
return [{label: 'Date Operators', options: dateOperators}];
} else if (fieldType === 'user') {
return [{label: 'String Operators', options: stringOperators}];
}
// Fallback: show all grouped
return [
{label: 'String', options: stringOperators},
{label: 'Number', options: numberOperators},
{label: 'Boolean', options: boolOperators},
{label: 'Date', options: dateOperators},
{label: 'Other', options: anyOperators},
];
}

0 comments on commit fad7bc2

Please sign in to comment.