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

chore(Sidebar|Visibility): use createRef() API internally #3455

Merged
merged 1 commit into from
Feb 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 8 additions & 5 deletions docs/src/examples/elements/Button/Usage/ButtonExampleFocus.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import React, { Component } from 'react'
import React, { Component, createRef } from 'react'
import { Button, Grid } from 'semantic-ui-react'

export default class ButtonExampleFocus extends Component {
handleClick = () => this.ref.focus()

handleRef = c => (this.ref = c)
buttonRef = createRef()
handleClick = () => this.buttonRef.current.focus()

render() {
return (
<Grid>
<Grid.Column width={8}>
<Button content='A button that can be focused' primary ref={this.handleRef} />
<Button
content='A button that can be focused'
primary
ref={this.buttonRef}
/>
</Grid.Column>
<Grid.Column width={8}>
<Button content='Set focused' onClick={this.handleClick} />
Expand Down
15 changes: 5 additions & 10 deletions docs/src/examples/elements/Input/Usage/InputExampleRefFocus.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import React, { Component } from 'react'
import React, { Component, createRef } from 'react'
import { Input, Button } from 'semantic-ui-react'

class InputExampleRefFocus extends Component {
handleRef = (c) => {
this.inputRef = c
}

focus = () => {
this.inputRef.focus()
}
inputRef = createRef()
handleClick = () => this.inputRef.current.focus()

render() {
return (
<div>
<Button content='focus' onClick={this.focus} />
<Input ref={this.handleRef} placeholder='Search...' />
<Button content='focus' onClick={this.handleClick} />
<Input ref={this.inputRef} placeholder='Search...' />
</div>
)
}
Expand Down
14 changes: 4 additions & 10 deletions src/behaviors/Visibility/Visibility.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash'
import PropTypes from 'prop-types'
import React, { Component } from 'react'
import React, { Component, createRef } from 'react'

import {
eventStack,
Expand Down Expand Up @@ -180,8 +180,8 @@ export default class Visibility extends Component {
topPassed: false,
topVisible: false,
}

firedCallbacks = []
ref = createRef()

// ----------------------------------------
// Lifecycle
Expand Down Expand Up @@ -360,7 +360,7 @@ export default class Visibility extends Component {

computeCalculations() {
const { offset } = this.props
const { bottom, height, top, width } = this.ref.getBoundingClientRect()
const { bottom, height, top, width } = this.ref.current.getBoundingClientRect()
const [topOffset, bottomOffset] = normalizeOffset(offset)

const direction = window.pageYOffset > this.pageYOffset ? 'down' : 'up'
Expand Down Expand Up @@ -396,12 +396,6 @@ export default class Visibility extends Component {
}
}

// ----------------------------------------
// Refs
// ----------------------------------------

handleRef = c => (this.ref = c)

// ----------------------------------------
// Render
// ----------------------------------------
Expand All @@ -412,7 +406,7 @@ export default class Visibility extends Component {
const rest = getUnhandledProps(Visibility, this.props)

return (
<ElementType {...rest} ref={this.handleRef}>
<ElementType {...rest} ref={this.ref}>
{children}
</ElementType>
)
Expand Down
9 changes: 4 additions & 5 deletions src/modules/Sidebar/Sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import EventStack from '@semantic-ui-react/event-stack'
import cx from 'classnames'
import _ from 'lodash'
import PropTypes from 'prop-types'
import React, { Component } from 'react'
import React, { Component, createRef } from 'react'

import Ref from '../../addons/Ref'
import {
Expand Down Expand Up @@ -99,6 +99,7 @@ class Sidebar extends Component {
static Pusher = SidebarPusher

state = {}
ref = createRef()

componentDidUpdate(prevProps) {
const { visible: prevVisible } = prevProps
Expand Down Expand Up @@ -137,14 +138,12 @@ class Sidebar extends Component {
}

handleDocumentClick = (e) => {
if (!doesNodeContainClick(this.ref, e)) {
if (!doesNodeContainClick(this.ref.current, e)) {
this.skipNextCallback = true
_.invoke(this.props, 'onHide', e, { ...this.props, visible: false })
}
}

handleRef = c => (this.ref = c)

render() {
const {
animation,
Expand Down Expand Up @@ -172,7 +171,7 @@ class Sidebar extends Component {
const ElementType = getElementType(Sidebar, this.props)

return (
<Ref innerRef={this.handleRef}>
<Ref innerRef={this.ref}>
<ElementType {...rest} className={classes}>
{childrenUtils.isNil(children) ? content : children}
{visible && <EventStack name='click' on={this.handleDocumentClick} target={target} />}
Expand Down
2 changes: 1 addition & 1 deletion test/specs/behaviors/Visibility/Visibility-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const wrapperMount = (node, opts) => {

const mockScroll = (top, bottom) => {
if (wrapper) {
wrapper.instance().ref = {
wrapper.instance().ref.current = {
getBoundingClientRect: () => ({
bottom,
top,
Expand Down