mand-mobile/components/selector/index.vue

222 lines
4.5 KiB
Vue
Raw Normal View History

2018-03-26 16:04:04 +08:00
<template>
<div
class="md-selector"
:class="{
'is-normal': !isCheck,
2018-09-22 22:20:10 +08:00
'is-check': isCheck
2018-03-26 16:04:04 +08:00
}"
>
<md-popup
v-model="isSelectorShow"
position="bottom"
:mask-closable="maskClosable"
@show="$_onSelectorShow"
@hide="$_onSelectorHide"
@maskClick="$_onSelectorCancel"
2018-03-26 16:04:04 +08:00
>
<md-popup-title-bar
:title="title"
2018-11-20 11:19:34 +08:00
:describe="describe"
2018-03-26 16:04:04 +08:00
:ok-text="okText"
:cancel-text="cancelText"
@confirm="$_onSelectorConfirm"
@cancel="$_onSelectorCancel"
2018-08-20 20:15:49 +08:00
>
<md-icon
v-if="!isCheck && !isNeedConfirm && !cancelText"
2018-10-15 16:22:21 +08:00
name="close"
2018-08-20 20:15:49 +08:00
size="lg"
slot="cancel"
></md-icon>
</md-popup-title-bar>
<md-scroll-view
ref="scroll"
class="md-selector-container"
:scrolling-x="false"
:style="{maxHeight: `${maxHeight}px`}"
>
2018-09-22 22:20:10 +08:00
<md-radio-list
class="md-selector-list"
ref="radio"
:key="radioKey"
:value="defaultValue"
:options="data"
:is-slot-scope="hasSlot"
2018-09-22 22:20:10 +08:00
icon="right"
icon-inverse=""
icon-position="right"
icon-size="md"
@change="$_onSelectorChoose"
>
<template slot-scope="{ option }">
<slot :option="option"></slot>
</template>
</md-radio-list>
</md-scroll-view>
2018-03-26 16:04:04 +08:00
</md-popup>
</div>
</template>
2018-08-20 20:15:49 +08:00
<script> import Icon from '../icon'
import Popup from '../popup'
2018-03-26 16:04:04 +08:00
import PopupTitlebar from '../popup/title-bar'
2018-09-22 22:20:10 +08:00
import RadioList from '../radio-list'
import ScrollView from '../scroll-view'
2018-03-26 16:04:04 +08:00
export default {
name: 'md-selector',
components: {
2018-08-20 20:15:49 +08:00
[Icon.name]: Icon,
2018-09-22 22:20:10 +08:00
[RadioList.name]: RadioList,
2018-03-26 16:04:04 +08:00
[Popup.name]: Popup,
[PopupTitlebar.name]: PopupTitlebar,
[ScrollView.name]: ScrollView,
2018-03-26 16:04:04 +08:00
},
props: {
value: {
type: Boolean,
default: false,
},
data: {
type: Array,
default() {
return []
},
},
2018-09-22 22:20:10 +08:00
defaultValue: {
type: String,
default: '',
2018-03-26 16:04:04 +08:00
},
title: {
type: String,
default: '',
},
2018-11-20 11:19:34 +08:00
describe: {
type: String,
default: '',
},
2018-03-26 16:04:04 +08:00
okText: {
type: String,
default: '',
},
cancelText: {
type: String,
2018-08-20 20:15:49 +08:00
default() {
return this.okText ? '取消' : ''
},
2018-03-26 16:04:04 +08:00
},
maskClosable: {
type: Boolean,
default: true,
},
2018-03-26 16:04:04 +08:00
isCheck: {
type: Boolean,
default: false,
},
maxHeight: {
type: Number,
default: 400,
},
2018-03-26 16:04:04 +08:00
},
data() {
return {
2018-09-22 22:20:10 +08:00
isSelectorShow: this.value,
2018-03-26 16:04:04 +08:00
radioKey: Date.now(),
activeIndex: -1,
tmpActiveIndex: -1,
}
},
computed: {
isNeedConfirm() {
return this.okText !== ''
},
hasSlot() {
return !!this.$scopedSlots.default
},
},
watch: {
value(val) {
this.isSelectorShow = val
},
isSelectorShow(val) {
this.$emit('input', val)
},
},
methods: {
// MARK: private methods
$_setScroller() {
this.$refs.scroll.reflowScroller()
},
2018-03-26 16:04:04 +08:00
// MARK: events handler
$_onSelectorConfirm() {
if (this.tmpActiveIndex > -1) {
this.activeIndex = this.tmpActiveIndex
this.isSelectorShow = false
this.$emit('confirm', this.data[this.activeIndex])
}
},
$_onSelectorCancel() {
this.isSelectorShow = false
this.tmpActiveIndex = this.activeIndex
if (this.tmpActiveIndex !== -1) {
this.$refs.radio.selectByIndex(this.tmpActiveIndex)
2018-03-26 16:04:04 +08:00
} else {
// reset radio
this.radioKey = Date.now()
}
this.$emit('cancel')
},
$_onSelectorChoose(item, index) {
this.tmpActiveIndex = index
if (!this.isNeedConfirm) {
this.activeIndex = index
this.isSelectorShow = false
}
this.$emit('choose', item)
},
$_onSelectorShow() {
/* istanbul ignore next */
this.$_setScroller()
this.$emit('show')
},
$_onSelectorHide() {
/* istanbul ignore next */
this.$emit('hide')
},
2018-03-26 16:04:04 +08:00
},
}
</script>
<style lang="stylus">
.md-selector
.md-popup
2018-09-22 22:20:10 +08:00
z-index selector-zindex
.md-popup-title-bar .md-popup-cancel
.md-icon
align-self flex-start
margin-left h-gap-lg
.md-selector-container
padding-left h-gap-sl
padding-right h-gap-sl
padding-bottom constant(safe-area-inset-bottom)
overflow hidden
.md-selector
2018-03-26 16:04:04 +08:00
&.is-normal
2018-09-22 22:20:10 +08:00
.md-radio-item
text-align center
.md-cell-item-left,
.md-cell-item-right
display none
2018-03-26 16:04:04 +08:00
</style>