Skip to content

Commit

Permalink
feat(core): deprecate addWidget & removeWidget (#4131)
Browse files Browse the repository at this point in the history
* feat(core): deprecate addWidget & removeWidget

Migration guide already mentions this since dc75d2f, discussed with @samouss

* Apply suggestions from code review

Co-Authored-By: François Chalifour <francoischalifour@users.noreply.github.com>
Co-Authored-By: Samuel Vaillant <samuel.vllnt@gmail.com>

* docs: use only plural form

* Update src/lib/InstantSearch.ts

Co-Authored-By: Samuel Vaillant <samuel.vllnt@gmail.com>

* chore: fix typo

* track warning message
  • Loading branch information
Haroenv committed Oct 23, 2019
1 parent 7eb6810 commit e5dafef
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 49 deletions.
4 changes: 2 additions & 2 deletions MIGRATION.md
Expand Up @@ -165,13 +165,13 @@ The `indices` option has been removed, in favour of using `index` widgets (see t
```js
const autocomplete = connectAutocomplete(() => {/* ... */});

search.addWidget(
search.addWidgets([
autocomplete({
indices: [{
name: "additional"
}]
})
);
]);
```

Will be replaced with this:
Expand Down
20 changes: 8 additions & 12 deletions README.md
Expand Up @@ -59,31 +59,27 @@ const search = instantsearch({
searchClient: algoliasearch('latency', '6be0576ff61c053d5f9a3225e2a90f76'),
});

// 2. Create an interactive search box
search.addWidget(
search.addWidgets([
// 2. Create an interactive search box
instantsearch.widgets.searchBox({
container: '#searchbox',
placeholder: 'Search for products',
})
);
}),

// 3. Plug the search results into the product container
search.addWidget(
// 3. Plug the search results into the product container
instantsearch.widgets.hits({
container: '#products',
templates: {
item: '{{#helpers.highlight}}{ "attribute": "name" }{{/helpers.highlight}}',
},
})
);
}),

// 4. Make the brands refinable
search.addWidget(
// 4. Make the brands refinable
instantsearch.widgets.refinementList({
container: '#brand',
attribute: 'brand',
})
);
}),
]);

