From afe4248799e9a4f5db7f9df1fa65227a06173bc2 Mon Sep 17 00:00:00 2001 From: Lindy <33708359+Lindysen@users.noreply.github.com> Date: Mon, 7 Sep 2020 20:11:47 +0800 Subject: [PATCH 1/4] fix(CouponCell): fix discounted price (#7123) --- src/coupon-cell/index.tsx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/coupon-cell/index.tsx b/src/coupon-cell/index.tsx index 46259f10f..87a9c1aa1 100644 --- a/src/coupon-cell/index.tsx +++ b/src/coupon-cell/index.tsx @@ -1,5 +1,5 @@ // Utils -import { createNamespace } from '../utils'; +import { createNamespace, isDef } from '../utils'; import { inherit } from '../utils/functional'; // Components @@ -26,7 +26,15 @@ function formatValue(props: CouponCellProps) { const coupon = coupons[+chosenCoupon]; if (coupon) { - const value = coupon.value || coupon.denominations || 0; + let value = 0; + if (isDef(coupon.value)) { + ({ value } = coupon); + } + + if (isDef(coupon.denominations)) { + value = coupon.denominations!; + } + return `-${currency} ${(value / 100).toFixed(2)}`; } From c7303786ae223ece2c37476282026c5ae3fc6dae Mon Sep 17 00:00:00 2001 From: neverland Date: Mon, 7 Sep 2020 20:16:54 +0800 Subject: [PATCH 2/4] types: improve isDef typing (#7124) * types: improve isDef typing * chore: update --- src/coupon-cell/index.tsx | 3 ++- src/utils/index.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/coupon-cell/index.tsx b/src/coupon-cell/index.tsx index 87a9c1aa1..86e3b3d59 100644 --- a/src/coupon-cell/index.tsx +++ b/src/coupon-cell/index.tsx @@ -27,12 +27,13 @@ function formatValue(props: CouponCellProps) { if (coupon) { let value = 0; + if (isDef(coupon.value)) { ({ value } = coupon); } if (isDef(coupon.denominations)) { - value = coupon.denominations!; + value = coupon.denominations; } return `-${currency} ${(value / 100).toFixed(2)}`; diff --git a/src/utils/index.ts b/src/utils/index.ts index 83f5ff38b..6386cc111 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -9,7 +9,7 @@ export const isServer: boolean = Vue.prototype.$isServer; // eslint-disable-next-line @typescript-eslint/no-empty-function export function noop() {} -export function isDef(val: unknown): boolean { +export function isDef(val: T): val is NonNullable { return val !== undefined && val !== null; } From 4ffa6ead1e0ef16ce0344c56e1f30ee1c395ae2e Mon Sep 17 00:00:00 2001 From: neverland Date: Mon, 7 Sep 2020 20:36:49 +0800 Subject: [PATCH 3/4] chore: prefer nullish coalescing (#7125) --- src/collapse-item/index.js | 4 ++-- src/goods-action-icon/index.js | 4 ++-- src/grid-item/index.js | 4 ++-- src/icon/index.tsx | 7 ++----- src/picker/index.js | 10 +++------- src/progress/index.js | 4 ++-- src/share-sheet/index.js | 4 ++-- src/sidebar-item/index.js | 4 ++-- src/sku/Sku.js | 4 ++-- src/stepper/index.js | 2 +- src/tab/index.js | 4 ++-- src/tabbar-item/index.js | 5 +---- src/tabs/index.js | 2 +- src/tree-select/index.tsx | 4 ++-- src/utils/index.ts | 2 +- 15 files changed, 27 insertions(+), 37 deletions(-) diff --git a/src/collapse-item/index.js b/src/collapse-item/index.js index 25826cde8..f84910079 100644 --- a/src/collapse-item/index.js +++ b/src/collapse-item/index.js @@ -1,5 +1,5 @@ // Utils -import { createNamespace, isDef } from '../utils'; +import { createNamespace } from '../utils'; import { raf, doubleRaf } from '../utils/dom/raf'; // Mixins @@ -35,7 +35,7 @@ export default createComponent({ computed: { currentName() { - return isDef(this.name) ? this.name : this.index; + return this.name ?? this.index; }, expanded() { diff --git a/src/goods-action-icon/index.js b/src/goods-action-icon/index.js index b9bb118dc..dff2a5ff6 100644 --- a/src/goods-action-icon/index.js +++ b/src/goods-action-icon/index.js @@ -1,4 +1,4 @@ -import { createNamespace, isDef } from '../utils'; +import { createNamespace } from '../utils'; import { route, routeProps } from '../utils/router'; import { ChildrenMixin } from '../mixins/relation'; import Info from '../info'; @@ -28,7 +28,7 @@ export default createComponent({ genIcon() { const slot = this.slots('icon'); - const info = isDef(this.badge) ? this.badge : this.info; + const info = this.badge ?? this.info; if (slot) { return ( diff --git a/src/grid-item/index.js b/src/grid-item/index.js index ca44c7da7..3ced482e4 100644 --- a/src/grid-item/index.js +++ b/src/grid-item/index.js @@ -1,5 +1,5 @@ // Utils -import { createNamespace, addUnit, isDef } from '../utils'; +import { createNamespace, addUnit } from '../utils'; import { BORDER } from '../utils/constant'; import { route, routeProps } from '../utils/router'; @@ -71,7 +71,7 @@ export default createComponent({ genIcon() { const iconSlot = this.slots('icon'); - const info = isDef(this.badge) ? this.badge : this.info; + const info = this.badge ?? this.info; if (iconSlot) { return ( diff --git a/src/icon/index.tsx b/src/icon/index.tsx index 7c087e6f4..d9962dfa1 100644 --- a/src/icon/index.tsx +++ b/src/icon/index.tsx @@ -1,5 +1,5 @@ // Utils -import { createNamespace, addUnit, isDef } from '../utils'; +import { createNamespace, addUnit } from '../utils'; import { inherit } from '../utils/functional'; // Components @@ -64,10 +64,7 @@ function Icon( > {slots.default && slots.default()} {imageIcon && } - + ); } diff --git a/src/picker/index.js b/src/picker/index.js index 004b8599a..96cbf07ba 100644 --- a/src/picker/index.js +++ b/src/picker/index.js @@ -1,5 +1,5 @@ // Utils -import { createNamespace, isDef } from '../utils'; +import { createNamespace } from '../utils'; import { preventDefault } from '../utils/dom/event'; import { BORDER_UNSET_TOP_BOTTOM } from '../utils/constant'; import { pickerProps, DEFAULT_ITEM_HEIGHT } from './shared'; @@ -86,9 +86,7 @@ export default createComponent({ let cursor = { children: this.columns }; while (cursor && cursor.children) { - const defaultIndex = isDef(cursor.defaultIndex) - ? cursor.defaultIndex - : +this.defaultIndex; + const defaultIndex = cursor.defaultIndex ?? +this.defaultIndex; formatted.push({ values: cursor.children, @@ -328,9 +326,7 @@ export default createComponent({ allowHtml={this.allowHtml} className={item.className} itemHeight={this.itemPxHeight} - defaultIndex={ - isDef(item.defaultIndex) ? item.defaultIndex : +this.defaultIndex - } + defaultIndex={item.defaultIndex ?? +this.defaultIndex} swipeDuration={this.swipeDuration} visibleItemCount={this.visibleItemCount} initialOptions={item.values} diff --git a/src/progress/index.js b/src/progress/index.js index f12ca19a9..9d90b94ba 100644 --- a/src/progress/index.js +++ b/src/progress/index.js @@ -1,4 +1,4 @@ -import { createNamespace, isDef, addUnit } from '../utils'; +import { createNamespace, addUnit } from '../utils'; const [createComponent, bem] = createNamespace('progress'); @@ -49,7 +49,7 @@ export default createComponent({ render() { const { pivotText, percentage } = this; - const text = isDef(pivotText) ? pivotText : percentage + '%'; + const text = pivotText ?? percentage + '%'; const showPivot = this.showPivot && text; const background = this.inactive ? '#cacaca' : this.color; diff --git a/src/share-sheet/index.js b/src/share-sheet/index.js index 744ca0ae7..39335f436 100644 --- a/src/share-sheet/index.js +++ b/src/share-sheet/index.js @@ -1,5 +1,5 @@ // Utils -import { createNamespace, isDef } from '../utils'; +import { createNamespace } from '../utils'; // Mixins import { popupMixinProps } from '../mixins/popup'; @@ -112,7 +112,7 @@ export default createComponent({ }, genCancelText() { - const cancelText = isDef(this.cancelText) ? this.cancelText : t('cancel'); + const cancelText = this.cancelText ?? t('cancel'); if (cancelText) { return ( diff --git a/src/sidebar-item/index.js b/src/sidebar-item/index.js index 41a64767b..54d587f1d 100644 --- a/src/sidebar-item/index.js +++ b/src/sidebar-item/index.js @@ -1,4 +1,4 @@ -import { createNamespace, isDef } from '../utils'; +import { createNamespace } from '../utils'; import { ChildrenMixin } from '../mixins/relation'; import { route, routeProps } from '../utils/router'; import Info from '../info'; @@ -46,7 +46,7 @@ export default createComponent({ {this.title} diff --git a/src/sku/Sku.js b/src/sku/Sku.js index 230c26191..919ab40b1 100644 --- a/src/sku/Sku.js +++ b/src/sku/Sku.js @@ -10,7 +10,7 @@ import SkuRowPropItem from './components/SkuRowPropItem'; import SkuStepper from './components/SkuStepper'; import SkuMessages from './components/SkuMessages'; import SkuActions from './components/SkuActions'; -import { createNamespace, isDef } from '../utils'; +import { createNamespace } from '../utils'; import { isAllSelected, isSkuChoosable, @@ -361,7 +361,7 @@ export default createComponent({ resetStepper() { const { skuStepper } = this.$refs; const { selectedNum } = this.initialSku; - const num = isDef(selectedNum) ? selectedNum : this.startSaleNum; + const num = selectedNum ?? this.startSaleNum; // 用来缓存不合法的情况 this.stepperError = null; diff --git a/src/stepper/index.js b/src/stepper/index.js index ecf0b0942..b31a85177 100644 --- a/src/stepper/index.js +++ b/src/stepper/index.js @@ -72,7 +72,7 @@ export default createComponent({ }, data() { - const defaultValue = isDef(this.value) ? this.value : this.defaultValue; + const defaultValue = this.value ?? this.defaultValue; const value = this.format(defaultValue); if (!equal(value, this.value)) { diff --git a/src/tab/index.js b/src/tab/index.js index 75489232e..892f798c2 100644 --- a/src/tab/index.js +++ b/src/tab/index.js @@ -1,4 +1,4 @@ -import { isDef, createNamespace } from '../utils'; +import { createNamespace } from '../utils'; import { ChildrenMixin } from '../mixins/relation'; import { routeProps } from '../utils/router'; @@ -26,7 +26,7 @@ export default createComponent({ computed: { computedName() { - return isDef(this.name) ? this.name : this.index; + return this.name ?? this.index; }, isActive() { diff --git a/src/tabbar-item/index.js b/src/tabbar-item/index.js index 80b273532..309663f44 100644 --- a/src/tabbar-item/index.js +++ b/src/tabbar-item/index.js @@ -71,10 +71,7 @@ export default createComponent({
{this.genIcon(active)} - +
{this.slots('default', { active })}
diff --git a/src/tabs/index.js b/src/tabs/index.js index ddafe2556..86ea14c26 100644 --- a/src/tabs/index.js +++ b/src/tabs/index.js @@ -359,7 +359,7 @@ export default createComponent({ refInFor type={type} dot={item.dot} - info={isDef(item.badge) ? item.badge : item.info} + info={item.badge ?? item.info} title={item.title} color={this.color} style={item.titleStyle} diff --git a/src/tree-select/index.tsx b/src/tree-select/index.tsx index 0ca699f6d..6c1685a67 100644 --- a/src/tree-select/index.tsx +++ b/src/tree-select/index.tsx @@ -1,5 +1,5 @@ // Utils -import { createNamespace, addUnit, isDef } from '../utils'; +import { createNamespace, addUnit } from '../utils'; import { emit, inherit } from '../utils/functional'; // Components @@ -65,7 +65,7 @@ function TreeSelect( const Navs = items.map((item) => ( { - result = isDef(result[key]) ? result[key] : ''; + result = result[key] ?? ''; }); return result; From 601277fbe81796199a039a843a95ff31ecffc3f3 Mon Sep 17 00:00:00 2001 From: neverland Date: Mon, 7 Sep 2020 20:37:14 +0800 Subject: [PATCH 4/4] test(ActionSheet): update snapsoht (#7126) --- src/action-sheet/test/__snapshots__/index.spec.js.snap | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/action-sheet/test/__snapshots__/index.spec.js.snap b/src/action-sheet/test/__snapshots__/index.spec.js.snap index e27c3187b..a5685dfb5 100644 --- a/src/action-sheet/test/__snapshots__/index.spec.js.snap +++ b/src/action-sheet/test/__snapshots__/index.spec.js.snap @@ -16,6 +16,12 @@ exports[`close-icon prop 1`] = ` `; +exports[`closeable prop 1`] = ` +
+
Title
+
+`; + exports[`color option 1`] = `
`; exports[`description prop 1`] = `