Skip to content

Commit 498e1ab

Browse files
authored
feat: add initial value property
feat: add initial value property
2 parents be56582 + 4faa787 commit 498e1ab

File tree

10 files changed

+4666
-5920
lines changed

10 files changed

+4666
-5920
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ node_modules
55
dist
66
coverage
77
*.tgz
8+
storybook-static

.storybook/main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = {
66
addons: ['@storybook/addon-links', '@storybook/addon-essentials'],
77
typescript: {
88
check: true,
9+
reactDocgen: 'react-docgen'
910
},
1011
reactOptions: {
1112
fastRefresh: true,
@@ -23,4 +24,4 @@ module.exports = {
2324
},
2425
}
2526
},
26-
};
27+
};

.storybook/preview.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@ addParameters({
66
actions: {
77
argTypesRegex: '^on.*'
88
},
9-
})
9+
docs: {
10+
extractComponentDescription: (component, { notes }) => {
11+
if (notes) {
12+
return typeof notes === 'string' ? notes : notes.markdown || notes.text;
13+
}
14+
return null;
15+
},
16+
},
17+
});
1018

1119
addDecorator((Story) => (
1220
<ChakraProvider resetCSS>
1321
<Story />
1422
</ChakraProvider>
15-
));
23+
));

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,10 @@
6464
"@emotion/react": "^11.1.4",
6565
"@emotion/styled": "^11.0.0",
6666
"@size-limit/preset-small-lib": "^4.9.1",
67-
"@storybook/addon-essentials": "^6.1.11",
68-
"@storybook/addon-info": "^5.3.21",
69-
"@storybook/addon-links": "^6.1.11",
70-
"@storybook/addons": "^6.1.11",
71-
"@storybook/react": "^6.1.11",
67+
"@storybook/addon-essentials": "^6.2.9",
68+
"@storybook/addon-links": "^6.2.9",
69+
"@storybook/addons": "^6.2.9",
70+
"@storybook/react": "^6.2.9",
7271
"@testing-library/react-hooks": "^7.0.0",
7372
"@types/react": "^17.0.0",
7473
"@types/react-dom": "^17.0.0",
@@ -85,7 +84,7 @@
8584
"size-limit": "^4.9.1",
8685
"tsdx": "^0.14.1",
8786
"tslib": "^2.0.3",
88-
"typescript": "^4.3.2"
87+
"typescript": "^4.3.4"
8988
},
9089
"dependencies": {
9190
"uuid": "^8.3.2"

src/components/chakra-ui/FilterSelection.stories.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import React from 'react';
22
import { Meta, Story } from '@storybook/react';
33
import { PropertyDescription } from '../../';
44
import { FilterSelection } from './FilterSelection';
5+
import { Filter } from '../../types';
6+
import { OperationType } from '../../operations';
7+
import { Binding } from '../../bindings';
58

69
const meta: Meta = {
710
title: 'Chakra UI/Filter Selection',
@@ -33,8 +36,22 @@ const properties: PropertyDescription[] = [
3336
},
3437
];
3538

39+
const initialValue: Filter[] = [
40+
{
41+
id: '35f924a2-9e1d-423c-8565-41619a5b8e8e',
42+
field: 'name',
43+
operation: OperationType.IS,
44+
value: 'Artemis',
45+
type: 'string',
46+
},
47+
{
48+
id: '91f7e872-f0e9-4fdf-8db1-6b51c0670817',
49+
binding: Binding.AND,
50+
},
51+
];
52+
3653
const Template: Story = args => (
37-
<FilterSelection properties={properties} {...args} />
54+
<FilterSelection value={initialValue} properties={properties} {...args} />
3855
);
3956

4057
// By passing using the Args format for exported stories, you can control the props for a component for reuse in a test

src/components/chakra-ui/FilterSelection.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ interface Props {
99
properties: PropertyDescription[];
1010
}
1111

12-
export const FilterSelection: FC<Props> = ({ properties }) => {
12+
export const FilterSelection: FC<Props> = ({ value, properties }) => {
1313
const { filters, onAddFilter, createFilterRowProps } = useQueryFilters({
14+
initialValue: value,
1415
properties,
1516
});
1617

src/components/react-select/FilterRow.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ export const FilterRow: FC<FilterRowProps> = ({
4040
<Select
4141
styles={selectStyles}
4242
value={binding}
43-
onChange={onChangeBinding}
43+
onChange={selectedBinding => {
44+
if (!selectedBinding) {
45+
return;
46+
}
47+
48+
onChangeBinding(selectedBinding);
49+
}}
4450
options={bindings}
4551
/>
4652
) : (
@@ -50,15 +56,27 @@ export const FilterRow: FC<FilterRowProps> = ({
5056
<Select
5157
styles={selectStyles}
5258
value={field}
53-
onChange={onChangeField}
59+
onChange={selectedField => {
60+
if (!selectedField) {
61+
return;
62+
}
63+
64+
onChangeField(selectedField);
65+
}}
5466
placeholder="Field"
5567
options={fields}
5668
/>
5769

5870
<Select
5971
styles={selectStyles}
6072
value={operation}
61-
onChange={onChangeOperation}
73+
onChange={selectedOperation => {
74+
if (!selectedOperation) {
75+
return;
76+
}
77+
78+
onChangeOperation(selectedOperation);
79+
}}
6280
placeholder="Operation"
6381
options={operations}
6482
/>

src/hooks/useQueryFilters.test.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { act, renderHook } from '@testing-library/react-hooks';
22
import { ChangeEvent } from 'react';
3+
import { Binding } from '../bindings';
34
import {
45
defaultTypeOperationsMap,
56
defaultOperationLabels,
67
mapOperationToSelectOption,
78
OperationType,
89
} from '../operations';
10+
import { Filter } from '../types';
911
import { useQueryFilters, HookProps } from './useQueryFilters';
1012

1113
const properties: HookProps['properties'] = [
@@ -53,6 +55,27 @@ describe('useQueryFilters', () => {
5355
createTestFilter();
5456
});
5557

58+
it('should initialize using a given initial state', () => {
59+
const initialValue: Filter[] = [
60+
{
61+
id: '35f924a2-9e1d-423c-8565-41619a5b8e8e',
62+
field: 'name',
63+
operation: OperationType.IS,
64+
value: 'Artemis',
65+
type: 'string',
66+
},
67+
{
68+
id: '91f7e872-f0e9-4fdf-8db1-6b51c0670817',
69+
binding: Binding.AND,
70+
},
71+
];
72+
const { result } = renderHook(() =>
73+
useQueryFilters({ initialValue, properties })
74+
);
75+
76+
expect(result.current.filters).toStrictEqual(initialValue);
77+
});
78+
5679
describe('createFilterRowProps', () => {
5780
it('should return the string operation choices by default if the filter has no field selected', () => {
5881
const result = createTestFilter();

0 commit comments

Comments
 (0)