forked from marmelab/react-admin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FilterForm.tsx
208 lines (190 loc) · 5.86 KB
/
FilterForm.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import * as React from 'react';
import { useEffect, useCallback, HtmlHTMLAttributes, ReactNode } from 'react';
import PropTypes from 'prop-types';
import { Form, FormRenderProps, FormSpy } from 'react-final-form';
import arrayMutators from 'final-form-arrays';
import classnames from 'classnames';
import { makeStyles } from '@material-ui/core/styles';
import lodashSet from 'lodash/set';
import lodashGet from 'lodash/get';
import FilterFormInput from './FilterFormInput';
import { ClassesOverride } from '../../types';
const useStyles = makeStyles(
theme => ({
form: {
marginTop: -theme.spacing(2),
paddingTop: 0,
display: 'flex',
alignItems: 'flex-end',
flexWrap: 'wrap',
minHeight: theme.spacing(10),
pointerEvents: 'none',
},
clearFix: { clear: 'right' },
}),
{ name: 'RaFilterForm' }
);
const sanitizeRestProps = ({
active,
dirty,
dirtyFields,
dirtyFieldsSinceLastSubmit,
dirtySinceLastSubmit,
error,
errors,
filterValues,
form,
handleSubmit,
hasSubmitErrors,
hasValidationErrors,
invalid,
modified,
modifiedSinceLastSubmit,
pristine,
setFilters,
submitError,
submitErrors,
submitFailed,
submitSucceeded,
submitting,
touched,
valid,
validating,
values,
visited,
...props
}: Partial<FilterFormProps>) => props;
export interface FilterFormProps
extends Omit<FormRenderProps, 'initialValues'>,
Omit<HtmlHTMLAttributes<HTMLFormElement>, 'children'> {
classes?: ClassesOverride<typeof useStyles>;
className?: string;
resource?: string;
filterValues: any;
hideFilter: (filterName: string) => void;
setFilters: (filters: any, displayedFilters: any) => void;
displayedFilters: any;
filters: ReactNode[];
initialValues?: any;
margin?: 'none' | 'normal' | 'dense';
variant?: 'standard' | 'outlined' | 'filled';
}
export const FilterForm = ({
classes = {},
className,
resource,
margin,
variant,
filters,
displayedFilters = {},
hideFilter,
initialValues,
...rest
}: FilterFormProps) => {
useEffect(() => {
filters.forEach((filter: JSX.Element) => {
if (filter.props.alwaysOn && filter.props.defaultValue) {
throw new Error(
'Cannot use alwaysOn and defaultValue on a filter input. Please set the filterDefaultValues props on the <List> element instead.'
);
}
});
}, [filters]);
const getShownFilters = () =>
filters.filter(
(filterElement: JSX.Element) =>
filterElement.props.alwaysOn ||
displayedFilters[filterElement.props.source] ||
typeof lodashGet(initialValues, filterElement.props.source) !==
'undefined'
);
const handleHide = useCallback(
event => hideFilter(event.currentTarget.dataset.key),
[hideFilter]
);
return (
<form
className={classnames(className, classes.form)}
{...sanitizeRestProps(rest)}
onSubmit={handleSubmit}
>
{getShownFilters().map((filterElement: JSX.Element) => (
<FilterFormInput
key={filterElement.props.source}
filterElement={filterElement}
handleHide={handleHide}
resource={resource}
variant={filterElement.props.variant || variant}
margin={filterElement.props.margin || margin}
/>
))}
<div className={classes.clearFix} />
</form>
);
};
const handleSubmit = event => {
event.preventDefault();
return false;
};
FilterForm.propTypes = {
resource: PropTypes.string,
filters: PropTypes.arrayOf(PropTypes.node).isRequired,
displayedFilters: PropTypes.object,
hideFilter: PropTypes.func.isRequired,
initialValues: PropTypes.object,
classes: PropTypes.object,
className: PropTypes.string,
};
export const mergeInitialValuesWithDefaultValues = ({
initialValues,
filters,
}) => ({
...filters
.filter(
(filterElement: JSX.Element) =>
filterElement.props.alwaysOn && filterElement.props.defaultValue
)
.reduce(
(acc, filterElement: JSX.Element) =>
lodashSet(
{ ...acc },
filterElement.props.source,
filterElement.props.defaultValue
),
{} as any
),
...initialValues,
});
const EnhancedFilterForm = props => {
const { classes: classesOverride, ...rest } = props;
const classes = useStyles(props);
const mergedInitialValuesWithDefaultValues = mergeInitialValuesWithDefaultValues(
props
);
const { initialValues, ...rest2 } = rest;
return (
<Form
onSubmit={handleFinalFormSubmit}
initialValues={mergedInitialValuesWithDefaultValues}
mutators={{ ...arrayMutators }}
render={formProps => (
<>
<FormSpy
subscription={FormSpySubscription}
onChange={({ pristine, values }) => {
if (pristine) {
return;
}
rest && rest.setFilters(values);
}}
/>
<FilterForm classes={classes} {...formProps} {...rest2} />
</>
)}
/>
);
};
const handleFinalFormSubmit = () => {};
// Options to instruct the FormSpy that it should only listen to the values and pristine changes
const FormSpySubscription = { values: true, pristine: true };
export default EnhancedFilterForm;