¶Ô±ÈÐÂÎļþ |
| | |
| | | <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'; |
| | | |
| | | /** |
| | | * search æç´¢æ¡ |
| | | * @description æç´¢ç»ä»¶ï¼éæäºå¸¸è§æç´¢æ¡æéåè½ï¼ç¨æ·å¯ä»¥ä¸é®å¼å
¥ï¼å¼ç®±å³ç¨ã |
| | | * @tutorial https://www.uviewui.com/components/search.html |
| | | * @property {String} shape æç´¢æ¡å½¢ç¶ï¼round-åå½¢ï¼square-æ¹å½¢ï¼é»è®¤ 'round' ï¼ |
| | | * @property {String} bgColor æç´¢æ¡èæ¯é¢è²ï¼é»è®¤ '#f2f2f2' ï¼ |
| | | * @property {String} placeholder å 使åå
容ï¼é»è®¤ '请è¾å
¥å
³é®å' ï¼ |
| | | * @property {Boolean} clearabled æ¯å¦å¯ç¨æ¸
餿§ä»¶ï¼é»è®¤ true ï¼ |
| | | * @property {Boolean} focus æ¯å¦èªå¨è·å¾ç¦ç¹ï¼é»è®¤ false ï¼ |
| | | * @property {Boolean} showAction æ¯å¦æ¾ç¤ºå³ä¾§æ§ä»¶ï¼é»è®¤ true ï¼ |
| | | * @property {Object} actionStyle å³ä¾§æ§ä»¶çæ ·å¼ï¼å¯¹è±¡å½¢å¼ |
| | | * @property {String} actionText å³ä¾§æ§ä»¶æåï¼é»è®¤ 'æç´¢' ï¼ |
| | | * @property {String} inputAlign è¾å
¥æ¡å
å®¹æ°´å¹³å¯¹é½æ¹å¼ ï¼é»è®¤ 'left' ï¼ |
| | | * @property {Object} inputStyle èªå®ä¹è¾å
¥æ¡æ ·å¼ï¼å¯¹è±¡å½¢å¼ |
| | | * @property {Boolean} disabled æ¯å¦å¯ç¨è¾å
¥æ¡ï¼é»è®¤ false ï¼ |
| | | * @property {String} borderColor è¾¹æ¡é¢è²ï¼é
ç½®äºé¢è²ï¼æä¼æè¾¹æ¡ (é»è®¤ 'transparent' ) |
| | | * @property {String} searchIconColor æç´¢å¾æ çé¢è²ï¼é»è®¤åè¾å
¥æ¡åä½é¢è² (é»è®¤ '#909399' ) |
| | | * @property {Number | String} searchIconSize æç´¢å¾æ çåä½ï¼é»è®¤22 |
| | | * @property {String} color è¾å
¥æ¡åä½é¢è²ï¼é»è®¤ '#606266' ï¼ |
| | | * @property {String} placeholderColor placeholderçé¢è²ï¼é»è®¤ '#909399' ï¼ |
| | | * @property {String} searchIcon è¾å
¥æ¡å·¦è¾¹ç徿 ï¼å¯ä»¥ä¸ºuView徿 åç§°æå¾çè·¯å¾ (é»è®¤ 'search' ) |
| | | * @property {String} margin ç»ä»¶ä¸å
¶ä»ä¸ä¸å·¦å³å
ç´ ä¹é´çè·ç¦»ï¼å¸¦åä½çå符串形å¼ï¼å¦"30px" (é»è®¤ '0' ) |
| | | * @property {Boolean} animation æ¯å¦å¼å¯å¨ç»ï¼è§ä¸æ¹è¯´æï¼é»è®¤ false ï¼ |
| | | * @property {String} value è¾å
¥æ¡åå§å¼ |
| | | * @property {String | Number} maxlength è¾å
¥æ¡æå¤§è½è¾å
¥çé¿åº¦ï¼-1为ä¸éå¶é¿åº¦ (é»è®¤ '-1' ) |
| | | * @property {String | Number} height è¾å
¥æ¡é«åº¦ï¼åä½pxï¼é»è®¤ 64 ï¼ |
| | | * @property {String | Number} label æç´¢æ¡å·¦è¾¹æ¾ç¤ºå
容 |
| | | * @property {Object} customStyle å®ä¹éè¦ç¨å°çå¤é¨æ ·å¼ |
| | | * |
| | | * @event {Function} change è¾å
¥æ¡å
容åçååæ¶è§¦å |
| | | * @event {Function} search ç¨æ·ç¡®å®æç´¢æ¶è§¦åï¼ç¨æ·æå车é®ï¼æè
ææºé®çå³ä¸è§ç"æç´¢"鮿¶è§¦å |
| | | * @event {Function} custom ç¨æ·ç¹å»å³ä¾§æ§ä»¶æ¶è§¦å |
| | | * @event {Function} clear ç¨æ·ç¹å»æ¸
餿鮿¶è§¦å |
| | | * @example <u-search placeholder="æ¥ç
§é¦ççç´«ç" v-model="keyword"></u-search> |
| | | */ |
| | | export default { |
| | | name: "u-search", |
| | | mixins: [uni.$u.mpMixin, uni.$u.mixin,props], |
| | | data() { |
| | | return { |
| | | keyword: '', |
| | | showClear: false, // æ¯å¦æ¾ç¤ºå³è¾¹çæ¸
é¤å¾æ |
| | | show: false, |
| | | // æ è®°inputå½åç¶ææ¯å¦å¤äºèç¦ä¸ï¼å¦ææ¯ï¼æä¼æ¾ç¤ºå³ä¾§çæ¸
餿§ä»¶ |
| | | focused: this.focus |
| | | // ç»å®è¾å
¥æ¡çå¼ |
| | | // inputValue: this.value |
| | | }; |
| | | }, |
| | | watch: { |
| | | keyword(nVal) { |
| | | // ååç»å®å¼ï¼è®©v-modelç»å®çå¼åååå |
| | | this.$emit('input', nVal); |
| | | // 触åchangeäºä»¶ï¼äºä»¶ææåv-modelååç»å®çææä¸æ ·ï¼è®©ç¨æ·å¤ä¸ä¸ªéæ© |
| | | this.$emit('change', nVal); |
| | | }, |
| | | value: { |
| | | immediate: true, |
| | | handler(nVal) { |
| | | this.keyword = nVal; |
| | | } |
| | | } |
| | | }, |
| | | computed: { |
| | | showActionBtn() { |
| | | return !this.animation && this.showAction |
| | | } |
| | | }, |
| | | methods: { |
| | | // ç®åHX2.6.9 v-modelååç»å®æ æï¼æ
çå¬inputäºä»¶è·åè¾å
¥æ¡å
容çåå |
| | | inputChange(e) { |
| | | this.keyword = e.detail.value; |
| | | }, |
| | | // æ¸
空è¾å
¥ |
| | | // ä¹å¯ä»¥ä½ä¸ºç¨æ·éè¿this.$refså½¢å¼è°ç¨æ¸
空è¾å
¥æ¡å
容 |
| | | clear() { |
| | | this.keyword = ''; |
| | | // å»¶åååºäºä»¶ï¼é¿å
å¨ç¶ç»ä»¶çå¬clearäºä»¶æ¶ï¼valueä¸ºæ´æ°åçå¼(ä¸ä¸ºç©º) |
| | | 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() { |
| | | // æå¼å§ä½¿ç¨çæ¯çå¬å¾æ @touchstartäºä»¶ï¼èªä»hx2.8.4åï¼æ¤æ¹æ³å¨å¾®ä¿¡å°ç¨åºåºé |
| | | // è¿éæ¹ä¸ºçå¬ç¹å»äºä»¶ï¼æç¹å»æ¸
é¤å¾æ æ¶ï¼åæ¶ä¹åçäº@bluräºä»¶ï¼å¯¼è´å¾æ æ¶å¤±èæ æ³ç¹å»ï¼è¿éåä¸ä¸ªå»¶æ¶ |
| | | setTimeout(() => { |
| | | this.focused = false; |
| | | }, 100) |
| | | this.show = false; |
| | | this.$emit('blur', this.keyword); |
| | | }, |
| | | // ç¹å»æç´¢æ¡ï¼åªædisabled=trueæ¶æååºäºä»¶ï¼å ä¸ºç¦æ¢äºè¾å
¥ï¼æå³çæ¯æ³è·³è½¬çæ£çæç´¢é¡µ |
| | | 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; |
| | | |
| | | /* #ifdef H5 */ |
| | | // iOS15å¨H5ä¸ï¼hxçæäºçæ¬ï¼input type=searchæ¶ï¼ä¼å¤äºä¸ä¸ªæç´¢å¾æ ï¼è¿è¡ç§»é¤ |
| | | [type="search"]::-webkit-search-decoration { |
| | | display: none; |
| | | } |
| | | /* #endif */ |
| | | |
| | | .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; |
| | | /* #ifndef APP-NVUE */ |
| | | white-space: nowrap; |
| | | /* #endif */ |
| | | text-align: center; |
| | | |
| | | &--active { |
| | | width: $u-search-action-active-width; |
| | | margin-left: $u-search-action-margin-left; |
| | | } |
| | | } |
| | | } |
| | | </style> |