/
CNumberInput.vue
246 lines (232 loc) · 5.72 KB
/
CNumberInput.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<template>
<div
class="ext-growthExperiments-NumberInput"
:class="rootClasses"
:style="rootStyle"
>
<input
ref="input"
v-model="wrappedModel"
class="ext-growthExperiments-NumberInput__input"
:class="inputClasses"
v-bind="otherAttrs"
type="number"
:disabled="disabled"
@input="onInput"
@change="onChange"
@focus="onFocus"
@blur="onBlur"
>
</div>
</template>
<script>
const { toRef, computed } = require( 'vue' );
const { useModelWrapper, useSplitAttributes } = require( '@wikimedia/codex' );
/**
* HTML `<input>` element with type "number" wrapped in a `<div>`.
*
* `v-model` is used to track the current value of the input. See the events docs for details on
* emitted events and their properties.
*/
// @vue/component
module.exports = exports = {
compatConfig: { MODE: 3 },
compilerOptions: { whitespace: 'condense' },
name: 'CNumberInput',
/**
* We want the input to inherit attributes, not the root element.
*/
inheritAttrs: false,
expose: [ 'focus' ],
props: {
/**
* Current value of the input.
*
* Provided by `v-model` binding in the parent component.
*/
/* eslint-disable-next-line vue/no-unused-properties */
modelValue: {
type: Number,
default: null
},
/**
* Whether the input is disabled.
*/
disabled: {
type: Boolean,
default: false
}
},
emits: [
/**
* When the input value changes
*
* @property {number} modelValue The new model value
*/
'update:modelValue',
/**
* When the input value changes via direct use of the input
*
* @property {InputEvent} event
*/
'input',
/**
* When an input value change is committed by the user (e.g. on blur)
*
* @property {Event} event
*/
'change',
/**
* When the input comes into focus
*
* @property {FocusEvent} event
*/
'focus',
/**
* When the input loses focus
*
* @property {FocusEvent} event
*/
'blur'
],
setup( props, { emit, attrs } ) {
// Take the modelValue provided by the parent component via v-model and
// generate a wrapped model that we can use for the input element in
// this component.
const wrappedModel = useModelWrapper( toRef( props, 'modelValue' ), emit );
const internalClasses = {};
// Get helpers from useSplitAttributes.
const {
rootClasses,
rootStyle,
otherAttrs
} = useSplitAttributes( attrs, internalClasses );
const inputClasses = computed( () => {
return {
'ext-growthExperiments-NumberInput__input--has-value': !!wrappedModel.value
};
} );
// Emit other events to the parent in case they're needed.
const onInput = ( event ) => {
emit( 'input', event );
};
const onChange = ( event ) => {
emit( 'change', event );
};
const onFocus = ( event ) => {
emit( 'focus', event );
};
const onBlur = ( event ) => {
emit( 'blur', event );
};
return {
wrappedModel,
rootClasses,
rootStyle,
otherAttrs,
inputClasses,
onInput,
onChange,
onFocus,
onBlur
};
},
methods: {
/**
* Focus the component's input element.
*
* @public
*/
focus() {
this.$refs.input.focus();
}
}
};
</script>
<style lang="less">
@import 'mediawiki.skin.variables.less';
// TODO: these should be design tokens.
@font-size-browser: 16;
@font-size-base: 14 / @font-size-browser;
@line-height-component: unit( ( 20 / @font-size-browser / @font-size-base ), em );
.ext-growthExperiments-NumberInput {
position: relative;
box-sizing: @box-sizing-base;
// TODO: hide the step arrows based on prop
/* Chrome, Safari, Edge, Opera */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
/* Firefox */
input[ type='number' ] {
-moz-appearance: textfield;
}
}
.ext-growthExperiments-NumberInput__input {
display: block;
box-sizing: @box-sizing-base;
min-height: @min-size-interactive-pointer;
width: @size-full;
margin: 0;
border-width: @border-width-base;
border-style: @border-style-base;
border-radius: @border-radius-base;
padding: @spacing-25 @spacing-50;
font-family: inherit;
font-size: inherit;
line-height: @line-height-component;
&:enabled {
background-color: @background-color-base;
color: @color-base;
border-color: @border-color-base;
box-shadow: @box-shadow-inset-small @box-shadow-color-transparent;
transition-property: @transition-property-base;
transition-duration: @transition-duration-medium;
~ .ext-growthExperiments-NumberInput__icon {
color: @color-placeholder;
}
&:focus,
&.ext-growthExperiments-NumberInput__input--has-value {
~ .ext-growthExperiments-NumberInput__icon {
color: @color-base;
}
}
&:hover {
border-color: @border-color-input--hover;
}
&:focus {
border-color: @border-color-progressive--focus;
box-shadow: @box-shadow-inset-small @box-shadow-color-progressive--focus;
outline: @outline-base--focus;
}
&:invalid {
border-color: @border-color-destructive;
box-shadow: @box-shadow-inset-small @box-shadow-color-destructive--focus;
outline: @outline-base--focus;
}
}
/* stylelint-disable-next-line no-descending-specificity */
&:disabled {
background-color: @background-color-disabled-subtle;
color: @color-disabled;
-webkit-text-fill-color: @color-disabled;
border-color: @border-color-disabled;
// Don't implement coined effect on text-shadow from OOUI.
// This has never gone through design review and was a hack to increase
// color contrast.
// text-shadow: @text-shadow-base--disabled;
/* stylelint-disable-next-line no-descending-specificity */
~ .ext-growthExperiments-NumberInput__icon {
color: @color-disabled;
pointer-events: none;
}
}
// Normalize placeholder styling, see T139034.
&::placeholder {
color: @color-placeholder;
opacity: @opacity-base;
}
}
</style>