@bitrise/bitkit 12.69.1 → 12.70.0-alpha-filter-2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +30 -30
- package/src/Components/Button/Button.theme.ts +1 -1
- package/src/Components/DatePicker/DatePicker.tsx +4 -3
- package/src/Components/DatePicker/DatePickerFooter.tsx +7 -0
- package/src/Components/Filter/Filter.context.tsx +6 -0
- package/src/Components/Filter/Filter.storyData.ts +83 -0
- package/src/Components/Filter/Filter.theme.ts +161 -0
- package/src/Components/Filter/Filter.tsx +176 -0
- package/src/Components/Filter/Filter.types.ts +33 -0
- package/src/Components/Filter/Filter.utils.ts +33 -0
- package/src/Components/Filter/FilterAdd/FilterAdd.tsx +92 -0
- package/src/Components/Filter/FilterDate/FilterDate.tsx +77 -0
- package/src/Components/Filter/FilterForm/FilterForm.tsx +171 -0
- package/src/Components/Filter/FilterItem/FilterItem.tsx +97 -0
- package/src/Components/Filter/FilterSearch/FilterSearch.tsx +66 -0
- package/src/Components/{Form → Filter}/FilterSwitch/FilterSwitch.theme.ts +11 -9
- package/src/Components/{Form → Filter}/FilterSwitch/FilterSwitch.tsx +3 -5
- package/src/Components/Filter/FilterSwitchAdapter/FilterSwitchAdapter.tsx +31 -0
- package/src/Components/Form/Checkbox/CheckboxGroup.tsx +4 -1
- package/src/Components/Form/Input/Input.tsx +1 -0
- package/src/Components/Form/Textarea/Textarea.tsx +10 -1
- package/src/Components/Icons/Icons.tsx +2 -2
- package/src/Components/Select/Select.tsx +1 -0
- package/src/Components/Text/Text.tsx +0 -39
- package/src/index.ts +7 -3
- package/src/theme.ts +3 -1
- package/src/utils/utils.ts +23 -1
- /package/src/Components/{Form → Filter}/FilterSwitch/FilterSwitchGroup.tsx +0 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bitrise/bitkit",
|
|
3
3
|
"description": "Bitrise React component library",
|
|
4
|
-
"version": "12.
|
|
4
|
+
"version": "12.70.0-alpha-filter-2.1",
|
|
5
5
|
"repository": "git@github.com:bitrise-io/bitkit.git",
|
|
6
6
|
"main": "src/index.ts",
|
|
7
7
|
"license": "UNLICENSED",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"@emotion/react": "^11.11.1",
|
|
29
29
|
"@emotion/styled": "^11.11.0",
|
|
30
30
|
"@floating-ui/react-dom-interactions": "^0.8.1",
|
|
31
|
-
"framer-motion": "^10.16.
|
|
31
|
+
"framer-motion": "^10.16.16",
|
|
32
32
|
"luxon": "^3.4.4",
|
|
33
33
|
"react": "^18.2.0",
|
|
34
34
|
"react-dom": "^18.2.0",
|
|
@@ -40,38 +40,38 @@
|
|
|
40
40
|
"react-dom": "^18.2.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@babel/core": "^7.23.
|
|
44
|
-
"@babel/preset-env": "^7.23.
|
|
43
|
+
"@babel/core": "^7.23.6",
|
|
44
|
+
"@babel/preset-env": "^7.23.6",
|
|
45
45
|
"@babel/preset-react": "^7.23.3",
|
|
46
46
|
"@babel/preset-typescript": "^7.23.3",
|
|
47
|
-
"@bitrise/eslint-plugin": "^2.3.
|
|
47
|
+
"@bitrise/eslint-plugin": "^2.3.3",
|
|
48
48
|
"@commitlint/cli": "^17.8.0",
|
|
49
49
|
"@commitlint/config-conventional": "^17.8.0",
|
|
50
|
-
"@google-cloud/storage": "^7.
|
|
50
|
+
"@google-cloud/storage": "^7.7.0",
|
|
51
51
|
"@semantic-release/commit-analyzer": "^11.1.0",
|
|
52
52
|
"@semantic-release/git": "^10.0.1",
|
|
53
|
-
"@storybook/addon-actions": "^7.
|
|
54
|
-
"@storybook/addon-essentials": "^7.
|
|
55
|
-
"@storybook/addon-interactions": "^7.
|
|
56
|
-
"@storybook/addon-links": "^7.
|
|
57
|
-
"@storybook/addons": "^7.
|
|
58
|
-
"@storybook/blocks": "^7.
|
|
59
|
-
"@storybook/react": "^7.
|
|
60
|
-
"@storybook/react-webpack5": "^7.
|
|
53
|
+
"@storybook/addon-actions": "^7.6.4",
|
|
54
|
+
"@storybook/addon-essentials": "^7.6.4",
|
|
55
|
+
"@storybook/addon-interactions": "^7.6.4",
|
|
56
|
+
"@storybook/addon-links": "^7.6.4",
|
|
57
|
+
"@storybook/addons": "^7.6.4",
|
|
58
|
+
"@storybook/blocks": "^7.6.4",
|
|
59
|
+
"@storybook/react": "^7.6.4",
|
|
60
|
+
"@storybook/react-webpack5": "^7.6.4",
|
|
61
61
|
"@storybook/testing-library": "^0.2.2",
|
|
62
|
-
"@storybook/theming": "^7.
|
|
62
|
+
"@storybook/theming": "^7.6.4",
|
|
63
63
|
"@testing-library/dom": "^9.3.3",
|
|
64
|
-
"@testing-library/jest-dom": "^6.1.
|
|
65
|
-
"@testing-library/react": "^14.1.
|
|
64
|
+
"@testing-library/jest-dom": "^6.1.5",
|
|
65
|
+
"@testing-library/react": "^14.1.2",
|
|
66
66
|
"@testing-library/user-event": "^14.5.1",
|
|
67
|
-
"@types/jest": "^29.5.
|
|
68
|
-
"@types/luxon": "^3.3.
|
|
69
|
-
"@types/react": "^18.2.
|
|
70
|
-
"@types/react-dom": "^18.2.
|
|
71
|
-
"@typescript-eslint/eslint-plugin": "^6.
|
|
72
|
-
"@typescript-eslint/parser": "^6.
|
|
67
|
+
"@types/jest": "^29.5.11",
|
|
68
|
+
"@types/luxon": "^3.3.7",
|
|
69
|
+
"@types/react": "^18.2.43",
|
|
70
|
+
"@types/react-dom": "^18.2.17",
|
|
71
|
+
"@typescript-eslint/eslint-plugin": "^6.14.0",
|
|
72
|
+
"@typescript-eslint/parser": "^6.14.0",
|
|
73
73
|
"axios": "^1.6.2",
|
|
74
|
-
"eslint": "^8.
|
|
74
|
+
"eslint": "^8.55.0",
|
|
75
75
|
"eslint-plugin-import": "^2.29.0",
|
|
76
76
|
"eslint-plugin-jest": "^27.6.0",
|
|
77
77
|
"eslint-plugin-jsx-a11y": "^6.8.0",
|
|
@@ -79,15 +79,15 @@
|
|
|
79
79
|
"eslint-plugin-react": "^7.33.2",
|
|
80
80
|
"eslint-plugin-react-hooks": "^4.6.0",
|
|
81
81
|
"eslint-plugin-storybook": "^0.6.15",
|
|
82
|
-
"eslint-plugin-testing-library": "^6.
|
|
82
|
+
"eslint-plugin-testing-library": "^6.2.0",
|
|
83
83
|
"glob": "^10.3.10",
|
|
84
84
|
"jest": "^29.7.0",
|
|
85
85
|
"jest-environment-jsdom": "^29.7.0",
|
|
86
|
-
"jsdom": "^
|
|
87
|
-
"prettier": "^3.1.
|
|
88
|
-
"react-hook-form": "^7.
|
|
89
|
-
"semantic-release": "^22.0.
|
|
90
|
-
"storybook": "^7.
|
|
86
|
+
"jsdom": "^23.0.1",
|
|
87
|
+
"prettier": "^3.1.1",
|
|
88
|
+
"react-hook-form": "^7.49.0",
|
|
89
|
+
"semantic-release": "^22.0.12",
|
|
90
|
+
"storybook": "^7.6.4",
|
|
91
91
|
"ts-jest": "^29.1.1",
|
|
92
92
|
"typescript": "^4.8.4"
|
|
93
93
|
},
|
|
@@ -22,6 +22,7 @@ export interface DatePickerProps {
|
|
|
22
22
|
selected?: DateRange;
|
|
23
23
|
onApply?: (range: DateRange) => void;
|
|
24
24
|
onClose: () => void;
|
|
25
|
+
onClear?: () => void;
|
|
25
26
|
visible: boolean;
|
|
26
27
|
}
|
|
27
28
|
|
|
@@ -30,7 +31,7 @@ export interface DatePickerProps {
|
|
|
30
31
|
* range selection.
|
|
31
32
|
*/
|
|
32
33
|
const DatePicker = (props: DatePickerProps) => {
|
|
33
|
-
const { children, onApply, onClose, visible, selectable, selected } = props;
|
|
34
|
+
const { children, onApply, onClose, onClear, visible, selectable, selected } = props;
|
|
34
35
|
|
|
35
36
|
const { isMobile } = useResponsive();
|
|
36
37
|
const today = DateTime.now().startOf('day');
|
|
@@ -63,7 +64,7 @@ const DatePicker = (props: DatePickerProps) => {
|
|
|
63
64
|
};
|
|
64
65
|
|
|
65
66
|
const { leftViewDate, rightViewDate, updateLeftViewDate, updateRightViewDate } = useViewDate({
|
|
66
|
-
initalView: dateFrom || selectable?.
|
|
67
|
+
initalView: dateFrom || selectable?.to,
|
|
67
68
|
});
|
|
68
69
|
|
|
69
70
|
const [preview, setPreview] = useState<'from' | 'to' | undefined>(undefined);
|
|
@@ -164,7 +165,7 @@ const DatePicker = (props: DatePickerProps) => {
|
|
|
164
165
|
/>
|
|
165
166
|
)}
|
|
166
167
|
</Box>
|
|
167
|
-
<DatePickerFooter onApply={handleApply} onClose={handleClose} selected={selected} />
|
|
168
|
+
<DatePickerFooter onApply={handleApply} onClose={handleClose} onClear={onClear} selected={selected} />
|
|
168
169
|
</>
|
|
169
170
|
)}
|
|
170
171
|
</Box>
|
|
@@ -8,10 +8,12 @@ const DatePickerFooter = ({
|
|
|
8
8
|
selected,
|
|
9
9
|
onClose,
|
|
10
10
|
onApply,
|
|
11
|
+
onClear,
|
|
11
12
|
}: {
|
|
12
13
|
selected?: DateRange;
|
|
13
14
|
onClose: () => void;
|
|
14
15
|
onApply: () => void;
|
|
16
|
+
onClear?: () => void;
|
|
15
17
|
}) => {
|
|
16
18
|
return (
|
|
17
19
|
<Box
|
|
@@ -20,6 +22,11 @@ const DatePickerFooter = ({
|
|
|
20
22
|
gridTemplateRows={['1.25rem 2rem', 'unset']}
|
|
21
23
|
gap="24"
|
|
22
24
|
>
|
|
25
|
+
{!!onClear && (
|
|
26
|
+
<Button size="small" variant="tertiary" width="fit-content" onClick={() => onClear()}>
|
|
27
|
+
Clear
|
|
28
|
+
</Button>
|
|
29
|
+
)}
|
|
23
30
|
<Text alignSelf="center" justifySelf="center" size="2" color="text.secondary" gridColumn={['1', '2']}>
|
|
24
31
|
{selected?.from?.toFormat('DD', { locale: 'en-US' })}
|
|
25
32
|
{selected?.from || selected?.to ? ' - ' : undefined}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { FilterContextType, FilterData, FilterOptions, FilterState } from './Filter.types';
|
|
2
|
+
|
|
3
|
+
export const FILTER_STORY_OPTIONS: FilterOptions =
|
|
4
|
+
'Lorem ipsum dolor sit amet consectetur adipisicing elit Reiciendis reprehenderit laudantium laborum excepturi nam quae quod sunt expedita vel repellat cum cupiditate esse similique est ducimus provident eos numquam voluptas'.split(
|
|
5
|
+
' ',
|
|
6
|
+
);
|
|
7
|
+
|
|
8
|
+
export const FILTER_STORY_DATA: FilterData = {
|
|
9
|
+
date_range: {
|
|
10
|
+
categoryName: 'Date',
|
|
11
|
+
categoryNamePlural: 'dates',
|
|
12
|
+
type: 'dateRange',
|
|
13
|
+
},
|
|
14
|
+
pipeline: {
|
|
15
|
+
categoryName: 'Pipeline',
|
|
16
|
+
categoryNamePlural: 'Pipelines',
|
|
17
|
+
options: FILTER_STORY_OPTIONS,
|
|
18
|
+
type: 'tag',
|
|
19
|
+
},
|
|
20
|
+
stage: {
|
|
21
|
+
categoryName: 'Stage',
|
|
22
|
+
categoryNamePlural: 'Stages',
|
|
23
|
+
options: FILTER_STORY_OPTIONS,
|
|
24
|
+
dependsOn: ['pipeline'],
|
|
25
|
+
type: 'tag',
|
|
26
|
+
},
|
|
27
|
+
workflow: {
|
|
28
|
+
categoryName: 'Workflow',
|
|
29
|
+
isMultiple: true,
|
|
30
|
+
options: FILTER_STORY_OPTIONS,
|
|
31
|
+
type: 'tag',
|
|
32
|
+
},
|
|
33
|
+
branch: {
|
|
34
|
+
options: [
|
|
35
|
+
'BIVS-2231-create-rename-to-utilisation-views',
|
|
36
|
+
'revert-11937-revert-11884-RA-2060-release-manager-role',
|
|
37
|
+
'master',
|
|
38
|
+
'CI-2264-consolidate-other-provider-type-to-custom',
|
|
39
|
+
],
|
|
40
|
+
type: 'tag',
|
|
41
|
+
},
|
|
42
|
+
app: {
|
|
43
|
+
categoryName: 'App',
|
|
44
|
+
options: ['46b6b9a78a418ee8', '32b14416be4b7b24', '0a248b278e135ea7'],
|
|
45
|
+
optionsMap: {
|
|
46
|
+
'46b6b9a78a418ee8': 'bitrise-website',
|
|
47
|
+
'32b14416be4b7b24': 'bitkit',
|
|
48
|
+
'0a248b278e135ea7': 'pipeline-service',
|
|
49
|
+
},
|
|
50
|
+
type: 'select',
|
|
51
|
+
},
|
|
52
|
+
test_case: {
|
|
53
|
+
categoryName: 'Test case',
|
|
54
|
+
onAsyncSearch: (category, q) => {
|
|
55
|
+
console.log('onAsyncSearch', { category, q });
|
|
56
|
+
return new Promise((resolve) => {
|
|
57
|
+
setTimeout(() => {
|
|
58
|
+
resolve(['found 1', 'found 2']);
|
|
59
|
+
}, 2000);
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
options: ['default 1', 'default 2', 'default 3'],
|
|
63
|
+
type: 'tag',
|
|
64
|
+
},
|
|
65
|
+
cache_type: {
|
|
66
|
+
options: ['gradle', 'bazel'],
|
|
67
|
+
type: 'switch',
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export const FILTER_STORY_INIT_STATE: FilterState = {
|
|
72
|
+
pipeline: ['ipsum'],
|
|
73
|
+
app: ['46b6b9a78a418ee8'],
|
|
74
|
+
cache_type: ['gradle'],
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export const FILTER_STORY_CONTEXT: FilterContextType = {
|
|
78
|
+
data: FILTER_STORY_DATA,
|
|
79
|
+
setPopoverOpen: () => {},
|
|
80
|
+
state: FILTER_STORY_INIT_STATE,
|
|
81
|
+
onFilterChange: () => {},
|
|
82
|
+
onFilterClear: () => {},
|
|
83
|
+
};
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { createMultiStyleConfigHelpers, SystemStyleObject } from '@chakra-ui/styled-system';
|
|
2
|
+
import { rem } from '../../utils/utils';
|
|
3
|
+
import { disabledStates } from '../Button/Button.theme';
|
|
4
|
+
|
|
5
|
+
export const parts = [
|
|
6
|
+
'container',
|
|
7
|
+
'icon',
|
|
8
|
+
'content',
|
|
9
|
+
'rightContent',
|
|
10
|
+
'item',
|
|
11
|
+
'tagEdit',
|
|
12
|
+
'tagClear',
|
|
13
|
+
'form',
|
|
14
|
+
'formHeader',
|
|
15
|
+
'formTitle',
|
|
16
|
+
'formBadge',
|
|
17
|
+
'formSearch',
|
|
18
|
+
'formInputGroup',
|
|
19
|
+
'formButtonGroup',
|
|
20
|
+
'searchInput',
|
|
21
|
+
] as const;
|
|
22
|
+
export type FilterStyle = Record<(typeof parts)[number], SystemStyleObject>;
|
|
23
|
+
|
|
24
|
+
const { defineMultiStyleConfig } = createMultiStyleConfigHelpers(parts);
|
|
25
|
+
|
|
26
|
+
const FilterTheme = defineMultiStyleConfig({
|
|
27
|
+
baseStyle: {
|
|
28
|
+
container: {
|
|
29
|
+
background: 'neutral.100',
|
|
30
|
+
display: 'flex',
|
|
31
|
+
flexDirection: ['column', 'row'],
|
|
32
|
+
gap: '12',
|
|
33
|
+
},
|
|
34
|
+
icon: {
|
|
35
|
+
margin: '4',
|
|
36
|
+
color: 'neutral.60',
|
|
37
|
+
},
|
|
38
|
+
content: {
|
|
39
|
+
display: 'flex',
|
|
40
|
+
gap: '12',
|
|
41
|
+
flexWrap: 'wrap',
|
|
42
|
+
},
|
|
43
|
+
rightContent: {
|
|
44
|
+
display: 'flex',
|
|
45
|
+
gap: '16',
|
|
46
|
+
marginInlineStart: 'auto',
|
|
47
|
+
},
|
|
48
|
+
item: {
|
|
49
|
+
border: '1px solid',
|
|
50
|
+
borderColor: 'neutral.80',
|
|
51
|
+
borderRadius: '4',
|
|
52
|
+
display: 'flex',
|
|
53
|
+
width: 'fit-content',
|
|
54
|
+
background: 'neutral.100',
|
|
55
|
+
wordBreak: 'break-word',
|
|
56
|
+
},
|
|
57
|
+
tagEdit: {
|
|
58
|
+
color: 'purple.10',
|
|
59
|
+
display: 'flex',
|
|
60
|
+
gap: '8',
|
|
61
|
+
alignItems: 'center',
|
|
62
|
+
paddingBlock: '4',
|
|
63
|
+
paddingInlineStart: rem(11),
|
|
64
|
+
paddingInlineEnd: rem(7),
|
|
65
|
+
borderRadius: '4',
|
|
66
|
+
_hover: {
|
|
67
|
+
background: 'neutral.93',
|
|
68
|
+
},
|
|
69
|
+
_active: {
|
|
70
|
+
background: 'neutral.90',
|
|
71
|
+
},
|
|
72
|
+
_disabled: {
|
|
73
|
+
cursor: 'not-allowed',
|
|
74
|
+
...disabledStates.tertiary,
|
|
75
|
+
_hover: disabledStates.tertiary,
|
|
76
|
+
_active: disabledStates.tertiary,
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
tagClear: {
|
|
80
|
+
color: 'purple.10',
|
|
81
|
+
borderRadius: '4',
|
|
82
|
+
border: 0,
|
|
83
|
+
minWidth: rem(27),
|
|
84
|
+
height: rem(30),
|
|
85
|
+
justifyContent: 'flex-start',
|
|
86
|
+
svg: {
|
|
87
|
+
marginInlineStart: '4',
|
|
88
|
+
},
|
|
89
|
+
_hover: {
|
|
90
|
+
background: 'neutral.93',
|
|
91
|
+
},
|
|
92
|
+
_active: {
|
|
93
|
+
background: 'neutral.90',
|
|
94
|
+
},
|
|
95
|
+
_disabled: {
|
|
96
|
+
cursor: 'not-allowed',
|
|
97
|
+
...disabledStates.tertiary,
|
|
98
|
+
_hover: disabledStates.tertiary,
|
|
99
|
+
_active: disabledStates.tertiary,
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
form: {
|
|
103
|
+
background: 'neutral.100',
|
|
104
|
+
borderRadius: '8',
|
|
105
|
+
padding: '16',
|
|
106
|
+
minWidth: rem(352),
|
|
107
|
+
maxWidth: rem(480),
|
|
108
|
+
},
|
|
109
|
+
formHeader: {
|
|
110
|
+
display: 'flex',
|
|
111
|
+
gap: '8',
|
|
112
|
+
justifyContent: 'space-between',
|
|
113
|
+
marginBlockEnd: '8',
|
|
114
|
+
},
|
|
115
|
+
formTitle: {
|
|
116
|
+
color: 'purpe.10',
|
|
117
|
+
fontWeight: 'demiBold',
|
|
118
|
+
},
|
|
119
|
+
formBadge: {
|
|
120
|
+
backgroundColor: 'neutral.93',
|
|
121
|
+
color: 'neutral.40',
|
|
122
|
+
fontWeight: 'bold',
|
|
123
|
+
fontVariantNumeric: 'tabular-nums',
|
|
124
|
+
},
|
|
125
|
+
formSearch: {
|
|
126
|
+
marginBlockEnd: '16',
|
|
127
|
+
},
|
|
128
|
+
formInputGroup: {
|
|
129
|
+
display: 'flex',
|
|
130
|
+
flexDirection: 'column',
|
|
131
|
+
gap: '12',
|
|
132
|
+
maxHeight: rem(196),
|
|
133
|
+
overflowY: 'scroll',
|
|
134
|
+
paddingInline: '12',
|
|
135
|
+
paddingBlock: rem(3),
|
|
136
|
+
},
|
|
137
|
+
formButtonGroup: {
|
|
138
|
+
display: 'flex',
|
|
139
|
+
justifyContent: 'flex-end',
|
|
140
|
+
marginBlockStart: '24',
|
|
141
|
+
},
|
|
142
|
+
searchInput: {
|
|
143
|
+
paddingLeft: '32',
|
|
144
|
+
paddingY: '4',
|
|
145
|
+
paddingRight: '8',
|
|
146
|
+
border: undefined,
|
|
147
|
+
borderRadius: '4',
|
|
148
|
+
borderColor: undefined,
|
|
149
|
+
boxShadow: undefined,
|
|
150
|
+
fontSize: '2',
|
|
151
|
+
_focusVisible: {
|
|
152
|
+
backgroundColor: 'neutral.95',
|
|
153
|
+
},
|
|
154
|
+
_hover: {
|
|
155
|
+
backgroundColor: 'neutral.95',
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
export default FilterTheme;
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import { Modal, ModalOverlay, useMultiStyleConfig } from '@chakra-ui/react';
|
|
3
|
+
import Box, { BoxProps } from '../Box/Box';
|
|
4
|
+
import Button from '../Button/Button';
|
|
5
|
+
import Divider from '../Divider/Divider';
|
|
6
|
+
import Icon from '../Icon/Icon';
|
|
7
|
+
import { FilterContext } from './Filter.context';
|
|
8
|
+
import { FilterStyle } from './Filter.theme';
|
|
9
|
+
import {
|
|
10
|
+
FilterCategoryProps,
|
|
11
|
+
FilterContextType,
|
|
12
|
+
FilterData,
|
|
13
|
+
FilterState,
|
|
14
|
+
FilterType,
|
|
15
|
+
FilterValue,
|
|
16
|
+
} from './Filter.types';
|
|
17
|
+
import { getDependents } from './Filter.utils';
|
|
18
|
+
import FilterAdd from './FilterAdd/FilterAdd';
|
|
19
|
+
import FilterItem from './FilterItem/FilterItem';
|
|
20
|
+
import FilterSearch from './FilterSearch/FilterSearch';
|
|
21
|
+
import FilterDate from './FilterDate/FilterDate';
|
|
22
|
+
import FilterSwitchAdapter from './FilterSwitchAdapter/FilterSwitchAdapter';
|
|
23
|
+
|
|
24
|
+
export interface FilterProps extends Omit<BoxProps, 'onChange'> {
|
|
25
|
+
filtersDependOn?: string[];
|
|
26
|
+
initialData: FilterData;
|
|
27
|
+
initialState: FilterState;
|
|
28
|
+
isLoading?: boolean;
|
|
29
|
+
onChange: (state: FilterState) => void;
|
|
30
|
+
showSearch?: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const Filter = (props: FilterProps) => {
|
|
34
|
+
const { filtersDependOn, initialData, initialState, isLoading, onChange, showSearch, ...rest } = props;
|
|
35
|
+
|
|
36
|
+
const isInited = useRef<boolean>(false);
|
|
37
|
+
|
|
38
|
+
const filterStyle = useMultiStyleConfig('Filter') as FilterStyle;
|
|
39
|
+
|
|
40
|
+
const state: FilterState = {};
|
|
41
|
+
Object.entries(initialState).forEach(([category, values]) => {
|
|
42
|
+
if (values?.length) {
|
|
43
|
+
const cleanValues = values.filter((v) => v !== null && v !== '' && v !== undefined);
|
|
44
|
+
if (cleanValues.length) {
|
|
45
|
+
state[category] = cleanValues;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const [data] = useState<FilterData>(initialData);
|
|
51
|
+
const [isPopoverOpen, setPopoverOpen] = useState<boolean>(false);
|
|
52
|
+
|
|
53
|
+
const deleteFromState = (category: string, stateProp: FilterState): FilterState => {
|
|
54
|
+
const filteredState = { ...stateProp };
|
|
55
|
+
delete filteredState[category];
|
|
56
|
+
|
|
57
|
+
const dependents = getDependents(data, category, filtersDependOn);
|
|
58
|
+
|
|
59
|
+
dependents.forEach((dependent) => {
|
|
60
|
+
delete filteredState[dependent];
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
return filteredState;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const onFilterChange = (category: string, value: FilterValue) => {
|
|
67
|
+
if (isInited.current === false) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
let newState = { ...state };
|
|
71
|
+
if (value && value.length > 0) {
|
|
72
|
+
newState[category] = value;
|
|
73
|
+
} else if (newState[category]) {
|
|
74
|
+
newState = deleteFromState(category, newState);
|
|
75
|
+
}
|
|
76
|
+
onChange(newState);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const onFilterClear = (category: string) => {
|
|
80
|
+
onChange(deleteFromState(category, state));
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const onClearFilters = () => {
|
|
84
|
+
onChange({});
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const filters = {
|
|
88
|
+
search: {},
|
|
89
|
+
select: {},
|
|
90
|
+
switch: {},
|
|
91
|
+
dateRange: {},
|
|
92
|
+
tag: {},
|
|
93
|
+
} as Record<FilterType, Record<string, FilterCategoryProps>>;
|
|
94
|
+
|
|
95
|
+
Object.entries(data).forEach(([category, value]) => {
|
|
96
|
+
filters[value.type || 'tag'][category] = value;
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const stateCategories = Object.keys(state).filter((c) => !['date_range', 'search'].includes(c));
|
|
100
|
+
|
|
101
|
+
const showClearFilters = stateCategories.length > 0 || (state.search && state.search.length > 0);
|
|
102
|
+
|
|
103
|
+
const contextValue: FilterContextType = useMemo(
|
|
104
|
+
() => ({
|
|
105
|
+
data: initialData,
|
|
106
|
+
filtersDependOn,
|
|
107
|
+
isLoading,
|
|
108
|
+
onFilterChange,
|
|
109
|
+
onFilterClear,
|
|
110
|
+
setPopoverOpen,
|
|
111
|
+
state,
|
|
112
|
+
}),
|
|
113
|
+
[filtersDependOn, isLoading, initialData, onFilterChange, onFilterClear, setPopoverOpen, state],
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
useEffect(() => {
|
|
117
|
+
if (isInited.current === false) {
|
|
118
|
+
isInited.current = true;
|
|
119
|
+
}
|
|
120
|
+
}, [isInited.current]);
|
|
121
|
+
|
|
122
|
+
return (
|
|
123
|
+
<FilterContext value={contextValue}>
|
|
124
|
+
<Box sx={filterStyle.container} {...rest}>
|
|
125
|
+
<Modal isOpen={isPopoverOpen} onClose={() => {}}>
|
|
126
|
+
{isPopoverOpen && <ModalOverlay />}
|
|
127
|
+
</Modal>
|
|
128
|
+
<Box sx={filterStyle.content}>
|
|
129
|
+
<Icon name="Filter" sx={filterStyle.icon} />
|
|
130
|
+
|
|
131
|
+
{Object.keys(filters.switch).map((category) => (
|
|
132
|
+
<FilterSwitchAdapter category={category} key={category} />
|
|
133
|
+
))}
|
|
134
|
+
|
|
135
|
+
{Object.keys(filters.dateRange).map((category) => (
|
|
136
|
+
<FilterDate category={category} key={category} />
|
|
137
|
+
))}
|
|
138
|
+
|
|
139
|
+
{Object.keys(filters.select).map((category) => (
|
|
140
|
+
<FilterItem category={category} key={category} />
|
|
141
|
+
))}
|
|
142
|
+
{Object.keys(filters.tag).map((category) => {
|
|
143
|
+
if (!state[category]) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
return <FilterItem category={category} key={category} />;
|
|
147
|
+
})}
|
|
148
|
+
|
|
149
|
+
<FilterAdd onChange={onFilterChange} />
|
|
150
|
+
</Box>
|
|
151
|
+
{(showClearFilters || showSearch) && (
|
|
152
|
+
<Box sx={filterStyle.rightContent}>
|
|
153
|
+
{showClearFilters && (
|
|
154
|
+
<Button
|
|
155
|
+
isDisabled={isLoading}
|
|
156
|
+
leftIconName="CloseSmall"
|
|
157
|
+
minWidth="7.5rem"
|
|
158
|
+
onClick={onClearFilters}
|
|
159
|
+
size="small"
|
|
160
|
+
variant="tertiary"
|
|
161
|
+
>
|
|
162
|
+
Clear filters
|
|
163
|
+
</Button>
|
|
164
|
+
)}
|
|
165
|
+
{showClearFilters && showSearch && (
|
|
166
|
+
<Divider orientation="vertical" size="1" variant="solid" flexShrink="0" />
|
|
167
|
+
)}
|
|
168
|
+
{showSearch && <FilterSearch onChange={onFilterChange} value={(state.Search && state.Search[0]) || ''} />}
|
|
169
|
+
</Box>
|
|
170
|
+
)}
|
|
171
|
+
</Box>
|
|
172
|
+
</FilterContext>
|
|
173
|
+
);
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
export default Filter;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Dispatch, SetStateAction } from 'react';
|
|
2
|
+
|
|
3
|
+
export type FilterType = 'dateRange' | 'search' | 'select' | 'switch' | 'tag';
|
|
4
|
+
|
|
5
|
+
export type FilterOptions = string[];
|
|
6
|
+
export type FilterValue = string[];
|
|
7
|
+
export type FilterOptionsMap = Record<string, string>;
|
|
8
|
+
|
|
9
|
+
export type FilterSearchCallback = (category: string, q: string) => Promise<FilterValue>;
|
|
10
|
+
|
|
11
|
+
export type FilterCategoryProps = {
|
|
12
|
+
categoryName?: string;
|
|
13
|
+
categoryNamePlural?: string;
|
|
14
|
+
dependsOn?: string[];
|
|
15
|
+
isMultiple?: boolean;
|
|
16
|
+
onAsyncSearch?: FilterSearchCallback;
|
|
17
|
+
options?: FilterOptions;
|
|
18
|
+
optionsMap?: FilterOptionsMap;
|
|
19
|
+
type?: FilterType;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type FilterData = Record<string, FilterCategoryProps>;
|
|
23
|
+
export type FilterState = Record<string, FilterValue>;
|
|
24
|
+
|
|
25
|
+
export interface FilterContextType {
|
|
26
|
+
data: FilterData;
|
|
27
|
+
filtersDependOn?: string[];
|
|
28
|
+
isLoading?: boolean;
|
|
29
|
+
onFilterClear: (category: string) => void;
|
|
30
|
+
onFilterChange: (category: string, value: FilterValue) => void;
|
|
31
|
+
setPopoverOpen: Dispatch<SetStateAction<boolean>>;
|
|
32
|
+
state: FilterState;
|
|
33
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { FilterData, FilterOptionsMap } from './Filter.types';
|
|
2
|
+
|
|
3
|
+
export const hasAllDependencies = (stateKeys: string[], dependsOn?: string[]): boolean => {
|
|
4
|
+
if (!dependsOn || dependsOn.length === 0) {
|
|
5
|
+
return true;
|
|
6
|
+
}
|
|
7
|
+
return dependsOn.every((key) => stateKeys.includes(key));
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export const getDependents = (data: FilterData, categoryKey: string, filtersDependOn?: string[]): string[] => {
|
|
11
|
+
const dependents: string[] = [];
|
|
12
|
+
if (filtersDependOn && filtersDependOn.includes(categoryKey)) {
|
|
13
|
+
Object.keys(data).forEach((category) => {
|
|
14
|
+
if (data[category].type !== 'select') {
|
|
15
|
+
dependents.push(category);
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
} else {
|
|
19
|
+
Object.keys(data).forEach((category) => {
|
|
20
|
+
if (data[category].dependsOn?.includes(categoryKey)) {
|
|
21
|
+
dependents.push(category);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return dependents;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const getOptionLabel = (option: string, optionsMap?: FilterOptionsMap) => {
|
|
29
|
+
if (!optionsMap) {
|
|
30
|
+
return option;
|
|
31
|
+
}
|
|
32
|
+
return optionsMap[option] || option;
|
|
33
|
+
};
|