123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- <template>
- <view class="u-input" :class="inputClass" :style="[wrapperStyle]">
- <view class="u-input__content">
- <view
- class="u-input__content__prefix-icon"
- v-if="prefixIcon || $slots.prefix"
- >
- <slot name="prefix">
- <u-icon
- :name="prefixIcon"
- size="18"
- :customStyle="prefixIconStyle"
- ></u-icon>
- </slot>
- </view>
- <view class="u-input__content__field-wrapper" @tap="clickHandler">
- <!-- 根据uni-app的input组件文档,H5和APP中只要声明了password参数(无论true还是false),type均失效,此时
- 为了防止type=number时,又存在password属性,type无效,此时需要设置password为undefined
- -->
- <input
- class="u-input__content__field-wrapper__field"
- :style="[inputStyle]"
- :type="type"
- :focus="focus"
- :cursor="cursor"
- :value="innerValue"
- :auto-blur="autoBlur"
- :disabled="disabled || readonly"
- :maxlength="maxlength"
- :placeholder="placeholder"
- :placeholder-style="placeholderStyle"
- :placeholder-class="placeholderClass"
- :confirm-type="confirmType"
- :confirm-hold="confirmHold"
- :hold-keyboard="holdKeyboard"
- :cursor-spacing="cursorSpacing"
- :adjust-position="adjustPosition"
- :selection-end="selectionEnd"
- :selection-start="selectionStart"
- :password="password || type === 'password' || undefined"
- @input="onInput"
- @blur="onBlur"
- @focus="onFocus"
- @confirm="onConfirm"
- @keyboardheightchange="onkeyboardheightchange"
- />
- </view>
- <view
- class="u-input__content__clear"
- v-if="isShowClear"
- @tap="onClear"
- >
- <u-icon
- name="close"
- size="11"
- color="#ffffff"
- customStyle="line-height: 12px"
- ></u-icon>
- </view>
- <view
- class="u-input__content__subfix-icon"
- v-if="suffixIcon || $slots.suffix"
- >
- <slot name="suffix">
- <u-icon
- :name="suffixIcon"
- size="18"
- :customStyle="suffixIconStyle"
- ></u-icon>
- </slot>
- </view>
- </view>
- </view>
- </template>
- <script>
- import props from "./props.js";
- export default {
- name: "u-input",
- mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
- data() {
- return {
-
- innerValue: "",
-
- focused: false,
-
- firstChange: true,
-
- changeFromInner: false,
-
- innerFormatter: value => value
- };
- },
- watch: {
- value: {
- immediate: true,
- handler(newVal, oldVal) {
- this.innerValue = newVal;
-
-
- if (
- this.firstChange === false &&
- this.changeFromInner === false
- ) {
- this.valueChange();
- }
-
- this.firstChange = false;
-
- this.changeFromInner = false;
- },
- },
- },
- computed: {
-
- isShowClear() {
- const { clearable, readonly, focused, innerValue } = this;
- return !!clearable && !readonly && !!focused && innerValue !== "";
- },
-
- inputClass() {
- let classes = [],
- { border, disabled, shape } = this;
- border === "surround" &&
- (classes = classes.concat(["u-border", "u-input--radius"]));
- classes.push(`u-input--${shape}`);
- border === "bottom" &&
- (classes = classes.concat([
- "u-border-bottom",
- "u-input--no-radius",
- ]));
- return classes.join(" ");
- },
-
- wrapperStyle() {
- const style = {};
-
- if (this.disabled) {
- style.backgroundColor = this.disabledColor;
- }
-
- if (this.border === "none") {
- style.padding = "0";
- } else {
-
- style.paddingTop = "6px";
- style.paddingBottom = "6px";
- style.paddingLeft = "9px";
- style.paddingRight = "9px";
- }
- return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle));
- },
-
- inputStyle() {
- const style = {
- color: this.color,
- fontSize: uni.$u.addUnit(this.fontSize),
- textAlign: this.inputAlign
- };
- return style;
- },
- },
- methods: {
-
- setFormatter(e) {
- this.innerFormatter = e
- },
-
- onInput(e) {
- let { value = "" } = e.detail || {};
-
- const formatter = this.formatter || this.innerFormatter
- const formatValue = formatter(value)
-
- this.innerValue = value
- this.$nextTick(() => {
- this.innerValue = formatValue;
- this.valueChange();
- })
- },
-
- onBlur(event) {
- this.$emit("blur", event.detail.value);
-
-
- uni.$u.sleep(50).then(() => {
- this.focused = false;
- });
-
- uni.$u.formValidate(this, "blur");
- },
-
- onFocus(event) {
- this.focused = true;
- this.$emit("focus");
- },
-
- onConfirm(event) {
- this.$emit("confirm", this.innerValue);
- },
-
-
- onkeyboardheightchange() {
- this.$emit("keyboardheightchange");
- },
-
- valueChange() {
- const value = this.innerValue;
- this.$nextTick(() => {
- this.$emit("input", value);
-
- this.changeFromInner = true;
- this.$emit("change", value);
-
- uni.$u.formValidate(this, "change");
- });
- },
-
- onClear() {
- this.innerValue = "";
- this.$nextTick(() => {
- this.valueChange();
- this.$emit("clear");
- });
- },
-
- clickHandler() {
-
- if (uni.$u.os() === "android") {
- const formItem = uni.$u.$parent.call(this, "u-form-item");
- if (formItem) {
- formItem.clickHandler();
- }
- }
-
- },
- },
- };
- </script>
- <style lang="scss" scoped>
- @import "../../libs/css/components.scss";
- .u-input {
- @include flex(row);
- align-items: center;
- justify-content: space-between;
- flex: 1;
- &--radius,
- &--square {
- border-radius: 4px;
- }
- &--no-radius {
- border-radius: 0;
- }
- &--circle {
- border-radius: 100px;
- }
- &__content {
- flex: 1;
- @include flex(row);
- align-items: center;
- justify-content: space-between;
- &__field-wrapper {
- position: relative;
- @include flex(row);
- margin: 0;
- flex: 1;
-
- &__field {
- line-height: 26px;
- text-align: left;
- color: $u-main-color;
- height: 24px;
- font-size: 15px;
- flex: 1;
- }
- }
- &__clear {
- width: 20px;
- height: 20px;
- border-radius: 100px;
- background-color: #c6c7cb;
- @include flex(row);
- align-items: center;
- justify-content: center;
- transform: scale(0.82);
- margin-left: 4px;
- }
- &__subfix-icon {
- margin-left: 4px;
- }
- &__prefix-icon {
- margin-right: 4px;
- }
- }
- }
- </style>
|