Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | 4x 4x 4x 4x 4x 4x | import React from 'react'
import { OptionalKeys } from '../types'
/**
* 定义组件的选项。
*/
export type DefineComponentOptions<
/**
* 组件属性。
*/
TProps extends Record<string, any>,
/**
* 是否转发 ref。
*/
TForwardRef extends boolean,
/**
* 要转发的 ref。
*/
TRef extends any = never
> = {
/**
* 可选属性的默认值。
*/
defaultProps?: {
[K in OptionalKeys<TProps>]?: TProps[K]
}
/**
* 组件展示名称。
*/
displayName?: string
/**
* 组件。
*/
component: TForwardRef extends true
? React.ForwardRefRenderFunction<TRef, Omit<TProps, 'key' | 'ref'>>
: React.FC<Omit<TProps, 'key' | 'ref'>>
} & (TForwardRef extends true
? {
/**
* 是否转发 ref。
*/
forwardRef?: true
}
: {
/**
* 是否转发 ref。
*/
forwardRef: false
})
/**
* 定义组件。
*
* @param options 选项
*/
export function defineComponent<
TProps extends Record<string, any>,
TRef extends any = any
>(
options: DefineComponentOptions<TProps, true, TRef>,
): React.ForwardRefExoticComponent<TProps & { ref?: React.Ref<TRef> }>
/**
* 定义组件。
*
* @param options 选项
*/
export function defineComponent<
TProps extends Record<string, any>,
TRef extends any = any
>(options: DefineComponentOptions<TProps, false>): React.FC<TProps>
export function defineComponent(
options: DefineComponentOptions<any, any, any>,
): any {
const forwardRef = options.forwardRef ?? true
const displayName = options.displayName ?? options.component.name
const component = forwardRef
? React.forwardRef(options.component as any)
: options.component
component.displayName = displayName
component.defaultProps = options.defaultProps
return component
}
|