|
|
|
@ -1,47 +1,47 @@
|
|
|
|
|
<template> |
|
|
|
|
<view class="u-textarea" :class="textareaClass" :style="[textareaStyle]"> |
|
|
|
|
<textarea |
|
|
|
|
class="u-textarea__field" |
|
|
|
|
:value="innerValue" |
|
|
|
|
:style="{ height: $u.addUnit(height) }" |
|
|
|
|
:placeholder="placeholder" |
|
|
|
|
:placeholder-style="$u.addStyle(placeholderStyle, 'string')" |
|
|
|
|
:placeholder-class="placeholderClass" |
|
|
|
|
:disabled="disabled" |
|
|
|
|
:focus="focus" |
|
|
|
|
:autoHeight="autoHeight" |
|
|
|
|
:fixed="fixed" |
|
|
|
|
:cursorSpacing="cursorSpacing" |
|
|
|
|
:cursor="cursor" |
|
|
|
|
:showConfirmBar="showConfirmBar" |
|
|
|
|
:selectionStart="selectionStart" |
|
|
|
|
:selectionEnd="selectionEnd" |
|
|
|
|
:adjustPosition="adjustPosition" |
|
|
|
|
:disableDefaultPadding="disableDefaultPadding" |
|
|
|
|
:holdKeyboard="holdKeyboard" |
|
|
|
|
:maxlength="maxlength" |
|
|
|
|
:confirmType="confirmType" |
|
|
|
|
:ignoreCompositionEvent="ignoreCompositionEvent" |
|
|
|
|
@focus="onFocus" |
|
|
|
|
@blur="onBlur" |
|
|
|
|
@linechange="onLinechange" |
|
|
|
|
@input="onInput" |
|
|
|
|
@confirm="onConfirm" |
|
|
|
|
@keyboardheightchange="onKeyboardheightchange" |
|
|
|
|
></textarea> |
|
|
|
|
<text |
|
|
|
|
class="u-textarea__count" |
|
|
|
|
:style="{ |
|
|
|
|
'background-color': disabled ? 'transparent' : '#fff', |
|
|
|
|
}" |
|
|
|
|
v-if="count" |
|
|
|
|
>{{ innerValue.length }}/{{ maxlength }}</text |
|
|
|
|
> |
|
|
|
|
</view> |
|
|
|
|
<view class="u-textarea" :class="textareaClass" :style="[textareaStyle]"> |
|
|
|
|
<textarea |
|
|
|
|
class="u-textarea__field" |
|
|
|
|
:value="innerValue" |
|
|
|
|
:placeholder="placeholder" |
|
|
|
|
:placeholder-style="$u.addStyle(placeholderStyle, 'string')" |
|
|
|
|
:placeholder-class="placeholderClass" |
|
|
|
|
:disabled="disabled" |
|
|
|
|
:focus="focus" |
|
|
|
|
:autoHeight="autoHeight" |
|
|
|
|
:fixed="fixed" |
|
|
|
|
:cursorSpacing="cursorSpacing" |
|
|
|
|
:cursor="cursor" |
|
|
|
|
:showConfirmBar="showConfirmBar" |
|
|
|
|
:selectionStart="selectionStart" |
|
|
|
|
:selectionEnd="selectionEnd" |
|
|
|
|
:adjustPosition="adjustPosition" |
|
|
|
|
:disableDefaultPadding="disableDefaultPadding" |
|
|
|
|
:holdKeyboard="holdKeyboard" |
|
|
|
|
:maxlength="maxlength" |
|
|
|
|
:confirmType="confirmType" |
|
|
|
|
:ignoreCompositionEvent="ignoreCompositionEvent" |
|
|
|
|
@focus="onFocus" |
|
|
|
|
@blur="onBlur" |
|
|
|
|
@linechange="onLinechange" |
|
|
|
|
@input="onInput" |
|
|
|
|
@confirm="onConfirm" |
|
|
|
|
@keyboardheightchange="onKeyboardheightchange" |
|
|
|
|
></textarea> |
|
|
|
|
<text |
|
|
|
|
class="u-textarea__count" |
|
|
|
|
:style="{ |
|
|
|
|
'background-color': disabled ? 'transparent' : '#fff' |
|
|
|
|
}" |
|
|
|
|
v-if="count" |
|
|
|
|
> |
|
|
|
|
{{ innerValue.length }}/{{ maxlength }} |
|
|
|
|
</text> |
|
|
|
|
</view> |
|
|
|
|
</template> |
|
|
|
|
|
|
|
|
|
<script> |
|
|
|
|
import props from "./props.js"; |
|
|
|
|
import props from './props.js' |
|
|
|
|
/** |
|
|
|
|
* Textarea 文本域 |
|
|
|
|
* @description 文本域此组件满足了可能出现的表单信息补充,编辑等实际逻辑的功能,内置了字数校验等 |
|
|
|
@ -80,160 +80,157 @@ import props from "./props.js";
|
|
|
|
|
* @example <u--textarea v-model="value1" placeholder="请输入内容" ></u--textarea> |
|
|
|
|
*/ |
|
|
|
|
export default { |
|
|
|
|
name: "u-textarea", |
|
|
|
|
mixins: [uni.$u.mpMixin, uni.$u.mixin, props], |
|
|
|
|
data() { |
|
|
|
|
return { |
|
|
|
|
// 输入框的值 |
|
|
|
|
innerValue: "", |
|
|
|
|
// 是否处于获得焦点状态 |
|
|
|
|
focused: false, |
|
|
|
|
// value是否第一次变化,在watch中,由于加入immediate属性,会在第一次触发,此时不应该认为value发生了变化 |
|
|
|
|
firstChange: true, |
|
|
|
|
// value绑定值的变化是由内部还是外部引起的 |
|
|
|
|
changeFromInner: false, |
|
|
|
|
// 过滤处理方法 |
|
|
|
|
innerFormatter: value => value |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
watch: { |
|
|
|
|
value: { |
|
|
|
|
immediate: true, |
|
|
|
|
handler(newVal, oldVal) { |
|
|
|
|
this.innerValue = newVal; |
|
|
|
|
/* #ifdef H5 */ |
|
|
|
|
// 在H5中,外部value变化后,修改input中的值,不会触发@input事件,此时手动调用值变化方法 |
|
|
|
|
if ( |
|
|
|
|
this.firstChange === false && |
|
|
|
|
this.changeFromInner === false |
|
|
|
|
) { |
|
|
|
|
this.valueChange(); |
|
|
|
|
} |
|
|
|
|
/* #endif */ |
|
|
|
|
this.firstChange = false; |
|
|
|
|
// 重置changeFromInner的值为false,标识下一次引起默认为外部引起的 |
|
|
|
|
this.changeFromInner = false; |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
computed: { |
|
|
|
|
// 组件的类名 |
|
|
|
|
textareaClass() { |
|
|
|
|
let classes = [], |
|
|
|
|
{ border, disabled, shape } = this; |
|
|
|
|
border === "surround" && |
|
|
|
|
(classes = classes.concat(["u-border", "u-textarea--radius"])); |
|
|
|
|
border === "bottom" && |
|
|
|
|
(classes = classes.concat([ |
|
|
|
|
"u-border-bottom", |
|
|
|
|
"u-textarea--no-radius", |
|
|
|
|
])); |
|
|
|
|
disabled && classes.push("u-textarea--disabled"); |
|
|
|
|
return classes.join(" "); |
|
|
|
|
}, |
|
|
|
|
// 组件的样式 |
|
|
|
|
textareaStyle() { |
|
|
|
|
const style = {}; |
|
|
|
|
// #ifdef APP-NVUE |
|
|
|
|
// 由于textarea在安卓nvue上的差异性,需要额外再调整其内边距 |
|
|
|
|
if (uni.$u.os() === "android") { |
|
|
|
|
style.paddingTop = "6px"; |
|
|
|
|
style.paddingLeft = "9px"; |
|
|
|
|
style.paddingBottom = "3px"; |
|
|
|
|
style.paddingRight = "6px"; |
|
|
|
|
} |
|
|
|
|
// #endif |
|
|
|
|
return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle)); |
|
|
|
|
}, |
|
|
|
|
name: 'u-textarea', |
|
|
|
|
mixins: [uni.$u.mpMixin, uni.$u.mixin, props], |
|
|
|
|
data() { |
|
|
|
|
return { |
|
|
|
|
// 输入框的值 |
|
|
|
|
innerValue: '', |
|
|
|
|
// 是否处于获得焦点状态 |
|
|
|
|
focused: false, |
|
|
|
|
// value是否第一次变化,在watch中,由于加入immediate属性,会在第一次触发,此时不应该认为value发生了变化 |
|
|
|
|
firstChange: true, |
|
|
|
|
// value绑定值的变化是由内部还是外部引起的 |
|
|
|
|
changeFromInner: false, |
|
|
|
|
// 过滤处理方法 |
|
|
|
|
innerFormatter: value => value |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
watch: { |
|
|
|
|
value: { |
|
|
|
|
immediate: true, |
|
|
|
|
handler(newVal, oldVal) { |
|
|
|
|
this.innerValue = newVal |
|
|
|
|
/* #ifdef H5 */ |
|
|
|
|
// 在H5中,外部value变化后,修改input中的值,不会触发@input事件,此时手动调用值变化方法 |
|
|
|
|
if (this.firstChange === false && this.changeFromInner === false) { |
|
|
|
|
this.valueChange() |
|
|
|
|
} |
|
|
|
|
/* #endif */ |
|
|
|
|
this.firstChange = false |
|
|
|
|
// 重置changeFromInner的值为false,标识下一次引起默认为外部引起的 |
|
|
|
|
this.changeFromInner = false |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
computed: { |
|
|
|
|
// 组件的类名 |
|
|
|
|
textareaClass() { |
|
|
|
|
let classes = [], |
|
|
|
|
{ border, disabled, shape } = this |
|
|
|
|
border === 'surround' && |
|
|
|
|
(classes = classes.concat(['u-border', 'u-textarea--radius'])) |
|
|
|
|
border === 'bottom' && |
|
|
|
|
(classes = classes.concat([ |
|
|
|
|
'u-border-bottom', |
|
|
|
|
'u-textarea--no-radius' |
|
|
|
|
])) |
|
|
|
|
disabled && classes.push('u-textarea--disabled') |
|
|
|
|
return classes.join(' ') |
|
|
|
|
}, |
|
|
|
|
// 组件的样式 |
|
|
|
|
textareaStyle() { |
|
|
|
|
const style = {} |
|
|
|
|
// #ifdef APP-NVUE |
|
|
|
|
// 由于textarea在安卓nvue上的差异性,需要额外再调整其内边距 |
|
|
|
|
if (uni.$u.os() === 'android') { |
|
|
|
|
style.paddingTop = '6px' |
|
|
|
|
style.paddingLeft = '9px' |
|
|
|
|
style.paddingBottom = '3px' |
|
|
|
|
style.paddingRight = '6px' |
|
|
|
|
} |
|
|
|
|
// #endif |
|
|
|
|
return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle)) |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
methods: { |
|
|
|
|
// 在微信小程序中,不支持将函数当做props参数,故只能通过ref形式调用 |
|
|
|
|
setFormatter(e) { |
|
|
|
|
this.innerFormatter = e |
|
|
|
|
}, |
|
|
|
|
onFocus(e) { |
|
|
|
|
this.$emit('focus', e) |
|
|
|
|
}, |
|
|
|
|
methods: { |
|
|
|
|
// 在微信小程序中,不支持将函数当做props参数,故只能通过ref形式调用 |
|
|
|
|
setFormatter(e) { |
|
|
|
|
this.innerFormatter = e |
|
|
|
|
}, |
|
|
|
|
onFocus(e) { |
|
|
|
|
this.$emit("focus", e); |
|
|
|
|
}, |
|
|
|
|
onBlur(e) { |
|
|
|
|
this.$emit("blur", e); |
|
|
|
|
// 尝试调用u-form的验证方法 |
|
|
|
|
uni.$u.formValidate(this, "blur"); |
|
|
|
|
}, |
|
|
|
|
onLinechange(e) { |
|
|
|
|
this.$emit("linechange", e); |
|
|
|
|
}, |
|
|
|
|
onInput(e) { |
|
|
|
|
let { value = "" } = e.detail || {}; |
|
|
|
|
// 格式化过滤方法 |
|
|
|
|
const formatter = this.formatter || this.innerFormatter |
|
|
|
|
const formatValue = formatter(value) |
|
|
|
|
// 为了避免props的单向数据流特性,需要先将innerValue值设置为当前值,再在$nextTick中重新赋予设置后的值才有效 |
|
|
|
|
this.innerValue = value |
|
|
|
|
this.$nextTick(() => { |
|
|
|
|
this.innerValue = formatValue; |
|
|
|
|
this.valueChange(); |
|
|
|
|
}) |
|
|
|
|
}, |
|
|
|
|
// 内容发生变化,进行处理 |
|
|
|
|
valueChange() { |
|
|
|
|
const value = this.innerValue; |
|
|
|
|
this.$nextTick(() => { |
|
|
|
|
this.$emit("input", value); |
|
|
|
|
// 标识value值的变化是由内部引起的 |
|
|
|
|
this.changeFromInner = true; |
|
|
|
|
this.$emit("change", value); |
|
|
|
|
// 尝试调用u-form的验证方法 |
|
|
|
|
uni.$u.formValidate(this, "change"); |
|
|
|
|
}); |
|
|
|
|
}, |
|
|
|
|
onConfirm(e) { |
|
|
|
|
this.$emit("confirm", e); |
|
|
|
|
}, |
|
|
|
|
onKeyboardheightchange(e) { |
|
|
|
|
this.$emit("keyboardheightchange", e); |
|
|
|
|
}, |
|
|
|
|
onBlur(e) { |
|
|
|
|
this.$emit('blur', e) |
|
|
|
|
// 尝试调用u-form的验证方法 |
|
|
|
|
uni.$u.formValidate(this, 'blur') |
|
|
|
|
}, |
|
|
|
|
}; |
|
|
|
|
onLinechange(e) { |
|
|
|
|
this.$emit('linechange', e) |
|
|
|
|
}, |
|
|
|
|
onInput(e) { |
|
|
|
|
let { value = '' } = e.detail || {} |
|
|
|
|
// 格式化过滤方法 |
|
|
|
|
const formatter = this.formatter || this.innerFormatter |
|
|
|
|
const formatValue = formatter(value) |
|
|
|
|
// 为了避免props的单向数据流特性,需要先将innerValue值设置为当前值,再在$nextTick中重新赋予设置后的值才有效 |
|
|
|
|
this.innerValue = value |
|
|
|
|
this.$nextTick(() => { |
|
|
|
|
this.innerValue = formatValue |
|
|
|
|
this.valueChange() |
|
|
|
|
}) |
|
|
|
|
}, |
|
|
|
|
// 内容发生变化,进行处理 |
|
|
|
|
valueChange() { |
|
|
|
|
const value = this.innerValue |
|
|
|
|
this.$nextTick(() => { |
|
|
|
|
this.$emit('input', value) |
|
|
|
|
// 标识value值的变化是由内部引起的 |
|
|
|
|
this.changeFromInner = true |
|
|
|
|
this.$emit('change', value) |
|
|
|
|
// 尝试调用u-form的验证方法 |
|
|
|
|
uni.$u.formValidate(this, 'change') |
|
|
|
|
}) |
|
|
|
|
}, |
|
|
|
|
onConfirm(e) { |
|
|
|
|
this.$emit('confirm', e) |
|
|
|
|
}, |
|
|
|
|
onKeyboardheightchange(e) { |
|
|
|
|
this.$emit('keyboardheightchange', e) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
</script> |
|
|
|
|
|
|
|
|
|
<style lang="scss" scoped> |
|
|
|
|
@import "../../libs/css/components.scss"; |
|
|
|
|
@import '../../libs/css/components.scss'; |
|
|
|
|
|
|
|
|
|
.u-textarea { |
|
|
|
|
border-radius: 4px; |
|
|
|
|
background-color: #fff; |
|
|
|
|
position: relative; |
|
|
|
|
@include flex; |
|
|
|
|
flex: 1; |
|
|
|
|
padding: 9px; |
|
|
|
|
border-radius: 4px; |
|
|
|
|
background-color: #fff; |
|
|
|
|
position: relative; |
|
|
|
|
@include flex; |
|
|
|
|
flex: 1; |
|
|
|
|
padding: 9px; |
|
|
|
|
|
|
|
|
|
&--radius { |
|
|
|
|
border-radius: 4px; |
|
|
|
|
} |
|
|
|
|
&--radius { |
|
|
|
|
border-radius: 4px; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
&--no-radius { |
|
|
|
|
border-radius: 0; |
|
|
|
|
} |
|
|
|
|
&--no-radius { |
|
|
|
|
border-radius: 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
&--disabled { |
|
|
|
|
background-color: #f5f7fa; |
|
|
|
|
} |
|
|
|
|
&--disabled { |
|
|
|
|
background-color: #f5f7fa; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
&__field { |
|
|
|
|
flex: 1; |
|
|
|
|
font-size: 15px; |
|
|
|
|
color: $u-content-color; |
|
|
|
|
width: 100%; |
|
|
|
|
} |
|
|
|
|
&__field { |
|
|
|
|
flex: 1; |
|
|
|
|
font-size: 15px; |
|
|
|
|
color: $u-content-color; |
|
|
|
|
width: 100%; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
&__count { |
|
|
|
|
position: absolute; |
|
|
|
|
right: 5px; |
|
|
|
|
bottom: 2px; |
|
|
|
|
font-size: 12px; |
|
|
|
|
color: $u-tips-color; |
|
|
|
|
background-color: #ffffff; |
|
|
|
|
padding: 1px 4px; |
|
|
|
|
} |
|
|
|
|
&__count { |
|
|
|
|
position: absolute; |
|
|
|
|
right: 5px; |
|
|
|
|
bottom: 2px; |
|
|
|
|
font-size: 12px; |
|
|
|
|
color: $u-tips-color; |
|
|
|
|
background-color: #ffffff; |
|
|
|
|
padding: 1px 4px; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
</style> |
|
|
|
|