Skip to content

Commit 72bc47e

Browse files
committed
used composition api to build the component
1 parent 75ae809 commit 72bc47e

File tree

3 files changed

+34
-39
lines changed

3 files changed

+34
-39
lines changed

src/component.vue

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
@input="input"
1010
/>
1111
</template>
12-
1312
<script lang="ts">
14-
import { defineComponent } from 'vue'
13+
import { defineComponent, ref, computed } from 'vue'
1514
import { cloneDeep, CustomInputEvent, Input } from './core'
1615
import directive from './directive'
1716
import defaultOptions from './options'
@@ -30,7 +29,8 @@ export default defineComponent({
3029
},
3130
nullValue: {
3231
type: [Number, String],
33-
default: () => options.nullValue
32+
required: false,
33+
default: options.nullValue
3434
},
3535
masked: {
3636
type: Boolean,
@@ -78,42 +78,31 @@ export default defineComponent({
7878
}
7979
},
8080
emits: ['update:model-value', 'input:model-value'],
81-
data() {
82-
return {
83-
maskedValue: this.modelValue,
84-
unmaskedValue: '' as Input | undefined
81+
setup(props, { emit }) {
82+
const maskedValue = ref(props.modelValue)
83+
const unmaskedValue = ref('' as Input | undefined)
84+
const config = computed(() => ({ ...props }))
85+
86+
const emittedValue = computed(() => {
87+
return props.masked ? maskedValue.value : unmaskedValue.value
88+
})
89+
90+
const input = (event: Event) => {
91+
const { target } = event as CustomInputEvent
92+
maskedValue.value = target.value
93+
unmaskedValue.value = target.unmaskedValue
94+
emit('input:model-value', emittedValue)
8595
}
86-
},
87-
computed: {
88-
emittedValue() {
89-
return this.masked ? this.maskedValue : this.unmaskedValue
90-
},
91-
config() {
92-
return {
93-
nullValue: this.nullValue,
94-
masked: this.masked,
95-
reverseFill: this.reverseFill,
96-
prefill: this.prefill,
97-
precision: this.precision,
98-
minimumFractionDigits: this.minimumFractionDigits,
99-
decimal: this.decimal,
100-
min: this.min,
101-
max: this.max,
102-
separator: this.separator,
103-
prefix: this.prefix,
104-
suffix: this.suffix
105-
}
96+
97+
const change = () => {
98+
emit('update:model-value', emittedValue)
10699
}
107-
},
108-
methods: {
109-
input(event: Event) {
110-
const { target } = event as CustomInputEvent
111-
this.maskedValue = target.value
112-
this.unmaskedValue = target.unmaskedValue
113-
this.$emit('input:model-value', this.emittedValue)
114-
},
115-
change() {
116-
this.$emit('update:model-value', this.emittedValue)
100+
101+
return {
102+
config,
103+
maskedValue,
104+
input,
105+
change
117106
}
118107
}
119108
})

src/shims-vue.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module '*.vue' {
2+
import Vue from 'vue'
3+
export default Vue
4+
}

tsconfig.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "ES2018",
3+
"target": "ESNext",
44
"module": "ESNext",
55
"lib": ["ESNext", "DOM"],
66
"moduleResolution": "Node",
@@ -12,6 +12,8 @@
1212
"rootDir": ".",
1313
"skipLibCheck": true,
1414
"noUnusedLocals": true,
15+
"declaration": true,
16+
"declarationDir": "dist/types",
1517
"baseUrl": ".",
1618
"paths": {
1719
"@/*": [
@@ -23,5 +25,5 @@
2325
"src/**/*.ts",
2426
"src/**/*.tsx",
2527
"src/**/*.vue"
26-
],
28+
]
2729
}

0 commit comments

Comments
 (0)