Skip to content

Commit f1e6df7

Browse files
author
Alexander Fedyashov
committed
feat(Ref): add component
1 parent 5307f11 commit f1e6df7

File tree

14 files changed

+43
-18
lines changed

14 files changed

+43
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"babel-plugin-__coverage__": "^11.0.0",
7070
"babel-plugin-lodash": "^3.2.10",
7171
"babel-plugin-react-transform": "^2.0.2",
72-
"babel-plugin-transform-react-handled-props": "^0.2.3",
72+
"babel-plugin-transform-react-handled-props": "^0.2.5",
7373
"babel-plugin-transform-react-remove-prop-types": "^0.3.2",
7474
"babel-plugin-transform-runtime": "^6.15.0",
7575
"babel-preset-es2015": "^6.18.0",

src/addons/Ref/Ref.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default class Ref extends Component {
1818
*
1919
* @param {HTMLElement} node - Referred node.
2020
*/
21-
innerRef: PropTypes.func.isRequired,
21+
innerRef: PropTypes.func,
2222
}
2323

2424
static _meta = {
@@ -28,9 +28,10 @@ export default class Ref extends Component {
2828

2929
componentDidMount() {
3030
const { innerRef } = this.props
31-
const node = findDOMNode(this)
3231

33-
innerRef(node)
32+
// Heads up! Don't move this condition, it's a short circle that avoids run of `findDOMNode`
33+
// if `innerRef` isn't passed
34+
if (innerRef) innerRef(findDOMNode(this))
3435
}
3536

3637
render() {

src/addons/TextArea/TextArea.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ class TextArea extends Component {
127127
return (
128128
<ElementType
129129
{...rest}
130+
innerRef={this.handleRef}
130131
onChange={this.handleChange}
131-
ref={this.handleRef}
132132
rows={rows}
133133
style={{ height, resize, ...style }}
134134
value={value}

src/behaviors/Visibility/Visibility.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,6 @@ export default class Visibility extends Component {
305305
const ElementType = getElementType(Visibility, this.props)
306306
const rest = getUnhandledProps(Visibility, this.props)
307307

308-
return <ElementType {...rest} ref={this.handleRef}>{children}</ElementType>
308+
return <ElementType {...rest} innerRef={this.handleRef}>{children}</ElementType>
309309
}
310310
}

src/elements/Button/Button.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ class Button extends Component {
281281
{...rest}
282282
className={classes}
283283
disabled={(disabled && ElementType === 'button') || undefined}
284+
innerRef={this.handleRef}
284285
onClick={this.handleClick}
285-
ref={this.handleRef}
286286
tabIndex={tabIndex}
287287
>
288288
{hasChildren && children}

src/elements/Image/Image.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ function Image(props) {
7070
className,
7171
)
7272
const rest = getUnhandledProps(Image, props)
73+
const wrappedImage = !_.isNil(dimmer) || !_.isNil(label) || !_.isNil(wrapped) || !childrenUtils.isNil(children)
7374
const ElementType = getElementType(Image, props, () => {
74-
if (!_.isNil(dimmer) || !_.isNil(label) || !_.isNil(wrapped) || !childrenUtils.isNil(children)) return 'div'
75+
if (wrappedImage) return 'div'
7576
})
7677

7778
if (!childrenUtils.isNil(children)) {
@@ -81,7 +82,7 @@ function Image(props) {
8182
const rootProps = { ...rest, className: classes }
8283
const imgTagProps = { alt, src, height, width }
8384

84-
if (ElementType === 'img') return <ElementType {...rootProps} {...imgTagProps} />
85+
if (!wrappedImage) return <ElementType {...rootProps} {...imgTagProps} />
8586

8687
return (
8788
<ElementType {...rootProps} href={href}>

src/addons/Ref/withRef.js renamed to src/hoc/withRef.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
import PropTypes from 'prop-types'
22
import React, { Component } from 'react'
33

4-
import Ref from './Ref'
4+
import Ref from '../addons/Ref'
55

6-
const withRef = Child => class extends Component {
6+
const withRef = Child => class refHOC extends Component {
77
static propTypes = {
88
/**
99
* Called when componentDidMount.
1010
*
1111
* @param {HTMLElement} node - Referred node.
1212
*/
13-
innerRef: PropTypes.func.isRequired,
13+
innerRef: PropTypes.func,
1414
}
1515

1616
render() {
1717
const { innerRef, ...rest } = this.props
1818

1919
if (typeof Child === 'string') return <Child {...rest} ref={innerRef} />
20+
if (Child._meta) return <Child {...rest} innerRef={innerRef} />
21+
2022
return (
2123
<Ref innerRef={innerRef}>
2224
<Child {...rest} />

src/lib/getElementType.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import withRef from 'src/addons/Ref/withRef'
1+
import withRef from 'src/hoc/withRef'
22

33
/**
44
* Returns a createElement() type based on the props of the Component.

src/modules/Dropdown/Dropdown.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1263,13 +1263,13 @@ export default class Dropdown extends Component {
12631263
{...rest}
12641264
{...ariaOptions}
12651265
className={classes}
1266+
innerRef={this.handleRef}
12661267
onBlur={this.handleBlur}
12671268
onClick={this.handleClick}
12681269
onMouseDown={this.handleMouseDown}
12691270
onFocus={this.handleFocus}
12701271
onChange={this.handleChange}
12711272
tabIndex={this.computeTabIndex()}
1272-
ref={this.handleRef}
12731273
>
12741274
{this.renderLabels()}
12751275
{this.renderSearchInput()}

src/modules/Modal/Modal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,15 +288,15 @@ class Modal extends Component {
288288

289289
if (!childrenUtils.isNil(children)) {
290290
return (
291-
<ElementType {...rest} className={classes} style={{ marginTop, ...style }} ref={this.handleRef}>
291+
<ElementType {...rest} className={classes} style={{ marginTop, ...style }} innerRef={this.handleRef}>
292292
{closeIconJSX}
293293
{children}
294294
</ElementType>
295295
)
296296
}
297297

298298
return (
299-
<ElementType {...rest} className={classes} style={{ marginTop, ...style }} ref={this.handleRef}>
299+
<ElementType {...rest} className={classes} style={{ marginTop, ...style }} innerRef={this.handleRef}>
300300
{closeIconJSX}
301301
{ModalHeader.create(header)}
302302
{ModalContent.create(content)}

0 commit comments

Comments
 (0)