-
Notifications
You must be signed in to change notification settings - Fork 4
/
AdvancedSearchBooleanFilter.tsx
153 lines (133 loc) · 3.72 KB
/
AdvancedSearchBooleanFilter.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
import * as classNames from "classnames";
import * as React from "react";
import { ConnectDropTarget, useDrop } from "react-dnd";
import { AdvancedSearchQuery } from "../interfaces";
import AdvancedSearchFilter from "./AdvancedSearchFilter";
export interface AdvancedSearchBooleanFilterProps {
query: AdvancedSearchQuery;
readOnly?: boolean;
selectedQueryId?: string;
onBooleanChange: (id: string, bool: string) => void;
onMove: (id: string, targetId: string) => void;
onSelect: (id: string) => void;
onRemove: (id: string) => void;
}
const renderSeparator = (query, index) => {
if (index < 1) {
return null;
}
return <span>{query.and ? "and" : "or"}</span>;
};
export default function AdvancedSearchBooleanFilter({
query,
readOnly,
selectedQueryId,
onBooleanChange,
onMove,
onSelect,
onRemove,
}: AdvancedSearchBooleanFilterProps) {
const children = query && (query.and || query.or);
if (!children) {
return null;
}
const handleBooleanChange = (
event: React.SyntheticEvent<HTMLSelectElement>
) => {
const bool = event.currentTarget.value;
if (!query[bool]) {
onBooleanChange?.(query.id, event.currentTarget.value);
}
};
const handleClick = (event: React.SyntheticEvent) => {
event.stopPropagation();
event.preventDefault();
onSelect?.(query.id);
};
const handleKeyDown = (event: React.KeyboardEvent) => {
event.stopPropagation();
event.preventDefault();
if (event.key === " ") {
onSelect?.(query.id);
}
};
const handleRemoveButtonClick = (event: React.SyntheticEvent) => {
event.stopPropagation();
event.preventDefault();
onRemove?.(query.id);
};
const [dropProps, drop]: [
{ canDrop: boolean; isOver: boolean },
ConnectDropTarget
] = useDrop(
{
accept: "filter",
canDrop: (item: any) => item.id !== query.id,
drop: (item: any, monitor) => {
if (!monitor.didDrop()) {
onMove(item.id, query.id);
return {
id: query.id,
};
}
},
collect: (monitor) => ({
canDrop: !!monitor.canDrop(),
isOver: !!monitor.isOver({ shallow: true }),
}),
},
[query.id, onMove]
);
const isSelected = !readOnly && selectedQueryId === query.id;
const className = classNames({
"advanced-search-boolean-filter": true,
"drag-drop": dropProps.isOver && dropProps.canDrop,
selected: isSelected,
});
return (
<div
aria-selected={isSelected}
className={className}
onClick={handleClick}
onKeyDown={handleKeyDown}
ref={readOnly ? undefined : drop}
role="treeitem"
tabIndex={0}
>
<header>
<div>
<select
disabled={readOnly}
onBlur={handleBooleanChange}
onChange={handleBooleanChange}
value={query.and ? "and" : "or"}
>
<option aria-selected={!!query.and} value="and">
All of these filters must be matched:
</option>
<option aria-selected={!!query.or} value="or">
Any of these filters may be matched:
</option>
</select>
</div>
{!readOnly && <button onClick={handleRemoveButtonClick}>×</button>}
</header>
<ul>
{children.map((child, index) => (
<li key={child.id}>
{renderSeparator(query, index)}{" "}
<AdvancedSearchFilter
query={child}
readOnly={readOnly}
selectedQueryId={selectedQueryId}
onBooleanChange={onBooleanChange}
onMove={onMove}
onSelect={onSelect}
onRemove={onRemove}
/>
</li>
))}
</ul>
</div>
);
}