tui-input.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. <template>
  2. <view class="tui-input__wrap"
  3. :class="{'tui-border__top':borderTop && !inputBorder,'tui-border__bottom':borderBottom && !inputBorder,'tui-radius__fillet':isFillet && !getRadius,'tui-input__border-nvue':inputBorder}"
  4. :style="getStyles" @tap="fieldClick">
  5. <!-- #ifndef APP-NVUE -->
  6. <view class="tui-input__border-top" v-if="borderTop && !inputBorder" :style="{borderTopColor:borderColor}">
  7. </view>
  8. <view class="tui-input__border-bottom" :class="{'tui-line__left':lineLeft}" v-if="borderBottom && !inputBorder"
  9. :style="{borderBottomColor:borderColor}"></view>
  10. <view class="tui-input__border" :class="{'tui-radius__fillet':isFillet && !getRadius}" v-if="inputBorder"
  11. :style="{borderColor:borderColor,borderRadius:(getRadius*2)+'rpx'}"></view>
  12. <!-- #endif -->
  13. <!-- #ifdef APP-NVUE -->
  14. <view class="tui-input__required" v-if="required">
  15. <text :style="{color:getRequiredColor}">*</text>
  16. </view>
  17. <!-- #endif -->
  18. <!-- #ifndef APP-NVUE -->
  19. <view class="tui-input__required" :style="{color:getRequiredColor}" v-if="required">*</view>
  20. <!-- #endif -->
  21. <view class="tui-input__label"
  22. :style="{fontSize:getLabelSize+'rpx',color:getLabelColor,minWidth:labelWidth+'rpx'}" v-if="label">
  23. <text :style="{fontSize:getLabelSize+'rpx',color:getLabelColor}">{{label}}</text>
  24. </view>
  25. <slot name="left"></slot>
  26. <input class="tui-input__self" :class="{'tui-text__right':textRight,'tui-input__disabled':disabled}"
  27. :style="{fontSize:getSize+'rpx',color:color}" placeholder-class="tui-input__placeholder" :type="type"
  28. :name="name" :value="inputVal" :password="password" :placeholder="inputVal?'':placeholder"
  29. :placeholder-style="placeholderStyl" :disabled="disabled" :cursor-spacing="cursorSpacing"
  30. :maxlength="maxlength" :focus="focused" :confirm-type="confirmType" :confirm-hold="confirmHold"
  31. :cursor="cursor" :selection-start="selectionStart" :selection-end="selectionEnd"
  32. :adjust-position="adjustPosition" :hold-keyboard="holdKeyboard" :auto-blur="autoBlur" @focus="onFocus"
  33. @blur="onBlur" @input="onInput" @confirm="onConfirm" @keyboardheightchange="onKeyboardheightchange" />
  34. <icon type="clear" :size="clearSize" :color="clearColor" v-if="clearable && inputVal != ''" @tap.stop="onClear">
  35. </icon>
  36. <slot name="right"></slot>
  37. </view>
  38. </template>
  39. <script>
  40. export default {
  41. name: "tui-input",
  42. emits: ['input', 'update:modelValue', 'focus', 'blur', 'confirm', 'click', 'keyboardheightchange'],
  43. //这里加group是为了避免在表单中使用时给组件加value属性
  44. // #ifdef MP-WEIXIN
  45. behaviors: ['wx://form-field-group'],
  46. // #endif
  47. // #ifdef MP-BAIDU
  48. behaviors: ['swan://form-field'],
  49. // #endif
  50. // #ifdef MP-QQ
  51. behaviors: ['qq://form-field'],
  52. // #endif
  53. // #ifdef H5
  54. behaviors: ['uni://form-field'],
  55. // #endif
  56. // #ifdef MP-WEIXIN
  57. options: {
  58. addGlobalClass: true,
  59. virtualHost: true
  60. },
  61. // #endif
  62. props: {
  63. //是否为必填项
  64. required: {
  65. type: Boolean,
  66. default: false
  67. },
  68. requiredColor: {
  69. type: String,
  70. default: ''
  71. },
  72. //左侧标题
  73. label: {
  74. type: String,
  75. default: ''
  76. },
  77. //标题字体大小
  78. labelSize: {
  79. type: [Number, String],
  80. default: 0
  81. },
  82. labelColor: {
  83. type: String,
  84. default: ''
  85. },
  86. //label 最小宽度 rpx
  87. labelWidth: {
  88. type: Number,
  89. default: 140
  90. },
  91. clearable: {
  92. type: Boolean,
  93. default: false
  94. },
  95. //px
  96. clearSize: {
  97. type: Number,
  98. default: 15
  99. },
  100. clearColor: {
  101. type: String,
  102. default: '#bfbfbf'
  103. },
  104. //获取焦点
  105. focus: {
  106. type: Boolean,
  107. default: false
  108. },
  109. placeholder: {
  110. type: String,
  111. default: ''
  112. },
  113. placeholderStyle: {
  114. type: String,
  115. default: ''
  116. },
  117. //输入框名称
  118. name: {
  119. type: String,
  120. default: ''
  121. },
  122. //输入框值
  123. value: {
  124. type: [Number, String],
  125. default: ''
  126. },
  127. // #ifdef VUE3
  128. //输入框值
  129. modelValue: {
  130. type: [Number, String],
  131. default: ''
  132. },
  133. // #endif
  134. modelModifiers: {
  135. default: () => ({})
  136. },
  137. //与官方input type属性一致
  138. type: {
  139. type: String,
  140. default: 'text'
  141. },
  142. password: {
  143. type: Boolean,
  144. default: false
  145. },
  146. disabled: {
  147. type: Boolean,
  148. default: false
  149. },
  150. maxlength: {
  151. type: [Number, String],
  152. default: 140
  153. },
  154. min: {
  155. type: [Number, String],
  156. default: 'NaN'
  157. },
  158. max: {
  159. type: [Number, String],
  160. default: 'NaN'
  161. },
  162. cursorSpacing: {
  163. type: Number,
  164. default: 0,
  165. },
  166. confirmType: {
  167. type: String,
  168. default: 'done'
  169. },
  170. confirmHold: {
  171. type: Boolean,
  172. default: false,
  173. },
  174. cursor: {
  175. type: Number,
  176. default: -1
  177. },
  178. selectionStart: {
  179. type: Number,
  180. default: -1
  181. },
  182. selectionEnd: {
  183. type: Number,
  184. default: -1
  185. },
  186. adjustPosition: {
  187. type: Boolean,
  188. default: true
  189. },
  190. holdKeyboard: {
  191. type: Boolean,
  192. default: false
  193. },
  194. autoBlur: {
  195. type: Boolean,
  196. default: false
  197. },
  198. //输入框字体大小 rpx
  199. size: {
  200. type: [Number, String],
  201. default: 0
  202. },
  203. //输入框字体颜色
  204. color: {
  205. type: String,
  206. default: ''
  207. },
  208. // 是否显示 input 边框
  209. inputBorder: {
  210. type: Boolean,
  211. default: false
  212. },
  213. borderColor: {
  214. type: String,
  215. default: 'rgba(0, 0, 0, 0.1)'
  216. },
  217. //input是否显示为圆角
  218. isFillet: {
  219. type: Boolean,
  220. default: false
  221. },
  222. // 是否显示上边框
  223. borderTop: {
  224. type: Boolean,
  225. default: false
  226. },
  227. // 是否显示下边框
  228. borderBottom: {
  229. type: Boolean,
  230. default: true
  231. },
  232. //下边框线条是否有左偏移距离
  233. lineLeft: {
  234. type: Boolean,
  235. default: true
  236. },
  237. // 是否自动去除两端的空格
  238. trim: {
  239. type: Boolean,
  240. default: true
  241. },
  242. textRight: {
  243. type: Boolean,
  244. default: false
  245. },
  246. //输入框padding值
  247. padding: {
  248. type: String,
  249. default: ''
  250. },
  251. //输入框背景颜色
  252. backgroundColor: {
  253. type: String,
  254. default: ''
  255. },
  256. radius: {
  257. type: [Number, String],
  258. default: -1
  259. },
  260. //输入框margin-top值 rpx
  261. marginTop: {
  262. type: [Number, String],
  263. default: 0
  264. }
  265. },
  266. computed: {
  267. getLabelSize() {
  268. return this.labelSize || (uni && uni.$tui && uni.$tui.tuiInput.labelSize) || 32
  269. },
  270. getLabelColor() {
  271. return this.labelColor || (uni && uni.$tui && uni.$tui.tuiInput.labelColor) || '#333'
  272. },
  273. getSize() {
  274. return this.size || (uni && uni.$tui && uni.$tui.tuiInput.size) || 32
  275. },
  276. getColor() {
  277. return this.color || (uni && uni.$tui && uni.$tui.tuiInput.color) || '#333'
  278. },
  279. getRadius() {
  280. let radius = this.radius
  281. if (radius === -1 || radius === true) {
  282. radius = uni && uni.$tui && uni.$tui.tuiInput.radius
  283. }
  284. return Number(radius || 0)
  285. },
  286. getStyles() {
  287. const padding = this.padding || (uni && uni.$tui && uni.$tui.tuiInput.padding) || '26rpx 30rpx';
  288. const bgColor = this.backgroundColor || (uni && uni.$tui && uni.$tui.tuiInput.backgroundColor) ||
  289. '#FFFFFF';
  290. let radius = this.getRadius;
  291. let styles = `padding:${padding};background:${bgColor};margin-top:${this.marginTop}rpx;`
  292. if (radius && radius !== true && radius !== -1) {
  293. styles += `border-radius:${radius}rpx;`
  294. }
  295. if (this.borderTop || this.borderBottom || this.inputBorder) {
  296. styles += `border-color:${this.borderColor};`
  297. }
  298. return styles
  299. },
  300. getRequiredColor() {
  301. return this.requiredColor || (uni && uni.$tui && uni.$tui.tuiInput.requiredColor) || '#EB0909'
  302. }
  303. },
  304. data() {
  305. return {
  306. placeholderStyl: '',
  307. focused: false,
  308. inputVal: ''
  309. }
  310. },
  311. watch: {
  312. focus(val) {
  313. this.$nextTick(() => {
  314. setTimeout(() => {
  315. this.focused = val
  316. }, 50)
  317. })
  318. },
  319. placeholderStyle() {
  320. this.fieldPlaceholderStyle()
  321. },
  322. // #ifdef VUE3
  323. modelValue(newVal) {
  324. this.inputVal = newVal
  325. },
  326. // #endif
  327. value(newVal) {
  328. this.inputVal = newVal
  329. }
  330. },
  331. created() {
  332. this.fieldPlaceholderStyle()
  333. setTimeout(() => {
  334. // #ifndef VUE3
  335. this.inputVal = this.value
  336. // #endif
  337. // #ifdef VUE3
  338. if (this.value && !this.modelValue) {
  339. this.inputVal = this.value
  340. } else {
  341. this.inputVal = this.modelValue
  342. }
  343. // #endif
  344. }, 50)
  345. },
  346. mounted() {
  347. this.$nextTick(() => {
  348. setTimeout(() => {
  349. this.focused = this.focus
  350. }, 300)
  351. })
  352. },
  353. methods: {
  354. fieldPlaceholderStyle() {
  355. if (this.placeholderStyle) {
  356. this.placeholderStyl = this.placeholderStyle
  357. } else {
  358. const size = uni.upx2px(this.size || (uni && uni.$tui && uni.$tui.tuiInput.size) || 32)
  359. this.placeholderStyl = `font-size:${size}px`
  360. }
  361. },
  362. onInput(event) {
  363. let value = event.detail.value;
  364. if (this.trim) value = this.trimStr(value);
  365. this.inputVal = value
  366. //数字类型 数值不能超过最大整数安全范围,一但超过则返回字符串
  367. const cVal = Number(value)
  368. if ((this.modelModifiers.number || this.type === 'digit' || this.type === 'number') && !isNaN(cVal) &&
  369. Number.isSafeInteger(cVal)) {
  370. let eVal = this.type === 'digit' ? value : cVal
  371. if (typeof cVal === 'number') {
  372. const min = Number(this.min)
  373. const max = Number(this.max)
  374. if (typeof min === 'number' && cVal < min) {
  375. eVal = min
  376. } else if (typeof max === 'number' && max < cVal) {
  377. eVal = max
  378. }
  379. }
  380. value = isNaN(eVal) ? value : eVal
  381. }
  382. this.$nextTick(() => {
  383. event.detail.value !== '' && (this.inputVal = value);
  384. })
  385. const inputValue = event.detail.value !== '' ? value : ''
  386. this.$emit('input', inputValue);
  387. // #ifdef VUE3
  388. this.$emit('update:modelValue', inputValue)
  389. // #endif
  390. },
  391. onFocus(event) {
  392. this.$emit('focus', event);
  393. },
  394. onBlur(event) {
  395. this.$emit('blur', event);
  396. },
  397. onConfirm(e) {
  398. this.$emit('confirm', e);
  399. },
  400. onClear(event) {
  401. if (this.disabled) return;
  402. uni.hideKeyboard()
  403. this.inputVal = '';
  404. this.$emit('input', '');
  405. // #ifdef VUE3
  406. this.$emit('update:modelValue', '')
  407. // #endif
  408. },
  409. fieldClick() {
  410. this.$emit('click', {
  411. name: this.name
  412. });
  413. },
  414. onKeyboardheightchange(e) {
  415. this.$emit('keyboardheightchange', e.detail)
  416. },
  417. trimStr(str) {
  418. return str.replace(/^\s+|\s+$/g, '');
  419. }
  420. }
  421. }
  422. </script>
  423. <style scoped>
  424. .tui-input__wrap {
  425. /* #ifndef APP-NVUE */
  426. width: 100%;
  427. box-sizing: border-box;
  428. display: flex;
  429. /* #endif */
  430. flex-direction: row;
  431. align-items: center;
  432. position: relative;
  433. flex: 1;
  434. /* #ifdef APP-NVUE */
  435. padding: 26rpx 30rpx;
  436. /* #endif */
  437. border-width: 0;
  438. }
  439. /* #ifndef APP-NVUE */
  440. .tui-input__border-top {
  441. position: absolute;
  442. top: 0;
  443. right: 0;
  444. left: 0;
  445. border-top: 1px solid var(--thorui-line-color, rgba(0, 0, 0, 0.1));
  446. -webkit-transform: scaleY(0.5);
  447. transform: scaleY(0.5);
  448. transform-origin: 0 0;
  449. z-index: 2;
  450. pointer-events: none;
  451. }
  452. .tui-input__border-bottom {
  453. position: absolute;
  454. border-bottom: 1px solid var(--thorui-line-color, rgba(0, 0, 0, 0.1));
  455. -webkit-transform: scaleY(0.5);
  456. transform: scaleY(0.5);
  457. transform-origin: 0 100%;
  458. bottom: 0;
  459. right: 0;
  460. left: 0;
  461. z-index: 2;
  462. pointer-events: none;
  463. }
  464. .tui-line__left {
  465. left: 30rpx !important;
  466. }
  467. /* #endif */
  468. /* #ifdef APP-NVUE */
  469. .tui-border__top {
  470. border-top-width: 0.5px;
  471. border-top-style: solid;
  472. }
  473. .tui-border__bottom {
  474. border-top-width: 0.5px;
  475. border-top-style: solid;
  476. }
  477. /* #endif */
  478. .tui-input__required {
  479. position: absolute;
  480. left: 12rpx;
  481. /* #ifndef APP-NVUE */
  482. height: 30rpx;
  483. top: 50%;
  484. transform: translateY(-50%);
  485. line-height: 1.15;
  486. /* #endif */
  487. /* #ifdef APP-NVUE */
  488. flex: 1;
  489. align-items: center;
  490. justify-content: center;
  491. line-height: 1;
  492. /* #endif */
  493. }
  494. .tui-input__label {
  495. padding-right: 12rpx;
  496. /* #ifndef APP-NVUE */
  497. flex-shrink: 0;
  498. /* #endif */
  499. }
  500. .tui-input__self {
  501. flex: 1;
  502. padding-right: 12rpx;
  503. /* #ifndef APP-NVUE */
  504. box-sizing: border-box;
  505. overflow: visible;
  506. /* #endif */
  507. background-color: transparent;
  508. }
  509. .tui-input__placeholder {
  510. /* #ifndef APP-NVUE */
  511. color: var(--thorui-text-color-placeholder, #ccc);
  512. overflow: visible;
  513. /* #endif */
  514. /* #ifdef APP-NVUE */
  515. color: #ccc;
  516. font-size: 32rpx;
  517. /* #endif */
  518. }
  519. /* #ifdef MP */
  520. ::v-deep .tui-input__placeholder {
  521. color: var(--thorui-text-color-placeholder, #ccc);
  522. overflow: visible;
  523. }
  524. /* #endif */
  525. /* #ifdef APP-NVUE */
  526. .tui-input__border-nvue {
  527. border-width: 0.5px;
  528. border-style: solid;
  529. }
  530. /* #endif */
  531. /* #ifndef APP-NVUE */
  532. .tui-input__disabled {
  533. pointer-events: none;
  534. }
  535. .tui-input__border {
  536. position: absolute;
  537. height: 200%;
  538. width: 200%;
  539. border: 1px solid var(--thorui-border-color, #d1d1d1);
  540. transform-origin: 0 0;
  541. transform: scale(0.5);
  542. left: 0;
  543. top: 0;
  544. border-radius: 8rpx;
  545. pointer-events: none;
  546. }
  547. /* #endif */
  548. .tui-radius__fillet {
  549. border-radius: 100px !important;
  550. }
  551. .tui-text__right {
  552. text-align: right;
  553. }
  554. </style>