yh-code-input.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <view class="code-input-wrap">
  3. <view class="code-input-item" :class="{border:mode === 'border'}" :style="itemStyle" v-for="(item,index) in codeLength" :key="index">
  4. <view class="dot" v-if="dot && codeList.length > index"></view>
  5. <text v-else >{{codeList[index]}}</text>
  6. <view class="cursor" :style="cursorStyle" v-if="isFocus && codeList.length === index"></view>
  7. <view class="line" v-if="mode === 'line'" :style="lineStyle"></view>
  8. </view>
  9. <input class="input"
  10. :maxlength="maxlength"
  11. :type="inputType"
  12. :value="inputValue"
  13. :adjustPosition="adjustPosition"
  14. :disabled="disabledKeyboard"
  15. @input="input"
  16. @focus="isFocus = true"
  17. @blur="isFocus = false"
  18. />
  19. </view>
  20. </template>
  21. <script>
  22. export default {
  23. props:{
  24. // #ifdef VUE2
  25. value: {
  26. type: [String, Number],
  27. default: ""
  28. },
  29. // #endif
  30. // #ifdef VUE3
  31. modelValue: {
  32. type: [String, Number],
  33. default: ""
  34. },
  35. // #endif
  36. dot:{//是否输入框内容用圆点替换
  37. type: Boolean,
  38. default: false
  39. },
  40. adjustPosition: {// 键盘弹起时,是否自动上推页面
  41. type: Boolean,
  42. default: true
  43. },
  44. disabledKeyboard: {//是否隐藏原生键盘
  45. type: Boolean,
  46. default: false
  47. },
  48. inputType:{//输入框类型
  49. type:String,
  50. default:"text"
  51. },
  52. maxlength:{//最大长度
  53. type:Number,
  54. default:4
  55. },
  56. color:{
  57. type:String,
  58. default:"#000"
  59. },
  60. itemW:{//输入框宽
  61. type:String,
  62. default:'70rpx'
  63. },
  64. itemH:{//输入框高
  65. type:String,
  66. default:'60rpx'
  67. },
  68. itemBg:{//输入框背景色
  69. type:String,
  70. default:'#f5f5f5'
  71. },
  72. itemRound:{//输入框圆角
  73. type:String,
  74. default:'4rpx'
  75. },
  76. itemFontSize:{//输入内容文字大小
  77. type:String,
  78. default:'28rpx'
  79. },
  80. bold:{
  81. type:String,
  82. default:'500'
  83. },
  84. cursorW:{//聚焦线宽
  85. type:String,
  86. default:'2rpx'
  87. },
  88. cursorH:{//聚焦线高
  89. type:String,
  90. default:'40rpx'
  91. },
  92. cursorBg:{//聚焦线背景
  93. type:String,
  94. default:'#000'
  95. },
  96. lineW:{//底线宽
  97. type:String,
  98. default:'100%'
  99. },
  100. lineH:{//底线高
  101. type:String,
  102. default:'4rpx'
  103. },
  104. lineBg:{//底线背景
  105. type:String,
  106. default:'#A8A8A8'
  107. },
  108. lineRound:{//底线圆角
  109. type:String,
  110. default:'0rpx'
  111. },
  112. gutter:{//输入框间隙
  113. type:String,
  114. default:'10rpx'
  115. },
  116. mode:{//输入框模式 box 为背景色样式 line 为下划线样式 border 边框样式
  117. type:String,
  118. default:'line'
  119. },
  120. borderColor:{
  121. type:String,
  122. default:'#A8A8A8'
  123. },
  124. },
  125. // #ifdef VUE3
  126. emits: ['update:modelValue','finish','change'],
  127. // #endif
  128. // #ifdef MP
  129. options: {
  130. styleIsolation: 'shared'
  131. },
  132. // #endif
  133. data(){
  134. return {
  135. inputValue:"",
  136. isFocus:false
  137. }
  138. },
  139. watch:{
  140. // #ifdef VUE2
  141. value:
  142. // #endif
  143. // #ifdef VUE3
  144. modelValue:
  145. // #endif
  146. {
  147. handler(v){
  148. this.inputValue = String(v).substring(0, this.maxlength)
  149. },
  150. immediate:true
  151. }
  152. },
  153. computed:{
  154. codeLength(){
  155. return new Array(+this.maxlength)
  156. },
  157. codeList(){
  158. return String(this.inputValue).split('')
  159. },
  160. itemStyle(){
  161. return {
  162. width:this.itemW,
  163. height:this.itemH,
  164. backgroundColor:this.mode === 'box' && this.itemBg ? this.itemBg : 'transparent',
  165. marginRight:this.gutter,
  166. borderRadius:this.itemRound,
  167. fontSize:this.itemFontSize,
  168. fontWeight:this.bold,
  169. '--color':this.color,
  170. '--border-color':this.borderColor
  171. }
  172. },
  173. cursorStyle(){
  174. return {
  175. width:this.cursorW,
  176. height:this.cursorH,
  177. backgroundColor:this.cursorBg
  178. }
  179. },
  180. lineStyle(){
  181. return {
  182. width:this.lineW,
  183. height:this.lineH,
  184. backgroundColor:this.lineBg,
  185. borderRadius:this.lineRound
  186. }
  187. },
  188. },
  189. methods:{
  190. input(e){
  191. const value = e.detail.value
  192. this.inputValue = value
  193. this.$emit('change', value)
  194. // #ifdef VUE2
  195. this.$emit('input', value)
  196. // #endif
  197. // #ifdef VUE3
  198. this.$emit('update:modelValue', value)
  199. // #endif
  200. if (String(value).length >= +this.maxlength) {
  201. this.$emit('finish', value)
  202. }
  203. }
  204. }
  205. }
  206. </script>
  207. <style lang="scss" scoped>
  208. .code-input-wrap{
  209. position: relative;
  210. display: flex;
  211. overflow: hidden;
  212. box-sizing: border-box;
  213. .code-input-item{
  214. position: relative;
  215. display: flex;
  216. align-items: center;
  217. justify-content: center;
  218. color: var(--color);
  219. &.border{
  220. border: 1rpx solid var(--border-color);
  221. }
  222. &:last-of-type{
  223. margin-right: 0 !important;
  224. }
  225. .cursor{
  226. opacity: 1;
  227. animation: cursor-flicker 1s infinite;
  228. }
  229. /* #ifndef APP-NVUE */
  230. @keyframes cursor-flicker{
  231. 0%{
  232. opacity: 1;
  233. }
  234. 50%{
  235. opacity: 0;
  236. }
  237. 100%{
  238. opacity: 1;
  239. }
  240. }
  241. /* #endif */
  242. .line{
  243. position: absolute;
  244. bottom: 0;
  245. left: 0;
  246. }
  247. .dot {
  248. width: 7px;
  249. height: 7px;
  250. border-radius: 100px;
  251. background-color: var(--color);
  252. }
  253. }
  254. .input{
  255. width: 1500rpx;
  256. position: absolute;
  257. height: 100%;
  258. left: -750rpx;
  259. top: 0;
  260. text-align: left;
  261. background-color: transparent;
  262. color: transparent;
  263. opacity: 0;
  264. // caret-color: transparent !important;
  265. &:deep(.uni-input-input){
  266. user-select: none;
  267. -webkit-user-select: none;
  268. &::selection {
  269. background: transparent;
  270. color: transparent;
  271. }
  272. }
  273. }
  274. }
  275. </style>