-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFacet.tsx
161 lines (146 loc) · 3.47 KB
/
Facet.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
import React from "react";
import {
Facet as FacetType,
FacetState,
buildFacet,
FacetValue,
} from "@coveo/headless";
import headlessEngine from "../Engine";
import {
Autocomplete,
Box,
Button,
Checkbox,
FormControl,
FormControlLabel,
FormGroup,
FormLabel,
TextField,
} from "@mui/material";
export interface IFacetProps {
title: string;
field: string;
}
export default class Facet extends React.Component<IFacetProps, {}> {
private headlessFacet: FacetType;
state: FacetState & {
inputValue: "";
};
constructor(props: any) {
super(props);
this.headlessFacet = buildFacet(headlessEngine, {
options: {
numberOfValues: 3,
field: this.props.field,
},
});
this.state = {
...this.headlessFacet.state,
inputValue: "",
};
}
componentDidMount() {
this.headlessFacet.subscribe(() => this.updateState());
}
componentWillUnmount() {
this.headlessFacet.subscribe(() => {});
}
updateState() {
this.setState(this.headlessFacet.state);
}
toggleSelect(value: FacetValue) {
this.headlessFacet.toggleSelect(value);
}
showMore() {
this.headlessFacet.showMoreValues();
}
showLess() {
this.headlessFacet.showLessValues();
}
getFacetValues() {
return this.state.values.map((value: FacetValue) => (
<Box mb={1} key={value.value}>
<FormControlLabel
label={`${value.value} (${value.numberOfResults})`}
control={
<Checkbox
checked={this.headlessFacet.isValueSelected(value)}
color="secondary"
onChange={(event) => this.toggleSelect(value)}
/>
}
/>
</Box>
));
}
getFacetSearch() {
return (
<Autocomplete
inputValue={this.state.inputValue}
onInputChange={(_, newInputValue) => {
this.setState({ inputValue: newInputValue });
this.headlessFacet.facetSearch.updateText(newInputValue);
this.headlessFacet.facetSearch.search();
}}
onChange={(_, chosenValue: any) => {
if (chosenValue != null) {
this.headlessFacet.facetSearch.select(chosenValue);
}
this.setState({ inputValue: "" });
}}
options={this.state.facetSearch.values}
getOptionLabel={(option: any) => option.displayValue}
blurOnSelect
clearOnBlur
style={{ width: "auto" }}
renderInput={(params) => (
<TextField
{...params}
placeholder="Search"
variant="outlined"
size="small"
/>
)}
/>
);
}
getShowMore() {
return (
<Button
onClick={() => {
this.showMore();
}}
>
Show More
</Button>
);
}
getShowLess() {
return (
<Button
onClick={() => {
this.showLess();
}}
>
Show Less
</Button>
);
}
render() {
return (
<Box mt={5} mr={3} p={1}>
<FormControl component="fieldset">
<Box mb={1}>
<FormLabel component="legend" color="primary">
{this.props.title}
</FormLabel>
</Box>
<FormGroup>{this.getFacetValues()}</FormGroup>
</FormControl>
{this.state.canShowMoreValues && this.getFacetSearch()}
{this.state.canShowMoreValues && this.getShowMore()}
{this.state.canShowLessValues && this.getShowLess()}
</Box>
);
}
}