input.uvue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view style="flex: 1">
  4. <!-- #endif -->
  5. <page-head :title="title"></page-head>
  6. <view class="uni-common-mt uni-padding-wrap" style="padding-bottom: 30px;">
  7. <view>
  8. <view class="uni-title">
  9. <text class="uni-title-text">设置输入框的初始内容</text>
  10. </view>
  11. <view class="input-wrapper">
  12. <input id="uni-input-default" class="uni-input" value="hello uni-app x" />
  13. </view>
  14. </view>
  15. <view>
  16. <view class="uni-title">
  17. <text class="uni-title-text">type取值(不同输入法表现可能不一致)</text>
  18. </view>
  19. <view class="input-wrapper">
  20. <input id="uni-input-type-text" class="uni-input" type="text" placeholder="文本输入键盘" />
  21. </view>
  22. <view class="input-wrapper">
  23. <input id="uni-input-type-number" class="uni-input" type="number" placeholder="数字输入键盘" />
  24. </view>
  25. <view class="input-wrapper">
  26. <input id="uni-input-type-digit" class="uni-input" type="digit" placeholder="带小数点的数字输入键盘" />
  27. </view>
  28. <view class="input-wrapper">
  29. <input id="uni-input-type-tel" class="uni-input" :type="inputTypeTel" placeholder="电话输入键盘" />
  30. </view>
  31. <!-- #ifdef APP-IOS || APP-ANDROID -->
  32. <view class="input-wrapper">
  33. <input id="uni-input-type-tel" class="uni-input" type="search" placeholder="搜索输入键盘" />
  34. </view>
  35. <view class="input-wrapper">
  36. <input id="uni-input-type-tel" class="uni-input" type="email" placeholder="邮箱输入键盘" />
  37. </view>
  38. <view class="input-wrapper">
  39. <input id="uni-input-type-tel" class="uni-input" type="url" placeholder="url输入键盘" />
  40. </view>
  41. <view class="input-wrapper">
  42. <input id="uni-input-type-tel" class="uni-input" type="none" placeholder="聚焦但不弹出键盘" />
  43. </view>
  44. <!-- #endif -->
  45. </view>
  46. <view>
  47. <view class="uni-title">
  48. <text class="uni-title-text">密码输入框</text>
  49. </view>
  50. <view class="input-wrapper">
  51. <input id="uni-input-password" class="uni-input" :password="inputPassword" type="text" :value="inputPasswordValue" />
  52. </view>
  53. </view>
  54. <view>
  55. <view class="uni-title">
  56. <text class="uni-title-text">无value设置的密码输入框</text>
  57. </view>
  58. <view class="input-wrapper">
  59. <input class="uni-input" :password="true" type="text" />
  60. </view>
  61. </view>
  62. <view>
  63. <view class="uni-title">
  64. <text class="uni-title-text">占位符样式</text>
  65. </view>
  66. <view class="input-wrapper">
  67. <input id="uni-input-placeholder1" class="uni-input" :placeholder-style="inputPlaceHolderStyle"
  68. placeholder="占位符文字颜色为红色" />
  69. </view>
  70. <view class="input-wrapper">
  71. <input id="uni-input-placeholder2" class="uni-input" :placeholder-class="inputPlaceHolderClass"
  72. placeholder="占位符字体大小为10px" />
  73. </view>
  74. <view class="input-wrapper">
  75. <input id="uni-input-placeholder3" class="uni-input" placeholder-class=""
  76. placeholder="placeholder-class 传入空字符串" />
  77. </view>
  78. <view class="input-wrapper">
  79. <input class="uni-input" :placeholder-style="inputPlaceHolderStyle"
  80. value="不设置placeholder只设置placeholder-style" />
  81. </view>
  82. <view class="input-wrapper">
  83. <input id="uni-input-placeholder2" style="font-family: AlimamaDaoLiTiOTF;font-weight: bold;text-align: right;" class="uni-input" :placeholder-style="placeholderStyle"
  84. placeholder="占位符以及内容右对齐" />
  85. </view>
  86. </view>
  87. <view>
  88. <view class="uni-title">
  89. <text class="uni-title-text">设置禁用输入框</text>
  90. </view>
  91. <view class="input-wrapper">
  92. <input id="uni-input-disable" class="uni-input" :disabled="true" />
  93. </view>
  94. </view>
  95. <view>
  96. <view class="uni-title">
  97. <text class="uni-title-text">设置最大输入长度</text>
  98. </view>
  99. <view class="input-wrapper">
  100. <input id="uni-input-maxlength" class="uni-input" :maxlength="10" placeholder="最大输入长度为10"
  101. :value="inputMaxLengthValue" @input="onMaxLengthInput" :focus="inputMaxLengthFocus" />
  102. </view>
  103. </view>
  104. <view>
  105. <view class="uni-title">
  106. <text class="uni-title-text">设置光标与键盘的距离</text>
  107. </view>
  108. <view class="input-wrapper">
  109. <input class="uni-input" :cursor-spacing="1000" placeholder="光标与键盘的距离为1000px" />
  110. </view>
  111. </view>
  112. <view>
  113. <view class="uni-title">
  114. <text class="uni-title-text">自动获取焦点</text>
  115. </view>
  116. <view class="input-wrapper">
  117. <input id="uni-input-focus" class="uni-input" :focus="focus"
  118. @keyboardheightchange="inputFocusKeyBoardChange" />
  119. </view>
  120. </view>
  121. <view>
  122. <view class="uni-title">
  123. <text class="uni-title-text">confirm-type取值(不同输入法表现可能不一致)</text>
  124. </view>
  125. <view class="input-wrapper">
  126. <input id="uni-input-confirm-send" class="uni-input" confirmType="send" placeholder="键盘右下角按钮显示为发送" />
  127. </view>
  128. <view class="input-wrapper">
  129. <input id="uni-input-confirm-search" class="uni-input" confirmType="search" placeholder="键盘右下角按钮显示为搜索" />
  130. </view>
  131. <view class="input-wrapper">
  132. <input id="uni-input-confirm-next" class="uni-input" confirmType="next" placeholder="键盘右下角按钮显示为下一个" />
  133. </view>
  134. <view class="input-wrapper">
  135. <input id="uni-input-confirm-go" class="uni-input" confirmType="go" placeholder="键盘右下角按钮显示为前往" />
  136. </view>
  137. <view class="input-wrapper">
  138. <input id="uni-input-confirm-done" class="uni-input" confirmType="done" placeholder="键盘右下角按钮显示为完成" />
  139. </view>
  140. </view>
  141. <view>
  142. <view class="uni-title">
  143. <text class="uni-title-text">点击键盘右下角按钮时保持键盘不收起</text>
  144. </view>
  145. <view class="input-wrapper">
  146. <input class="uni-input" :confirm-hold="true" />
  147. </view>
  148. </view>
  149. <view>
  150. <view class="uni-title" @click="setCursor(4)">
  151. <text class="uni-title-text">设置输入框聚焦时光标的位置(点这里)</text>
  152. </view>
  153. <view class="input-wrapper">
  154. <input ref="input" class="uni-input" value="0123456789" :focus="cursorInputFocus" :cursor="cursor" @blur="onCursorBlurChange" />
  155. </view>
  156. </view>
  157. <view>
  158. <view class="uni-title" @click="setSelection(0, 4)">
  159. <text class="uni-title-text">设置输入框聚焦时光标的起始位置和结束位置(点这里)</text>
  160. </view>
  161. <view class="input-wrapper">
  162. <input ref="input2" class="uni-input" value="0123456789" :selection-start="selectionStart"
  163. :selection-end="selectionEnd" :focus="selectionInputFocus" @blur="onSelectionBlurChange" />
  164. </view>
  165. </view>
  166. <view>
  167. <view class="uni-title" style="flex-direction: row; align-items: center">
  168. <text class="uni-title-text" style="flex-shrink: 1;">设置光标颜色为红色(Android的微信小程序非skyline下仅支持黑和绿)</text>
  169. <switch style="margin-left: 10px" @change="changeCursorColor" :checked="cursor_color == '#FF0000'"></switch>
  170. </view>
  171. <view class="input-wrapper">
  172. <input id="uni-input-cursor-color" class="uni-input" :cursor-color="cursor_color" :focus="cursorColorInputFocus" @blur="cursorColorInputBlur" value="光标颜色" />
  173. </view>
  174. </view>
  175. <view>
  176. <view class="uni-title">
  177. <text class="uni-title-text">键盘弹起时,自动上推页面</text>
  178. </view>
  179. <view class="input-wrapper">
  180. <input class="uni-input" :adjust-position="true" />
  181. </view>
  182. </view>
  183. <view>
  184. <view class="uni-title" style="flex-direction: row; align-items: center">
  185. <text class="uni-title-text" style="flex-shrink: 1;">设置hold-keyboard</text>
  186. <switch style="margin-left: 10px" @change="changeHoldKeyboard" :checked="holdKeyboard"></switch>
  187. </view>
  188. <view class="input-wrapper">
  189. <input class="uni-input" :hold-keyboard="holdKeyboard" value="hold keyboard " />
  190. </view>
  191. </view>
  192. <view>
  193. <view class="uni-title">
  194. <text class="uni-title-text">input事件</text>
  195. <text class="uni-subtitle-text" v-if="inputEventDetail">{{
  196. inputEventDetail
  197. }}</text>
  198. </view>
  199. <view class="input-wrapper">
  200. <input class="uni-input" @input="onInput" />
  201. </view>
  202. </view>
  203. <view>
  204. <view class="uni-title">
  205. <text class="uni-title-text">focus事件和blur事件</text>
  206. <text class="uni-subtitle-text" v-if="focusAndBlurEventDetail">{{
  207. focusAndBlurEventDetail
  208. }}</text>
  209. </view>
  210. <view class="input-wrapper">
  211. <input class="uni-input" @focus="onFocus" @blur="onBlur" />
  212. </view>
  213. </view>
  214. <view>
  215. <view class="uni-title">
  216. <text class="uni-title-text">confirm事件</text>
  217. <text class="uni-subtitle-text" v-if="confirmEventDetail">{{
  218. confirmEventDetail
  219. }}</text>
  220. </view>
  221. <view class="input-wrapper">
  222. <input class="uni-input" @confirm="onConfirm" />
  223. </view>
  224. </view>
  225. <view>
  226. <view class="uni-title">
  227. <text class="uni-title-text">change事件</text>
  228. <text class="uni-subtitle-text" v-if="changeEventDetail">{{
  229. changeEventDetail
  230. }}</text>
  231. </view>
  232. <view class="input-wrapper">
  233. <input class="uni-input" @change="onChange" />
  234. </view>
  235. </view>
  236. <view>
  237. <view class="uni-title">
  238. <text class="uni-title-text">keyboardheightchange事件</text>
  239. <text class="uni-subtitle-text"
  240. v-if="keyboardHeightChangeEventDetail">{{ keyboardHeightChangeEventDetail }}</text>
  241. </view>
  242. <view class="input-wrapper">
  243. <input class="uni-input" @keyboardheightchange="onKeyboardHeightChange"
  244. :focus="focusedForKeyboardHeightChangeTest" />
  245. </view>
  246. </view>
  247. <view>
  248. <view class="uni-title">
  249. <text class="uni-title-text">带清除按钮的输入框</text>
  250. </view>
  251. <view class="input-wrapper">
  252. <input class="uni-input" placeholder="带清除按钮的输入框" :value="inputClearValue" @input="clearInput" />
  253. <image class="uni-icon" src="/static/icons/clear.png" v-if="showClearIcon" @click="clearIcon">
  254. </image>
  255. </view>
  256. </view>
  257. <view>
  258. <view class="uni-title">
  259. <text class="uni-title-text">可查看密码的输入框</text>
  260. </view>
  261. <view class="input-wrapper">
  262. <input class="uni-input" placeholder="请输入密码" :password="showPassword" />
  263. <image class="uni-icon" :src="
  264. !showPassword
  265. ? '/static/icons/eye-active.png'
  266. : '/static/icons/eye.png'
  267. " @click="changePassword"></image>
  268. </view>
  269. </view>
  270. <view>
  271. <view class="uni-title">
  272. <text class="uni-title-text">同时存在 v-model 和 value</text>
  273. </view>
  274. <view class="input-wrapper">
  275. <input id="both-model-value" class="uni-input" v-model="demoValue" value="456" />
  276. </view>
  277. </view>
  278. <view>
  279. <view class="uni-title" style="flex-direction: row; align-items: center">
  280. <text class="uni-title-text" style="flex-shrink: 1;">设置adjust-position</text>
  281. <switch style="margin-left: 10px" @change="changeAdjustPosition" :checked="adjustPosition"></switch>
  282. </view>
  283. <view class="input-wrapper">
  284. <input class="uni-input" :adjust-position="adjustPosition" />
  285. </view>
  286. </view>
  287. <view>
  288. <view class="uni-title">
  289. <text class="uni-title-text">输入内容的同步更新UI</text>
  290. </view>
  291. <text>{{syncDisplayValue}}</text>
  292. <view class="input-wrapper">
  293. <input class="uni-input" v-model="syncDisplayValue"/>
  294. </view>
  295. </view>
  296. </view>
  297. <!-- #ifdef APP -->
  298. </scroll-view>
  299. <!-- #endif -->
  300. </template>
  301. <script lang="uts">
  302. export default {
  303. data() {
  304. return {
  305. title: 'input',
  306. showClearIcon: false,
  307. inputClearValue: '',
  308. showPassword: true,
  309. cursor: -1,
  310. cursorInputFocus: false,
  311. cursor_color: "#3393E2",
  312. cursorColorInputFocus: false,
  313. selectionStart: -1,
  314. selectionEnd: -1,
  315. selectionInputFocus: false,
  316. inputEventDetail: '',
  317. focusAndBlurEventDetail: '',
  318. confirmEventDetail: '',
  319. changeEventDetail: '',
  320. keyboardHeightChangeEventDetail: '',
  321. focus: true,
  322. inputPassword: true,
  323. inputTypeTel: "tel",
  324. inputPlaceHolderStyle: "color:red",
  325. placeholderStyle: "font-family: AlimamaDaoLiTiOTF;font-weight: bold;text-align: right",
  326. inputPlaceHolderClass: "uni-input-placeholder-class" as string.ClassString,
  327. inputMaxLengthValue: "",
  328. onMaxLengthInputValue: "",
  329. inputMaxLengthFocus: false,
  330. inputPasswordValue: "cipher",
  331. inputFocusKeyBoardChangeValue: true,
  332. holdKeyboard: false,
  333. keyboardHeight: 0,
  334. focusedForKeyboardHeightChangeTest: false,
  335. demoValue: '123',
  336. adjustPosition: false,
  337. syncDisplayValue: ''
  338. }
  339. },
  340. methods: {
  341. inputFocusKeyBoardChange(e : UniInputKeyboardHeightChangeEvent) {
  342. this.inputFocusKeyBoardChangeValue = e.detail.height > 50
  343. },
  344. onMaxLengthInput(event : UniInputEvent) {
  345. this.onMaxLengthInputValue = event.detail.value
  346. },
  347. setCursor: function (cursor : number) {
  348. // (this.$refs['input'] as UniInputElement).focus();
  349. this.cursorInputFocus = true
  350. this.cursor = cursor;
  351. },
  352. onCursorBlurChange() {
  353. this.cursor = 0
  354. this.cursorInputFocus = false
  355. },
  356. setSelection: function (selectionStart : number, selectionEnd : number) {
  357. // (this.$refs['input2'] as UniInputElement).focus();
  358. this.selectionInputFocus = true
  359. this.selectionStart = selectionStart;
  360. this.selectionEnd = selectionEnd;
  361. },
  362. onSelectionBlurChange() {
  363. this.selectionEnd = 0;
  364. this.selectionInputFocus = false
  365. },
  366. clearInput: function (event : UniInputEvent) {
  367. this.inputClearValue = event.detail.value
  368. if (event.detail.value.length > 0) {
  369. this.showClearIcon = true
  370. } else {
  371. this.showClearIcon = false
  372. }
  373. },
  374. clearIcon: function () {
  375. this.inputClearValue = ''
  376. this.showClearIcon = false
  377. },
  378. changePassword: function () {
  379. this.showPassword = !this.showPassword
  380. },
  381. onInput: function (event : UniInputEvent) {
  382. console.log("键盘输入", JSON.stringify(event.detail));
  383. this.inputEventDetail = JSON.stringify(event.detail)
  384. },
  385. onFocus: function (event : UniInputFocusEvent) {
  386. console.log("输入框聚焦", JSON.stringify(event.detail));
  387. this.focusAndBlurEventDetail = JSON.stringify(event.detail);
  388. },
  389. onBlur: function (event : UniInputBlurEvent) {
  390. console.log("输入框失去焦点", JSON.stringify(event.detail));
  391. this.focusAndBlurEventDetail = JSON.stringify(event.detail);
  392. },
  393. onConfirm: function (event : UniInputConfirmEvent) {
  394. console.log("点击完成按钮", JSON.stringify(event.detail));
  395. this.confirmEventDetail = JSON.stringify(event.detail);
  396. },
  397. onChange: function (event : UniInputChangeEvent) {
  398. console.log("输入内容变化", JSON.stringify(event.detail));
  399. this.changeEventDetail = JSON.stringify(event.detail);
  400. },
  401. onKeyboardHeightChange: function (event : UniInputKeyboardHeightChangeEvent) {
  402. console.log("键盘高度发生变化", JSON.stringify(event.detail));
  403. this.keyboardHeightChangeEventDetail = JSON.stringify(event.detail);
  404. this.keyboardHeight = event.detail.height;
  405. },
  406. test_check_input_value() : number {
  407. return this.onMaxLengthInputValue.length
  408. },
  409. changeCursorColor(event : UniSwitchChangeEvent) {
  410. const checked = event.detail.value;
  411. if (checked) {
  412. this.cursor_color = "#FF0000"
  413. } else {
  414. this.cursor_color = "#3393E2"
  415. }
  416. // const input = uni.getElementById<UniInputElement>("uni-input-cursor-color")
  417. // input?.focus()
  418. this.cursorColorInputFocus = true
  419. },
  420. cursorColorInputBlur(event : UniInputBlurEvent) {
  421. this.cursorColorInputFocus = false
  422. },
  423. changeHoldKeyboard(event : UniSwitchChangeEvent) {
  424. const checked = event.detail.value;
  425. this.holdKeyboard = checked
  426. },
  427. changeAdjustPosition(event : UniSwitchChangeEvent) {
  428. const checked = event.detail.value;
  429. this.adjustPosition = checked
  430. }
  431. }
  432. }
  433. </script>
  434. <style scoped>
  435. .input-wrapper {
  436. display: flex;
  437. padding: 8px 13px;
  438. margin: 5px 0;
  439. flex-direction: row;
  440. flex-wrap: nowrap;
  441. background-color: #ffffff;
  442. }
  443. .uni-input {
  444. height: 28px;
  445. font-size: 15px;
  446. padding: 0px;
  447. flex: 1;
  448. background-color: #ffffff;
  449. }
  450. .uni-icon {
  451. width: 24px;
  452. height: 24px;
  453. }
  454. .uni-input-placeholder-class {
  455. font-size: 10px;
  456. }
  457. @font-face {
  458. font-family: AlimamaDaoLiTiOTF;
  459. src: url('/static/font/AlimamaDaoLiTi.otf');
  460. }
  461. </style>