input.uvue 13.9 KB
Newer Older
Y
init  
yurj26 已提交
1
<template>
张磊 已提交
2 3 4 5 6 7 8 9 10
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1">
  <!-- #endif -->
    <page-head :title="title"></page-head>
    <view class="uni-common-mt uni-padding-wrap">
      <view>
        <view class="uni-title">
          <text class="uni-title-text">设置输入框的初始内容</text>
        </view>
雪洛's avatar
雪洛 已提交
11
        <view class="input-wrapper">
12
          <input id="uni-input-default" class="uni-input" value="hello uni-app x" />
张磊 已提交
13 14
        </view>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
15

张磊 已提交
16 17 18 19
      <view>
        <view class="uni-title">
          <text class="uni-title-text">type取值(不同输入法表现可能不一致)</text>
        </view>
雪洛's avatar
雪洛 已提交
20
        <view class="input-wrapper">
21
          <input id="uni-input-type-text" class="uni-input" type="text" placeholder="文本输入键盘" />
张磊 已提交
22
        </view>
雪洛's avatar
雪洛 已提交
23
        <view class="input-wrapper">
24
          <input id="uni-input-type-number" class="uni-input" type="number" placeholder="数字输入键盘" />
张磊 已提交
25
        </view>
雪洛's avatar
雪洛 已提交
26
        <view class="input-wrapper">
27
          <input id="uni-input-type-digit" class="uni-input" type="digit" placeholder="带小数点的数字输入键盘" />
张磊 已提交
28
        </view>
雪洛's avatar
雪洛 已提交
29
        <view class="input-wrapper">
30
          <input id="uni-input-type-tel" class="uni-input" :type="inputTypeTel" placeholder="电话输入键盘" />
张磊 已提交
31 32
        </view>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
33

张磊 已提交
34 35 36 37
      <view>
        <view class="uni-title">
          <text class="uni-title-text">密码输入框</text>
        </view>
雪洛's avatar
雪洛 已提交
38
        <view class="input-wrapper">
39
          <input id="uni-input-password" class="uni-input" :password="inputPassword" :value="inputPasswordValue" />
张磊 已提交
40 41
        </view>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
42

张磊 已提交
43 44 45 46
      <view>
        <view class="uni-title">
          <text class="uni-title-text">占位符样式</text>
        </view>
雪洛's avatar
雪洛 已提交
47
        <view class="input-wrapper">
H
hdx 已提交
48 49
          <input id="uni-input-placeholder1" class="uni-input" :placeholder-style="inputPlaceHolderStyle"
            placeholder="占位符文字颜色为红色" />
张磊 已提交
50
        </view>
雪洛's avatar
雪洛 已提交
51
        <view class="input-wrapper">
H
hdx 已提交
52 53
          <input id="uni-input-placeholder2" class="uni-input" :placeholder-class="inputPlaceHolderClass"
            placeholder="占位符字体大小为10px" />
54 55 56 57 58
        </view>
        <view class="input-wrapper">
          <input class="uni-input" :placeholder-style="inputPlaceHolderStyle"
            value="不设置placeholder只设置placeholder-style" />
        </view>
张磊 已提交
59
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
60

张磊 已提交
61 62 63 64
      <view>
        <view class="uni-title">
          <text class="uni-title-text">设置禁用输入框</text>
        </view>
雪洛's avatar
雪洛 已提交
65
        <view class="input-wrapper">
66
          <input id="uni-input-disable" class="uni-input" :disabled="true"/>
张磊 已提交
67 68
        </view>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
69

张磊 已提交
70 71 72 73
      <view>
        <view class="uni-title">
          <text class="uni-title-text">设置最大输入长度</text>
        </view>
雪洛's avatar
雪洛 已提交
74
        <view class="input-wrapper">
H
hdx 已提交
75 76
          <input id="uni-input-maxlength" class="uni-input" :maxlength="10" placeholder="最大输入长度为10"
            :value="inputMaxLengthValue" @input="onMaxLengthInput" :focus="inputMaxLengthFocus" />
张磊 已提交
77 78
        </view>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
79

张磊 已提交
80 81 82 83
      <view>
        <view class="uni-title">
          <text class="uni-title-text">设置光标与键盘的距离</text>
        </view>
雪洛's avatar
雪洛 已提交
84
        <view class="input-wrapper">
张磊 已提交
85 86
          <input class="uni-input" :cursor-spacing="1000" placeholder="光标与键盘的距离为1000px" />
        </view>
雪洛's avatar
雪洛 已提交
87 88
      </view>

张磊 已提交
89 90 91 92
      <view>
        <view class="uni-title">
          <text class="uni-title-text">自动获取焦点</text>
        </view>
