| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- import { use } from '../utils';
- import { stopPropagation } from '../utils/event';
- import Key from './Key';
- const [sfc, bem, t] = use('number-keyboard');
- const CLOSE_KEY_TYPE = ['blue', 'big'];
- const DELETE_KEY_TYPE = ['delete', 'big', 'gray'];
- export default sfc({
- props: {
- show: Boolean,
- title: String,
- closeButtonText: String,
- deleteButtonText: String,
- safeAreaInsetBottom: Boolean,
- theme: {
- type: String,
- default: 'default'
- },
- extraKey: {
- type: String,
- default: ''
- },
- zIndex: {
- type: Number,
- default: 100
- },
- transition: {
- type: Boolean,
- default: true
- },
- showDeleteKey: {
- type: Boolean,
- default: true
- },
- hideOnClickOutside: {
- type: Boolean,
- default: true
- }
- },
- mounted() {
- this.handler(true);
- },
- destroyed() {
- this.handler(false);
- },
- activated() {
- this.handler(true);
- },
- deactivated() {
- this.handler(false);
- },
- watch: {
- show() {
- if (!this.transition) {
- this.$emit(this.show ? 'show' : 'hide');
- }
- }
- },
- computed: {
- keys() {
- const keys = [];
- for (let i = 1; i <= 9; i++) {
- keys.push({ text: i });
- }
- switch (this.theme) {
- case 'default':
- keys.push(
- { text: this.extraKey, type: ['gray'] },
- { text: 0 },
- { text: this.deleteText, type: ['gray', 'delete'] }
- );
- break;
- case 'custom':
- keys.push({ text: 0, type: ['middle'] }, { text: this.extraKey });
- break;
- }
- return keys;
- },
- deleteText() {
- return this.deleteButtonText || t('delete');
- }
- },
- methods: {
- handler(action) {
- /* istanbul ignore if */
- if (this.$isServer) {
- return;
- }
- if (action !== this.handlerStatus && this.hideOnClickOutside) {
- this.handlerStatus = action;
- document.body[(action ? 'add' : 'remove') + 'EventListener']('touchstart', this.onBlur);
- }
- },
- onBlur() {
- this.$emit('blur');
- },
- onClose() {
- this.$emit('close');
- this.onBlur();
- },
- onAnimationEnd() {
- this.$emit(this.show ? 'show' : 'hide');
- },
- onPress(text) {
- if (text === '') {
- return;
- }
- if (text === this.deleteText) {
- this.$emit('delete');
- } else if (text === this.closeButtonText) {
- this.onClose();
- } else {
- this.$emit('input', text);
- }
- }
- },
- render(h) {
- const { title, theme, onPress, closeButtonText } = this;
- const titleLeftSlot = this.slots('title-left');
- const showTitleClose = closeButtonText && theme === 'default';
- const showTitle = title || showTitleClose || titleLeftSlot;
- return (
- <transition name={this.transition ? 'van-slide-up' : ''}>
- <div
- vShow={this.show}
- style={{ zIndex: this.zIndex }}
- class={bem([theme, { 'safe-area-inset-bottom': this.safeAreaInsetBottom }])}
- onTouchstart={stopPropagation}
- onAnimationend={this.onAnimationEnd}
- onWebkitAnimationEnd={this.onAnimationEnd}
- >
- {showTitle && (
- <div class={[bem('title'), 'van-hairline--top']}>
- {titleLeftSlot && (
- <span class={bem('title-left')}>
- {titleLeftSlot}
- </span>
- )}
- {title && <span>{title}</span>}
- {showTitleClose && (
- <span class={bem('close')} onClick={this.onClose}>
- {closeButtonText}
- </span>
- )}
- </div>
- )}
- <div class={bem('body')}>
- {this.keys.map(key => (
- <Key key={key.text} text={key.text} type={key.type} onPress={onPress} />
- ))}
- </div>
- {theme === 'custom' && (
- <div class={bem('sidebar')}>
- <Key text={this.deleteText} type={DELETE_KEY_TYPE} onPress={onPress} />
- <Key text={closeButtonText} type={CLOSE_KEY_TYPE} onPress={onPress} />
- </div>
- )}
- </div>
- </transition>
- );
- }
- });
|