tui-input.vue 11 KB

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