Skip to content

Commit bb5c5b0

Browse files
committed
feat(runtime-utils): refactor mount + render helpers
1 parent 706b407 commit bb5c5b0

13 files changed

+981
-303
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<script setup lang="ts">
2+
const {
3+
none = false,
4+
show = true,
5+
label = '',
6+
} = defineProps<{
7+
none?: boolean
8+
show?: boolean
9+
label?: string
10+
}>()
11+
12+
const emit = defineEmits<{
13+
blur: []
14+
unmount: []
15+
unmounted: []
16+
}>()
17+
18+
const disabled = ref(false)
19+
const inputRef = useTemplateRef('inputRef')
20+
const modelValue = defineModel<string>({ default: '' })
21+
const counter = computed(() => disabled.value ? '' : `[${modelValue.value.length}/30]`)
22+
23+
function setValue(value: string) {
24+
modelValue.value = value
25+
}
26+
27+
function getValue() {
28+
return modelValue.value
29+
}
30+
31+
onBeforeUnmount(() => emit('unmount'))
32+
onUnmounted(() => emit('unmounted'))
33+
34+
defineExpose({
35+
disabled,
36+
setValue,
37+
getValue,
38+
})
39+
</script>
40+
41+
<template>
42+
<div
43+
v-if="!none"
44+
v-show="show"
45+
>
46+
<label
47+
for="input"
48+
:class="$style.label"
49+
>
50+
{{ label }}
51+
</label>
52+
<input
53+
id="input"
54+
ref="inputRef"
55+
v-model="modelValue"
56+
:disabled
57+
@blur="emit('blur')"
58+
>
59+
<span>{{ counter }}</span>
60+
<slot />
61+
<SomeComponent />
62+
</div>
63+
</template>
64+
65+
<style module>
66+
.label {
67+
color: red;
68+
}
69+
</style>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<script>
2+
export default defineComponent({
3+
props: {
4+
label: {
5+
type: String,
6+
default: () => '',
7+
},
8+
none: {
9+
type: Boolean,
10+
default: false,
11+
},
12+
show: {
13+
type: Boolean,
14+
default: true,
15+
},
16+
modelValue: {
17+
type: String,
18+
default: () => '',
19+
},
20+
modelModifiers: {
21+
type: Object,
22+
default: () => ({}),
23+
},
24+
},
25+
emits: ['blur', 'update:modelValue', 'unmount', 'unmounted'],
26+
expose: ['disabled', 'getValue', 'setValue'],
27+
data() {
28+
return {
29+
disabled: false,
30+
}
31+
},
32+
computed: {
33+
counter() {
34+
return this.disabled ? '' : `[${this.modelValue.length}/30]`
35+
},
36+
},
37+
beforeUnmount() {
38+
this.$emit('unmount')
39+
},
40+
unmounted() {
41+
this.$emit('unmounted')
42+
},
43+
methods: {
44+
setValue(value) {
45+
this.$emit('update:modelValue', value)
46+
},
47+
getValue() {
48+
return this.modelValue
49+
},
50+
},
51+
})
52+
</script>
53+
54+
<template>
55+
<div
56+
v-if="!none"
57+
v-show="show"
58+
>
59+
<label
60+
for="input"
61+
:class="$style.label"
62+
>
63+
{{ label }}
64+
</label>
65+
<input
66+
id="input"
67+
ref="inputRef"
68+
:value="modelValue"
69+
:disabled
70+
@blur="$emit('blur')"
71+
@input="$emit('update:modelValue', $event.target.value)"
72+
>
73+
<span>{{ counter }}</span>
74+
<slot />
75+
<SomeComponent />
76+
</div>
77+
</template>
78+
79+
<style module>
80+
.label {
81+
color: red;
82+
}
83+
</style>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<script>
2+
export default defineComponent({
3+
props: {
4+
label: {
5+
type: String,
6+
default: () => '',
7+
},
8+
none: {
9+
type: Boolean,
10+
default: false,
11+
},
12+
show: {
13+
type: Boolean,
14+
default: true,
15+
},
16+
modelValue: {
17+
type: String,
18+
default: () => '',
19+
},
20+
modelModifiers: {
21+
type: Object,
22+
default: () => ({}),
23+
},
24+
},
25+
emits: ['blur', 'update:modelValue', 'unmount', 'unmounted'],
26+
expose: ['disabled', 'getValue', 'setValue'],
27+
setup(props, { emit }) {
28+
const disabled = ref(false)
29+
30+
function setValue(value) {
31+
emit('update:modelValue', value)
32+
}
33+
34+
return {
35+
disabled,
36+
setValue,
37+
}
38+
},
39+
computed: {
40+
counter() {
41+
return this.disabled ? '' : `[${this.modelValue.length}/30]`
42+
},
43+
},
44+
beforeUnmount() {
45+
this.$emit('unmount')
46+
},
47+
unmounted() {
48+
this.$emit('unmounted')
49+
},
50+
methods: {
51+
getValue() {
52+
return this.modelValue
53+
},
54+
},
55+
})
56+
</script>
57+
58+
<template>
59+
<div
60+
v-if="!none"
61+
v-show="show"
62+
>
63+
<label
64+
for="input"
65+
:class="$style.label"
66+
>
67+
{{ label }}
68+
</label>
69+
<input
70+
id="input"
71+
ref="inputRef"
72+
:value="modelValue"
73+
:disabled
74+
@blur="$emit('blur')"
75+
@input="$emit('update:modelValue', $event.target.value)"
76+
>
77+
<span>[{{ counter }}/30]</span>
78+
<slot />
79+
<SomeComponent />
80+
</div>
81+
</template>
82+
83+
<style module>
84+
.label {
85+
color: red;
86+
}
87+
</style>

examples/app-vitest-full/components/OptionsApiComputed.vue

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
<li data-testid="object-with-get-and-set">
1010
{{ objectWithGetAndSet }}
1111
</li>
12+
<li>
13+
<button
14+
data-testid="hanlde-change-object-with-get-and-set"
15+
@click="hanldeChangeObjectWithGetAndSet"
16+
>
17+
Change
18+
</button>
19+
</li>
1220
</ul>
1321
</template>
1422

@@ -37,5 +45,10 @@ export default {
3745
},
3846
},
3947
},
48+
methods: {
49+
hanldeChangeObjectWithGetAndSet() {
50+
this.objectWithGetAndSet = `${this.objectWithGetAndSet} (changed)`
51+
},
52+
},
4053
}
4154
</script>

examples/app-vitest-full/components/WrapperTests.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ defineExpose({
2929
<button
3030
id="changeModelValue"
3131
@click="modelValue = true"
32-
/>
32+
>
33+
Change model!
34+
</button>
3335
</div>
3436
</template>

0 commit comments

Comments
 (0)