123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593 |
- <template>
- <view class="uni-easyinput" :class="{'uni-easyinput-error':msg}" :style="boxStyle">
- <view class="uni-easyinput__content" :class="inputContentClass" :style="inputContentStyle">
- <uni-icons v-if="prefixIcon" class="content-clear-icon" :type="prefixIcon" color="#c0c4cc"
- @click="onClickIcon('prefix')" size="22"></uni-icons>
- <textarea v-if="type === 'textarea'" class="uni-easyinput__content-textarea"
- :class="{'input-padding':inputBorder}" :name="name" :value="val" :placeholder="placeholder"
- :placeholderStyle="placeholderStyle" :disabled="disabled"
- placeholder-class="uni-easyinput__placeholder-class" :maxlength="inputMaxlength" :focus="focused"
- :autoHeight="autoHeight" @input="onInput" @blur="_Blur" @focus="_Focus" @confirm="onConfirm"></textarea>
- <input v-else :type="type === 'password'?'text':type" class="uni-easyinput__content-input"
- :style="inputStyle" :name="name" :value="val" :password="!showPassword && type === 'password'"
- :placeholder="placeholder" :placeholderStyle="placeholderStyle"
- placeholder-class="uni-easyinput__placeholder-class" :disabled="disabled" :maxlength="inputMaxlength"
- :focus="focused" :confirmType="confirmType" @focus="_Focus" @blur="_Blur" @input="onInput"
- @confirm="onConfirm" />
- <template v-if="type === 'password' && passwordIcon">
- <!-- 开启密码时显示小眼睛 -->
- <uni-icons v-if="isVal" class="content-clear-icon" :class="{'is-textarea-icon':type==='textarea'}"
- :type="showPassword?'eye-slash-filled':'eye-filled'" :size="22"
- :color="focusShow?'#2979ff':'#c0c4cc'" @click="onEyes">
- </uni-icons>
- </template>
- <template v-else-if="suffixIcon">
- <uni-icons v-if="suffixIcon" class="content-clear-icon" :type="suffixIcon" color="#c0c4cc"
- @click="onClickIcon('suffix')" size="22"></uni-icons>
- </template>
- <template v-else>
- <uni-icons v-if="clearable && isVal && !disabled && type !== 'textarea'" class="content-clear-icon"
- :class="{'is-textarea-icon':type==='textarea'}" type="clear" :size="clearSize"
- :color="msg?'#dd524d':(focusShow?'#2979ff':'#c0c4cc')" @click="onClear"></uni-icons>
- </template>
- <slot name="right"></slot>
- </view>
- </view>
- </template>
- <script>
-
- function obj2strClass(obj) {
- let classess = ''
- for (let key in obj) {
- const val = obj[key]
- if (val) {
- classess += `${key} `
- }
- }
- return classess
- }
- function obj2strStyle(obj) {
- let style = ''
- for (let key in obj) {
- const val = obj[key]
- style += `${key}:${val};`
- }
- return style
- }
- export default {
- name: 'uni-easyinput',
- emits: ['click', 'iconClick', 'update:modelValue', 'input', 'focus', 'blur', 'confirm', 'clear', 'eyes', 'change'],
- model: {
- prop: 'modelValue',
- event: 'update:modelValue'
- },
- options: {
- virtualHost: true
- },
- inject: {
- form: {
- from: 'uniForm',
- default: null
- },
- formItem: {
- from: 'uniFormItem',
- default: null
- },
- },
- props: {
- name: String,
- value: [Number, String],
- modelValue: [Number, String],
- type: {
- type: String,
- default: 'text'
- },
- clearable: {
- type: Boolean,
- default: true
- },
- autoHeight: {
- type: Boolean,
- default: false
- },
- placeholder: {
- type: String,
- default: ' '
- },
- placeholderStyle: String,
- focus: {
- type: Boolean,
- default: false
- },
- disabled: {
- type: Boolean,
- default: false
- },
- maxlength: {
- type: [Number, String],
- default: 140
- },
- confirmType: {
- type: String,
- default: 'done'
- },
- clearSize: {
- type: [Number, String],
- default: 24
- },
- inputBorder: {
- type: Boolean,
- default: true
- },
- prefixIcon: {
- type: String,
- default: ''
- },
- suffixIcon: {
- type: String,
- default: ''
- },
- trim: {
- type: [Boolean, String],
- default: true
- },
- passwordIcon: {
- type: Boolean,
- default: true
- },
- styles: {
- type: Object,
- default () {
- return {
- color: '#333',
- disableColor: '#F7F6F6',
- borderColor: '#e5e5e5'
- }
- }
- },
- errorMessage: {
- type: [String, Boolean],
- default: ''
- }
- },
- data() {
- return {
- focused: false,
- val: '',
- showMsg: '',
- border: false,
- isFirstBorder: false,
- showClearIcon: false,
- showPassword: false,
- focusShow: false,
- localMsg: ''
- };
- },
- computed: {
-
- isVal() {
- const val = this.val
-
- if (val || val === 0) {
- return true
- }
- return false
- },
- msg() {
-
-
-
-
-
- return this.localMsg || this.errorMessage
- },
-
- inputMaxlength() {
- return Number(this.maxlength);
- },
-
- boxStyle() {
- return `color:${this.inputBorder && this.msg?'#e43d33':this.styles.color};`
- },
-
- inputContentClass() {
- return obj2strClass({
- 'is-input-border': this.inputBorder,
- 'is-input-error-border': this.inputBorder && this.msg,
- 'is-textarea': this.type === 'textarea',
- 'is-disabled': this.disabled
- })
- },
- inputContentStyle() {
- const focusColor = this.focusShow ? '#2979ff' : this.styles.borderColor
- const borderColor = this.inputBorder && this.msg ? '#dd524d' : focusColor
- return obj2strStyle({
- 'border-color': borderColor || '#e5e5e5',
- 'background-color': this.disabled ? this.styles.disableColor : '#fff'
- })
- },
-
- inputStyle() {
- const paddingRight = this.type === 'password' || this.clearable || this.prefixIcon ? '' : '10px'
- return obj2strStyle({
- 'padding-right': paddingRight,
- 'padding-left': this.prefixIcon ? '' : '10px'
- })
- }
- },
- watch: {
- value(newVal) {
- this.val = newVal
- },
- modelValue(newVal) {
- this.val = newVal
- },
- focus(newVal) {
- this.$nextTick(() => {
- this.focused = this.focus
- this.focusShow = this.focus
- })
- }
- },
- created() {
- this.init()
-
- if (this.form && this.formItem) {
- this.$watch('formItem.errMsg', (newVal) => {
- this.localMsg = newVal
- })
- }
- },
- mounted() {
- this.$nextTick(() => {
- this.focused = this.focus
- this.focusShow = this.focus
- })
- },
- methods: {
-
- init() {
- if (this.value || this.value === 0) {
- this.val = this.value
- } else if (this.modelValue || this.modelValue === 0) {
- this.val = this.modelValue
- } else {
- this.val = null
- }
- },
-
- onClickIcon(type) {
- this.$emit('iconClick', type)
- },
-
- onEyes() {
- this.showPassword = !this.showPassword
- this.$emit('eyes', this.showPassword)
- },
-
- onInput(event) {
- let value = event.detail.value;
-
- if (this.trim) {
- if (typeof(this.trim) === 'boolean' && this.trim) {
- value = this.trimStr(value)
- }
- if (typeof(this.trim) === 'string') {
- value = this.trimStr(value, this.trim)
- }
- };
- if (this.errMsg) this.errMsg = ''
- this.val = value
-
- this.$emit('input', value);
-
- this.$emit('update:modelValue', value)
- },
-
- onFocus() {
- this.$nextTick(() => {
- this.focused = true
- })
- this.$emit('focus', null);
- },
- _Focus(event) {
- this.focusShow = true
- this.$emit('focus', event);
- },
-
- onBlur() {
- this.focused = false
- this.$emit('focus', null);
- },
- _Blur(event) {
- let value = event.detail.value;
- this.focusShow = false
- this.$emit('blur', event);
-
- this.$emit('change', this.val)
-
- if (this.form && this.formItem) {
- const {
- validateTrigger
- } = this.form
- if (validateTrigger === 'blur') {
- this.formItem.onFieldChange()
- }
- }
- },
-
- onConfirm(e) {
- this.$emit('confirm', this.val);
- this.$emit('change', this.val)
- },
-
- onClear(event) {
- this.val = '';
-
- this.$emit('input', '');
-
-
- this.$emit('update:modelValue', '')
-
- this.$emit('clear')
- },
-
- trimStr(str, pos = 'both') {
- if (pos === 'both') {
- return str.trim();
- } else if (pos === 'left') {
- return str.trimLeft();
- } else if (pos === 'right') {
- return str.trimRight();
- } else if (pos === 'start') {
- return str.trimStart()
- } else if (pos === 'end') {
- return str.trimEnd()
- } else if (pos === 'all') {
- return str.replace(/\s+/g, '');
- } else if (pos === 'none') {
- return str;
- }
- return str;
- }
- }
- };
- </script>
- <style lang="scss">
- $uni-error: #e43d33;
- $uni-border-1: #DCDFE6 !default;
- .uni-easyinput {
-
- width: 100%;
-
- flex: 1;
- position: relative;
- text-align: left;
- color: #333;
- font-size: 14px;
- }
- .uni-easyinput__content {
- flex: 1;
-
- width: 100%;
- display: flex;
- box-sizing: border-box;
-
-
- flex-direction: row;
- align-items: center;
-
- border-color: #fff;
- transition-property: border-color;
- transition-duration: 0.3s;
- }
- .uni-easyinput__content-input {
-
- width: auto;
-
- position: relative;
- overflow: hidden;
- flex: 1;
- line-height: 1;
- font-size: 14px;
- height: 35px;
-
- }
- .uni-easyinput__placeholder-class {
- color: #999;
- font-size: 12px;
-
- }
- .is-textarea {
- align-items: flex-start;
- }
- .is-textarea-icon {
- margin-top: 5px;
- }
- .uni-easyinput__content-textarea {
- position: relative;
- overflow: hidden;
- flex: 1;
- line-height: 1.5;
- font-size: 14px;
- margin: 6px;
- margin-left: 0;
- height: 80px;
- min-height: 80px;
-
- min-height: 80px;
- width: auto;
-
- }
- .input-padding {
- padding-left: 10px;
- }
- .content-clear-icon {
- padding: 0 5px;
- }
- .label-icon {
- margin-right: 5px;
- margin-top: -1px;
- }
-
- .is-input-border {
-
- display: flex;
- box-sizing: border-box;
-
- flex-direction: row;
- align-items: center;
- border: 1px solid $uni-border-1;
- border-radius: 4px;
-
- overflow: hidden;
-
- }
- .uni-error-message {
- position: absolute;
- bottom: -17px;
- left: 0;
- line-height: 12px;
- color: $uni-error;
- font-size: 12px;
- text-align: left;
- }
- .uni-error-msg--boeder {
- position: relative;
- bottom: 0;
- line-height: 22px;
- }
- .is-input-error-border {
- border-color: $uni-error;
- .uni-easyinput__placeholder-class {
- color: mix(#fff, $uni-error, 50%);
- ;
- }
- }
- .uni-easyinput--border {
- margin-bottom: 0;
- padding: 10px 15px;
-
- border-top: 1px #eee solid;
- }
- .uni-easyinput-error {
- padding-bottom: 0;
- }
- .is-first-border {
-
- border: none;
-
-
- border-width: 0;
-
- }
- .is-disabled {
- background-color: #F7F6F6;
- color: #D5D5D5;
- .uni-easyinput__placeholder-class {
- color: #D5D5D5;
- font-size: 12px;
- }
- }
- </style>
|