Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit a35edad

Browse files
authored
chore: remove dependency on @stardust-ui/react-proptypes in @stardust-ui/react-component-ref (#1877)
1 parent f2478e2 commit a35edad

7 files changed

Lines changed: 34 additions & 24 deletions

File tree

packages/react-component-ref/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"bugs": "https://github.com/stardust-ui/react/issues",
77
"dependencies": {
88
"@babel/runtime": "^7.1.2",
9-
"@stardust-ui/react-proptypes": "^0.37.0",
109
"prop-types": "^15.7.2",
1110
"react-is": "^16.6.3"
1211
},
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import * as customPropTypes from '@stardust-ui/react-proptypes'
21
import * as PropTypes from 'prop-types'
32
import * as React from 'react'
43
import * as ReactIs from 'react-is'
54

65
import RefFindNode from './RefFindNode'
76
import RefForward from './RefForward'
8-
import { RefProps } from './types'
7+
import { RefProps, refPropType } from './types'
98

109
const Ref: React.FunctionComponent<RefProps> = props => {
1110
const { children, innerRef } = props
@@ -17,9 +16,12 @@ const Ref: React.FunctionComponent<RefProps> = props => {
1716
}
1817

1918
Ref.displayName = 'Ref'
20-
Ref.propTypes = {
21-
children: PropTypes.element.isRequired,
22-
innerRef: customPropTypes.ref.isRequired as PropTypes.Validator<React.Ref<any>>,
19+
// TODO: use Babel plugin for this
20+
if (process.env.NODE_ENV !== 'production') {
21+
Ref.propTypes = {
22+
children: PropTypes.element.isRequired,
23+
innerRef: refPropType.isRequired,
24+
}
2325
}
2426

2527
export default Ref

packages/react-component-ref/src/RefFindNode.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
import * as customPropTypes from '@stardust-ui/react-proptypes'
21
import * as PropTypes from 'prop-types'
32
import * as React from 'react'
43
import * as ReactDOM from 'react-dom'
54

65
import handleRef from './handleRef'
7-
import { RefProps } from './types'
6+
import { RefProps, refPropType } from './types'
87

98
export default class RefFindNode extends React.Component<RefProps> {
109
static displayName = 'RefFindNode'
1110

12-
static propTypes = {
13-
children: PropTypes.element.isRequired,
14-
innerRef: customPropTypes.ref.isRequired as PropTypes.Validator<React.Ref<any>>,
15-
}
11+
// TODO: use Babel plugin for this
12+
static propTypes =
13+
process.env.NODE_ENV !== 'production'
14+
? {
15+
children: PropTypes.element.isRequired,
16+
innerRef: refPropType.isRequired,
17+
}
18+
: {}
1619

1720
prevNode: Node | null = null
1821

packages/react-component-ref/src/RefForward.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
import * as customPropTypes from '@stardust-ui/react-proptypes'
21
import * as PropTypes from 'prop-types'
32
import * as React from 'react'
43

54
import handleRef from './handleRef'
6-
import { RefProps } from './types'
5+
import { RefProps, refPropType } from './types'
76

87
export default class RefForward extends React.Component<RefProps> {
98
static displayName = 'RefForward'
109

11-
static propTypes = {
12-
children: PropTypes.element.isRequired,
13-
innerRef: customPropTypes.ref.isRequired as PropTypes.Validator<React.Ref<any>>,
14-
}
10+
// TODO: use Babel plugin for this
11+
static propTypes =
12+
process.env.NODE_ENV !== 'production'
13+
? {
14+
children: PropTypes.element.isRequired,
15+
innerRef: refPropType.isRequired,
16+
}
17+
: {}
1518

1619
handleRefOverride = (node: HTMLElement) => {
1720
const { children, innerRef } = this.props

packages/react-component-ref/src/isRefObject.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import * as React from 'react'
22

3-
/**
4-
* Check that the passed object is a valid React ref object.
5-
*/
3+
/** Checks that the passed object is a valid React ref object. */
64
const isRefObject = (ref: any): ref is React.RefObject<any> =>
75
// https://github.com/facebook/react/blob/v16.8.2/packages/react-reconciler/src/ReactFiberCommitWork.js#L665
86
ref !== null && typeof ref === 'object' && ref.hasOwnProperty('current')

packages/react-component-ref/src/toRefObject.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ const nullRefObject: React.RefObject<null> = { current: null }
55
// A map of created ref objects to provide memoization.
66
const refObjects = new WeakMap<Node, React.RefObject<Node>>()
77

8-
/**
9-
* Creates a React ref object from existing DOM node.
10-
*/
8+
/** Creates a React ref object from existing DOM node. */
119
const toRefObject = <T extends Node>(node: T): React.RefObject<T> => {
1210
// A "null" is not valid key for a WeakMap
1311
if (node === null) {

packages/react-component-ref/src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as PropTypes from 'prop-types'
12
import * as React from 'react'
23

34
export interface RefProps {
@@ -10,3 +11,9 @@ export interface RefProps {
1011
*/
1112
innerRef: React.Ref<any>
1213
}
14+
15+
/** A checker that matches the React.Ref type. */
16+
export const refPropType = PropTypes.oneOfType([
17+
PropTypes.func,
18+
PropTypes.object,
19+
]) as PropTypes.Requireable<React.Ref<any>>

0 commit comments

Comments
 (0)