diff --git a/src/_base/index.js b/src/_base/index.js deleted file mode 100644 index 18cd08e88..000000000 --- a/src/_base/index.js +++ /dev/null @@ -1,12 +0,0 @@ -export { default as NIconSwitchTransition } from './icon-switch-transition' -export { default as NFadeInExpandTransition } from './fade-in-expand-transition' -export { default as NBaseClose } from './close' -export { default as NBaseIcon } from './icon' -export { default as NBaseFocusDetector } from './focus-detector' -export { default as NBaseLoading } from './loading' -export { default as NBaseSelectMenu } from './select-menu' -export { default as NBaseWave } from './wave' -export { default as NBaseMenuMask } from './menu-mask' -export { default as NBaseSelection } from './selection' -export { default as NBaseSlotMachine } from './slot-machine' -export { default as NBaseClear } from './clear' diff --git a/src/_base/slot-machine/index.js b/src/_base/slot-machine/index.js deleted file mode 100644 index cd3bbe25f..000000000 --- a/src/_base/slot-machine/index.js +++ /dev/null @@ -1 +0,0 @@ -export { default } from './src/SlotMachine.vue' diff --git a/src/_base/slot-machine/index.ts b/src/_base/slot-machine/index.ts new file mode 100644 index 000000000..51c1ceef3 --- /dev/null +++ b/src/_base/slot-machine/index.ts @@ -0,0 +1 @@ +export { default } from './src/SlotMachine' diff --git a/src/_base/slot-machine/src/SlotMachine.tsx b/src/_base/slot-machine/src/SlotMachine.tsx new file mode 100644 index 000000000..d1f49298d --- /dev/null +++ b/src/_base/slot-machine/src/SlotMachine.tsx @@ -0,0 +1,101 @@ +import { + h, + defineComponent, + TransitionGroup, + computed, + ref, + watch, + toRef, + Ref +} from 'vue' +import NFadeInExpandTransition from '../../fade-in-expand-transition' +import { useStyle } from '../../../_mixins' +import SlotMachineNumber from './SlotMachineNumber' +import style from './styles/index.cssr' + +export default defineComponent({ + name: 'BaseSlotMachine', + components: { + NFadeInExpandTransition, + SlotMachineNumber + }, + props: { + value: { + type: [Number, String], + default: 0 + }, + max: { + type: Number, + default: undefined + }, + appeared: { + type: Boolean, + required: true + } + }, + setup (props) { + useStyle('BaseSlotMachine', style) + const oldValueRef = ref() + const newValueRef = ref() + const numbersRef = computed(() => { + if (typeof props.value === 'string') return [] + if (props.value < 1) return [0] + const numbers = [] + let value = props.value + if (props.max !== undefined) { + value = Math.min(props.max, value) + } + while (value >= 1) { + numbers.push(value % 10) + value /= 10 + value = Math.floor(value) + } + numbers.reverse() + return numbers + }) + watch(toRef(props, 'value') as Ref, (value, oldValue) => { + if (typeof value === 'string') { + newValueRef.value = undefined + oldValueRef.value = undefined + } else { + if (typeof oldValue === 'string') { + newValueRef.value = value + oldValueRef.value = undefined + } else { + newValueRef.value = value + oldValueRef.value = oldValue + } + } + }) + return () => { + const { value } = props + return typeof value === 'number' ? ( + + + {{ + default: () => + numbersRef.value.map((number, i) => ( + + )) + }} + + + {{ + default: () => + props.max !== undefined && props.max < value ? ( + + ) : null + }} + + + ) : ( + {value} + ) + } + } +}) diff --git a/src/_base/slot-machine/src/SlotMachine.vue b/src/_base/slot-machine/src/SlotMachine.vue deleted file mode 100644 index 939bcc1f1..000000000 --- a/src/_base/slot-machine/src/SlotMachine.vue +++ /dev/null @@ -1,87 +0,0 @@ - - - diff --git a/src/_base/slot-machine/src/SlotMachineNumber.tsx b/src/_base/slot-machine/src/SlotMachineNumber.tsx new file mode 100644 index 000000000..845e1fb60 --- /dev/null +++ b/src/_base/slot-machine/src/SlotMachineNumber.tsx @@ -0,0 +1,116 @@ +import { + defineComponent, + h, + nextTick, + ref, + computed, + PropType, + watch, + toRef, + Ref +} from 'vue' + +export default defineComponent({ + name: 'SlotMachineNumber', + props: { + value: { + // could be '+', 1, 2, ... + type: [Number, String] as PropType, + required: true + }, + oldOriginalNumber: { + type: Number, + default: undefined + }, + newOriginalNumber: { + type: Number, + default: undefined + } + }, + setup (props) { + const numberRef = ref(null) + const oldNumberRef = ref(props.value) + const newNumberRef = ref(props.value) + const scrollAnimationDirectionRef = ref<'up' | 'down'>('up') + const activeRef = ref(false) + const newNumberScrollAnimationClassRef = computed(() => { + return activeRef.value + ? `n-base-slot-machine-current-number--${scrollAnimationDirectionRef.value}-scroll` + : null + }) + const oldNumberScrollAnimationClassRef = computed(() => { + return activeRef.value + ? `n-base-slot-machine-old-number--${scrollAnimationDirectionRef.value}-scroll` + : null + }) + // BUG: may be typescript bug + watch(toRef(props, 'value') as Ref, (value, oldValue) => { + oldNumberRef.value = oldValue + newNumberRef.value = value + void nextTick(scroll) + }) + function scroll (): void { + const newOriginalNumber = props.newOriginalNumber + const oldOriginalNumber = props.oldOriginalNumber + if (oldOriginalNumber === undefined || newOriginalNumber === undefined) { return } + if (newOriginalNumber > oldOriginalNumber) { + scrollByDir('up') + } else if (oldOriginalNumber > newOriginalNumber) { + scrollByDir('down') + } + } + function scrollByDir (dir: 'up' | 'down'): void { + scrollAnimationDirectionRef.value = dir + activeRef.value = false + void nextTick(() => { + void numberRef.value?.offsetWidth + activeRef.value = true + }) + } + return () => { + return ( + + {oldNumberRef.value !== null ? ( + + {oldNumberRef.value} + + ) : null} + + + {newNumberRef.value} + + + {oldNumberRef.value !== null ? ( + + {oldNumberRef.value} + + ) : null} + + ) + } + } +}) diff --git a/src/_base/slot-machine/src/SlotMachineNumber.vue b/src/_base/slot-machine/src/SlotMachineNumber.vue deleted file mode 100644 index f92c0ca9e..000000000 --- a/src/_base/slot-machine/src/SlotMachineNumber.vue +++ /dev/null @@ -1,104 +0,0 @@ - - - diff --git a/src/_base/slot-machine/src/styles/index.cssr.js b/src/_base/slot-machine/src/styles/index.cssr.ts similarity index 100% rename from src/_base/slot-machine/src/styles/index.cssr.js rename to src/_base/slot-machine/src/styles/index.cssr.ts diff --git a/src/alert/src/styles/index.cssr.ts b/src/alert/src/styles/index.cssr.ts index 9a68cb1d7..0635fd6e4 100644 --- a/src/alert/src/styles/index.cssr.ts +++ b/src/alert/src/styles/index.cssr.ts @@ -55,8 +55,7 @@ export default cB('alert', ` width: 26px; height: 26px; font-size: 26px; - ` - ), + `), cE('close', ` transition: color .3s var(--bezier); position: absolute;