@empathyco/x-components
Version:
Empathy X Components
105 lines (79 loc) • 3.32 kB
Markdown
---
title: SortList
---
# SortList
The `SortList` component allows user to select the search results order. This component
also allows to change the selected sort programmatically.
## Props
| Name | Description | Type | Default |
| ---------------------- | --------------------------------------------- | --------------------------------- | ----------------------- |
| <code>items</code> | The list of possible sort values. | <code>Sort[]</code> | <code></code> |
| <code>animation</code> | The transition to use for rendering the list. | <code>string \| typeof Vue</code> | <code>() => 'ul'</code> |
## Slots
| Name | Description | Bindings<br />(name - type - description) |
| -------------------- | ----------- | ----------------------------------------- |
| <code>default</code> | | <br /> |
## Sort List
The `SortList` component can be used to change the way the search results are ordered.
To do so, the list of valid sort values has to be provided using the `items` prop. These are the
values that can then be received in the `SearchAdapter`.
The component also optionally accepts the selected sort, which can be set using the `value` prop.
This prop allows changing programmatically the selected sort, as it will be synced with the store
immediately. If this prop is not provided, the first item from the `items` prop will be the one
selected by default.
This component also allows customizing each one of the possible sort values. This can be done with
the `default` slot.
## Events
This component emits 2 different events:
- [`SelectedSortProvided`](https://github.com/empathyco/x/blob/main/packages/x-components/src/wiring/events.types.ts):
To sync the selected sort with the store state value. This event is emitted as soon as the list of
items is received, whenever this list changes if there is no provided value, and when the provided
value changes.
- [`UserClickedASort`](https://github.com/empathyco/x/blob/main/packages/x-components/src/wiring/events.types.ts):
As its name suggest, the event is emitted after the user clicks one of the sort options. This does
not mean that the sort has changed, only that the user has clicked it.
## Examples
### Only providing the list of items
```vue
<template>
<SortList :items="sortValues">
<template #item="{ item, isSelected }">Item: {{ item }}</template>
</SortList>
</template>
<script>
import { SortList } from '@empathyco/x-components/search'
export default {
components: {
SortList,
},
data() {
return { sortValues: ['Relevance', 'Price asc', 'Price desc'] }
},
}
</script>
```
### Providing also the selected value
```vue
<template>
<SortList v-model="selectedSort" :items="sortValues">
<template #item="{ item, isSelected }">
<span v-if="isSelected">✅</span>
{{ item }}
</template>
</SortList>
</template>
<script>
import { SortList } from '@empathyco/x-components/search'
export default {
components: {
SortList,
},
data() {
return {
selectedSort: 'Price asc',
sortValues: ['Relevance', 'Price asc', 'Price desc'],
}
},
}
</script>
```