-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathFilter.react.js
152 lines (140 loc) · 5.48 KB
/
Filter.react.js
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
/*
* Copyright (c) 2016-present, Parse, LLC
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import * as Filters from 'lib/Filters';
import { List, Map } from 'immutable';
import PropTypes from 'lib/PropTypes';
import React from 'react';
import stringCompare from 'lib/stringCompare';
import { CurrentApp } from 'context/currentApp';
function changeField(schema, filters, index, newField) {
const allowedConstraints = Filters.FieldConstraints[schema[newField].type];
const current = filters.get(index);
const constraint = current.get('constraint');
const compare = current.get('compareTo');
const defaultCompare = Filters.DefaultComparisons[schema[newField].type];
const useExisting = allowedConstraints.includes(constraint);
const newFilter = new Map({
field: newField,
constraint: useExisting ? constraint : Filters.FieldConstraints[schema[newField].type][0],
compareTo: (useExisting && typeof defaultCompare === typeof compare) ? compare : defaultCompare
});
return filters.set(index, newFilter);
}
function changeConstraint(schema, filters, index, newConstraint, prevCompareTo) {
let field = filters.get(index).get('field');
let compareType = schema[field].type;
if (Object.prototype.hasOwnProperty.call(Filters.Constraints[newConstraint], 'field')) {
compareType = Filters.Constraints[newConstraint].field;
}
let newFilter = new Map({
field: field,
constraint: newConstraint,
compareTo: prevCompareTo ?? Filters.DefaultComparisons[compareType]
})
return filters.set(index, newFilter);
}
function changeCompareTo(schema, filters, index, type, newCompare) {
let newValue = newCompare;
return filters.set(index, filters.get(index).set('compareTo', newValue));
}
function deleteRow(filters, index) {
return filters.delete(index);
}
let Filter = ({ schema, filters, renderRow, onChange, onSearch, blacklist, className }) => {
const currentApp = React.useContext(CurrentApp);
blacklist = blacklist || [];
let available = Filters.availableFilters(schema, filters);
return (
<div>
{filters.toArray().map((filter, i) => {
let field = filter.get('field');
let constraint = filter.get('constraint');
let compareTo = filter.get('compareTo');
let fields = Object.keys(available).concat([]);
if (fields.indexOf(field) < 0) {
fields.push(field);
}
// Get the column preference of the current class.
const currentColumnPreference = currentApp.columnPreference ? currentApp.columnPreference[className] : null;
// Check if the preference exists.
if (currentColumnPreference) {
const fieldsToSortToTop = currentColumnPreference
.filter(item => item.filterSortToTop)
.map(item => item.name);
// Sort the fields.
fields.sort((a, b) => {
// Only "a" should sorted to the top.
if (fieldsToSortToTop.includes(a) && !fieldsToSortToTop.includes(b)) {
return -1
}
// Only "b" should sorted to the top.
if (!fieldsToSortToTop.includes(a) && fieldsToSortToTop.includes(b)) {
return 1;
}
// Both should sorted to the top -> they should be sorted to the same order as in the "fieldsToSortToTop" array.
if (fieldsToSortToTop.includes(a) && fieldsToSortToTop.includes(b)) {
return fieldsToSortToTop.indexOf(a) - fieldsToSortToTop.indexOf(b);
}
return stringCompare(a, b);
});
}
// If there's no preference: Use the default sort function.
else {
fields.sort();
}
let constraints = Filters.FieldConstraints[schema[field].type].filter((c) => blacklist.indexOf(c) < 0);
let compareType = schema[field].type;
if (Object.prototype.hasOwnProperty.call(Filters.Constraints[constraint], 'field')) {
compareType = Filters.Constraints[constraint].field;
}
return renderRow({
fields,
constraints,
compareInfo: {
type: compareType,
targetClass: schema[field].targetClass,
},
currentField: field,
currentConstraint: constraint,
compareTo,
key: field + '-' + constraint + '-' + i,
autoFocus: i === 0,
onChangeField: newField => {
onChange(changeField(schema, filters, i, newField));
},
onChangeConstraint: (newConstraint, prevCompareTo) => {
onChange(changeConstraint(schema, filters, i, newConstraint, prevCompareTo));
},
onChangeCompareTo: newCompare => {
onChange(changeCompareTo(schema, filters, i, compareType, newCompare));
},
onKeyDown: ({key}) => {
if (key === 'Enter') {
onSearch();
}
},
onDeleteRow: () => {
onChange(deleteRow(filters, i));
}
});
})}
</div>
);
};
export default Filter;
Filter.propTypes = {
schema: PropTypes.object.isRequired.describe(
'A class schema, mapping field names to their Type strings'
),
filters: PropTypes.instanceOf(List).isRequired.describe(
'An array of filter objects. Each filter contains "field", "comparator", and "compareTo" fields.'
),
renderRow: PropTypes.func.isRequired.describe(
'A function for rendering a row of a filter.'
)
};