// 5. Start the search!
search.start();
Expand Down
44 changes: 25 additions & 19 deletions src/lib/InstantSearch.ts
Expand Up @@ -264,23 +264,24 @@ See ${createDocumentationLink({
}

/**
* Adds a widget. This can be done before and after InstantSearch has been started. Adding a
* widget after InstantSearch started is considered **EXPERIMENTAL** and therefore
* it is possibly buggy, if you find anything please
* [open an issue](https://github.com/algolia/instantsearch.js/issues/new?title=Problem%20with%20hot%20addWidget).
* @param widget The widget to add to InstantSearch. Widgets are simple objects
* that have methods that map the search life cycle in a UI perspective. Usually widgets are
* created by [widget factories](widgets.html) like the one provided with InstantSearch.js.
* Adds a widget to the search instance.
* A widget can be added either before or after InstantSearch has started.
* @param widget The widget to add to InstantSearch.
*
* @deprecated This method will still be supported in 4.x releases, but not further. It is replaced by `addWidgets([widget])`.
*/
public addWidget(widget: Widget) {
warning(
false,
'addWidget will still be supported in 4.x releases, but not further. It is replaced by `addWidgets([widget])`'
);
this.addWidgets([widget]);
}

/**
* Adds multiple widgets. This can be done before and after the InstantSearch has been started. This feature
* is considered **EXPERIMENTAL** and therefore it is possibly buggy, if you find anything please
* [open an issue](https://github.com/algolia/instantsearch.js/issues/new?title=Problem%20with%20addWidgets).
* @param {Widget[]} widgets The array of widgets to add to InstantSearch.
* Adds multiple widgets to the search instance.
* Widgets can be added either before or after InstantSearch has started.
* @param widgets The array of widgets to add to InstantSearch.
*/
public addWidgets(widgets: Widget[]) {
if (!Array.isArray(widgets)) {
Expand Down Expand Up @@ -309,20 +310,25 @@ See ${createDocumentationLink({
}

/**
* Removes a widget. This can be done after the InstantSearch has been started. This feature
* is considered **EXPERIMENTAL** and therefore it is possibly buggy, if you find anything please
* [open an issue](https://github.com/algolia/instantsearch.js/issues/new?title=Problem%20with%20removeWidget).
* @param {Widget} widget The widget instance to remove from InstantSearch. This widget must implement a `dispose()` method in order to be gracefully removed.
* Removes a widget from the search instance.
* @deprecated This method will still be supported in 4.x releases, but not further. It is replaced by `removeWidgets([widget])`
* @param widget The widget instance to remove from InstantSearch.
*
* The widget must implement a `dispose()` method to clear its state.
*/
public removeWidget(widget: Widget) {
warning(
false,
'removeWidget will still be supported in 4.x releases, but not further. It is replaced by `removeWidgets([widget])`'
);
this.removeWidgets([widget]);
}

/**
* Removes multiple widgets. This can be done only after the InstantSearch has been started. This feature
* is considered **EXPERIMENTAL** and therefore it is possibly buggy, if you find anything please
* [open an issue](https://github.com/algolia/instantsearch.js/issues/new?title=Problem%20with%20addWidgets).
* @param {Widget[]} widgets Array of widgets instances to remove from InstantSearch.
* Removes multiple widgets from the search instance.
* @param widgets Array of widgets instances to remove from InstantSearch.
*
* The widgets must implement a `dispose()` method to clear their states.
*/
public removeWidgets(widgets: Widget[]) {
if (!Array.isArray(widgets)) {
Expand Down
36 changes: 20 additions & 16 deletions src/lib/__tests__/InstantSearch-test.js
Expand Up @@ -280,7 +280,7 @@ See https://www.algolia.com/doc/api-reference/widgets/configure/js/`);
});

describe('addWidget(s)', () => {
it('forwards the call to `addWidget` to the main index', () => {
it('forwards the call of `addWidget` to the main index', () => {
const searchClient = createSearchClient();
const search = new InstantSearch({
indexName: 'indexName',
Expand All @@ -289,12 +289,14 @@ describe('addWidget(s)', () => {

expect(search.mainIndex.getWidgets()).toHaveLength(0);

search.addWidget(createWidget());
expect(() => search.addWidget(createWidget())).toWarnDev(
'[InstantSearch.js]: addWidget will still be supported in 4.x releases, but not further. It is replaced by `addWidgets([widget])`'
);

expect(search.mainIndex.getWidgets()).toHaveLength(1);
});

it('forwards the call to `addWidgets` to the main index', () => {
it('forwards the call of `addWidgets` to the main index', () => {
const searchClient = createSearchClient();
const search = new InstantSearch({
indexName: 'indexName',
Expand All @@ -319,11 +321,13 @@ describe('removeWidget(s)', () => {

const widget = createWidget();

search.addWidget(widget);
search.addWidgets([widget]);

expect(search.mainIndex.getWidgets()).toHaveLength(1);

search.removeWidget(widget);
expect(() => search.removeWidget(widget)).toWarnDev(
'[InstantSearch.js]: removeWidget will still be supported in 4.x releases, but not further. It is replaced by `removeWidgets([widget])`'
);

expect(search.mainIndex.getWidgets()).toHaveLength(0);
});
Expand Down Expand Up @@ -481,7 +485,7 @@ describe('start', () => {

const widget = createWidget();

search.addWidget(widget);
search.addWidgets([widget]);

search.start();

Expand Down Expand Up @@ -778,7 +782,7 @@ describe('scheduleSearch', () => {
searchClient: createSearchClient(),
});

search.addWidget(createWidget());
search.addWidgets([createWidget()]);

search.start();

Expand All @@ -799,7 +803,7 @@ describe('scheduleSearch', () => {
searchClient: createSearchClient(),
});

search.addWidget(createWidget());
search.addWidgets([createWidget()]);

search.start();

Expand Down Expand Up @@ -827,7 +831,7 @@ describe('scheduleRender', () => {

const widget = createWidget();

search.addWidget(widget);
search.addWidgets([widget]);

search.start();

Expand All @@ -846,7 +850,7 @@ describe('scheduleRender', () => {

const widget = createWidget();

search.addWidget(widget);
search.addWidgets([widget]);

search.start();

Expand All @@ -870,7 +874,7 @@ describe('scheduleRender', () => {

const widget = createWidget();

search.addWidget(widget);
search.addWidgets([widget]);

search.start();

Expand All @@ -893,7 +897,7 @@ describe('scheduleStalledRender', () => {

const widget = createWidget();

search.addWidget(widget);
search.addWidgets([widget]);

search.start();

Expand Down Expand Up @@ -926,7 +930,7 @@ describe('scheduleStalledRender', () => {

const widget = createWidget();

search.addWidget(widget);
search.addWidgets([widget]);

search.start();

Expand Down Expand Up @@ -962,7 +966,7 @@ describe('scheduleStalledRender', () => {

const widget = createWidget();

search.addWidget(widget);
search.addWidgets([widget]);

expect(widget.render).toHaveBeenCalledTimes(0);

Expand Down Expand Up @@ -1061,7 +1065,7 @@ describe('createURL', () => {
},
});

search.addWidget(connectSearchBox(noop)({}));
search.addWidgets([connectSearchBox(noop)({})]);
search.start();

expect(search.createURL({ indexName: { query: 'Apple' } })).toBe(
Expand All @@ -1084,7 +1088,7 @@ describe('createURL', () => {
},
});

search.addWidget(connectSearchBox(noop)({}));
search.addWidgets([connectSearchBox(noop)({})]);
search.start();
search.createURL();

Expand Down

0 comments on commit e5dafef

Please sign in to comment.