123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <template>
- <view
- class="u-search"
- @tap="clickHandler"
- :style="[{
- margin: margin,
- }, $u.addStyle(customStyle)]"
- >
- <view
- class="u-search__content"
- :style="{
- backgroundColor: bgColor,
- borderRadius: shape == 'round' ? '100px' : '4px',
- borderColor: borderColor,
- }"
- >
- <template v-if="$slots.label || label !== null">
- <slot name="label">
- <text class="u-search__content__label">{{ label }}</text>
- </slot>
- </template>
- <view class="u-search__content__icon">
- <u-icon
- @tap="clickIcon"
- :size="searchIconSize"
- :name="searchIcon"
- :color="searchIconColor ? searchIconColor : color"
- ></u-icon>
- </view>
- <input
- confirm-type="search"
- @blur="blur"
- :value="value"
- @confirm="search"
- @input="inputChange"
- :disabled="disabled"
- @focus="getFocus"
- :focus="focus"
- :maxlength="maxlength"
- placeholder-class="u-search__content__input--placeholder"
- :placeholder="placeholder"
- :placeholder-style="`color: ${placeholderColor}`"
- class="u-search__content__input"
- type="text"
- :style="[{
- textAlign: inputAlign,
- color: color,
- backgroundColor: bgColor,
- height: $u.addUnit(height)
- }, inputStyle]"
- />
- <view
- class="u-search__content__icon u-search__content__close"
- v-if="keyword && clearabled && focused"
- @tap="clear"
- >
- <u-icon
- name="close"
- size="11"
- color="#ffffff"
- customStyle="line-height: 12px"
- ></u-icon>
- </view>
- </view>
- <text
- :style="[actionStyle]"
- class="u-search__action"
- :class="[(showActionBtn || show) && 'u-search__action--active']"
- @tap.stop.prevent="custom"
- >{{ actionText }}</text>
- </view>
- </template>
- <script>
- import props from './props.js';
-
- export default {
- name: "u-search",
- mixins: [uni.$u.mpMixin, uni.$u.mixin,props],
- data() {
- return {
- keyword: '',
- showClear: false,
- show: false,
-
- focused: this.focus
-
-
- };
- },
- watch: {
- keyword(nVal) {
-
- this.$emit('input', nVal);
-
- this.$emit('change', nVal);
- },
- value: {
- immediate: true,
- handler(nVal) {
- this.keyword = nVal;
- }
- }
- },
- computed: {
- showActionBtn() {
- return !this.animation && this.showAction
- }
- },
- methods: {
-
- inputChange(e) {
- this.keyword = e.detail.value;
- },
-
-
- clear() {
- this.keyword = '';
-
- this.$nextTick(() => {
- this.$emit('clear');
- })
- },
-
- search(e) {
- this.$emit('search', e.detail.value);
- try {
-
- uni.hideKeyboard();
- } catch (e) {}
- },
-
- custom() {
- this.$emit('custom', this.keyword);
- try {
-
- uni.hideKeyboard();
- } catch (e) {}
- },
-
- getFocus() {
- this.focused = true;
-
- if (this.animation && this.showAction) this.show = true;
- this.$emit('focus', this.keyword);
- },
-
- blur() {
-
-
- setTimeout(() => {
- this.focused = false;
- }, 100)
- this.show = false;
- this.$emit('blur', this.keyword);
- },
-
- clickHandler() {
- if (this.disabled) this.$emit('click');
- },
-
- clickIcon() {
- this.$emit('clickIcon');
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- @import "../../libs/css/components.scss";
- $u-search-content-padding: 0 10px !default;
- $u-search-label-color: $u-main-color !default;
- $u-search-label-font-size: 14px !default;
- $u-search-label-margin: 0 4px !default;
- $u-search-close-size: 20px !default;
- $u-search-close-radius: 100px !default;
- $u-search-close-bgColor: #C6C7CB !default;
- $u-search-close-transform: scale(0.82) !default;
- $u-search-input-font-size: 14px !default;
- $u-search-input-margin: 0 5px !default;
- $u-search-input-color: $u-main-color !default;
- $u-search-input-placeholder-color: $u-tips-color !default;
- $u-search-action-font-size: 14px !default;
- $u-search-action-color: $u-main-color !default;
- $u-search-action-width: 0 !default;
- $u-search-action-active-width: 40px !default;
- $u-search-action-margin-left: 5px !default;
- [type="search"]::-webkit-search-decoration {
- display: none;
- }
- .u-search {
- @include flex(row);
- align-items: center;
- flex: 1;
- &__content {
- @include flex;
- align-items: center;
- padding: $u-search-content-padding;
- flex: 1;
- justify-content: space-between;
- border-width: 1px;
- border-color: transparent;
- border-style: solid;
- overflow: hidden;
- &__icon {
- @include flex;
- align-items: center;
- }
- &__label {
- color: $u-search-label-color;
- font-size: $u-search-label-font-size;
- margin: $u-search-label-margin;
- }
- &__close {
- width: $u-search-close-size;
- height: $u-search-close-size;
- border-top-left-radius: $u-search-close-radius;
- border-top-right-radius: $u-search-close-radius;
- border-bottom-left-radius: $u-search-close-radius;
- border-bottom-right-radius: $u-search-close-radius;
- background-color: $u-search-close-bgColor;
- @include flex(row);
- align-items: center;
- justify-content: center;
- transform: $u-search-close-transform;
- }
- &__input {
- flex: 1;
- font-size: $u-search-input-font-size;
- line-height: 1;
- margin: $u-search-input-margin;
- color: $u-search-input-color;
- &--placeholder {
- color: $u-search-input-placeholder-color;
- }
- }
- }
- &__action {
- font-size: $u-search-action-font-size;
- color: $u-search-action-color;
- width: $u-search-action-width;
- overflow: hidden;
- transition-property: width;
- transition-duration: 0.3s;
-
- white-space: nowrap;
-
- text-align: center;
- &--active {
- width: $u-search-action-active-width;
- margin-left: $u-search-action-margin-left;
- }
- }
- }
- </style>
|