mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-24 18:36:51 +08:00
refactor(ShareSheet): use setup
This commit is contained in:
parent
48ff0f85fb
commit
8f208f3d4f
@ -1,11 +1,18 @@
|
|||||||
// Utils
|
// Utils
|
||||||
import { createNamespace, isDef } from '../utils';
|
import { createNamespace, isDef, pick } from '../utils';
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
import Popup, { popupSharedProps } from '../popup';
|
import Popup, { popupSharedProps } from '../popup';
|
||||||
|
|
||||||
const PRESET_ICONS = ['qq', 'weibo', 'wechat', 'link', 'qrcode', 'poster'];
|
const PRESET_ICONS = ['qq', 'weibo', 'wechat', 'link', 'qrcode', 'poster'];
|
||||||
|
|
||||||
|
function getIconURL(icon) {
|
||||||
|
if (PRESET_ICONS.indexOf(icon) !== -1) {
|
||||||
|
return `https://img.yzcdn.cn/vant/share-icon-${icon}.png`;
|
||||||
|
}
|
||||||
|
return icon;
|
||||||
|
}
|
||||||
|
|
||||||
const [createComponent, bem, t] = createNamespace('share-sheet');
|
const [createComponent, bem, t] = createNamespace('share-sheet');
|
||||||
|
|
||||||
export default createComponent({
|
export default createComponent({
|
||||||
@ -28,121 +35,106 @@ export default createComponent({
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
emits: ['cancel', 'select', 'update:show', 'click-overlay'],
|
emits: ['cancel', 'select', 'update:show'],
|
||||||
|
|
||||||
methods: {
|
setup(props, { emit, slots }) {
|
||||||
onCancel() {
|
const toggle = (value) => {
|
||||||
this.toggle(false);
|
emit('update:show', value);
|
||||||
this.$emit('cancel');
|
};
|
||||||
},
|
|
||||||
|
|
||||||
onSelect(option, index) {
|
const onCancel = () => {
|
||||||
this.$emit('select', option, index);
|
toggle(false);
|
||||||
},
|
emit('cancel');
|
||||||
|
};
|
||||||
|
|
||||||
toggle(val) {
|
const onSelect = (option, index) => {
|
||||||
this.$emit('update:show', val);
|
emit('select', option, index);
|
||||||
},
|
};
|
||||||
|
|
||||||
getIconURL(icon) {
|
const renderHeader = () => {
|
||||||
if (PRESET_ICONS.indexOf(icon) !== -1) {
|
const title = slots.title ? slots.title() : props.title;
|
||||||
return `https://img.yzcdn.cn/vant/share-icon-${icon}.png`;
|
const description = slots.description
|
||||||
}
|
? slots.description()
|
||||||
|
: props.description;
|
||||||
return icon;
|
|
||||||
},
|
|
||||||
|
|
||||||
genHeader() {
|
|
||||||
const title = this.$slots.title ? this.$slots.title() : this.title;
|
|
||||||
const description = this.$slots.description
|
|
||||||
? this.$slots.description()
|
|
||||||
: this.description;
|
|
||||||
|
|
||||||
if (!title && !description) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (title || description) {
|
||||||
return (
|
return (
|
||||||
<div class={bem('header')}>
|
<div class={bem('header')}>
|
||||||
{title && <h2 class={bem('title')}>{title}</h2>}
|
{title && <h2 class={bem('title')}>{title}</h2>}
|
||||||
{description && <span class={bem('description')}>{description}</span>}
|
{description && (
|
||||||
|
<span class={bem('description')}>{description}</span>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
},
|
}
|
||||||
|
};
|
||||||
|
|
||||||
genOptions(options, showBorder) {
|
const renderOption = (option, index) => {
|
||||||
|
const { name, icon, className, description } = option;
|
||||||
return (
|
return (
|
||||||
<div class={bem('options', { border: showBorder })}>
|
|
||||||
{options.map((option, index) => (
|
|
||||||
<div
|
<div
|
||||||
role="button"
|
role="button"
|
||||||
tabindex="0"
|
tabindex="0"
|
||||||
class={[bem('option'), option.className]}
|
class={[bem('option'), className]}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
this.onSelect(option, index);
|
onSelect(option, index);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<img src={this.getIconURL(option.icon)} class={bem('icon')} />
|
<img src={getIconURL(icon)} class={bem('icon')} />
|
||||||
{option.name && <span class={bem('name')}>{option.name}</span>}
|
{name && <span class={bem('name')}>{name}</span>}
|
||||||
{option.description && (
|
{description && (
|
||||||
<span class={bem('option-description')}>
|
<span class={bem('option-description')}>{description}</span>
|
||||||
{option.description}
|
|
||||||
</span>
|
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
);
|
);
|
||||||
},
|
};
|
||||||
|
|
||||||
genRows() {
|
const renderOptions = (options, border) => (
|
||||||
const { options } = this;
|
<div class={bem('options', { border })}>{options.map(renderOption)}</div>
|
||||||
|
);
|
||||||
|
|
||||||
|
const renderRows = () => {
|
||||||
|
const { options } = props;
|
||||||
if (Array.isArray(options[0])) {
|
if (Array.isArray(options[0])) {
|
||||||
return options.map((item, index) => this.genOptions(item, index !== 0));
|
return options.map((item, index) => renderOptions(item, index !== 0));
|
||||||
}
|
}
|
||||||
return this.genOptions(options);
|
return renderOptions(options);
|
||||||
},
|
};
|
||||||
|
|
||||||
genCancelText() {
|
const renderCancelText = () => {
|
||||||
const cancelText = isDef(this.cancelText) ? this.cancelText : t('cancel');
|
const text = isDef(props.cancelText) ? props.cancelText : t('cancel');
|
||||||
|
if (text) {
|
||||||
if (cancelText) {
|
|
||||||
return (
|
return (
|
||||||
<button type="button" class={bem('cancel')} onClick={this.onCancel}>
|
<button type="button" class={bem('cancel')} onClick={onCancel}>
|
||||||
{cancelText}
|
{text}
|
||||||
</button>
|
</button>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
|
|
||||||
onClickOverlay() {
|
return () => (
|
||||||
this.$emit('click-overlay');
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
render() {
|
|
||||||
return (
|
|
||||||
<Popup
|
<Popup
|
||||||
round
|
round
|
||||||
show={this.show}
|
|
||||||
class={bem()}
|
class={bem()}
|
||||||
position="bottom"
|
position="bottom"
|
||||||
overlay={this.overlay}
|
|
||||||
duration={this.duration}
|
|
||||||
lazyRender={this.lazyRender}
|
|
||||||
lockScroll={this.lockScroll}
|
|
||||||
teleport={this.teleport}
|
|
||||||
closeOnPopstate={this.closeOnPopstate}
|
|
||||||
closeOnClickOverlay={this.closeOnClickOverlay}
|
|
||||||
safeAreaInsetBottom={this.safeAreaInsetBottom}
|
|
||||||
{...{
|
{...{
|
||||||
'onUpdate:show': this.toggle,
|
...pick(props, [
|
||||||
'onClick-overlay': this.onClickOverlay,
|
'show',
|
||||||
|
'overlay',
|
||||||
|
'duration',
|
||||||
|
'teleport',
|
||||||
|
'lazyRender',
|
||||||
|
'lockScroll',
|
||||||
|
'closeOnPopstate',
|
||||||
|
'closeOnClickOverlay',
|
||||||
|
'safeAreaInsetBottom',
|
||||||
|
]),
|
||||||
|
'onUpdate:show': toggle,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{this.genHeader()}
|
{renderHeader()}
|
||||||
{this.genRows()}
|
{renderRows()}
|
||||||
{this.genCancelText()}
|
{renderCancelText()}
|
||||||
</Popup>
|
</Popup>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user