|
@ -75,7 +75,7 @@ |
|
|
</template> |
|
|
</template> |
|
|
|
|
|
|
|
|
<script> |
|
|
<script> |
|
|
import props from "./props.js"; |
|
|
import props from './props.js'; |
|
|
/** |
|
|
/** |
|
|
* Input 输入框 |
|
|
* Input 输入框 |
|
|
* @description 此组件为一个输入框,默认没有边框和样式,是专门为配合表单组件u-form而设计的,利用它可以快速实现表单验证,输入内容,下拉选择等功能。 |
|
|
* @description 此组件为一个输入框,默认没有边框和样式,是专门为配合表单组件u-form而设计的,利用它可以快速实现表单验证,输入内容,下拉选择等功能。 |
|
@ -89,7 +89,7 @@ import props from "./props.js"; |
|
|
* @property {Boolean} password 是否密码类型 ( 默认 false ) |
|
|
* @property {Boolean} password 是否密码类型 ( 默认 false ) |
|
|
* @property {String | Number} maxlength 最大输入长度,设置为 -1 的时候不限制最大长度 ( 默认 -1 ) |
|
|
* @property {String | Number} maxlength 最大输入长度,设置为 -1 的时候不限制最大长度 ( 默认 -1 ) |
|
|
* @property {String} placeholder 输入框为空时的占位符 |
|
|
* @property {String} placeholder 输入框为空时的占位符 |
|
|
* @property {String} placeholderClass 指定placeholder的样式类,注意页面或组件的style中写了scoped时,需要在类名前写/deep/ ( 默认 'input-placeholder' ) |
|
|
* @property {String} placeholderClass 指定placeholder的样式类,注意页面或组件的style中写了scoped时,需要在类名前写::v-deep ( 默认 'input-placeholder' ) |
|
|
* @property {String | Object} placeholderStyle 指定placeholder的样式,字符串/对象形式,如"color: red;" |
|
|
* @property {String | Object} placeholderStyle 指定placeholder的样式,字符串/对象形式,如"color: red;" |
|
|
* @property {Boolean} showWordLimit 是否显示输入字数统计,只在 type ="text"或type ="textarea"时有效 ( 默认 false ) |
|
|
* @property {Boolean} showWordLimit 是否显示输入字数统计,只在 type ="text"或type ="textarea"时有效 ( 默认 false ) |
|
|
* @property {String} confirmType 设置右下角按钮的文字,兼容性详见uni-app文档 ( 默认 'done' ) |
|
|
* @property {String} confirmType 设置右下角按钮的文字,兼容性详见uni-app文档 ( 默认 'done' ) |
|
@ -119,12 +119,12 @@ import props from "./props.js"; |
|
|
* @example <u-input v-model="value" :password="true" suffix-icon="lock-fill" /> |
|
|
* @example <u-input v-model="value" :password="true" suffix-icon="lock-fill" /> |
|
|
*/ |
|
|
*/ |
|
|
export default { |
|
|
export default { |
|
|
name: "u-input", |
|
|
name: 'u-input', |
|
|
mixins: [uni.$u.mpMixin, uni.$u.mixin, props], |
|
|
mixins: [uni.$u.mpMixin, uni.$u.mixin, props], |
|
|
data() { |
|
|
data() { |
|
|
return { |
|
|
return { |
|
|
// 输入框的值 |
|
|
// 输入框的值 |
|
|
innerValue: "", |
|
|
innerValue: '', |
|
|
// 是否处于获得焦点状态 |
|
|
// 是否处于获得焦点状态 |
|
|
focused: false, |
|
|
focused: false, |
|
|
// value是否第一次变化,在watch中,由于加入immediate属性,会在第一次触发,此时不应该认为value发生了变化 |
|
|
// value是否第一次变化,在watch中,由于加入immediate属性,会在第一次触发,此时不应该认为value发生了变化 |
|
@ -132,7 +132,7 @@ export default { |
|
|
// value绑定值的变化是由内部还是外部引起的 |
|
|
// value绑定值的变化是由内部还是外部引起的 |
|
|
changeFromInner: false, |
|
|
changeFromInner: false, |
|
|
// 过滤处理方法 |
|
|
// 过滤处理方法 |
|
|
innerFormatter: value => value |
|
|
innerFormatter: (value) => value, |
|
|
}; |
|
|
}; |
|
|
}, |
|
|
}, |
|
|
watch: { |
|
|
watch: { |
|
@ -159,21 +159,21 @@ export default { |
|
|
// 是否显示清除控件 |
|
|
// 是否显示清除控件 |
|
|
isShowClear() { |
|
|
isShowClear() { |
|
|
const { clearable, readonly, focused, innerValue } = this; |
|
|
const { clearable, readonly, focused, innerValue } = this; |
|
|
return !!clearable && !readonly && !!focused && innerValue !== ""; |
|
|
return !!clearable && !readonly && !!focused && innerValue !== ''; |
|
|
}, |
|
|
}, |
|
|
// 组件的类名 |
|
|
// 组件的类名 |
|
|
inputClass() { |
|
|
inputClass() { |
|
|
let classes = [], |
|
|
let classes = [], |
|
|
{ border, disabled, shape } = this; |
|
|
{ border, disabled, shape } = this; |
|
|
border === "surround" && |
|
|
border === 'surround' && |
|
|
(classes = classes.concat(["u-border", "u-input--radius"])); |
|
|
(classes = classes.concat(['u-border', 'u-input--radius'])); |
|
|
classes.push(`u-input--${shape}`); |
|
|
classes.push(`u-input--${shape}`); |
|
|
border === "bottom" && |
|
|
border === 'bottom' && |
|
|
(classes = classes.concat([ |
|
|
(classes = classes.concat([ |
|
|
"u-border-bottom", |
|
|
'u-border-bottom', |
|
|
"u-input--no-radius", |
|
|
'u-input--no-radius', |
|
|
])); |
|
|
])); |
|
|
return classes.join(" "); |
|
|
return classes.join(' '); |
|
|
}, |
|
|
}, |
|
|
// 组件的样式 |
|
|
// 组件的样式 |
|
|
wrapperStyle() { |
|
|
wrapperStyle() { |
|
@ -183,14 +183,14 @@ export default { |
|
|
style.backgroundColor = this.disabledColor; |
|
|
style.backgroundColor = this.disabledColor; |
|
|
} |
|
|
} |
|
|
// 无边框时,去除内边距 |
|
|
// 无边框时,去除内边距 |
|
|
if (this.border === "none") { |
|
|
if (this.border === 'none') { |
|
|
style.padding = "0"; |
|
|
style.padding = '0'; |
|
|
} else { |
|
|
} else { |
|
|
// 由于uni-app的iOS开发者能力有限,导致需要分开写才有效 |
|
|
// 由于uni-app的iOS开发者能力有限,导致需要分开写才有效 |
|
|
style.paddingTop = "6px"; |
|
|
style.paddingTop = '6px'; |
|
|
style.paddingBottom = "6px"; |
|
|
style.paddingBottom = '6px'; |
|
|
style.paddingLeft = "9px"; |
|
|
style.paddingLeft = '9px'; |
|
|
style.paddingRight = "9px"; |
|
|
style.paddingRight = '9px'; |
|
|
} |
|
|
} |
|
|
return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle)); |
|
|
return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle)); |
|
|
}, |
|
|
}, |
|
@ -199,7 +199,7 @@ export default { |
|
|
const style = { |
|
|
const style = { |
|
|
color: this.color, |
|
|
color: this.color, |
|
|
fontSize: uni.$u.addUnit(this.fontSize), |
|
|
fontSize: uni.$u.addUnit(this.fontSize), |
|
|
textAlign: this.inputAlign |
|
|
textAlign: this.inputAlign, |
|
|
}; |
|
|
}; |
|
|
return style; |
|
|
return style; |
|
|
}, |
|
|
}, |
|
@ -207,64 +207,64 @@ export default { |
|
|
methods: { |
|
|
methods: { |
|
|
// 在微信小程序中,不支持将函数当做props参数,故只能通过ref形式调用 |
|
|
// 在微信小程序中,不支持将函数当做props参数,故只能通过ref形式调用 |
|
|
setFormatter(e) { |
|
|
setFormatter(e) { |
|
|
this.innerFormatter = e |
|
|
this.innerFormatter = e; |
|
|
}, |
|
|
}, |
|
|
// 当键盘输入时,触发input事件 |
|
|
// 当键盘输入时,触发input事件 |
|
|
onInput(e) { |
|
|
onInput(e) { |
|
|
let { value = "" } = e.detail || {}; |
|
|
let { value = '' } = e.detail || {}; |
|
|
// 格式化过滤方法 |
|
|
// 格式化过滤方法 |
|
|
const formatter = this.formatter || this.innerFormatter |
|
|
const formatter = this.formatter || this.innerFormatter; |
|
|
const formatValue = formatter(value) |
|
|
const formatValue = formatter(value); |
|
|
// 为了避免props的单向数据流特性,需要先将innerValue值设置为当前值,再在$nextTick中重新赋予设置后的值才有效 |
|
|
// 为了避免props的单向数据流特性,需要先将innerValue值设置为当前值,再在$nextTick中重新赋予设置后的值才有效 |
|
|
this.innerValue = value |
|
|
this.innerValue = value; |
|
|
this.$nextTick(() => { |
|
|
this.$nextTick(() => { |
|
|
this.innerValue = formatValue; |
|
|
this.innerValue = formatValue; |
|
|
this.valueChange(); |
|
|
this.valueChange(); |
|
|
}) |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
// 输入框失去焦点时触发 |
|
|
// 输入框失去焦点时触发 |
|
|
onBlur(event) { |
|
|
onBlur(event) { |
|
|
this.$emit("blur", event.detail.value); |
|
|
this.$emit('blur', event.detail.value); |
|
|
// H5端的blur会先于点击清除控件的点击click事件触发,导致focused |
|
|
// H5端的blur会先于点击清除控件的点击click事件触发,导致focused |
|
|
// 瞬间为false,从而隐藏了清除控件而无法被点击到 |
|
|
// 瞬间为false,从而隐藏了清除控件而无法被点击到 |
|
|
uni.$u.sleep(50).then(() => { |
|
|
uni.$u.sleep(50).then(() => { |
|
|
this.focused = false; |
|
|
this.focused = false; |
|
|
}); |
|
|
}); |
|
|
// 尝试调用u-form的验证方法 |
|
|
// 尝试调用u-form的验证方法 |
|
|
uni.$u.formValidate(this, "blur"); |
|
|
uni.$u.formValidate(this, 'blur'); |
|
|
}, |
|
|
}, |
|
|
// 输入框聚焦时触发 |
|
|
// 输入框聚焦时触发 |
|
|
onFocus(event) { |
|
|
onFocus(event) { |
|
|
this.focused = true; |
|
|
this.focused = true; |
|
|
this.$emit("focus"); |
|
|
this.$emit('focus'); |
|
|
}, |
|
|
}, |
|
|
// 点击完成按钮时触发 |
|
|
// 点击完成按钮时触发 |
|
|
onConfirm(event) { |
|
|
onConfirm(event) { |
|
|
this.$emit("confirm", this.innerValue); |
|
|
this.$emit('confirm', this.innerValue); |
|
|
}, |
|
|
}, |
|
|
// 键盘高度发生变化的时候触发此事件 |
|
|
// 键盘高度发生变化的时候触发此事件 |
|
|
// 兼容性:微信小程序2.7.0+、App 3.1.0+ |
|
|
// 兼容性:微信小程序2.7.0+、App 3.1.0+ |
|
|
onkeyboardheightchange() { |
|
|
onkeyboardheightchange() { |
|
|
this.$emit("keyboardheightchange"); |
|
|
this.$emit('keyboardheightchange'); |
|
|
}, |
|
|
}, |
|
|
// 内容发生变化,进行处理 |
|
|
// 内容发生变化,进行处理 |
|
|
valueChange() { |
|
|
valueChange() { |
|
|
const value = this.innerValue; |
|
|
const value = this.innerValue; |
|
|
this.$nextTick(() => { |
|
|
this.$nextTick(() => { |
|
|
this.$emit("input", value); |
|
|
this.$emit('input', value); |
|
|
// 标识value值的变化是由内部引起的 |
|
|
// 标识value值的变化是由内部引起的 |
|
|
this.changeFromInner = true; |
|
|
this.changeFromInner = true; |
|
|
this.$emit("change", value); |
|
|
this.$emit('change', value); |
|
|
// 尝试调用u-form的验证方法 |
|
|
// 尝试调用u-form的验证方法 |
|
|
uni.$u.formValidate(this, "change"); |
|
|
uni.$u.formValidate(this, 'change'); |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
// 点击清除控件 |
|
|
// 点击清除控件 |
|
|
onClear() { |
|
|
onClear() { |
|
|
this.innerValue = ""; |
|
|
this.innerValue = ''; |
|
|
this.$nextTick(() => { |
|
|
this.$nextTick(() => { |
|
|
this.valueChange(); |
|
|
this.valueChange(); |
|
|
this.$emit("clear"); |
|
|
this.$emit('clear'); |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
/** |
|
|
/** |
|
@ -274,8 +274,8 @@ export default { |
|
|
*/ |
|
|
*/ |
|
|
clickHandler() { |
|
|
clickHandler() { |
|
|
// #ifdef APP-NVUE |
|
|
// #ifdef APP-NVUE |
|
|
if (uni.$u.os() === "android") { |
|
|
if (uni.$u.os() === 'android') { |
|
|
const formItem = uni.$u.$parent.call(this, "u-form-item"); |
|
|
const formItem = uni.$u.$parent.call(this, 'u-form-item'); |
|
|
if (formItem) { |
|
|
if (formItem) { |
|
|
formItem.clickHandler(); |
|
|
formItem.clickHandler(); |
|
|
} |
|
|
} |
|
@ -287,7 +287,7 @@ export default { |
|
|
</script> |
|
|
</script> |
|
|
|
|
|
|
|
|
<style lang="scss" scoped> |
|
|
<style lang="scss" scoped> |
|
|
@import "../../libs/css/components.scss"; |
|
|
@import '../../libs/css/components.scss'; |
|
|
|
|
|
|
|
|
.u-input { |
|
|
.u-input { |
|
|
@include flex(row); |
|
|
@include flex(row); |
|
|