123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- <template>
- <view
- class="tn-input-class tn-input"
- :class="{
- 'tn-input--border': border,
- 'tn-input--error': validateState
- }"
- :style="{
- padding: `0 ${border ? 20 : 0}rpx`,
- borderColor: borderColor,
- textAlign: inputAlign
- }"
- @tap.stop="inputClick"
- >
- <textarea
- v-if="type === 'textarea'"
- class="tn-input__input tn-input__textarea"
- :style="[inputStyle]"
- :value="defaultValue"
- :placeholder="placeholder"
- :placeholderStyle="placeholderStyle"
- :disabled="disabled || type === 'select'"
- :maxlength="maxLength"
- :fixed="fixed"
- :focus="focus"
- :autoHeight="autoHeight"
- :selectionStart="elSelectionStart"
- :selectionEnd="elSelectionEnd"
- :cursorSpacing="cursorSpacing"
- :showConfirmBar="showConfirmBar"
- @input="handleInput"
- @blur="handleBlur"
- @focus="onFocus"
- @confirm="onConfirm"
- />
- <input
- v-else
- class="tn-input__input"
- :type="type === 'password' ? 'text' : type"
- :style="[inputStyle]"
- :value="defaultValue"
- :password="type === 'password' && !showPassword"
- :placeholder="placeholder"
- :placeholderStyle="placeholderStyle"
- :disabled="disabled || type === 'select'"
- :maxlength="maxLength"
- :focus="focus"
- :confirmType="confirmType"
- :selectionStart="elSelectionStart"
- :selectionEnd="elSelectionEnd"
- :cursorSpacing="cursorSpacing"
- :showConfirmBar="showConfirmBar"
- @input="handleInput"
- @blur="handleBlur"
- @focus="onFocus"
- @confirm="onConfirm"
- />
-
-
- <view class="tn-input__right-icon tn-flex tn-flex-col-center">
-
- <view
- v-if="clearable && value !== '' && focused"
- class="tn-input__right-icon__item tn-input__right-icon__clear"
- @tap="onClear"
- >
- <view class="icon tn-icon-close"></view>
- </view>
- <view
- v-else-if="type === 'text' && !focused && showRightIcon && rightIcon !== ''"
- class="tn-input__right-icon__item tn-input__right-icon__clear"
- >
- <view class="icon" :class="[`tn-icon-${rightIcon}`]"></view>
- </view>
-
- <view
- v-if="passwordIcon && type === 'password'"
- class="tn-input__right-icon__item tn-input__right-icon__clear"
- @tap="showPassword = !showPassword"
- >
- <view v-if="!showPassword" class="tn-icon-eye-hide"></view>
- <view v-else class="icon tn-icon-eye"></view>
- </view>
-
- <view
- v-if="type === 'select'"
- class="tn-input__right-icon__item tn-input__right-icon__select"
- :class="{
- 'tn-input__right-icon__select--reverse': selectOpen
- }"
- >
- <view class="icon tn-icon-up-triangle"></view>
- </view>
- </view>
- </view>
- </template>
- <script>
- import Emitter from '../../libs/utils/emitter.js'
-
- export default {
- mixins: [Emitter],
- name: 'tn-input',
- props: {
- value: {
- type: [String, Number],
- default: ''
- },
-
- type: {
- type: String,
- default: 'text'
- },
-
- inputAlign: {
- type: String,
- default: 'left'
- },
-
- placeholder: {
- type: String,
- default: ''
- },
- placeholderStyle: {
- type: String,
- default: 'color: #AAAAAA'
- },
-
- disabled: {
- type: Boolean,
- default: false
- },
-
- maxLength: {
- type: Number,
- default: 255
- },
-
- height: {
- type: Number,
- default: 0
- },
-
- autoHeight: {
- type: Boolean,
- default: true
- },
-
- confirmType: {
- type: String,
- default: 'done'
- },
-
- customStyle: {
- type: Object,
- default() {
- return {}
- }
- },
-
- fixed: {
- type: Boolean,
- default: false
- },
-
- focus: {
- type: Boolean,
- default: false
- },
-
- passwordIcon: {
- type: Boolean,
- default: true
- },
-
- border: {
- type: Boolean,
- default: false
- },
-
- borderColor: {
- type: String,
- default: '#dcdfe6'
- },
-
- selectOpen: {
- type: Boolean,
- default: false
- },
-
- clearable: {
- type: Boolean,
- default: true
- },
-
- cursorSpacing: {
- type: Number,
- default: 0
- },
-
-
- selectionStart: {
- type: Number,
- default: -1
- },
-
- selectionEnd: {
- type: Number,
- default: -1
- },
-
- trim: {
- type: Boolean,
- default: true
- },
-
- showConfirmBar: {
- type: Boolean,
- default: true
- },
-
- showRightIcon: {
- type: Boolean,
- default: false
- },
-
- rightIcon: {
- type: String,
- default: ''
- }
- },
- computed: {
-
- inputStyle() {
- let style = {}
-
- style.minHeight = this.height ? this.height + 'rpx' :
- this.type === 'textarea' ? this.textareaHeight + 'rpx' : this.inputHeight + 'rpx'
-
- style = Object.assign(style, this.customStyle)
-
- return style
- },
-
- elSelectionStart() {
- return String(this.selectionStart)
- },
-
- elSelectionEnd() {
- return String(this.selectionEnd)
- }
- },
- data() {
- return {
-
- defaultValue: this.value,
-
- inputHeight: 70,
-
- textareaHeight: 100,
-
- validateState: false,
-
- focused: false,
-
- showPassword: false,
-
- lastValue: '',
- }
- },
- watch: {
- value(newVal, oldVal) {
- this.defaultValue = newVal
-
-
- if (newVal !== oldVal && this.type === 'select') {
- this.handleInput({
- detail: {
- value: newVal
- }
- })
- }
- }
- },
- created() {
-
- this.$on("on-form-item-error", this.onFormItemError)
- },
- methods: {
-
- handleInput(event) {
- let value = event.detail.value
-
- if (this.trim) value = this.$t.string.trim(value)
-
- this.$emit('input', value)
-
- this.defaultValue = value
-
-
-
- setTimeout(() => {
-
-
- if (this.$t.string.trim(value) === this.lastValue) return
- this.lastValue = value
-
-
-
- this.dispatch('tn-form-item','on-form-change', value)
- }, 40)
- },
-
- handleBlur(event) {
- let value = event.detail.value
-
-
- setTimeout(() => {
- this.focused = false
- }, 100)
-
-
- this.$emit('blur', value)
-
-
-
- setTimeout(() => {
-
-
- if (this.$t.string.trim(value) === this.lastValue) return
- this.lastValue = value
-
-
-
- this.dispatch('tn-form-item','on-form-blur', value)
- }, 40)
- },
-
- onFormItemError(status) {
- this.validateState = status
- },
-
- onFocus(event) {
- this.focused = true
- this.$emit('focus')
- },
-
- onConfirm(event) {
- this.$emit('confirm', event.detail.value)
- },
-
- onClear(event) {
- this.$emit('input', '')
- },
-
- inputClick() {
- this.$emit('click')
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .tn-input {
- display: flex;
- flex-direction: row;
- position: relative;
- flex: 1;
-
- &__input {
- font-size: 28rpx;
- color: $tn-font-color;
- flex: 1;
- }
-
- &__textarea {
- width: auto;
- font-size: 28rpx;
- color: $tn-font-color;
- padding: 10rpx 0;
- line-height: normal;
- flex: 1;
- }
-
- &--border {
- border-radius: 6rpx;
- border: 2rpx solid $tn-border-solid-color;
- }
-
- &--error {
- border-color: $tn-color-red !important;
- }
-
- &__right-icon {
- line-height: 1;
- .icon {
- color: $tn-font-sub-color;
- }
-
- &__item {
- margin-left: 10rpx;
- }
-
- &__clear {
- .icon {
- font-size: 32rpx;
- }
- }
-
- &__select {
- transition: transform .4s;
-
- .icon {
- font-size: 26rpx;
- }
-
- &--reverse {
- transform: rotate(-180deg);
- }
- }
- }
- }
- </style>
|