123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- <template>
- <view
- class="u-input"
- :class="{
- 'u-input--border': border,
- 'u-input--error': validateState
- }"
- :style="{
- padding: padding ? padding : `0 ${border ? 20 : 0}rpx`,
- borderColor: borderColor,
- textAlign: inputAlignCom,
- backgroundColor: backgroundColor,
- }"
-
- <!-- #ifndef MP-HARMONY -->
- @tap.stop="inputClick"
-
-
- @tap="inputClick"
-
- >
- <textarea
- v-if="type == 'textarea'"
- class="u-input__input u-input__textarea"
- :style="[getStyle]"
- :value="defaultValue"
- :placeholder="placeholder"
- :placeholderStyle="placeholderStyle"
- :disabled="disabled"
- :fixed="fixed"
- :focus="focus"
- :maxlength="-1"
- :autoHeight="autoHeight"
- :selection-end="uSelectionEnd"
- :selection-start="uSelectionStart"
- :cursor-spacing="getCursorSpacing"
- :show-confirm-bar="showConfirmbar"
- :adjust-position="adjustPosition"
- @input="handleInput"
- @blur="handleBlur"
- @focus="onFocus"
- @confirm="onConfirm"
- />
- <input
- v-else
- class="u-input__input"
- :class="'u-input__' + type"
- :type="type == 'password' ? 'text' : type"
- :style="[getStyle]"
- :value="defaultValue"
- :maxlength="10000"
- :password="type == 'password' && !showPassword"
- :placeholder="placeholder"
- :placeholderStyle="placeholderStyle"
- :disabled="disabled || (type === 'select' && !showCover)"
- :focus="focus"
- :confirmType="confirmType"
- :cursor-spacing="getCursorSpacing"
- :selection-end="uSelectionEnd"
- :selection-start="uSelectionStart"
- :show-confirm-bar="showConfirmbar"
- :adjust-position="adjustPosition"
- @focus="onFocus"
- @blur="handleBlur"
- @input="handleInput"
- @confirm="onConfirm"
- />
- <view v-if="type === 'select' && showCover" class="cover-input" @tap.stop="inputClick"></view>
- <view class="u-input__right-icon u-flex">
- <view
- class="u-input__right-icon__clear u-input__right-icon__item"
- @tap="onClear"
- v-if="clearableCom && valueCom != '' && focused"
- >
- <u-icon size="32" name="close-circle-fill" color="#c0c4cc" />
- </view>
- <view
- class="u-input__right-icon__clear u-input__right-icon__item"
- v-if="passwordIcon && type == 'password'"
- >
- <u-icon
- size="32"
- :name="!showPassword ? 'eye' : 'eye-fill'"
- color="#c0c4cc"
- @click="showPassword = !showPassword"
- />
- </view>
- <view
- class="u-input__right-icon--select u-input__right-icon__item"
- v-if="type == 'select'"
- :class="{
- 'u-input__right-icon--select--reverse': selectOpen
- }"
- >
- <u-icon name="arrow-down-fill" size="26" color="#c0c4cc"></u-icon>
- </view>
- </view>
- </view>
- </template>
- <script>
- import Emitter from "../../libs/util/emitter.js";
- export default {
- name: "u-input",
- emits: ["update:modelValue", "input", "change", "confirm", "clear", "blur", "focus", "click", "touchstart"],
- mixins: [Emitter],
- props: {
- value: {
- type: [String, Number],
- default: ""
- },
- modelValue: {
- type: [String, Number],
- default: ""
- },
-
- type: {
- type: String,
- default: "text"
- },
- inputAlign: {
- type: String,
- default: ""
- },
- placeholder: {
- type: String,
- default: "请输入内容"
- },
- disabled: {
- type: Boolean,
- default: false
- },
- maxlength: {
- type: [Number, String],
- default: 140
- },
- placeholderStyle: {
- type: String,
- default: "color: #c0c4cc;"
- },
- 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"
- },
- autoHeight: {
- type: Boolean,
- default: true
- },
-
-
- selectOpen: {
- type: Boolean,
- default: false
- },
-
- height: {
- type: [Number, String],
- default: ""
- },
-
- clearable: {
- type: [Boolean, String],
- },
-
- cursorSpacing: {
- type: [Number, String],
- default: 0
- },
-
- selectionStart: {
- type: [Number, String],
- default: -1
- },
-
- selectionEnd: {
- type: [Number, String],
- default: -1
- },
-
- trim: {
- type: Boolean,
- default: true
- },
-
- showConfirmbar: {
- type: Boolean,
- default: true
- },
-
- adjustPosition: {
- type: Boolean,
- default: true
- },
-
- backgroundColor: {
- type: String,
- },
-
- padding: {
- type: String,
- },
- },
- data() {
- return {
- defaultValue: "",
- inputHeight: 70,
- textareaHeight: 100,
- validateState: false,
- focused: false,
- showPassword: false,
- lastValue: "" ,
- uForm:{
- inputAlign: "",
- clearable: ""
- },
- showCover: false
- };
- },
- watch: {
- valueCom(nVal, oVal) {
- this.defaultValue = nVal;
-
- if (nVal != oVal && this.type == "select")
- this.handleInput({
- detail: {
- value: nVal
- }
- });
- },
- defaultValue(nVal, oVal) {
-
- if (nVal && nVal.length > this.maxlength) {
- setTimeout(() => {
- nVal = nVal.substring(0, this.maxlength);
- this.handleInput({
- detail: {
- value: nVal
- }
- });
- }, 0);
- }
- }
- },
- computed: {
- valueCom() {
-
- return this.value;
-
-
- return this.modelValue;
-
- },
- inputAlignCom(){
- return this.inputAlign || this.uForm.inputAlign || "left";
- },
- clearableCom(){
- if (typeof this.clearable == "boolean") return this.clearable;
- if (typeof this.uForm.clearable == "boolean") return this.uForm.clearable;
- return true;
- },
-
- inputMaxlength() {
- return Number(this.maxlength);
- },
- getStyle() {
- 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;
- },
-
- getCursorSpacing() {
- return Number(this.cursorSpacing);
- },
-
- uSelectionStart() {
- return String(this.selectionStart);
- },
-
- uSelectionEnd() {
- return String(this.selectionEnd);
- }
- },
- created() {
-
-
- this.$on("onFormItemError", this.onFormItemError);
-
- this.defaultValue = this.valueCom;
- },
- mounted() {
- let parent = this.$u.$parent.call(this, 'u-form');
- if (parent) {
- Object.keys(this.uForm).map(key => {
- this.uForm[key] = parent[key];
- });
- }
-
- if (this.type === 'select') {
- this.showCover = true;
- }
-
- },
- methods: {
-
- handleInput(event) {
- let value = event.detail.value;
-
- if (this.trim) value = this.$u.trim(value);
-
- this.$emit("input", value);
- this.$emit("update:modelValue", value);
-
- this.defaultValue = value;
-
-
-
- setTimeout(() => {
-
-
- if (this.$u.trim(value) == this.lastValue) return;
- this.lastValue = value;
-
-
- this.dispatch("u-form-item", "onFieldChange", value);
- }, 40);
- },
-
- handleBlur(event) {
-
-
- setTimeout(() => {
- this.focused = false;
- }, 100);
- let value = event.detail.value;
-
- this.$emit("blur", value);
- setTimeout(() => {
-
-
- if (this.$u.trim(value) == this.lastValue) return;
- this.lastValue = value;
-
-
- this.dispatch("u-form-item", "onFieldBlur", value);
- }, 40);
- },
- onFormItemError(status) {
- this.validateState = status;
- },
- onFocus(event) {
- this.focused = true;
- this.$emit("focus");
- },
- onConfirm(e) {
- this.$emit("confirm", e.detail.value);
- },
- onClear(event) {
- this.$emit("input", "");
- this.$emit("update:modelValue", "");
- this.$emit("clear");
- },
- inputClick() {
- this.$emit("click");
- }
- }
- };
- </script>
- <style lang="scss" scoped>
- @import "../../libs/css/style.components.scss";
- .u-input {
- position: relative;
- flex: 1;
- @include vue-flex;
-
- &__input {
- //height: $u-form-item-height;
- font-size: 28rpx;
- color: $u-main-color;
- flex: 1;
- }
- /* #ifdef H5 */
- &__select {
- pointer-events: none;
- }
- /* #endif */
- &__textarea {
- width: auto;
- font-size: 28rpx;
- color: $u-main-color;
- padding: 10rpx 0;
- line-height: normal;
- flex: 1;
- }
- &--border {
- border-radius: 6rpx;
- border-radius: 4px;
- border: 1px solid $u-form-item-border-color;
- }
- &--error {
- border-color: $u-type-error !important;
- }
- &__right-icon {
- &__item {
- margin-left: 10rpx;
- }
- &--select {
- transition: transform 0.4s;
- &--reverse {
- transform: rotate(-180deg);
- }
- }
- }
-
- .cover-input {
- position: absolute;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- }
- }
- </style>
|