diff --git a/src/number-keyboard/index.js b/src/number-keyboard/index.js index 1d6ab5735..b093afae7 100644 --- a/src/number-keyboard/index.js +++ b/src/number-keyboard/index.js @@ -1,20 +1,12 @@ -import { Teleport, Transition } from 'vue'; +import { ref, watch, computed, Teleport, Transition } from 'vue'; import { createNamespace } from '../utils'; import { stopPropagation } from '../utils/dom/event'; -import { BindEventMixin } from '../mixins/bind-event'; +import { useClickOutside } from '../composition/use-click-outside'; import Key from './Key'; const [createComponent, bem] = createNamespace('number-keyboard'); export default createComponent({ - mixins: [ - BindEventMixin(function (bind) { - if (this.hideOnClickOutside) { - bind(document.body, 'touchstart', this.onBlur); - } - }), - ], - props: { show: Boolean, title: String, @@ -67,47 +59,30 @@ export default createComponent({ 'update:modelValue', ], - watch: { - show(val) { - if (!this.transition) { - this.$emit(val ? 'show' : 'hide'); - } - }, - }, + setup(props, { emit, slots }) { + const rootRef = ref(); - computed: { - keys() { - if (this.theme === 'custom') { - return this.genCustomKeys(); - } - return this.genDefaultKeys(); - }, - }, - - methods: { - genBasicKeys() { + const genBasicKeys = () => { const keys = []; for (let i = 1; i <= 9; i++) { keys.push({ text: i }); } return keys; - }, + }; - genDefaultKeys() { - return [ - ...this.genBasicKeys(), - { text: this.extraKey, type: 'extra' }, - { text: 0 }, - { - text: this.showDeleteKey ? this.deleteButtonText : '', - type: this.showDeleteKey ? 'delete' : '', - }, - ]; - }, + const genDefaultKeys = () => [ + ...genBasicKeys(), + { text: props.extraKey, type: 'extra' }, + { text: 0 }, + { + text: props.showDeleteKey ? props.deleteButtonText : '', + type: props.showDeleteKey ? 'delete' : '', + }, + ]; - genCustomKeys() { - const keys = this.genBasicKeys(); - const { extraKey } = this; + const genCustomKeys = () => { + const keys = genBasicKeys(); + const { extraKey } = props; const extraKeys = Array.isArray(extraKey) ? extraKey : [extraKey]; if (extraKeys.length === 1) { @@ -124,47 +99,53 @@ export default createComponent({ } return keys; - }, + }; - onBlur() { - this.show && this.$emit('blur'); - }, + const keys = computed(() => + props.theme === 'custom' ? genCustomKeys() : genDefaultKeys() + ); - onClose() { - this.$emit('close'); - this.onBlur(); - }, + const onBlur = () => { + if (props.show) { + emit('blur'); + } + }; - onAnimationEnd() { - this.$emit(this.show ? 'show' : 'hide'); - }, + const onClose = () => { + emit('close'); + onBlur(); + }; - onPress(text, type) { + const onAnimationEnd = () => { + emit(props.show ? 'show' : 'hide'); + }; + + const onPress = (text, type) => { if (text === '') { if (type === 'extra') { - this.onBlur(); + onBlur(); } return; } - const value = this.modelValue; + const value = props.modelValue; if (type === 'delete') { - this.$emit('delete'); - this.$emit('update:modelValue', value.slice(0, value.length - 1)); + emit('delete'); + emit('update:modelValue', value.slice(0, value.length - 1)); } else if (type === 'close') { - this.onClose(); - } else if (value.length < this.maxlength) { - this.$emit('input', text); - this.$emit('update:modelValue', value + text); + onClose(); + } else if (value.length < props.maxlength) { + emit('input', text); + emit('update:modelValue', value + text); } - }, + }; - genTitle() { - const { title, theme, closeButtonText } = this; - const titleLeft = this.$slots['title-left']; + const renderTitle = () => { + const { title, theme, closeButtonText } = props; + const leftSlot = slots['title-left']; const showClose = closeButtonText && theme === 'default'; - const showTitle = title || showClose || titleLeft; + const showTitle = title || showClose || leftSlot; if (!showTitle) { return; @@ -172,26 +153,26 @@ export default createComponent({ return (
- {titleLeft && {titleLeft}} + {leftSlot && {leftSlot()}} {title &&

{title}

} {showClose && ( - )}
); - }, + }; - genKeys() { - return this.keys.map((key) => { + const renderKeys = () => { + return keys.value.map((key) => { const slots = {}; if (key.type === 'delete') { - slots.default = this.$slots.delete; + slots.default = slots.delete; } if (key.type === 'extra') { - slots.default = this.$slots['extra-key']; + slots.default = slots['extra-key']; } return ( @@ -202,64 +183,83 @@ export default createComponent({ type={key.type} wider={key.wider} color={key.color} - onPress={this.onPress} + onPress={onPress} /> ); }); - }, + }; - genSidebar() { - if (this.theme === 'custom') { + const renderSidebar = () => { + if (props.theme === 'custom') { return (
- {this.showDeleteKey && ( + {props.showDeleteKey && ( )}
); } - }, - }, + }; - render() { - const Title = this.genTitle(); - const Content = ( - -
- {Title} -
-
{this.genKeys()}
- {this.genSidebar()} -
-
-
+ watch( + () => props.show, + (value) => { + if (!props.transition) { + emit(value ? 'show' : 'hide'); + } + } ); - if (this.teleport) { - return {Content}; - } + useClickOutside({ + event: 'touchstart', + element: rootRef, + callback: onClose, + }); - return Content; + return () => { + const Title = renderTitle(); + const Content = ( + +
+ {Title} +
+
{renderKeys()}
+ {renderSidebar()} +
+
+
+ ); + + if (props.teleport) { + return {Content}; + } + + return Content; + }; }, });