¶Ô±ÈÐÂÎļþ |
| | |
| | | <template> |
| | | <view class="u-toast"> |
| | | <u-overlay |
| | | :show="isShow" |
| | | :custom-style="overlayStyle" |
| | | > |
| | | <view |
| | | class="u-toast__content" |
| | | :style="[contentStyle]" |
| | | :class="['u-type-' + tmpConfig.type, (tmpConfig.type === 'loading' || tmpConfig.loading) ? 'u-toast__content--loading' : '']" |
| | | > |
| | | <u-loading-icon |
| | | v-if="tmpConfig.type === 'loading'" |
| | | mode="circle" |
| | | color="rgb(255, 255, 255)" |
| | | inactiveColor="rgb(120, 120, 120)" |
| | | size="25" |
| | | ></u-loading-icon> |
| | | <u-icon |
| | | v-else-if="tmpConfig.type !== 'defalut' && iconName" |
| | | :name="iconName" |
| | | size="17" |
| | | :color="tmpConfig.type" |
| | | :customStyle="iconStyle" |
| | | ></u-icon> |
| | | <u-gap |
| | | v-if="tmpConfig.type === 'loading' || tmpConfig.loading" |
| | | height="12" |
| | | bgColor="transparent" |
| | | ></u-gap> |
| | | <text |
| | | class="u-toast__content__text" |
| | | :class="['u-toast__content__text--' + tmpConfig.type]" |
| | | style="max-width: 400rpx;" |
| | | >{{ tmpConfig.message }}</text> |
| | | </view> |
| | | </u-overlay> |
| | | </view> |
| | | </template> |
| | | |
| | | <script> |
| | | /** |
| | | * toast æ¶æ¯æç¤º |
| | | * @description æ¤ç»ä»¶è¡¨ç°å½¢å¼ç±»ä¼¼uniçuni.showToastAPIï¼ä½ä¹æä¸åçå°æ¹ã |
| | | * @tutorial https://www.uviewui.com/components/toast.html |
| | | * @property {String | Number} zIndex toastå±ç¤ºæ¶çzIndexå¼ (é»è®¤ 10090 ) |
| | | * @property {Boolean} loading æ¯å¦å è½½ä¸ ï¼é»è®¤ false ï¼ |
| | | * @property {String | Number} message æ¾ç¤ºçæåå
容 |
| | | * @property {String} icon 徿 ï¼æè
ç»å¯¹è·¯å¾çå¾ç |
| | | * @property {String} type 主é¢ç±»å ï¼é»è®¤ defaultï¼ |
| | | * @property {Boolean} show æ¯å¦æ¾ç¤ºè¯¥ç»ä»¶ ï¼é»è®¤ falseï¼ |
| | | * @property {Boolean} overlay æ¯å¦æ¾ç¤ºéæé®ç½©ï¼é²æ¢ç¹å»ç©¿é ï¼é»è®¤ false ï¼ |
| | | * @property {String} position ä½ç½® ï¼é»è®¤ 'center' ï¼ |
| | | * @property {Object} params 跳转çåæ° |
| | | * @property {String | Number} duration å±ç¤ºæ¶é´ï¼åä½ms ï¼é»è®¤ 2000 ï¼ |
| | | * @property {Boolean} isTab æ¯å¦è¿åç为tabé¡µé¢ ï¼é»è®¤ false ï¼ |
| | | * @property {String} url toastæ¶å¤±åæ¯å¦è·³è½¬é¡µé¢ï¼æå跳转ï¼ä¼å
级é«äºbackåæ° |
| | | * @property {Function} complete æ§è¡å®åçåè°å½æ° |
| | | * @property {Boolean} back ç»ætoastæ¯å¦èªå¨è¿åä¸ä¸é¡µ ï¼é»è®¤ false ï¼ |
| | | * @property {Object} customStyle ç»ä»¶çæ ·å¼ï¼å¯¹è±¡å½¢å¼ |
| | | * @event {Function} show æ¾ç¤ºtoastï¼å¦éä¸è¿å
¥é¡µé¢å°±æ¾ç¤ºtoastï¼è¯·å¨onReadyçå½å¨æè°ç¨ |
| | | * @example <u-toast ref="uToast" /> |
| | | */ |
| | | export default { |
| | | name: 'u-toast', |
| | | mixins: [uni.$u.mpMixin, uni.$u.mixin], |
| | | data() { |
| | | return { |
| | | isShow: false, |
| | | timer: null, // 宿¶å¨ |
| | | config: { |
| | | message: '', // æ¾ç¤ºææ¬ |
| | | type: '', // 主é¢ç±»åï¼primaryï¼successï¼errorï¼warningï¼black |
| | | duration: 2000, // æ¾ç¤ºçæ¶é´ï¼æ¯«ç§ |
| | | icon: true, // æ¾ç¤ºç徿 |
| | | position: 'center', // toaståºç°çä½ç½® |
| | | complete: null, // æ§è¡å®åçåè°å½æ° |
| | | overlay: false, // æ¯å¦é²æ¢è§¦æ¸ç©¿é |
| | | loading: false, // æ¯å¦å è½½ä¸ç¶æ |
| | | }, |
| | | tmpConfig: {}, // å°ç¨æ·é
ç½®åå
ç½®é
ç½®åå¹¶åç临æ¶é
ç½®åé |
| | | } |
| | | }, |
| | | computed: { |
| | | iconName() { |
| | | // åªæä¸ä¸ºnoneï¼å¹¶ä¸type为error|warning|succes|infoæ¶åï¼ææ¾ç¤ºå¾æ |
| | | if(!this.tmpConfig.icon || this.tmpConfig.icon == 'none') { |
| | | return ''; |
| | | } |
| | | if (['error', 'warning', 'success', 'primary'].includes(this.tmpConfig.type)) { |
| | | return uni.$u.type2icon(this.tmpConfig.type) |
| | | } else { |
| | | return '' |
| | | } |
| | | }, |
| | | overlayStyle() { |
| | | const style = { |
| | | justifyContent: 'center', |
| | | alignItems: 'center', |
| | | display: 'flex' |
| | | } |
| | | // å°é®ç½©è®¾ç½®ä¸º100%éæåº¦ï¼é¿å
åºç°ç°è²èæ¯ |
| | | style.backgroundColor = 'rgba(0, 0, 0, 0)' |
| | | return style |
| | | }, |
| | | iconStyle() { |
| | | const style = {} |
| | | // 徿 éè¦ä¸ä¸ªå³è¾¹è·ï¼ä»¥è·å³è¾¹çæåæéå¼çè·ç¦» |
| | | style.marginRight = '4px' |
| | | // #ifdef APP-NVUE |
| | | // iOSAPPä¸ï¼å¾æ æ1pxçåä¸åç§»ï¼è¿éè¿è¡ä¿®æ£ |
| | | if (uni.$u.os() === 'ios') { |
| | | style.marginTop = '-1px' |
| | | } |
| | | // #endif |
| | | return style |
| | | }, |
| | | loadingIconColor() { |
| | | let color = 'rgb(255, 255, 255)' |
| | | if (['error', 'warning', 'success', 'primary'].includes(this.tmpConfig.type)) { |
| | | // loading-iconç»ä»¶å
é¨ä¼å¯¹coloråæ°è¿è¡ä¸ä¸ªéæåº¦å¤çï¼è¯¥æ¹æ³è¦æ±ä¼ å
¥çé¢è²å¼ |
| | | // å¿
须为rgbæ ¼å¼çï¼æä»¥è¿éåä¸ä¸ªå¤ç |
| | | color = uni.$u.hexToRgb(uni.$u.color[this.tmpConfig.type]) |
| | | } |
| | | return color |
| | | }, |
| | | // å
容çåçæ ·å¼ |
| | | contentStyle() { |
| | | const windowHeight = uni.$u.sys().windowHeight, style = {} |
| | | let value = 0 |
| | | // æ ¹æ®topåbottomï¼å¯¹Yè½´è¿è¡çªä½é«åº¦çç¾åæ¯åç§» |
| | | if(this.tmpConfig.position === 'top') { |
| | | value = - windowHeight * 0.25 |
| | | } else if(this.tmpConfig.position === 'bottom') { |
| | | value = windowHeight * 0.25 |
| | | } |
| | | style.transform = `translateY(${value}px)` |
| | | return style |
| | | } |
| | | }, |
| | | created() { |
| | | // éè¿ä¸»é¢çå½¢å¼è°ç¨toastï¼æ¹éçææ¹æ³å½æ° |
| | | ['primary', 'success', 'error', 'warning', 'default', 'loading'].map(item => { |
| | | this[item] = message => this.show({ |
| | | type: item, |
| | | message |
| | | }) |
| | | }) |
| | | }, |
| | | methods: { |
| | | // æ¾ç¤ºtoastç»ä»¶ï¼ç±ç¶ç»ä»¶éè¿this.$refs.xxx.show(options)å½¢å¼è°ç¨ |
| | | show(options) { |
| | | // ä¸å°ç»æåå¹¶å°this.configåéï¼é¿å
夿¬¡è°ç¨u-toastï¼ååçé
ç½®é ææ··ä¹± |
| | | this.tmpConfig = uni.$u.deepMerge(this.config, options) |
| | | // æ¸
é¤å®æ¶å¨ |
| | | this.clearTimer() |
| | | this.isShow = true |
| | | this.timer = setTimeout(() => { |
| | | // å计æ¶ç»æï¼æ¸
é¤å®æ¶å¨ï¼éètoastç»ä»¶ |
| | | this.clearTimer() |
| | | // 夿æ¯å¦åå¨callbackæ¹æ³ï¼å¦æåå¨å°±æ§è¡ |
| | | typeof(this.tmpConfig.complete) === 'function' && this.tmpConfig.complete() |
| | | }, this.tmpConfig.duration) |
| | | }, |
| | | // éètoastç»ä»¶ï¼ç±ç¶ç»ä»¶éè¿this.$refs.xxx.hide()å½¢å¼è°ç¨ |
| | | hide() { |
| | | this.clearTimer() |
| | | }, |
| | | clearTimer() { |
| | | this.isShow = false |
| | | // æ¸
é¤å®æ¶å¨ |
| | | clearTimeout(this.timer) |
| | | this.timer = null |
| | | } |
| | | }, |
| | | beforeDestroy() { |
| | | this.clearTimer() |
| | | } |
| | | } |
| | | </script> |
| | | |
| | | <style lang="scss" scoped> |
| | | @import "../../libs/css/components.scss"; |
| | | |
| | | $u-toast-color:#fff !default; |
| | | $u-toast-border-radius:4px !default; |
| | | $u-toast-border-background-color:#585858 !default; |
| | | $u-toast-border-font-size:14px !default; |
| | | $u-toast-border-padding:12px 20px !default; |
| | | $u-toast-loading-border-padding: 20px 20px !default; |
| | | $u-toast-content-text-color:#fff !default; |
| | | $u-toast-content-text-font-size:15px !default; |
| | | $u-toast-u-icon:10rpx !default; |
| | | $u-toast-u-type-primary-color:$u-primary !default; |
| | | $u-toast-u-type-primary-background-color:#ecf5ff !default; |
| | | $u-toast-u-type-primary-border-color:rgb(215, 234, 254) !default; |
| | | $u-toast-u-type-primary-border-width:1px !default; |
| | | $u-toast-u-type-success-color: $u-success !default; |
| | | $u-toast-u-type-success-background-color: #dbf1e1 !default; |
| | | $u-toast-u-type-success-border-color: #BEF5C8 !default; |
| | | $u-toast-u-type-success-border-width: 1px !default; |
| | | $u-toast-u-type-error-color:$u-error !default; |
| | | $u-toast-u-type-error-background-color:#fef0f0 !default; |
| | | $u-toast-u-type-error-border-color:#fde2e2 !default; |
| | | $u-toast-u-type-error-border-width: 1px !default; |
| | | $u-toast-u-type-warning-color:$u-warning !default; |
| | | $u-toast-u-type-warning-background-color:#fdf6ec !default; |
| | | $u-toast-u-type-warning-border-color:#faecd8 !default; |
| | | $u-toast-u-type-warning-border-width: 1px !default; |
| | | $u-toast-u-type-default-color:#fff !default; |
| | | $u-toast-u-type-default-background-color:#585858 !default; |
| | | |
| | | .u-toast { |
| | | &__content { |
| | | @include flex; |
| | | padding: $u-toast-border-padding; |
| | | border-radius: $u-toast-border-radius; |
| | | background-color: $u-toast-border-background-color; |
| | | color: $u-toast-color; |
| | | align-items: center; |
| | | /* #ifndef APP-NVUE */ |
| | | max-width: 600rpx; |
| | | /* #endif */ |
| | | position: relative; |
| | | |
| | | &--loading { |
| | | flex-direction: column; |
| | | padding: $u-toast-loading-border-padding; |
| | | } |
| | | |
| | | &__text { |
| | | color: $u-toast-content-text-color; |
| | | font-size: $u-toast-content-text-font-size; |
| | | line-height: $u-toast-content-text-font-size; |
| | | |
| | | &--default { |
| | | color: $u-toast-content-text-color; |
| | | } |
| | | |
| | | &--error { |
| | | color: $u-error; |
| | | } |
| | | |
| | | &--primary { |
| | | color: $u-primary; |
| | | } |
| | | |
| | | &--success { |
| | | color: $u-success; |
| | | } |
| | | |
| | | &--warning { |
| | | color: $u-warning; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | .u-type-primary { |
| | | color: $u-toast-u-type-primary-color; |
| | | background-color: $u-toast-u-type-primary-background-color; |
| | | border-color: $u-toast-u-type-primary-border-color; |
| | | border-width: $u-toast-u-type-primary-border-width; |
| | | } |
| | | |
| | | .u-type-success { |
| | | color: $u-toast-u-type-success-color; |
| | | background-color: $u-toast-u-type-success-background-color; |
| | | border-color: $u-toast-u-type-success-border-color; |
| | | border-width: 1px; |
| | | } |
| | | |
| | | .u-type-error { |
| | | color: $u-toast-u-type-error-color; |
| | | background-color: $u-toast-u-type-error-background-color; |
| | | border-color: $u-toast-u-type-error-border-color; |
| | | border-width: $u-toast-u-type-error-border-width; |
| | | } |
| | | |
| | | .u-type-warning { |
| | | color: $u-toast-u-type-warning-color; |
| | | background-color: $u-toast-u-type-warning-background-color; |
| | | border-color: $u-toast-u-type-warning-border-color; |
| | | border-width: 1px; |
| | | } |
| | | |
| | | .u-type-default { |
| | | color: $u-toast-u-type-default-color; |
| | | background-color: $u-toast-u-type-default-background-color; |
| | | } |
| | | </style> |