Skip to content

Instantly share code, notes, and snippets.

@marcusvbp
Created August 5, 2017 11:36
Show Gist options
  • Select an option

  • Save marcusvbp/430c14469e497e1fab2b1473853fd9fd to your computer and use it in GitHub Desktop.

Select an option

Save marcusvbp/430c14469e497e1fab2b1473853fd9fd to your computer and use it in GitHub Desktop.
A v-ons-select (Onsen UI + Vue) alternative
<template>
<div class="vons-select">
<v-ons-button modifier="quiet" @click="dialog = true">
<span v-if="value.length > 0">{{ value }}</span>
<span v-else>{{ label }}</span>
<span class="arrow">&dtrif;</span>
</v-ons-button>
<v-ons-alert-dialog cancelable modifier="rowfooter" :visible.sync="dialog" class="vons-select-dialog">
<span slot="title">{{ label }}</span>
<v-ons-list v-if="multiple">
<v-ons-list-item v-for="(item, index) in itemsValue" :key="index">
<label class="left">
<v-ons-checkbox
:input-id="'checkbox-' + index"
:value="item.value"
v-model="valueMultiple"
>
</v-ons-checkbox>
</label>
<label class="center" :for="'checkbox-' + index">
{{ item.value }}
</label>
</v-ons-list-item>
</v-ons-list>
<v-ons-list v-else>
<v-ons-list-item v-for="(item, index) in itemsValue" :key="index">
<label class="left">
<v-ons-radio
:input-id="'radio-' + index"
:value="item.value"
v-model="value"
>
</v-ons-radio>
</label>
<label :for="'radio-' + index" class="center">
{{ item.label }}
</label>
</v-ons-list-item>
</v-ons-list>
<template slot="footer">
<div class="alert-dialog-button">
<v-ons-button modifier="quiet" @click="save" :disabled="isDisabled">
Ok
</v-ons-button>
</div>
</template>
</v-ons-alert-dialog>
</div>
</template>
<script>
export default {
name: 'VonsSelect',
props: {
items: {
type: Array,
required: true
},
multiple: {
type: Boolean,
default: false
},
required: {
type: Boolean,
default: false
},
label: {
type: String,
required: true
}
},
data () {
return {
value: '',
valueMultiple: [],
dialog: false
}
},
computed: {
itemsValue () {
let arr = []
this.items.forEach(function (element) {
if (typeof element === 'string') {
arr.push({ label: element, value: element })
}
if (typeof element === 'object') {
arr.push(element)
}
})
return arr
},
isDisabled () {
if (this.required && this.value.length === 0 && this.valueMultiple.length === 0) {
return true
}
return false
}
},
methods: {
save () {
this.dialog = false
if (this.multiple) {
this.$emit('input', this.valueMultiple)
} else {
this.$emit('input', this.value)
}
}
}
}
</script>
<style>
.vons-select-dialog .alert-dialog-content {
border-top: 2px solid #eee;
border-bottom: 2px solid #eee;
}
.vons-select-dialog .alert-dialog-container .alert-dialog-content {
max-height: 200px;
overflow: auto;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment