Skip to content

Commit 44096c0

Browse files
francoischalifourbobylito
authored andcommitted
chore(doc): Add guide "Create a Calendar Widget" (#2687)
* docs(guide): add Calendar Widget Guide * docs(header): add the Calendar Widget Guide to the header * docs(examples): add Calendar Widget demo * chore(guide): update guide * chore(guide): update terminal screenshot * chore(demo): fix dates in Safari * chore(demo): add link to source code and zip * chore: add link to dataset * chore(docs): remove `name` attribute
1 parent abb01f1 commit 44096c0

File tree

7 files changed

+935
-0
lines changed

7 files changed

+935
-0
lines changed

docgen/src/data/communityHeader.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@
7676
"name": "Create new widgets",
7777
"url": "guides/custom-widget.html"
7878
},
79+
{
80+
"name": "Create a calendar widget",
81+
"url": "guides/calendar-widget.html"
82+
},
7983
{
8084
"name": "Migrate from V1",
8185
"url": "guides/migration.html"
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const ONE_DAY_IN_MS = 3600 * 24 * 1000;
2+
3+
const search = instantsearch({
4+
appId: 'latency',
5+
apiKey: '059c79ddd276568e990286944276464a',
6+
indexName: 'concert_events_instantsearchjs',
7+
});
8+
9+
search.addWidget(
10+
instantsearch.widgets.searchBox({
11+
container: '#search-box',
12+
placeholder: 'Search events',
13+
})
14+
);
15+
16+
search.addWidget(
17+
instantsearch.widgets.hits({
18+
container: '#hits',
19+
templates: {
20+
item: hit => `
21+
<li class="hit">
22+
<h3>
23+
${hit._highlightResult.name.value}
24+
<small>in ${hit._highlightResult.location.value}</small>
25+
</h3>
26+
<small>on <strong>${moment(hit.date).format(
27+
'dddd MMMM Do YYYY'
28+
)}</strong></small>
29+
</li>
30+
`,
31+
},
32+
})
33+
);
34+
35+
const makeRangeWidget = instantsearch.connectors.connectRange(
36+
(options, isFirstRendering) => {
37+
if (!isFirstRendering) return;
38+
39+
const { refine } = options;
40+
41+
new Calendar({
42+
element: $('#calendar'),
43+
same_day_range: true,
44+
callback: function() {
45+
const start = new Date(this.start_date).getTime();
46+
const end = new Date(this.end_date).getTime();
47+
const actualEnd = start === end ? end + ONE_DAY_IN_MS - 1 : end;
48+
49+
refine([start, actualEnd]);
50+
},
51+
// Some good parameters based on our dataset:
52+
start_date: new Date(),
53+
end_date: new Date('01/01/2020'),
54+
earliest_date: new Date('01/01/2008'),
55+
latest_date: new Date('01/01/2020'),
56+
});
57+
}
58+
);
59+
60+
const dateRangeWidget = makeRangeWidget({
61+
attributeName: 'date',
62+
});
63+
64+
search.addWidget(dateRangeWidget);
65+
66+
search.start();

0 commit comments

Comments
 (0)