/
textarea.vue
183 lines (161 loc) · 4.85 KB
/
textarea.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
<!-- eslint-disable padded-blocks -->
<script setup lang="ts">
import type { CSSProperties } from 'vue'
import type * as CSS from 'csstype'
import { computed, defineComponent, ref, toRef } from 'vue'
import type { TextareaConfirmType, TextareaOnConfirmEvent } from '@uni-helper/uni-app-types'
import { getMainClass, isH5, isMpAlipay, pxCheck } from '../_utils'
import { BLUR_EVENT, CHANGE_EVENT, CONFIRM_EVENT, FOCUS_EVENT, PREFIX, UPDATE_MODEL_EVENT } from '../_constants'
import { useTranslate } from '../../locale'
import { useFormDisabled } from '../form/form'
import { textareaEmits, textareaProps } from './textarea'
export interface InputTarget extends HTMLInputElement {
composing?: boolean
}
const props = defineProps(textareaProps)
const emit = defineEmits(textareaEmits)
const disabled = useFormDisabled(toRef(props, 'disabled'))
const classes = computed(() => {
return getMainClass(props, componentName, {
[`${componentName}--disabled`]: disabled.value,
})
})
const styles = computed<CSSProperties>(() => {
const styleObj: CSSProperties = {
textAlign: props.textAlign as CSS.TextAlignProperty,
}
if (typeof props.autosize === 'object') {
const { minHeight, maxHeight } = props.autosize
if (minHeight != null)
styleObj.minHeight = pxCheck(minHeight)
if (maxHeight != null)
styleObj.maxHeight = pxCheck(maxHeight)
}
return styleObj
})
function emitChange(value: string, event: Event) {
if (props.maxLength && value.length > Number(props.maxLength))
value = value.substring(0, Number(props.maxLength))
emit(UPDATE_MODEL_EVENT, value, event)
emit(CHANGE_EVENT, value, event)
}
function change(event: any) {
if (isH5) {
if (!composing.value)
_onInput(event)
}
else {
_onInput(event)
}
}
function _onInput(event: any) {
const input = event.detail as HTMLInputElement
let value = input.value
if (props.maxLength && value.length > Number(props.maxLength))
value = value.slice(0, Number(props.maxLength))
emitChange(value, event)
}
function focus(event: any) {
if (disabled.value)
return
if (props.readonly)
return
emit(FOCUS_EVENT, event)
}
function blur(event: any) {
if (disabled.value)
return
if (props.readonly)
return
const input = event.detail as HTMLInputElement
const value = input.value
emitChange(value, event)
emit(BLUR_EVENT, event)
}
function confirm(event: TextareaOnConfirmEvent) {
emit(CONFIRM_EVENT, event)
}
const composing = ref(false)
function startComposing(event: Event) {
if (isH5)
composing.value = true
}
function endComposing({ target }: Event) {
if (isH5) {
if (composing.value) {
composing.value = false;
(target as InputTarget).dispatchEvent(new Event('input'))
}
}
}
</script>
<script lang="ts">
const componentName = `${PREFIX}-textarea`
const { translate } = useTranslate(componentName)
export default defineComponent({
name: componentName,
inheritAttrs: false,
options: {
virtualHost: true,
addGlobalClass: true,
styleIsolation: 'shared',
},
})
</script>
<template>
<view :class="classes" :style="customStyle">
<textarea
v-if="props.readonly"
class="nut-textarea__textarea nut-textarea__textarea__readonly"
:class="{ 'nut-textarea__ali': isMpAlipay }"
:style="styles"
:value="modelValue"
:rows="rows"
:disabled="true"
:show-count="false"
:placeholder="placeholder || translate('placeholder')"
:auto-height="!!autosize"
:disable-default-padding="disableDefaultPadding"
/>
<textarea
v-else
class="nut-textarea__textarea"
:class="{ 'nut-textarea__ali': isMpAlipay }"
:style="styles"
:value="modelValue"
:rows="rows"
:disabled="disabled || readonly"
:show-count="false"
:maxlength="maxLength ? +maxLength : -1"
:placeholder="placeholder || translate('placeholder')"
:placeholder-style="placeholderStyle"
:placeholder-class="placeholderClass"
:auto-focus="autofocus"
:auto-height="!!autosize"
:cursor-spacing="cursorSpacing"
:cursor="cursor"
:show-confirm-bar="showConfirmBar"
:selection-start="selectionStart"
:selection-end="selectionEnd"
:adjust-position="adjustPosition"
:hold-keyboard="holdKeyboard"
:disable-default-padding="disableDefaultPadding"
:confirm-type="confirmType as TextareaConfirmType"
:confirm-hold="confirmHold"
:adjust-keyboard-to="adjustKeyboardTo"
@confirm="confirm"
@input="change"
@blur="blur"
@focus="focus"
@change="(endComposing as any)"
@compositionend="(endComposing as any)"
@compositionstart="(startComposing as any)"
/>
<view v-if="limitShow" class="nut-textarea__limit">
{{ modelValue ? modelValue.length : 0 }}/{{ maxLength }}
</view>
</view>
</template>
<style lang="scss">
@import './index';
</style>