Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(props): add mergeProps utility to resolve all defaultProps warnings #2581

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/packages/actionsheet/actionsheet.taro.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { FunctionComponent, ReactNode } from 'react'
import Popup, { PopupProps } from '@/packages/popup/index.taro'
import { ComponentDefaults } from '@/utils/typings'
import { mergeProps } from '@/utils/merge-props'

export type ActionSheetOption<T> = { [key: string]: T }

Expand Down Expand Up @@ -40,7 +41,7 @@ export const ActionSheet: FunctionComponent<
className,
style,
...rest
} = { ...defaultProps, ...props }
} = mergeProps(defaultProps, props)

const classPrefix = 'nut-actionsheet'

Expand Down
3 changes: 2 additions & 1 deletion src/packages/actionsheet/actionsheet.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { FunctionComponent, ReactNode } from 'react'
import Popup, { PopupProps } from '@/packages/popup/index'
import { ComponentDefaults } from '@/utils/typings'
import { mergeProps } from '@/utils/merge-props'

export type ActionSheetOption<T> = { [key: string]: T }

Expand Down Expand Up @@ -40,7 +41,7 @@ export const ActionSheet: FunctionComponent<
className,
style,
...rest
} = { ...defaultProps, ...props }
} = mergeProps(defaultProps, props)

const classPrefix = 'nut-actionsheet'

Expand Down
7 changes: 2 additions & 5 deletions src/packages/animatingnumbers/countup.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
} from 'react'
import { createSelectorQuery } from '@tarojs/taro'
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
import { mergeProps } from '@/utils/merge-props'

export interface CountUpProps extends BasicComponent {
length: number
Expand All @@ -33,10 +34,7 @@
thousands,
style,
...reset
} = {
...defaultProps,
...props,
}
} = mergeProps(defaultProps, props)
const classPrefix = 'nut-countup'
const countupRef = useRef<HTMLDivElement>(null)
const timerRef = useRef(0)
Expand Down Expand Up @@ -92,7 +90,7 @@

useEffect(() => {
setNumberTransform()
}, [numerArr])

Check warning on line 93 in src/packages/animatingnumbers/countup.taro.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'setNumberTransform'. Either include it or remove the dependency array

useEffect(() => {
if (!isLoaded.current) {
Expand All @@ -106,7 +104,7 @@
return () => {
window.clearTimeout(timerRef.current)
}
}, [value])

Check warning on line 107 in src/packages/animatingnumbers/countup.taro.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has missing dependencies: 'delay' and 'getShowNumber'. Either include them or remove the dependency array

return (
<div className={`${classPrefix} ${className}`} ref={countupRef}>
Expand Down Expand Up @@ -141,5 +139,4 @@
)
}

CountUp.defaultProps = defaultProps // 不可删除
CountUp.displayName = 'NutCountUp'
7 changes: 2 additions & 5 deletions src/packages/animatingnumbers/countup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
useRef,
} from 'react'
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
import { mergeProps } from '@/utils/merge-props'

export interface CountUpProps extends BasicComponent {
length: number
Expand All @@ -32,10 +33,7 @@
thousands,
style,
...reset
} = {
...defaultProps,
...props,
}
} = mergeProps(defaultProps, props)
const classPrefix = 'nut-countup'
const countupRef = useRef<HTMLDivElement>(null)
const timerRef = useRef(0)
Expand Down Expand Up @@ -87,7 +85,7 @@
return () => {
window.clearTimeout(timerRef.current)
}
}, [numerArr])

Check warning on line 88 in src/packages/animatingnumbers/countup.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has missing dependencies: 'delay' and 'setNumberTransform'. Either include them or remove the dependency array

return (
<div className={`${classPrefix} ${className}`} ref={countupRef}>
Expand Down Expand Up @@ -122,5 +120,4 @@
)
}

CountUp.defaultProps = defaultProps // 不可删除
CountUp.displayName = 'NutCountUp'
4 changes: 2 additions & 2 deletions src/packages/dialog/dialog.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from '@/utils/use-custom-event'
import { BasicComponent } from '@/utils/typings'
import { useLockScrollTaro } from '@/utils/use-lock-scoll-taro'
import { mergeProps } from '@/utils/merge-props'

export type DialogProps = DialogBasicProps & BasicComponent
const defaultProps = {
Expand Down Expand Up @@ -78,7 +79,7 @@ export const BaseDialog: FunctionComponent<Partial<DialogProps>> & {
beforeClose,
},
setParams,
} = useParams({ ...defaultProps, ...props })
} = useParams(mergeProps(defaultProps, props))

