This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(StateResults): give access to search parameters (#627)
This changes the internal `connectStateResults` connector so that it exposes the state too, but without the methods. This is not a breaking change in the widget, so should be straightforward to migrate to the new usage: ```diff <ais-state-results> - <template slot-scope="{ query, hits }"> + <template slot-scope="{ results: { query, hits } }"> </template> </ais-state-results> ``` It allows you to read from the state as well, avoiding race conditions if for example hiding hits based on the query. ```diff <ais-state-results> - <div slot-scope="{ query }"> + <div slot-scope="{ state: { query } }"> <ais-hits v-if="query" /> </div> </ais-state-results> ```
- Loading branch information
Showing
7 changed files
with
389 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,40 @@ | ||
<template> | ||
<div | ||
:class="suit()" | ||
v-if="state && state.results" | ||
v-if="state && state.state && state.results" | ||
> | ||
<slot v-bind="state.results"> | ||
<p>Use this component to have a different layout based on a certain state.</p> | ||
<p>Fill in the slot, and get access to the following things on the <code>slot-scope</code>:</p> | ||
<pre>{{ Object.keys(state.results) }}</pre> | ||
<slot v-bind="stateResults"> | ||
<p> | ||
Use this component to have a different layout based on a certain state. | ||
</p> | ||
<p> | ||
Fill in the slot, and get access to the following things on the | ||
<code>slot-scope</code>: | ||
</p> | ||
<pre>results: {{ Object.keys(state.results) }}</pre> | ||
<pre>state: {{ Object.keys(state.state) }}</pre> | ||
</slot> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { createSuitMixin } from '../mixins/suit'; | ||
import { createWidgetMixin } from '../mixins/widget'; | ||
const connectStateResults = (renderFn, unmountFn) => (widgetParams = {}) => ({ | ||
init({ instantSearchInstance }) { | ||
renderFn( | ||
{ | ||
results: undefined, | ||
instantSearchInstance, | ||
widgetParams, | ||
}, | ||
true | ||
); | ||
}, | ||
render({ results, instantSearchInstance }) { | ||
const resultsCopy = Object.assign({}, results); | ||
// delete internal state, not to be exposed | ||
delete resultsCopy._state; | ||
renderFn( | ||
{ | ||
results: resultsCopy, | ||
instantSearchInstance, | ||
widgetParams, | ||
}, | ||
false | ||
); | ||
}, | ||
dispose() { | ||
unmountFn(); | ||
}, | ||
}); | ||
import { _objectSpread } from '../util/polyfills'; | ||
import connectStateResults from '../connectors/connectStateResults'; | ||
export default { | ||
name: 'AisStateResults', | ||
mixins: [ | ||
createWidgetMixin({ connector: connectStateResults }), | ||
createSuitMixin({ name: 'StateResults' }), | ||
], | ||
computed: { | ||
stateResults() { | ||
// @MAJOR: replace v-bind="stateResults" with :state="state.state" :results="state.results" | ||
const { state, results } = this.state; | ||
return _objectSpread({}, results, { results, state }); | ||
}, | ||
}, | ||
}; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.