Skip to content

Commit 437befb

Browse files
implementing v-model for spinbox (nodegui#22)
* implementing v-model for spinbox * updating vn-spinbox docs 📚 * using setup API instead of data
1 parent a5b33b6 commit 437befb

File tree

6 files changed

+50
-6
lines changed

6 files changed

+50
-6
lines changed

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export { createApp } from './renderer';
22
export { vModelText } from './renderer/directives/vModelText';
33
export { vModelSlider } from './renderer/directives/vModelSlider';
4+
export { vModelSpinBox } from './renderer/directives/vModelSpinBox';
45
export { vShow } from './renderer/directives/vShow';
56

67
export * from '@vue/runtime-core';
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { ObjectDirective } from '@vue/runtime-core';
2+
import { VNSpinBox } from 'widgets/SpinBox/VNSpinBox';
3+
4+
type ModelDirective<T> = ObjectDirective<T & { _assign: Function }>
5+
6+
export const vModelSpinBox: ModelDirective<VNSpinBox> = {
7+
beforeMount: (el, { value }, vnode) => {
8+
el.setValue(value);
9+
// eslint-disable-next-line no-param-reassign, no-underscore-dangle
10+
el._assign = vnode.props!['onUpdate:modelValue'] as Function;
11+
el.addEventListener('valueChanged', (spinBoxValue) => {
12+
// eslint-disable-next-line no-underscore-dangle
13+
el._assign(spinBoxValue);
14+
});
15+
},
16+
beforeUpdate: (el, { value, oldValue }) => {
17+
if (value === oldValue) {
18+
return;
19+
}
20+
el.setValue(value);
21+
},
22+
};

src/vueLoader.ts

+5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import { isNativeWidget } from './widgets/nativeWidget';
33

44
const V_MODEL_TEXT = Symbol('vModelText');
55
const V_MODEL_SLIDER = Symbol('vModelSlider');
6+
const V_MODEL_SPINBOX = Symbol('vModelSpinBox');
67

78
registerRuntimeHelpers({
89
[V_MODEL_TEXT]: 'vModelText',
910
[V_MODEL_SLIDER]: 'vModelSlider',
11+
[V_MODEL_SPINBOX]: 'vModelSpinBox',
1012
});
1113

1214
type CompilerOptions = {
@@ -30,6 +32,9 @@ export const compilerOptions: CompilerOptions = {
3032
case 'vn-slider':
3133
directiveToUse = V_MODEL_SLIDER;
3234
break;
35+
case 'vn-spinbox':
36+
directiveToUse = V_MODEL_SPINBOX;
37+
break;
3338
default:
3439
throw new Error(`cannot use v-model on tag: ${tag}`);
3540
}

src/widgets/SpinBox/VNSpinBox.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,21 @@ import { PropSetters, Prop } from '../../renderer/patchProp';
1212
* ```html
1313
* <template>
1414
* <vn-view>
15-
* <vn-text>How many slices of cake would you like?</vn-text>
16-
* <vn-spinbox />
15+
* <vn-text>How many slices of cake ($2) would you like?</vn-text>
16+
* <vn-spinbox v-model="spinBoxValue"/>
17+
* <vn-text>Total cost: ${{spinBoxValue * 2}}</vn-text>
1718
* </vn-view>
1819
* </template>
1920
*
2021
* <script>
21-
* export default { }
22+
* import { ref } from 'vue';
23+
*
24+
* export default {
25+
* setup() {
26+
* const spinBoxValue = ref(0)
27+
* return { spinBoxValue }
28+
* }
29+
* }
2230
* </script>
2331
* ```
2432
*

website/docs/api/interfaces/spinboxprops.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,21 @@ It is based on [NodeGui's QSpinBox](https://docs.nodegui.org/docs/api/generated/
1212
```html
1313
<template>
1414
<vn-view>
15-
<vn-text>How many slices of cake would you like?</vn-text>
16-
<vn-spinbox />
15+
<vn-text>How many slices of cake ($2) would you like?</vn-text>
16+
<vn-spinbox v-model="spinBoxValue"/>
17+
<vn-text>Total cost: ${{spinBoxValue * 2}}</vn-text>
1718
</vn-view>
1819
</template>
1920

2021
<script>
21-
export default { }
22+
import { ref } from 'vue';
23+
24+
export default {
25+
setup() {
26+
const spinBoxValue = ref(0)
27+
return { spinBoxValue }
28+
}
29+
}
2230
</script>
2331
```
2432

website/static/img/vn-spinbox.gif

76.3 KB
Loading

0 commit comments

Comments
 (0)