¶Ô±ÈÐÂÎļþ |
| | |
| | | <template> |
| | | <view class="u-code-input"> |
| | | <view |
| | | class="u-code-input__item" |
| | | :style="[itemStyle(index)]" |
| | | v-for="(item, index) in codeLength" |
| | | :key="index" |
| | | > |
| | | <view |
| | | class="u-code-input__item__dot" |
| | | v-if="dot && codeArray.length > index" |
| | | ></view> |
| | | <text |
| | | v-else |
| | | :style="{ |
| | | fontSize: $u.addUnit(fontSize), |
| | | fontWeight: bold ? 'bold' : 'normal', |
| | | color: color |
| | | }" |
| | | >{{codeArray[index]}}</text> |
| | | <view |
| | | class="u-code-input__item__line" |
| | | v-if="mode === 'line'" |
| | | :style="[lineStyle]" |
| | | ></view> |
| | | <!-- #ifndef APP-PLUS --> |
| | | <view v-if="isFocus && codeArray.length === index" :style="{backgroundColor: color}" class="u-code-input__item__cursor"></view> |
| | | <!-- #endif --> |
| | | </view> |
| | | <input |
| | | :disabled="disabledKeyboard" |
| | | type="number" |
| | | :focus="focus" |
| | | :value="inputValue" |
| | | :maxlength="maxlength" |
| | | :adjustPosition="adjustPosition" |
| | | class="u-code-input__input" |
| | | @input="inputHandler" |
| | | :style="{ |
| | | height: $u.addUnit(size) |
| | | }" |
| | | @focus="isFocus = true" |
| | | @blur="isFocus = false" |
| | | /> |
| | | </view> |
| | | </template> |
| | | |
| | | <script> |
| | | import props from './props.js'; |
| | | /** |
| | | * CodeInput éªè¯ç è¾å
¥ |
| | | * @description 该ç»ä»¶ä¸è¬ç¨äºéªè¯ç¨æ·çä¿¡éªè¯ç çåºæ¯ï¼ä¹å¯ä»¥ç»åuViewçé®çç»ä»¶ä½¿ç¨ |
| | | * @tutorial https://www.uviewui.com/components/codeInput.html |
| | | * @property {String | Number} maxlength æå¤§è¾å
¥é¿åº¦ ï¼é»è®¤ 6 ï¼ |
| | | * @property {Boolean} dot æ¯å¦ç¨åç¹å¡«å
ï¼é»è®¤ false ï¼ |
| | | * @property {String} mode æ¾ç¤ºæ¨¡å¼ï¼box-ç忍¡å¼ï¼line-åºé¨æ¨ªçº¿æ¨¡å¼ ï¼é»è®¤ 'box' ï¼ |
| | | * @property {Boolean} hairline æ¯å¦ç»è¾¹æ¡ ï¼é»è®¤ false ï¼ |
| | | * @property {String | Number} space å符é´çè·ç¦» ï¼é»è®¤ 10 ï¼ |
| | | * @property {String | Number} value é¢ç½®å¼ |
| | | * @property {Boolean} focus æ¯å¦èªå¨è·åç¦ç¹ ï¼é»è®¤ false ï¼ |
| | | * @property {Boolean} bold åä½åè¾å
¥æ¨ªçº¿æ¯å¦å ç² ï¼é»è®¤ false ï¼ |
| | | * @property {String} color åä½é¢è² ï¼é»è®¤ '#606266' ï¼ |
| | | * @property {String | Number} fontSize åä½å¤§å°ï¼åä½px ï¼é»è®¤ 18 ï¼ |
| | | * @property {String | Number} size è¾å
¥æ¡ç大å°ï¼å®½çäºé« ï¼é»è®¤ 35 ï¼ |
| | | * @property {Boolean} disabledKeyboard æ¯å¦éèåçé®çï¼å¦ææ³ç¨èªå®ä¹é®ççè¯ï¼é设置æ¤åæ°ä¸ºtrue ï¼é»è®¤ false ï¼ |
| | | * @property {String} borderColor è¾¹æ¡å线æ¡é¢è² ï¼é»è®¤ '#c9cacc' ï¼ |
| | | * @property {Boolean} disabledDot æ¯å¦ç¦æ¢è¾å
¥"."ç¬¦å· ï¼é»è®¤ true ï¼ |
| | | * |
| | | * @event {Function} change è¾å
¥å
容åçæ¹åæ¶è§¦åï¼å
·ä½è§ä¸æ¹è¯´æ valueï¼å½åè¾å
¥çå¼ |
| | | * @event {Function} finish è¾å
¥å符个æ°è¾¾maxlength弿¶è§¦åï¼è§ä¸æ¹è¯´æ valueï¼å½åè¾å
¥çå¼ |
| | | * @example <u-code-input v-model="value4" :focus="true"></u-code-input> |
| | | */ |
| | | export default { |
| | | name: 'u-code-input', |
| | | mixins: [uni.$u.mpMixin, uni.$u.mixin, props], |
| | | data() { |
| | | return { |
| | | inputValue: '', |
| | | isFocus: this.focus |
| | | } |
| | | }, |
| | | watch: { |
| | | value: { |
| | | immediate: true, |
| | | handler(val) { |
| | | // 转为å符串ï¼è¶
åºé¨åæªæ |
| | | this.inputValue = String(val).substring(0, this.maxlength) |
| | | } |
| | | }, |
| | | }, |
| | | computed: { |
| | | // æ ¹æ®é¿åº¦ï¼å¾ªç¯è¾å
¥æ¡ç个æ°ï¼å 为头æ¡å°ç¨åºæ°å¼ä¸è½ç¨äºv-for |
| | | codeLength() { |
| | | return new Array(Number(this.maxlength)) |
| | | }, |
| | | // 循ç¯itemçæ ·å¼ |
| | | itemStyle() { |
| | | return index => { |
| | | const addUnit = uni.$u.addUnit |
| | | const style = { |
| | | width: addUnit(this.size), |
| | | height: addUnit(this.size) |
| | | } |
| | | // ç忍¡å¼ä¸ï¼éè¦é¢å¤è¿è¡å¤ç |
| | | if (this.mode === 'box') { |
| | | // 设置çåçè¾¹æ¡ï¼å¦ææ¯ç»è¾¹æ¡ï¼å设置为0.5px宽度 |
| | | style.border = `${this.hairline ? 0.5 : 1}px solid ${this.borderColor}` |
| | | // 妿çåé´è·ä¸º0çè¯ |
| | | if (uni.$u.getPx(this.space) === 0) { |
| | | // ç»ç¬¬ä¸åæåä¸ä¸ªçå设置åè§ |
| | | if (index === 0) { |
| | | style.borderTopLeftRadius = '3px' |
| | | style.borderBottomLeftRadius = '3px' |
| | | } |
| | | if (index === this.codeLength.length - 1) { |
| | | style.borderTopRightRadius = '3px' |
| | | style.borderBottomRightRadius = '3px' |
| | | } |
| | | // æåä¸ä¸ªçåçå³è¾¹æ¡éè¦ä¿ç |
| | | if (index !== this.codeLength.length - 1) { |
| | | style.borderRight = 'none' |
| | | } |
| | | } |
| | | } |
| | | if (index !== this.codeLength.length - 1) { |
| | | // 设置éªè¯ç å符ä¹é´çè·ç¦»ï¼éè¿margin-rightè®¾ç½®ï¼æåä¸ä¸ªåç¬¦ï¼æ éå³è¾¹æ¡ |
| | | style.marginRight = addUnit(this.space) |
| | | } else { |
| | | // æåä¸ä¸ªçåçæè¾¹æ¡éè¦ä¿ç |
| | | style.marginRight = 0 |
| | | } |
| | | |
| | | return style |
| | | } |
| | | }, |
| | | // å°è¾å
¥çå¼ï¼è½¬ä¸ºæ°ç»ï¼ç»itemåéæ¶ï¼æ ¹æ®å½åçç´¢å¼æ¾ç¤ºæ°ç»çå
ç´ |
| | | codeArray() { |
| | | return String(this.inputValue).split('') |
| | | }, |
| | | // ä¸å线模å¼ä¸ï¼æ¨ªçº¿çæ ·å¼ |
| | | lineStyle() { |
| | | const style = {} |
| | | style.height = this.hairline ? '2px' : '4px' |
| | | style.width = uni.$u.addUnit(this.size) |
| | | // çº¿æ¡æ¨¡å¼ä¸ï¼èæ¯è²å³ä¸ºè¾¹æ¡é¢è² |
| | | style.backgroundColor = this.borderColor |
| | | return style |
| | | } |
| | | }, |
| | | methods: { |
| | | // çå¬è¾å
¥æ¡çå¼åçåå |
| | | inputHandler(e) { |
| | | const value = e.detail.value |
| | | this.inputValue = value |
| | | // æ¯å¦å
许è¾å
¥â.âç¬¦å· |
| | | if(this.disabledDot) { |
| | | this.$nextTick(() => { |
| | | this.inputValue = value.replace('.', '') |
| | | }) |
| | | } |
| | | // æªè¾¾å°maxlengthä¹åï¼åéchangeäºä»¶ï¼è¾¾å°ååéfinishäºä»¶ |
| | | this.$emit('change', value) |
| | | // ä¿®æ¹éè¿v-modelååç»å®çå¼ |
| | | this.$emit('input', value) |
| | | // è¾¾å°ç¨æ·æå®è¾å
¥é¿åº¦æ¶ï¼ååºå®æäºä»¶ |
| | | if (String(value).length >= Number(this.maxlength)) { |
| | | this.$emit('finish', value) |
| | | } |
| | | } |
| | | } |
| | | } |
| | | </script> |
| | | |
| | | <style lang="scss" scoped> |
| | | @import "../../libs/css/components.scss"; |
| | | $u-code-input-cursor-width: 1px; |
| | | $u-code-input-cursor-height: 40%; |
| | | $u-code-input-cursor-animation-duration: 1s; |
| | | $u-code-input-cursor-animation-name: u-cursor-flicker; |
| | | |
| | | .u-code-input { |
| | | @include flex; |
| | | position: relative; |
| | | overflow: hidden; |
| | | |
| | | &__item { |
| | | @include flex; |
| | | justify-content: center; |
| | | align-items: center; |
| | | position: relative; |
| | | |
| | | &__text { |
| | | font-size: 15px; |
| | | color: $u-content-color; |
| | | } |
| | | |
| | | &__dot { |
| | | width: 7px; |
| | | height: 7px; |
| | | border-radius: 100px; |
| | | background-color: $u-content-color; |
| | | } |
| | | |
| | | &__line { |
| | | position: absolute; |
| | | bottom: 0; |
| | | height: 4px; |
| | | border-radius: 100px; |
| | | width: 40px; |
| | | background-color: $u-content-color; |
| | | } |
| | | /* #ifndef APP-PLUS */ |
| | | &__cursor { |
| | | position: absolute; |
| | | top: 50%; |
| | | left: 50%; |
| | | transform: translate(-50%,-50%); |
| | | width: $u-code-input-cursor-width; |
| | | height: $u-code-input-cursor-height; |
| | | animation: $u-code-input-cursor-animation-duration u-cursor-flicker infinite; |
| | | } |
| | | /* #endif */ |
| | | |
| | | } |
| | | |
| | | &__input { |
| | | // 乿以éè¦inputè¾å
¥æ¡ï¼æ¯å 为æå®æè½å¤èµ·é®ç |
| | | // è¿éå°å®è®¾ç½®ä¸ºä¸¤åçå±å¹å®½åº¦ï¼åå°å·¦è¾¹çä¸åç§»åºå±å¹ï¼ä¸ºäºä¸è®©ç¨æ·çå°è¾å
¥çå
容 |
| | | position: absolute; |
| | | left: -750rpx; |
| | | width: 1500rpx; |
| | | top: 0; |
| | | background-color: transparent; |
| | | text-align: left; |
| | | } |
| | | } |
| | | |
| | | /* #ifndef APP-PLUS */ |
| | | @keyframes u-cursor-flicker { |
| | | 0% { |
| | | opacity: 0; |
| | | } |
| | | 50% { |
| | | opacity: 1; |
| | | } |
| | | 100% { |
| | | opacity: 0; |
| | | } |
| | | } |
| | | /* #endif */ |
| | | |
| | | </style> |