Make dropdown filter reusable

This commit is contained in:
mattwr18 2019-11-01 10:13:32 +01:00
parent 780a418254
commit ddb9fcdcb7
3 changed files with 40 additions and 26 deletions

View File

@ -1,16 +1,16 @@
import { storiesOf } from '@storybook/vue' import { storiesOf } from '@storybook/vue'
import { withA11y } from '@storybook/addon-a11y' import { withA11y } from '@storybook/addon-a11y'
import { action } from '@storybook/addon-actions' import { action } from '@storybook/addon-actions'
import NotificationsDropdownFilter from '~/components/NotificationsDropdownFilter/NotificationsDropdownFilter' import DropdownFilter from '~/components/DropdownFilter/DropdownFilter'
import helpers from '~/storybook/helpers' import helpers from '~/storybook/helpers'
helpers.init() helpers.init()
storiesOf('NotificationsDropdownFilter', module) storiesOf('DropdownFilter', module)
.addDecorator(withA11y) .addDecorator(withA11y)
.addDecorator(helpers.layout) .addDecorator(helpers.layout)
.add('filter dropdown', () => ({ .add('filter dropdown', () => ({
components: { NotificationsDropdownFilter }, components: { DropdownFilter },
methods: { methods: {
filterNotifications: action('filterNotifications'), filterNotifications: action('filterNotifications'),
}, },

View File

@ -1,14 +1,16 @@
<template> <template>
<dropdown offset="8"> <dropdown offset="8">
<a <a
:v-model="selected"
slot="default" slot="default"
slot-scope="{ toggleMenu }" slot-scope="{ toggleMenu }"
class="locale-menu" name="dropdown"
class="dropdown-filter"
href="#" href="#"
@click.prevent="toggleMenu()" @click.prevent="toggleMenu()"
> >
<ds-icon style="margin-right: 2px;" name="filter" /> <ds-icon style="margin-right: 2px;" name="filter" />
{{ selected }} <label for="dropdown">{{ selected }}</label>
<ds-icon style="margin-left: 2px" size="xx-small" name="angle-down" /> <ds-icon style="margin-left: 2px" size="xx-small" name="angle-down" />
</a> </a>
<ds-menu <ds-menu
@ -37,26 +39,25 @@ export default {
components: { components: {
Dropdown, Dropdown,
}, },
data() { props: {
return { selected: { type: String, default: '' },
selected: this.$t('notifications.filterLabel.all'), filterOptions: { type: Array, default: () => [] },
}
},
computed: {
filterOptions() {
return [
{ label: this.$t('notifications.filterLabel.all'), value: null },
{ label: this.$t('notifications.filterLabel.read'), value: true },
{ label: this.$t('notifications.filterLabel.unread'), value: false },
]
},
}, },
methods: { methods: {
filterNotifications(option, toggleMenu) { filterNotifications(option, toggleMenu) {
this.$emit('filterNotifications', option.value) this.$emit('filterNotifications', option)
this.selected = option.label
toggleMenu() toggleMenu()
}, },
}, },
} }
</script> </script>
<style lang="scss">
.dropdown-filter {
user-select: none;
display: flex;
align-items: center;
height: 100%;
padding: $space-xx-small;
color: $text-color-soft;
}
</style>

View File

@ -4,9 +4,13 @@
<ds-flex-item :width="{ lg: '85%' }"> <ds-flex-item :width="{ lg: '85%' }">
<ds-heading tag="h3">{{ $t('notifications.title') }}</ds-heading> <ds-heading tag="h3">{{ $t('notifications.title') }}</ds-heading>
</ds-flex-item> </ds-flex-item>
<ds-flex-item width="100px"> <ds-flex-item width="110px">
<client-only> <client-only>
<notifications-dropdown-filter @filterNotifications="filterNotifications" /> <dropdown-filter
@filterNotifications="filterNotifications"
:filterOptions="filterOptions"
:selected="selected"
/>
</client-only> </client-only>
</ds-flex-item> </ds-flex-item>
</ds-flex> </ds-flex>
@ -21,13 +25,13 @@
<script> <script>
import NotificationsTable from '~/components/NotificationsTable/NotificationsTable' import NotificationsTable from '~/components/NotificationsTable/NotificationsTable'
import NotificationsDropdownFilter from '~/components/NotificationsDropdownFilter/NotificationsDropdownFilter' import DropdownFilter from '~/components/DropdownFilter/DropdownFilter'
import Paginate from '~/components/Paginate/Paginate' import Paginate from '~/components/Paginate/Paginate'
import { notificationQuery, markAsReadMutation } from '~/graphql/User' import { notificationQuery, markAsReadMutation } from '~/graphql/User'
export default { export default {
components: { components: {
NotificationsDropdownFilter, DropdownFilter,
NotificationsTable, NotificationsTable,
Paginate, Paginate,
}, },
@ -40,16 +44,25 @@ export default {
pageSize, pageSize,
first: pageSize, first: pageSize,
hasNext: false, hasNext: false,
selected: this.$t('notifications.filterLabel.all'),
} }
}, },
computed: { computed: {
hasPrevious() { hasPrevious() {
return this.offset > 0 return this.offset > 0
}, },
filterOptions() {
return [
{ label: this.$t('notifications.filterLabel.all'), value: null },
{ label: this.$t('notifications.filterLabel.read'), value: true },
{ label: this.$t('notifications.filterLabel.unread'), value: false },
]
},
}, },
methods: { methods: {
filterNotifications(value) { filterNotifications(option) {
this.notificationRead = value this.notificationRead = option.value
this.selected = option.label
this.$apollo.queries.notifications.refresh() this.$apollo.queries.notifications.refresh()
}, },
async markNotificationAsRead(notificationSourceId) { async markNotificationAsRead(notificationSourceId) {