Skip to content

Commit c4f0c14

Browse files
authored
DOC-12439: Added TabBar to the Example (#57)
1 parent 89f3d74 commit c4f0c14

File tree

3 files changed

+122
-4
lines changed

3 files changed

+122
-4
lines changed

src/App.tsx

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,38 @@ import Pager from "./Components/Pager";
66
import Facet from "./Components/Facet";
77
import ResultsPerPage from "./Components/ResultsPerPage";
88
import FacetBreadcrumbs from "./Components/FacetBreadcrumbs";
9-
import { loadSearchAnalyticsActions, loadSearchActions } from "@coveo/headless";
9+
import {
10+
loadSearchAnalyticsActions,
11+
loadSearchActions,
12+
buildQueryExpression,
13+
TabOptions,
14+
TabProps,
15+
} from "@coveo/headless";
1016
import headlessEngine from "./Engine";
1117
import Sort from "./Components/Sort";
1218
import { Box, Container, Grid, Typography } from "@mui/material";
19+
import Tab from "./Components/Tab";
20+
import Tabs from "@mui/material/Tabs";
1321

1422
export default class App extends React.Component {
23+
constructor(props: any) {
24+
super(props);
25+
this.state = { currentTabIndex: 0 };
26+
}
27+
1528
componentDidMount() {
1629
const { logInterfaceLoad } = loadSearchAnalyticsActions(headlessEngine);
1730
const { executeSearch } = loadSearchActions(headlessEngine);
1831

1932
headlessEngine.dispatch(executeSearch(logInterfaceLoad()));
2033
}
2134

35+
handleTabChange = (e: any, tabIndex: number) => {
36+
this.setState({
37+
currentTabIndex: tabIndex,
38+
});
39+
};
40+
2241
render() {
2342
return (
2443
<Container maxWidth="xl">
@@ -33,14 +52,26 @@ export default class App extends React.Component {
3352
Coveo Headless + Material UI
3453
</Typography>
3554
</Box>
36-
55+
<Tabs onChange={this.handleTabChange}>
56+
<Tab
57+
initialState={anyProps.initialState!}
58+
options={anyProps.options!}
59+
/>
60+
<Tab
61+
initialState={intelProps.initialState!}
62+
options={intelProps.options!}
63+
/>
64+
<Tab
65+
initialState={amdProps.initialState!}
66+
options={amdProps.options!}
67+
/>
68+
</Tabs>
3769
<SearchBox />
3870
<Box my={1}>
3971
<FacetBreadcrumbs />
4072
<Grid container>
4173
<Grid item xs={4}>
4274
<Facet title="Brand" field="ec_brand" />
43-
<Facet title="Frequencies" field="eng_frequencies" />
4475
<Facet title="Processor" field="eng_processor" />
4576
<Facet title="Store name" field="store_name" />
4677
</Grid>
@@ -71,3 +102,57 @@ export default class App extends React.Component {
71102
);
72103
}
73104
}
105+
106+
const filterIntelProcessor = buildQueryExpression()
107+
.addStringField({
108+
field: "eng_processor",
109+
operator: "contains",
110+
values: ["Intel"],
111+
})
112+
.toQuerySyntax();
113+
114+
const filterAmdProcessor = buildQueryExpression()
115+
.addStringField({
116+
field: "eng_processor",
117+
operator: "contains",
118+
values: ["AMD"],
119+
})
120+
.toQuerySyntax();
121+
122+
const noFilter = buildQueryExpression()
123+
.addStringField({
124+
field: "store_name",
125+
operator: "contains",
126+
values: ["Barca"],
127+
})
128+
.toQuerySyntax();
129+
130+
const intelOptions: TabOptions = {
131+
id: "Intel",
132+
expression: filterIntelProcessor,
133+
};
134+
135+
const amdOptions: TabOptions = {
136+
id: "AMD",
137+
expression: filterAmdProcessor,
138+
};
139+
140+
const anyOptions: TabOptions = {
141+
id: "Any",
142+
expression: noFilter,
143+
};
144+
145+
const intelProps: TabProps = {
146+
initialState: { isActive: false },
147+
options: intelOptions,
148+
};
149+
150+
const amdProps: TabProps = {
151+
initialState: { isActive: false },
152+
options: amdOptions,
153+
};
154+
155+
const anyProps: TabProps = {
156+
initialState: { isActive: true },
157+
options: anyOptions,
158+
};

src/Components/Facet.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default class Facet extends React.Component<IFacetProps, {}> {
3434

3535
this.headlessFacet = buildFacet(headlessEngine, {
3636
options: {
37-
numberOfValues: 5,
37+
numberOfValues: 3,
3838
field: this.props.field,
3939
},
4040
});

src/Components/Tab.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import headlessEngine from "../Engine";
2+
import { buildTab, TabProps, Tab as HeadlessTab } from "@coveo/headless";
3+
import React from "react";
4+
import { Tab as MaterialTab } from "@mui/material";
5+
export default class Tab extends React.Component<TabProps> {
6+
private headlessTab: HeadlessTab;
7+
constructor(props: any) {
8+
super(props);
9+
this.headlessTab = buildTab(headlessEngine, props);
10+
this.state = this.headlessTab.state;
11+
}
12+
13+
componentDidMount() {
14+
this.headlessTab.subscribe(() => this.updateState());
15+
}
16+
17+
updateState() {
18+
this.setState(this.headlessTab.state);
19+
}
20+
21+
render() {
22+
return (
23+
<MaterialTab
24+
style={{ color: "blue" }}
25+
disabled={this.headlessTab.state.isActive}
26+
onClick={() => {
27+
this.headlessTab.select();
28+
}}
29+
label={this.props.options.id}
30+
/>
31+
);
32+
}
33+
}

0 commit comments

Comments
 (0)