雪洛's avatar
雪洛 已提交
93
        <view class="input-wrapper">
H
hdx 已提交
94 95
          <input id="uni-input-focus" class="uni-input" :focus="focus"
            @keyboardheightchange="inputFocusKeyBoardChange" />
张磊 已提交
96 97
        </view>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
98

张磊 已提交
99 100 101 102
      <view>
        <view class="uni-title">
          <text class="uni-title-text">confirm-type取值(不同输入法表现可能不一致)</text>
        </view>
雪洛's avatar
雪洛 已提交
103
        <view class="input-wrapper">
104
          <input id="uni-input-confirm-send" class="uni-input" confirmType="send" placeholder="键盘右下角按钮显示为发送" />
张磊 已提交
105
        </view>
雪洛's avatar
雪洛 已提交
106
        <view class="input-wrapper">
107
          <input id="uni-input-confirm-search" class="uni-input" confirmType="search" placeholder="键盘右下角按钮显示为搜索" />
张磊 已提交
108
        </view>
雪洛's avatar
雪洛 已提交
109
        <view class="input-wrapper">
110
          <input id="uni-input-confirm-next" class="uni-input" confirmType="next" placeholder="键盘右下角按钮显示为下一个" />
张磊 已提交
111
        </view>
雪洛's avatar
雪洛 已提交
112
        <view class="input-wrapper">
113
          <input id="uni-input-confirm-go" class="uni-input" confirmType="go" placeholder="键盘右下角按钮显示为前往" />
张磊 已提交
114
        </view>
雪洛's avatar
雪洛 已提交
115
        <view class="input-wrapper">
116
          <input id="uni-input-confirm-done" class="uni-input" confirmType="done" placeholder="键盘右下角按钮显示为完成" />
张磊 已提交
117 118
        </view>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
119

张磊 已提交
120 121 122 123
      <view>
        <view class="uni-title">
          <text class="uni-title-text">点击键盘右下角按钮时保持键盘不收起</text>
        </view>
雪洛's avatar
雪洛 已提交
124
        <view class="input-wrapper">
张磊 已提交
125 126 127
          <input class="uni-input" :confirm-hold="true" />
        </view>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
128

张磊 已提交
129 130 131 132
      <view>
        <view class="uni-title" @click="setCursor(4)">
          <text class="uni-title-text">设置输入框聚焦时光标的位置(点击生效)</text>
        </view>
雪洛's avatar
雪洛 已提交
133
        <view class="input-wrapper">
134
          <input ref="input" class="uni-input" value="0123456789" :cursor="cursor" @blur="onCursorBlurChange"/>
张磊 已提交
135 136
        </view>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
137

张磊 已提交
138 139 140 141
      <view>
        <view class="uni-title" @click="setSelection(0, 4)">
          <text class="uni-title-text">设置输入框聚焦时光标的起始位置和结束位置(点击生效)</text>
        </view>
雪洛's avatar
雪洛 已提交
142
        <view class="input-wrapper">
H
hdx 已提交
143
          <input ref="input2" class="uni-input" value="0123456789" :selection-start="selectionStart"
144
            :selection-end="selectionEnd" @blur="onSelectionBlurChange"/>
张磊 已提交
145
        </view>
雪洛's avatar
雪洛 已提交
146 147 148
      </view>

      <view>
149
        <view class="uni-title" style="flex-direction: row;align-items: center;">
H
hdx 已提交
150
          <text class="uni-title-text">设置光标颜色为红色</text>
151
          <switch style="margin-left: 10px;" @change="changeCursorColor" :checked="cursor_color=='red'"></switch>
雪洛's avatar
雪洛 已提交
152 153 154 155
        </view>
        <view class="input-wrapper">
          <input id="uni-input-cursor-color" class="uni-input" :cursor-color="cursor_color" value="光标颜色" />
        </view>
张磊 已提交
156
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
157

张磊 已提交
158 159 160 161
      <view>
        <view class="uni-title">
          <text class="uni-title-text">键盘弹起时,自动上推页面</text>
        </view>
雪洛's avatar
雪洛 已提交
162
        <view class="input-wrapper">
张磊 已提交
163 164
          <input class="uni-input" :adjust-position="true" />
        </view>
H
hdx 已提交
165 166 167 168 169 170 171 172 173
      </view>


      <view>
        <view class="uni-title" style="flex-direction: row;align-items: center;">
          <text class="uni-title-text">设置hold-keyboard</text>
          <switch style="margin-left: 10px;" @change="changeHoldKeyboard" :checked="holdKeyboard"></switch>
        </view>
        <view class="input-wrapper">
174
          <input class="uni-input" :hold-keyboard="holdKeyboard" value="hold keyboard " />
