Skip to content

Commit 2693275

Browse files
committed
feat(filter): enhancements over FilterRowProps
1 parent 74c1cc4 commit 2693275

File tree

10 files changed

+197
-189
lines changed

10 files changed

+197
-189
lines changed

src/bindings.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { SelectOption } from './select-option';
2+
3+
export enum Binding {
4+
AND = 'AND',
5+
OR = 'OR',
6+
}
7+
8+
export const defaultBindingOptions: SelectOption<Binding>[] = [
9+
{
10+
value: Binding.AND,
11+
label: 'And',
12+
},
13+
{
14+
value: Binding.OR,
15+
label: 'Or',
16+
},
17+
];
Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,8 @@
11
import React from 'react';
22
import { Meta, Story } from '@storybook/react';
3-
import { FilterRowProps, PropertyDescription } from '../../';
3+
import { FilterRowProps } from '../../';
44
import { FilterRow } from './FilterRow';
55

6-
const properties: PropertyDescription[] = [
7-
{
8-
label: 'Name',
9-
key: 'name',
10-
type: 'string',
11-
suggestions: ['Artemis', 'Apollo', 'Donna', 'Dhio'],
12-
},
13-
{
14-
label: 'Age',
15-
key: 'age',
16-
type: 'number',
17-
suggestions: [1, 2, 3],
18-
},
19-
{
20-
label: 'Has Owner',
21-
key: 'has_owner',
22-
type: 'boolean',
23-
},
24-
];
25-
266
const meta: Meta<FilterRowProps> = {
277
title: 'Chakra UI/Filter Row',
288
component: FilterRow,
@@ -31,8 +11,11 @@ const meta: Meta<FilterRowProps> = {
3111
},
3212
args: {
3313
filter: {},
34-
isFirst: true,
35-
properties,
14+
fields: [],
15+
operations: [],
16+
bindings: [],
17+
shouldRenderBindingSelect: false,
18+
shouldRenderValueInput: true,
3619
},
3720
};
3821

@@ -44,22 +27,6 @@ const Template: Story<FilterRowProps> = args => {
4427

4528
// By passing using the Args format for exported stories, you can control the props for a component for reuse in a test
4629
// https://storybook.js.org/docs/react/workflows/unit-testing
47-
export const FirstRow = Template.bind({});
48-
49-
FirstRow.args = {};
50-
51-
export const OtherRow = Template.bind({});
30+
export const Default = Template.bind({});
5231

53-
OtherRow.args = {
54-
isFirst: false,
55-
};
56-
57-
export const FilterWithoutValue = Template.bind({});
58-
59-
FilterWithoutValue.args = {
60-
isFirst: false,
61-
filter: {
62-
operation: 'is-not-empty',
63-
field: 'name',
64-
},
65-
};
32+
Default.args = {};

src/components/chakra-ui/FilterRow.tsx

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,46 @@ import {
77
Select,
88
Tooltip,
99
} from '@chakra-ui/react';
10-
import { useRowUtilities, FilterRowProps } from '../../';
10+
import { FilterRowProps } from '../../';
1111

1212
export const FilterRow: FC<FilterRowProps> = ({
13-
properties,
1413
filter,
15-
isFirst,
14+
fields,
15+
bindings,
16+
operations,
17+
shouldRenderBindingSelect,
18+
shouldRenderValueInput,
1619
onRemove,
1720
onChangeBinding,
1821
onChangeField,
1922
onChangeOperation,
2023
onChangeValue,
2124
}) => {
22-
const {
23-
getFilterOperationsForType,
24-
shouldRenderValueInputForOperation,
25-
} = useRowUtilities();
26-
2725
return (
2826
<HStack>
2927
<Tooltip shouldWrapChildren label="Remove Filter" placement="left">
3028
<CloseButton onClick={onRemove} />
3129
</Tooltip>
3230

33-
{isFirst ? (
34-
<Text fontSize="sm">Where&nbsp;</Text>
35-
) : (
31+
{shouldRenderBindingSelect ? (
3632
<Select
3733
size="sm"
3834
maxWidth="6rem"
3935
value={filter.binding}
4036
onChange={onChangeBinding}
4137
>
42-
<option value="and">And</option>
43-
<option value="or">Or</option>
38+
{bindings.map(binding => (
39+
<option
40+
value={binding.value}
41+
key={binding.value}
42+
selected={filter.binding === binding.value}
43+
>
44+
{binding.label}
45+
</option>
46+
))}
4447
</Select>
48+
) : (
49+
<Text fontSize="sm">Where&nbsp;</Text>
4550
)}
4651

4752
<Select
@@ -50,9 +55,13 @@ export const FilterRow: FC<FilterRowProps> = ({
5055
onChange={onChangeField}
5156
placeholder="Field"
5257
>
53-
{properties.map((prop, index) => (
54-
<option value={prop.key} key={index}>
55-
{prop.label}
58+
{fields.map(field => (
59+
<option
60+
value={field.value}
61+
key={field.value}
62+
selected={filter.field === field.value}
63+
>
64+
{field.label}
5665
</option>
5766
))}
5867
</Select>
@@ -63,14 +72,18 @@ export const FilterRow: FC<FilterRowProps> = ({
6372
onChange={onChangeOperation}
6473
placeholder="Operation"
6574
>
66-
{getFilterOperationsForType(filter.type).map((operation, index) => (
67-
<option value={operation.value} key={index}>
75+
{operations.map(operation => (
76+
<option
77+
value={operation.value}
78+
key={operation.value}
79+
selected={filter.operation === operation.value}
80+
>
6881
{operation.label}
6982
</option>
7083
))}
7184
</Select>
7285

73-
{shouldRenderValueInputForOperation(filter.operation) && (
86+
{shouldRenderValueInput && (
7487
<Input
7588
size="sm"
7689
placeholder="Value"

src/components/chakra-ui/FilterSelection.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ interface Props {
1010
}
1111

1212
export const FilterSelection: FC<Props> = ({ properties }) => {
13-
const { filters, onAddFilter, createFilterRowProps } = useQueryFilters(
14-
properties
15-
);
13+
const { filters, onAddFilter, createFilterRowProps } = useQueryFilters({
14+
properties,
15+
});
1616

1717
return (
1818
<SimpleGrid columns={1} spacingY={4}>

src/filterOperations.ts

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)