| 1 | // Copied directly from:
|
|---|
| 2 | // https://github.com/mridgway/hoist-non-react-statics/blob/main/src/index.js
|
|---|
| 3 | // https://unpkg.com/browse/@types/hoist-non-react-statics@3.3.6/index.d.ts
|
|---|
| 4 |
|
|---|
| 5 | /**
|
|---|
| 6 | * Copyright 2015, Yahoo! Inc.
|
|---|
| 7 | * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
|
|---|
| 8 | */
|
|---|
| 9 | import type { ForwardRefExoticComponent, MemoExoticComponent } from 'react'
|
|---|
| 10 | import { ForwardRef, Memo, isMemo } from '../utils/react-is'
|
|---|
| 11 |
|
|---|
| 12 | const REACT_STATICS = {
|
|---|
| 13 | childContextTypes: true,
|
|---|
| 14 | contextType: true,
|
|---|
| 15 | contextTypes: true,
|
|---|
| 16 | defaultProps: true,
|
|---|
| 17 | displayName: true,
|
|---|
| 18 | getDefaultProps: true,
|
|---|
| 19 | getDerivedStateFromError: true,
|
|---|
| 20 | getDerivedStateFromProps: true,
|
|---|
| 21 | mixins: true,
|
|---|
| 22 | propTypes: true,
|
|---|
| 23 | type: true,
|
|---|
| 24 | } as const
|
|---|
| 25 |
|
|---|
| 26 | const KNOWN_STATICS = {
|
|---|
| 27 | name: true,
|
|---|
| 28 | length: true,
|
|---|
| 29 | prototype: true,
|
|---|
| 30 | caller: true,
|
|---|
| 31 | callee: true,
|
|---|
| 32 | arguments: true,
|
|---|
| 33 | arity: true,
|
|---|
| 34 | } as const
|
|---|
| 35 |
|
|---|
| 36 | const FORWARD_REF_STATICS = {
|
|---|
| 37 | $$typeof: true,
|
|---|
| 38 | render: true,
|
|---|
| 39 | defaultProps: true,
|
|---|
| 40 | displayName: true,
|
|---|
| 41 | propTypes: true,
|
|---|
| 42 | } as const
|
|---|
| 43 |
|
|---|
| 44 | const MEMO_STATICS = {
|
|---|
| 45 | $$typeof: true,
|
|---|
| 46 | compare: true,
|
|---|
| 47 | defaultProps: true,
|
|---|
| 48 | displayName: true,
|
|---|
| 49 | propTypes: true,
|
|---|
| 50 | type: true,
|
|---|
| 51 | } as const
|
|---|
| 52 |
|
|---|
| 53 | const TYPE_STATICS = {
|
|---|
| 54 | [ForwardRef]: FORWARD_REF_STATICS,
|
|---|
| 55 | [Memo]: MEMO_STATICS,
|
|---|
| 56 | } as const
|
|---|
| 57 |
|
|---|
| 58 | function getStatics(component: any) {
|
|---|
| 59 | // React v16.11 and below
|
|---|
| 60 | if (isMemo(component)) {
|
|---|
| 61 | return MEMO_STATICS
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | // React v16.12 and above
|
|---|
| 65 | return TYPE_STATICS[component['$$typeof']] || REACT_STATICS
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | export type NonReactStatics<
|
|---|
| 69 | Source,
|
|---|
| 70 | C extends {
|
|---|
| 71 | [key: string]: true
|
|---|
| 72 | } = {},
|
|---|
| 73 | > = {
|
|---|
| 74 | [key in Exclude<
|
|---|
| 75 | keyof Source,
|
|---|
| 76 | Source extends MemoExoticComponent<any>
|
|---|
| 77 | ? keyof typeof MEMO_STATICS | keyof C
|
|---|
| 78 | : Source extends ForwardRefExoticComponent<any>
|
|---|
| 79 | ? keyof typeof FORWARD_REF_STATICS | keyof C
|
|---|
| 80 | : keyof typeof REACT_STATICS | keyof typeof KNOWN_STATICS | keyof C
|
|---|
| 81 | >]: Source[key]
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | const defineProperty = Object.defineProperty
|
|---|
| 85 | const getOwnPropertyNames = Object.getOwnPropertyNames
|
|---|
| 86 | const getOwnPropertySymbols = Object.getOwnPropertySymbols
|
|---|
| 87 | const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor
|
|---|
| 88 | const getPrototypeOf = Object.getPrototypeOf
|
|---|
| 89 | const objectPrototype = Object.prototype
|
|---|
| 90 |
|
|---|
| 91 | export default function hoistNonReactStatics<
|
|---|
| 92 | Target,
|
|---|
| 93 | Source,
|
|---|
| 94 | CustomStatic extends {
|
|---|
| 95 | [key: string]: true
|
|---|
| 96 | } = {},
|
|---|
| 97 | >(
|
|---|
| 98 | targetComponent: Target,
|
|---|
| 99 | sourceComponent: Source,
|
|---|
| 100 | ): Target & NonReactStatics<Source, CustomStatic> {
|
|---|
| 101 | if (typeof sourceComponent !== 'string') {
|
|---|
| 102 | // don't hoist over string (html) components
|
|---|
| 103 |
|
|---|
| 104 | if (objectPrototype) {
|
|---|
| 105 | const inheritedComponent = getPrototypeOf(sourceComponent)
|
|---|
| 106 | if (inheritedComponent && inheritedComponent !== objectPrototype) {
|
|---|
| 107 | hoistNonReactStatics(targetComponent, inheritedComponent)
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | let keys: (string | symbol)[] = getOwnPropertyNames(sourceComponent)
|
|---|
| 112 |
|
|---|
| 113 | if (getOwnPropertySymbols) {
|
|---|
| 114 | keys = keys.concat(getOwnPropertySymbols(sourceComponent))
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | const targetStatics = getStatics(targetComponent)
|
|---|
| 118 | const sourceStatics = getStatics(sourceComponent)
|
|---|
| 119 |
|
|---|
| 120 | for (let i = 0; i < keys.length; ++i) {
|
|---|
| 121 | const key = keys[i]
|
|---|
| 122 | if (
|
|---|
| 123 | !KNOWN_STATICS[key as keyof typeof KNOWN_STATICS] &&
|
|---|
| 124 | !(sourceStatics && sourceStatics[key as keyof typeof sourceStatics]) &&
|
|---|
| 125 | !(targetStatics && targetStatics[key as keyof typeof targetStatics])
|
|---|
| 126 | ) {
|
|---|
| 127 | const descriptor = getOwnPropertyDescriptor(sourceComponent, key)
|
|---|
| 128 | try {
|
|---|
| 129 | // Avoid failures from read-only properties
|
|---|
| 130 | defineProperty(targetComponent, key, descriptor!)
|
|---|
| 131 | } catch (e) {
|
|---|
| 132 | // ignore
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | return targetComponent as any
|
|---|
| 139 | }
|
|---|