H
hdx 已提交
175 176
        </view>
      </view>
177

DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
178

张磊 已提交
179 180 181 182 183
      <view>
        <view class="uni-title">
          <text class="uni-title-text">input事件</text>
          <text class="uni-subtitle-text" v-if="inputEventDetail">{{inputEventDetail}}</text>
        </view>
雪洛's avatar
雪洛 已提交
184
        <view class="input-wrapper">
张磊 已提交
185 186 187
          <input class="uni-input" @input="onInput" />
        </view>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
188

张磊 已提交
189 190 191 192 193
      <view>
        <view class="uni-title">
          <text class="uni-title-text">focus事件和blur事件</text>
          <text class="uni-subtitle-text" v-if="focusAndBlurEventDetail">{{focusAndBlurEventDetail}}</text>
        </view>
雪洛's avatar
雪洛 已提交
194
        <view class="input-wrapper">
张磊 已提交
195 196 197
          <input class="uni-input" @focus="onFocus" @blur="onBlur" />
        </view>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
198

张磊 已提交
199 200 201 202 203
      <view>
        <view class="uni-title">
          <text class="uni-title-text">confirm事件</text>
          <text class="uni-subtitle-text" v-if="confirmEventDetail">{{confirmEventDetail}}</text>
        </view>
雪洛's avatar
雪洛 已提交
204
        <view class="input-wrapper">
张磊 已提交
205 206 207
          <input class="uni-input" @confirm="onConfirm" />
        </view>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
208

张磊 已提交
209 210 211
      <view>
        <view class="uni-title">
          <text class="uni-title-text">keyboardheightchange事件</text>
H
hdx 已提交
212 213
          <text class="uni-subtitle-text"
            v-if="keyboardHeightChangeEventDetail">{{keyboardHeightChangeEventDetail}}</text>
张磊 已提交
214
        </view>
雪洛's avatar
雪洛 已提交
215
        <view class="input-wrapper">
张磊 已提交
216 217 218
          <input class="uni-input" @keyboardheightchange="onKeyborardHeightChange" />
        </view>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
219

张磊 已提交
220 221 222 223
      <view>
        <view class="uni-title">
          <text class="uni-title-text">带清除按钮的输入框</text>
        </view>
雪洛's avatar
雪洛 已提交
224
        <view class="input-wrapper">
张磊 已提交
225 226 227 228 229
          <input class="uni-input" placeholder="带清除按钮的输入框" :value="inputClearValue" @input="clearInput" />
          <image class="uni-icon" src="/static/icons/clear.png" v-if="showClearIcon" @click="clearIcon">
          </image>
        </view>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
230

张磊 已提交
231 232 233 234
      <view>
        <view class="uni-title">
          <text class="uni-title-text">可查看密码的输入框</text>
        </view>
雪洛's avatar
雪洛 已提交
235
        <view class="input-wrapper">
张磊 已提交
236
          <input class="uni-input" placeholder="请输入密码" :password="showPassword" />
H
hdx 已提交
237 238
          <image class="uni-icon" :src="!showPassword ? '/static/icons/eye-active.png' : '/static/icons/eye.png'"
            @click="changePassword"></image>
张磊 已提交
239 240 241 242 243 244
        </view>
      </view>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