useCustomEvent(
id as string,
Expand Down Expand Up @@ -225,7 +226,6 @@ export function close(selector: string) {
customEvents.trigger(path, { status: false })
}

BaseDialog.defaultProps = defaultProps // 不可删除
BaseDialog.displayName = 'NutDialog'
BaseDialog.open = open
BaseDialog.close = close
4 changes: 2 additions & 2 deletions src/packages/dialog/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
DialogConfirmProps,
} from './config'
import { ComponentDefaults } from '@/utils/typings'
import { mergeProps } from '@/utils/merge-props'

export type DialogProps = DialogBasicProps
const defaultProps = {
Expand Down Expand Up @@ -55,7 +56,7 @@ const BaseDialog: ForwardRefRenderFunction<unknown, Partial<DialogProps>> = (
beforeCancel,
beforeClose,
...restProps
} = props
} = mergeProps(defaultProps, props)
const classPrefix = 'nut-dialog'
const [loading, setLoading] = useState(false)

Expand Down Expand Up @@ -161,5 +162,4 @@ Dialog.confirm = (props: DialogConfirmProps): DialogReturnProps => {
}
})

Dialog.defaultProps = defaultProps // 不可删除
Dialog.displayName = 'NutDialog'
4 changes: 2 additions & 2 deletions src/packages/notify/notify.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useCustomEvent,
useCustomEventsPath,
} from '@/utils/use-custom-event'
import { mergeProps } from '@/utils/merge-props'

export type NotifyPosition = 'top' | 'bottom'
export type NotifyType = 'primary' | 'success' | 'danger' | 'warning'
Expand Down Expand Up @@ -49,7 +50,7 @@ export const Notify: FunctionComponent<Partial<NotifyProps>> & {
duration,
onClose,
onClick,
} = { ...defaultProps, ...props }
} = mergeProps(defaultProps, props)

useCustomEvent(id as string, (status: boolean) => {
status ? show() : hide()
Expand Down Expand Up @@ -130,7 +131,6 @@ export function close(selector: string) {
customEvents.trigger(path, false)
}

Notify.defaultProps = defaultProps // 不可删除
Notify.displayName = 'NutNotify'
Notify.open = open
Notify.close = close
4 changes: 2 additions & 2 deletions src/packages/toast/toast.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
useParams,
} from '@/utils/use-custom-event'
import { usePropsValue } from '@/utils/use-props-value'
import { mergeProps } from '@/utils/merge-props'

export type ToastPosition = 'top' | 'bottom' | 'center'
export type ToastSize = 'small' | 'base' | 'large'
Expand Down Expand Up @@ -91,7 +92,7 @@ export const Toast: FunctionComponent<
wordBreak,
},
setParams,
} = useParams({ ...defaultProps, ...props })
} = useParams(mergeProps(defaultProps, props))
const timer = useRef(-1)

const [innerVisible, setInnerVisible] = usePropsValue({
Expand Down Expand Up @@ -222,7 +223,6 @@ export function hide(selector: string) {
customEvents.trigger(path, { status: false })
}

Toast.defaultProps = defaultProps // 不可删除
Toast.displayName = 'NutToast'
Toast.show = show
Toast.hide = hide
38 changes: 32 additions & 6 deletions src/utils/merge-props.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
export function mergeProps<T, U>(
defaultProps: T,
props: U
): { [Key in Extract<keyof T, keyof U>]: U[Key] } &
{ [Key in Exclude<keyof U, keyof T>]?: U[Key] } {
return { ...defaultProps, ...props }
export function mergeProps<A, B>(a: A, b: B): B & A
export function mergeProps<A, B, C>(a: A, b: B, c: C): C & B & A
export function mergeProps<A, B, C, D>(a: A, b: B, c: C, d: D): D & C & B & A
export function mergeProps(...items: any[]) {
const ret: any = {}
items.forEach((item) => {
if (item) {
Object.keys(item).forEach((key) => {
if (item[key] !== undefined) {
ret[key] = item[key]
}
})
}
})
return ret
}

/**
* Merge props and return the first non-undefined value.
* The later has higher priority. e.g. (10, 1, 5) => 5 wins.
* This is useful with legacy props that have been deprecated.
*/
export function mergeProp<T, DefaultT extends T = T>(
defaultProp: DefaultT,
...propList: T[]
): T | undefined {
for (let i = propList.length - 1; i >= 0; i -= 1) {
if (propList[i] !== undefined) {
return propList[i]
}
}
return defaultProp
}
Comment on lines +18 to +32
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

批准更改并建议记录使用案例

mergeProp 函数的实现正确地处理了属性列表,并返回第一个非 undefined 值,后面的属性具有更高的优先级。这对于处理已弃用的属性非常有用。建议在文档中添加此函数的使用案例,以帮助开发者了解其在实际场景中的应用。

Loading