mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
[improvement] Actionsheet: jsx (#2444)
This commit is contained in:
parent
72c57663de
commit
f4160d7dd5
97
packages/actionsheet/index.js
Normal file
97
packages/actionsheet/index.js
Normal file
@ -0,0 +1,97 @@
|
||||
import create from '../utils/create';
|
||||
import createBem from '../utils/bem';
|
||||
import Popup from '../mixins/popup';
|
||||
|
||||
const bem = createBem('van-actionsheet');
|
||||
|
||||
export default create({
|
||||
name: 'actionsheet',
|
||||
|
||||
mixins: [Popup],
|
||||
|
||||
props: {
|
||||
title: String,
|
||||
value: Boolean,
|
||||
actions: Array,
|
||||
cancelText: String,
|
||||
overlay: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
closeOnClickOverlay: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
}
|
||||
},
|
||||
|
||||
methods: {
|
||||
onSelect(item, event) {
|
||||
event.stopPropagation();
|
||||
|
||||
if (!item.disabled && !item.loading) {
|
||||
if (item.callback) {
|
||||
item.callback(item);
|
||||
}
|
||||
|
||||
this.$emit('select', item);
|
||||
}
|
||||
},
|
||||
|
||||
onCancel() {
|
||||
this.$emit('input', false);
|
||||
this.$emit('cancel');
|
||||
}
|
||||
},
|
||||
|
||||
render(h) {
|
||||
if (!this.shouldRender) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { title, cancelText, onCancel } = this;
|
||||
|
||||
const Header = () => (
|
||||
<div class={[bem('header'), 'van-hairline--top-bottom']}>
|
||||
{title}
|
||||
<icon name="close" onClick={onCancel} />
|
||||
</div>
|
||||
);
|
||||
|
||||
const Option = item => (
|
||||
<div
|
||||
class={[
|
||||
bem('item', { disabled: item.disabled || item.loading }),
|
||||
item.className,
|
||||
'van-hairline--top'
|
||||
]}
|
||||
onClick={this.onSelect.bind(this, item)}
|
||||
>
|
||||
{item.loading ? (
|
||||
<loading class={bem('loading')} size="20px" />
|
||||
) : (
|
||||
[
|
||||
<span class={bem('name')}>{item.name}</span>,
|
||||
item.subname && <span class={bem('subname')}>{item.subname}</span>
|
||||
]
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
|
||||
const Footer = cancelText ? (
|
||||
<div class={bem('cancel')} onClick={onCancel}>
|
||||
{cancelText}
|
||||
</div>
|
||||
) : (
|
||||
<div class={bem('content')}>{this.$slots.default}</div>
|
||||
);
|
||||
|
||||
return (
|
||||
<transition name="van-slide-up">
|
||||
<div v-show={this.value} class={bem({ withtitle: title })}>
|
||||
{title ? Header() : this.actions.map(Option)}
|
||||
{Footer}
|
||||
</div>
|
||||
</transition>
|
||||
);
|
||||
}
|
||||
});
|
@ -1,107 +0,0 @@
|
||||
<template>
|
||||
<transition name="van-slide-up">
|
||||
<div
|
||||
v-if="shouldRender"
|
||||
v-show="value"
|
||||
:class="b({ 'withtitle': title })"
|
||||
>
|
||||
<div
|
||||
v-if="title"
|
||||
:class="b('header')"
|
||||
class="van-hairline--top-bottom"
|
||||
>
|
||||
<div v-text="title" />
|
||||
<icon
|
||||
name="close"
|
||||
@click="onCancel"
|
||||
/>
|
||||
</div>
|
||||
<ul
|
||||
v-else
|
||||
class="van-hairline--bottom"
|
||||
>
|
||||
<li
|
||||
v-for="item in actions"
|
||||
:class="[
|
||||
b('item', { disabled: item.disabled || item.loading }),
|
||||
item.className,
|
||||
'van-hairline--top'
|
||||
]"
|
||||
@click.stop="onSelect(item)"
|
||||
>
|
||||
<template v-if="!item.loading">
|
||||
<span
|
||||
:class="b('name')"
|
||||
v-text="item.name"
|
||||
/>
|
||||
<span
|
||||
v-if="item.subname"
|
||||
v-text="item.subname"
|
||||
:class="b('subname')"
|
||||
/>
|
||||
</template>
|
||||
<loading
|
||||
v-else
|
||||
:class="b('loading')"
|
||||
size="20px"
|
||||
/>
|
||||
</li>
|
||||
</ul>
|
||||
<div
|
||||
v-if="cancelText"
|
||||
v-text="cancelText"
|
||||
:class="[b('cancel'), 'van-hairline--top']"
|
||||
@click="onCancel"
|
||||
/>
|
||||
<div
|
||||
v-else
|
||||
:class="b('content')"
|
||||
>
|
||||
<slot />
|
||||
</div>
|
||||
</div>
|
||||
</transition>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import create from '../utils/create';
|
||||
import Popup from '../mixins/popup';
|
||||
|
||||
export default create({
|
||||
name: 'actionsheet',
|
||||
|
||||
mixins: [Popup],
|
||||
|
||||
props: {
|
||||
title: String,
|
||||
value: Boolean,
|
||||
actions: Array,
|
||||
cancelText: String,
|
||||
overlay: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
closeOnClickOverlay: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
}
|
||||
},
|
||||
|
||||
methods: {
|
||||
onSelect(item) {
|
||||
if (!item.disabled && !item.loading) {
|
||||
if (item.callback) {
|
||||
item.callback(item);
|
||||
}
|
||||
|
||||
this.$emit('select', item);
|
||||
}
|
||||
},
|
||||
|
||||
onCancel() {
|
||||
this.$emit('input', false);
|
||||
this.$emit('cancel');
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
@ -2,14 +2,8 @@
|
||||
|
||||
exports[`callback events 1`] = `
|
||||
<div class="van-actionsheet" name="van-slide-up">
|
||||
<ul class="van-hairline--bottom">
|
||||
<li class="van-actionsheet__item van-hairline--top"><span class="van-actionsheet__name">Option</span>
|
||||
<!---->
|
||||
</li>
|
||||
<li class="van-actionsheet__item van-actionsheet__item--disabled van-hairline--top"><span class="van-actionsheet__name">Option</span>
|
||||
<!---->
|
||||
</li>
|
||||
</ul>
|
||||
<div class="van-actionsheet__cancel van-hairline--top">Cancel</div>
|
||||
<div class="van-actionsheet__item van-hairline--top"><span class="van-actionsheet__name">Option</span></div>
|
||||
<div class="van-actionsheet__item van-actionsheet__item--disabled van-hairline--top"><span class="van-actionsheet__name">Option</span></div>
|
||||
<div class="van-actionsheet__cancel">Cancel</div>
|
||||
</div>
|
||||
`;
|
||||
|
@ -14,7 +14,7 @@ test('callback events', () => {
|
||||
}
|
||||
});
|
||||
|
||||
const options = wrapper.findAll('li');
|
||||
const options = wrapper.findAll('.van-actionsheet__item');
|
||||
options.at(0).trigger('click');
|
||||
options.at(1).trigger('click');
|
||||
wrapper.find('.van-actionsheet__cancel').trigger('click');
|
||||
|
@ -1,4 +1,7 @@
|
||||
import create from '../utils/create';
|
||||
import createBem from '../utils/bem';
|
||||
|
||||
const bem = createBem('van-badge-group');
|
||||
|
||||
export default create({
|
||||
name: 'badge-group',
|
||||
@ -24,7 +27,7 @@ export default create({
|
||||
|
||||
render(h) {
|
||||
return (
|
||||
<div class={['van-hairline--top-bottom', this.b()]}>
|
||||
<div class={['van-hairline--top-bottom', bem()]}>
|
||||
{this.$slots.default}
|
||||
</div>
|
||||
);
|
||||
|
@ -1,5 +1,8 @@
|
||||
import Info from '../info';
|
||||
import create from '../utils/create';
|
||||
import createBem from '../utils/bem';
|
||||
|
||||
const bem = createBem('van-badge');
|
||||
|
||||
export default create({
|
||||
name: 'badge',
|
||||
@ -45,12 +48,12 @@ export default create({
|
||||
return (
|
||||
<a
|
||||
href={this.url}
|
||||
class={[this.b({ select: this.select }), 'van-hairline']}
|
||||
class={[bem({ select: this.select }), 'van-hairline']}
|
||||
onClick={this.onClick}
|
||||
>
|
||||
<div class={this.b('text')}>
|
||||
<div class={bem('text')}>
|
||||
{this.title}
|
||||
<Info info={this.info} class={this.b('info')} />
|
||||
<Info info={this.info} class={bem('info')} />
|
||||
</div>
|
||||
</a>
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user