Y
init  
yurj26 已提交
245
</template>
Y
yurj26 已提交
246
<script lang="uts">
张磊 已提交
247 248 249 250 251 252 253
  export default {
    data() {
      return {
        title: 'input',
        showClearIcon: false,
        inputClearValue: '',
        showPassword: true,
雪洛's avatar
雪洛 已提交
254
        cursor: -1,
255
        cursor_color: "#3393E2",
张磊 已提交
256 257 258 259 260 261 262 263 264 265
        selectionStart: -1,
        selectionEnd: -1,
        inputEventDetail: '',
        focusAndBlurEventDetail: '',
        confirmEventDetail: '',
        keyboardHeightChangeEventDetail: '',
        focus: true,
        inputPassword: true,
        inputTypeTel: "tel",
        inputPlaceHolderStyle: "color:red",
W
wanganxp 已提交
266
        inputPlaceHolderClass: "uni-input-placeholder-class" as string.ClassString ,
H
hdx 已提交
267 268 269 270 271
        inputMaxLengthValue: "",
        onMaxLengthInputValue: "",
        inputMaxLengthFocus: false,
        inputPasswordValue: "",
        inputFocusKeyBoardChangeValue: true,
272
        holdKeyboard: false,
张磊 已提交
273 274
      }
    },
雪洛's avatar
雪洛 已提交
275
    methods: {
276
      inputFocusKeyBoardChange(e : UniInputKeyboardHeightChangeEvent) {
雪洛's avatar
雪洛 已提交
277 278
        this.inputFocusKeyBoardChangeValue = e.detail.height > 50
      },
279
      onMaxLengthInput(event : UniInputEvent) {
雪洛's avatar
雪洛 已提交
280
        this.onMaxLengthInputValue = event.detail.value
张磊 已提交
281 282
      },
      setCursor: function (cursor : number) {
Q
qiang 已提交
283
        (this.$refs['input'] as UniInputElement).focus();
284 285 286 287
        this.cursor = cursor;
      },
      onCursorBlurChange(){
        this.cursor = 0
张磊 已提交
288 289
      },
      setSelection: function (selectionStart : number, selectionEnd : number) {
Q
qiang 已提交
290
        (this.$refs['input2'] as UniInputElement).focus();
张磊 已提交
291
        this.selectionStart = selectionStart;
292 293 294 295
        this.selectionEnd = selectionEnd;
      },
      onSelectionBlurChange(){
        this.selectionEnd = 0;
张磊 已提交
296
      },
297
      clearInput: function (event : UniInputEvent) {
张磊 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311
        this.inputClearValue = event.detail.value
        if (event.detail.value.length > 0) {
          this.showClearIcon = true
        } else {
          this.showClearIcon = false
        }
      },
      clearIcon: function () {
        this.inputClearValue = ''
        this.showClearIcon = false
      },
      changePassword: function () {
        this.showPassword = !this.showPassword
      },
312
      onInput: function (event : UniInputEvent) {
张磊 已提交
313 314 315
        console.log("键盘输入", JSON.stringify(event.detail));
        this.inputEventDetail = JSON.stringify(event.detail)
      },
316
      onFocus: function (event : UniInputFocusEvent) {
张磊 已提交
317 318 319
        console.log("输入框聚焦", JSON.stringify(event.detail));
        this.focusAndBlurEventDetail = JSON.stringify(event.detail);
      },
320
      onBlur: function (event : UniInputBlurEvent) {
张磊 已提交
321 322 323
        console.log("输入框失去焦点", JSON.stringify(event.detail));
        this.focusAndBlurEventDetail = JSON.stringify(event.detail);
      },
324
      onConfirm: function (event : UniInputConfirmEvent) {
张磊 已提交
325 326 327
        console.log("点击完成按钮", JSON.stringify(event.detail));
        this.confirmEventDetail = JSON.stringify(event.detail);
      },
328
      onKeyborardHeightChange: function (event : UniInputKeyboardHeightChangeEvent) {
张磊 已提交
329 330
        console.log("键盘高度发生变化", JSON.stringify(event.detail));
        this.keyboardHeightChangeEventDetail = JSON.stringify(event.detail);
雪洛's avatar
雪洛 已提交
331
      },
H
hdx 已提交
332
      test_check_input_value() : number {
雪洛's avatar
雪洛 已提交
333 334
        return this.onMaxLengthInputValue.length
      },
H
hdx 已提交
335
      changeCursorColor(event : UniSwitchChangeEvent) {
H
hdx 已提交
336 337 338 339 340 341 342
        const checked = event.detail.value;
        if (checked) {
          this.cursor_color = "red"
        } else {
          this.cursor_color = "#3393E2"
        }
        const input = uni.getElementById<UniInputElement>("uni-input-cursor-color")
343
        input?.focus()
H
hdx 已提交
344
      },
H
hdx 已提交
345
      changeHoldKeyboard(event : UniSwitchChangeEvent) {
H
hdx 已提交
346 347
        const checked = event.detail.value;
        this.holdKeyboard = checked
348
      }
张磊 已提交
349 350
    }
  }
Y
init  
yurj26 已提交
351 352 353
</script>

<style scoped>
雪洛's avatar
雪洛 已提交
354
  .input-wrapper {
张磊 已提交
355 356
    display: flex;
    padding: 8px 13px;
H
hdx 已提交
357
    margin: 5px 0;
张磊 已提交
358 359 360 361
    flex-direction: row;
    flex-wrap: nowrap;
    background-color: #ffffff;
  }
Y
init  
yurj26 已提交
362

张磊 已提交
363 364 365 366 367 368 369
  .uni-input {
    height: 28px;
    font-size: 15px;
    padding: 0px;
    flex: 1;
    background-color: #ffffff;
  }
Y
init  
yurj26 已提交
370

张磊 已提交
371 372 373 374
  .uni-icon {
    width: 24px;
    height: 24px;
  }
Y
init  
yurj26 已提交
375

张磊 已提交
376
  .uni-input-placeholder-class {
377
    font-size: 10px;
雪洛's avatar
雪洛 已提交
378
  }
H
hdx 已提交
379
</style>