-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathinput-number.tsx
More file actions
53 lines (50 loc) · 2.01 KB
/
input-number.tsx
File metadata and controls
53 lines (50 loc) · 2.01 KB
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
import { defineComponent, toRefs, watch, inject } from 'vue';
import type { SetupContext } from 'vue';
import { inputNumberProps, InputNumberProps } from './input-number-types';
import { IncIcon, DecIcon } from './input-number-icons';
import { useRender, useEvent, useExpose } from './use-input-number';
import './input-number.scss';
import { FORM_ITEM_TOKEN, FormItemContext } from '@devui/shared/components/form';
export default defineComponent({
name: 'DInputNumber',
props: inputNumberProps,
emits: ['update:modelValue', 'change', 'input'],
setup(props: InputNumberProps, ctx: SetupContext) {
const { disabled } = toRefs(props);
const { wrapClass, customStyle, otherAttrs, controlButtonsClass, inputWrapClass, inputInnerClass } = useRender(props, ctx);
const { inputRef } = useExpose(ctx);
const { inputVal, minDisabled, maxDisabled, onAdd, onSubtract, onInput, onChange } = useEvent(props, ctx, inputRef);
const formItemContext = inject(FORM_ITEM_TOKEN, undefined) as FormItemContext;
watch(
() => props.modelValue,
() => {
formItemContext?.validate('change').catch(() => {});
}
);
return () => (
<div class={wrapClass.value} {...customStyle}>
<div class={controlButtonsClass.value}>
<span class={['control-button control-inc', { disabled: maxDisabled.value }]} onClick={onAdd}>
<IncIcon />
</span>
<span class={['control-button control-dec', { disabled: minDisabled.value }]} onClick={onSubtract}>
<DecIcon />
</span>
</div>
<div class={inputWrapClass.value}>
<input
ref={inputRef}
value={inputVal.value}
placeholder={props.placeholder}
disabled={disabled.value}
class={inputInnerClass.value}
{...otherAttrs}
onInput={onInput}
onChange={onChange}
onBlur={() => formItemContext?.validate('blur').catch(() => {})}
/>
</div>
</div>
);
},
});