uni-id-pages-x-input.uvue 4.2 KB
Newer Older
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
1 2 3 4 5 6
<template>
	<view class="input-box" :class="{border}">
		<!-- <solt></solt> -->
		<text v-if="hasTitleAndContent" class="title">{{title}}</text>
		<input class="input" ref="input" :placeholder="_placeholder" :class="{'move-down':hasTitleAndContent}"
			:value="modelValue" @input="updateValue" :type="type" :maxlength="maxlength"
DCloud_JSON's avatar
DCloud_JSON 已提交
7
			:password="password && !visiblePwd" :focus="focusInput" @blur="onBlur" @focus="onFocus" @confirm="confirm"
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
8 9
			:confirm-type="confirmType" />
		<view class="my-input-icon-box" v-if="modelValue">
A
Anne_LXM 已提交
10 11
			<uni-id-pages-x-icons v-if="password" :color="visiblePwd?'#0070ff':'#ddd'"
        @click="setVisiblePwd('click')" @touchstart="setVisiblePwd('touchstart')" @touchend="setVisiblePwd('touchend')" @touchcancel="setVisiblePwd('touchcancel')"
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
12 13 14 15 16 17 18 19 20 21 22
      type="eyes" />
			<view class="password-icon">
				<uni-id-pages-x-icons @click="clearValue" type="clear" color="#ddd" />
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'MyInput',
A
Anne_LXM 已提交
23
		emits: ["blur", "focus", "confirm", "update:modelValue"],
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
24 25
		data() {
			return {
DCloud_JSON's avatar
DCloud_JSON 已提交
26 27
				visiblePwd: false,
        focusInput: false
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
28 29
			}
		},
DCloud_JSON's avatar
DCloud_JSON 已提交
30 31 32 33 34
    mounted(){
      if(this.focus){
        this.setFocus(true)
      }
    },
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
		computed: {
			_placeholder() : string {
				if (this.placeholder.length > 0) {
					return this.placeholder
				} else if (this.title.length > 0) {
					return "请输入" + this.title
				} else {
					return ""
				}
			},
			hasTitleAndContent() : boolean {
				return this.title.length > 0 && this.modelValue.length > 0
			}
		},
		props: {
			title: {
				type: String,
				default: ""
			},
			modelValue: {
				type: String,
				default: ""
			},
			type: {
				type: String,
				default: "text"
			},
			placeholder: {
				type: String,
				default: ""
			},
			maxlength: {
				type: Number,
				default: 140
			},
			password: {
				type: Boolean,
				default: false
			},
			focus: {
				type: Boolean,
				default: false
			},
			border: {
				type: Boolean,
				default: false
			},
			confirmType: {
				type: String,
				default: "done"
			},
      /**
DCloud_JSON's avatar
DCloud_JSON 已提交
87 88 89 90
       * 显示密码的方式
       * @type {String}
       * @default click
       * @example click | touch
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
       */
      visiblePwdOption:{
        type: String,
        default: "click"
      }
		},
		methods: {
			updateValue(event : InputEvent) {
				// console.log('event.detail.value', event.detail.value);
				this.$emit('update:modelValue', event.detail.value);
			},
			clearValue() {
				this.$emit('update:modelValue', '');
				this.setFocus(true)
			},
			onBlur() {
				// this.$emit('update:focus', false);
				this.$emit('blur');
			},
			onFocus() {
				this.$emit('focus');
				// this.$emit('update:focus', true);
			},
			setFocus(state : boolean) {
DCloud_JSON's avatar
DCloud_JSON 已提交
115 116
        // console.error('setFocus',state)
				this.focusInput = state
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
117 118 119 120 121
			},
			confirm(event : InputConfirmEvent) {
				this.$emit('confirm', event)
			},
      setVisiblePwd(eventName:string){
DCloud_JSON's avatar
DCloud_JSON 已提交
122
        console.error('eventName',eventName)
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
123 124
        if(eventName == "click" && this.visiblePwdOption == 'click'){
          this.visiblePwd = !this.visiblePwd
DCloud_JSON's avatar
DCloud_JSON 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137
        }else if(eventName.includes("touch") && this.visiblePwdOption == 'touch'){
          console.error('eventName',eventName)
          switch (eventName){
            case "touchstart":
              this.visiblePwd = true
              break;
            case "touchend":
              this.visiblePwd = false
              break;
            case "touchcancel":
              this.visiblePwd = false
              break;
          }
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
        }
      }
		}
	}
</script>

<style lang="scss" scoped>
	.input-box {
		position: relative;
		background-color: #F8F8F8;
		height: 50px !important;
		border-radius: 5rpx;
		// border: 1px solid #000;
	}

	.border {
		border: 1px solid #86868b;
	}

	.input {
		border-radius: 5px;
		height: 50px;
		padding: 0 16rpx 0 12px;
		font-size: 14px;
		top: 0;
		position: relative;
	}

	.title {
		position: absolute;
		top: 0;
		left: 16rpx;
		color: #555;
		font-weight: 400;
		font-size: 12px;
		padding-left:5px;
		height: 30px;
		line-height: 30px;
	}

	.input.move-down {
		top: 22px;
		height: 22px;
	}

	.my-input-icon-box {
		position: absolute;
		right: 10px;
		bottom:14px;
		flex-direction: row;
DCloud_JSON's avatar
DCloud_JSON 已提交
188
    z-index: 2;
DCloud_JSON's avatar
init  
DCloud_JSON 已提交
189 190 191 192 193
	}

	.password-icon {
		margin-left: 5px;
	}
A
Anne_LXM 已提交
194
</style>