-
Notifications
You must be signed in to change notification settings - Fork 6
/
Facet.tsx
103 lines (92 loc) · 2.75 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
import {FunctionComponent, useEffect, useState, useContext} from 'react';
import {Facet as HeadlessFacet, buildFacet, FacetValue} from '@coveo/headless';
import Button from '@material-ui/core/Button';
import Checkbox from '@material-ui/core/Checkbox';
import Box from '@material-ui/core/Box';
import List from '@material-ui/core/List';
import './Facet.css';
import {Divider, ListItem, ListItemText, Typography} from '@material-ui/core';
import EngineContext from '../common/engineContext';
interface FacetProps {
title: string;
field: string;
}
interface FacetRendererProps extends FacetProps {
controller: HeadlessFacet;
}
const FacetRenderer: FunctionComponent<FacetRendererProps> = (props) => {
const {controller} = props;
const [state, setState] = useState(controller.state);
useEffect(
() => controller.subscribe(() => setState(controller.state)),
[controller]
);
const toggleSelect = (value: FacetValue) => {
controller.toggleSelect(value);
};
const showMore = () => {
controller.showMoreValues();
};
const showLess = () => {
controller.showLessValues();
};
return (
<Box mb={5} mr={3} p={1}>
<Box pb={1}>
<Typography variant="h6" component="h6">
{props.title}
</Typography>
</Box>
<Divider />
<List dense>
{state.values.map((value: FacetValue) => {
const labelId = `checkbox-list-label-${value}`;
return (
<ListItem
style={{padding: 0}}
key={value.value}
role={undefined}
button
onClick={() => toggleSelect(value)}
>
<Checkbox
size="small"
edge="start"
checked={controller.isValueSelected(value)}
tabIndex={-1}
disableRipple
inputProps={{'aria-labelledby': labelId}}
/>
<ListItemText
className="truncate inline"
primary={`${value.value}`}
secondary={`(${value.numberOfResults})`}
/>
</ListItem>
);
})}
</List>
{state.canShowLessValues && (
<Button size="small" onClick={() => showLess()}>
Show Less
</Button>
)}
{state.canShowMoreValues && (
<Button size="small" onClick={() => showMore()}>
Show More
</Button>
)}
</Box>
);
};
const Facet: FunctionComponent<FacetProps> = (props) => {
const engine = useContext(EngineContext)!;
const controller: HeadlessFacet = buildFacet(engine, {
options: {
numberOfValues: 5,
field: props.field,
},
});
return <FacetRenderer {...props} controller={controller} />;
};
export default Facet;