¶Ô±ÈÐÂÎļþ |
| | |
| | | <template> |
| | | <u-transition |
| | | mode="slide-down" |
| | | :customStyle="containerStyle" |
| | | :show="open" |
| | | > |
| | | <view |
| | | class="u-notify" |
| | | :class="[`u-notify--${tmpConfig.type}`]" |
| | | :style="[backgroundColor, $u.addStyle(customStyle)]" |
| | | > |
| | | <u-status-bar v-if="tmpConfig.safeAreaInsetTop"></u-status-bar> |
| | | <view class="u-notify__warpper"> |
| | | <slot name="icon"> |
| | | <u-icon |
| | | v-if="['success', 'warning', 'error'].includes(tmpConfig.type)" |
| | | :name="tmpConfig.icon" |
| | | :color="tmpConfig.color" |
| | | :size="1.3 * tmpConfig.fontSize" |
| | | :customStyle="{marginRight: '4px'}" |
| | | ></u-icon> |
| | | </slot> |
| | | <text |
| | | class="u-notify__warpper__text" |
| | | :style="{ |
| | | fontSize: $u.addUnit(tmpConfig.fontSize), |
| | | color: tmpConfig.color |
| | | }" |
| | | >{{ tmpConfig.message }}</text> |
| | | </view> |
| | | </view> |
| | | </u-transition> |
| | | </template> |
| | | |
| | | <script> |
| | | import props from './props.js'; |
| | | /** |
| | | * notify é¡¶é¨æç¤º |
| | | * @description 该ç»ä»¶ä¸è¬ç¨äºé¡µé¢é¡¶é¨å䏿»åºä¸ä¸ªæç¤ºï¼å°åèªå¨æ¶èµ·çåºæ¯ |
| | | * @tutorial |
| | | * @property {String | Number} top å°é¡¶é¨çè·ç¦» ( é»è®¤ 0 ) |
| | | * @property {String} type 主é¢ï¼primaryï¼successï¼warningï¼error ( é»è®¤ 'primary' ) |
| | | * @property {String} color åä½é¢è² ( é»è®¤ '#ffffff' ) |
| | | * @property {String} bgColor èæ¯é¢è² |
| | | * @property {String} message å±ç¤ºçæåå
容 |
| | | * @property {String | Number} duration å±ç¤ºæ¶é¿ï¼ä¸º0æ¶ä¸æ¶å¤±ï¼åä½ms ( é»è®¤ 3000 ) |
| | | * @property {String | Number} fontSize åä½å¤§å° ( é»è®¤ 15 ) |
| | | * @property {Boolean} safeAreaInsetTop æ¯å¦çåºé¡¶é¨å®å
¨è·ç¦»ï¼ç¶ææ é«åº¦ï¼ ( é»è®¤ false ) |
| | | * @property {Object} customStyle ç»ä»¶çæ ·å¼ï¼å¯¹è±¡å½¢å¼ |
| | | * @event {Function} open å¼å¯ç»ä»¶æ¶è°ç¨ç彿° |
| | | * @event {Function} close å
³éç»ä»¶å¼è°ç¨ç彿° |
| | | * @example <u-notify message="Hi uView"></u-notify> |
| | | */ |
| | | export default { |
| | | name: 'u-notify', |
| | | mixins: [uni.$u.mpMixin, uni.$u.mixin,props], |
| | | data() { |
| | | return { |
| | | // æ¯å¦å±ç¤ºç»ä»¶ |
| | | open: false, |
| | | timer: null, |
| | | config: { |
| | | // å°é¡¶é¨çè·ç¦» |
| | | top: uni.$u.props.notify.top, |
| | | // type主é¢ï¼primaryï¼successï¼warningï¼error |
| | | type: uni.$u.props.notify.type, |
| | | // åä½é¢è² |
| | | color: uni.$u.props.notify.color, |
| | | // èæ¯é¢è² |
| | | bgColor: uni.$u.props.notify.bgColor, |
| | | // å±ç¤ºçæåå
容 |
| | | message: uni.$u.props.notify.message, |
| | | // å±ç¤ºæ¶é¿ï¼ä¸º0æ¶ä¸æ¶å¤±ï¼åä½ms |
| | | duration: uni.$u.props.notify.duration, |
| | | // åä½å¤§å° |
| | | fontSize: uni.$u.props.notify.fontSize, |
| | | // æ¯å¦çåºé¡¶é¨å®å
¨è·ç¦»ï¼ç¶ææ é«åº¦ï¼ |
| | | safeAreaInsetTop: uni.$u.props.notify.safeAreaInsetTop |
| | | }, |
| | | // åå¹¶åçé
ç½®ï¼é¿å
夿¬¡è°ç¨ç»ä»¶åï¼å¯è½ä¼å¤ç¨ä¹å使ç¨çé
ç½®åæ° |
| | | tmpConfig: {} |
| | | } |
| | | }, |
| | | computed: { |
| | | containerStyle() { |
| | | let top = 0 |
| | | if (this.tmpConfig.top === 0) { |
| | | // #ifdef H5 |
| | | // H5端ï¼å¯¼èªæ 为æ®éå
ç´ ï¼éè¦å°ç»ä»¶ç§»å¨å°å¯¼èªæ çä¸è¾¹æ²¿ |
| | | // H5çå¯¼èªæ é«åº¦ä¸º44px |
| | | top = 44 |
| | | // #endif |
| | | } |
| | | const style = { |
| | | top: uni.$u.addUnit(this.tmpConfig.top === 0 ? top : this.tmpConfig.top), |
| | | // å 为ç»ä»¶åºå±ä¸ºu-transitionç»ä»¶ï¼å¿
é¡»å°å
¶è®¾ç½®ä¸ºfixedå®ä½ |
| | | // 让å
¶åºç°å¨å¯¼èªæ åºé¨ |
| | | position: 'fixed', |
| | | left: 0, |
| | | right: 0, |
| | | zIndex: 10076 |
| | | } |
| | | return style |
| | | }, |
| | | // ç»ä»¶èæ¯é¢è² |
| | | backgroundColor() { |
| | | const style = {} |
| | | if (this.tmpConfig.bgColor) { |
| | | style.backgroundColor = this.tmpConfig.bgColor |
| | | } |
| | | return style |
| | | }, |
| | | // é»è®¤ä¸»é¢ä¸ç徿 |
| | | icon() { |
| | | let icon |
| | | if (this.tmpConfig.type === 'success') { |
| | | icon = 'checkmark-circle' |
| | | } else if (this.tmpConfig.type === 'error') { |
| | | icon = 'close-circle' |
| | | } else if (this.tmpConfig.type === 'warning') { |
| | | icon = 'error-circle' |
| | | } |
| | | return icon |
| | | } |
| | | }, |
| | | created() { |
| | | // éè¿ä¸»é¢çå½¢å¼è°ç¨toastï¼æ¹éçææ¹æ³å½æ° |
| | | ['primary', 'success', 'error', 'warning'].map(item => { |
| | | this[item] = message => this.show({ |
| | | type: item, |
| | | message |
| | | }) |
| | | }) |
| | | }, |
| | | methods: { |
| | | show(options) { |
| | | // ä¸å°ç»æåå¹¶å°this.configåéï¼é¿å
夿¬¡è°ç¨u-toastï¼ååçé
ç½®é ææ··ä¹± |
| | | this.tmpConfig = uni.$u.deepMerge(this.config, options) |
| | | // ä»»ä½å®æ¶å¨åå§åä¹åï¼é½è¦æ§è¡æ¸
餿ä½ï¼å¦åå¯è½ä¼é ææ··ä¹± |
| | | this.clearTimer() |
| | | this.open = true |
| | | if (this.tmpConfig.duration > 0) { |
| | | this.timer = setTimeout(() => { |
| | | this.open = false |
| | | // å计æ¶ç»æï¼æ¸
é¤å®æ¶å¨ï¼éètoastç»ä»¶ |
| | | this.clearTimer() |
| | | // 夿æ¯å¦åå¨callbackæ¹æ³ï¼å¦æåå¨å°±æ§è¡ |
| | | typeof(this.tmpConfig.complete) === 'function' && this.tmpConfig.complete() |
| | | }, this.tmpConfig.duration) |
| | | } |
| | | }, |
| | | // å
³énotify |
| | | close() { |
| | | this.clearTimer() |
| | | }, |
| | | clearTimer() { |
| | | this.open = false |
| | | // æ¸
é¤å®æ¶å¨ |
| | | clearTimeout(this.timer) |
| | | this.timer = null |
| | | } |
| | | }, |
| | | beforeDestroy() { |
| | | this.clearTimer() |
| | | } |
| | | } |
| | | </script> |
| | | |
| | | <style lang="scss" scoped> |
| | | @import "../../libs/css/components.scss"; |
| | | |
| | | $u-notify-padding: 8px 10px !default; |
| | | $u-notify-text-font-size: 15px !default; |
| | | $u-notify-primary-bgColor: $u-primary !default; |
| | | $u-notify-success-bgColor: $u-success !default; |
| | | $u-notify-error-bgColor: $u-error !default; |
| | | $u-notify-warning-bgColor: $u-warning !default; |
| | | |
| | | |
| | | .u-notify { |
| | | padding: $u-notify-padding; |
| | | |
| | | &__warpper { |
| | | @include flex; |
| | | align-items: center; |
| | | text-align: center; |
| | | justify-content: center; |
| | | |
| | | &__text { |
| | | font-size: $u-notify-text-font-size; |
| | | text-align: center; |
| | | } |
| | | } |
| | | |
| | | &--primary { |
| | | background-color: $u-notify-primary-bgColor; |
| | | } |
| | | |
| | | &--success { |
| | | background-color: $u-notify-success-bgColor; |
| | | } |
| | | |
| | | &--error { |
| | | background-color: $u-notify-error-bgColor; |
| | | } |
| | | |
| | | &--warning { |
| | | background-color: $u-notify-warning-bgColor; |
| | | } |
| | | } |
| | | </style> |