Skip to content

Commit

Permalink
chore(button): forward refs to button dom element
Browse files Browse the repository at this point in the history
  • Loading branch information
mhuggins committed Aug 5, 2018
1 parent 3a48a09 commit dee8b79
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 37 deletions.
31 changes: 13 additions & 18 deletions src/renderer/shared/components/Forms/Button/Button.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
import React from 'react';
import classNames from 'classnames';
import { string } from 'prop-types';
import { string, func } from 'prop-types';
import { omit } from 'lodash';

import styles from './Button.scss';

export default class Button extends React.PureComponent {
class Button extends React.PureComponent {
render() {
return ( // eslint-disable-next-line react/button-has-type
<button
{...this.props}
ref={this.registerRef}
{...omit(this.props, 'forwardedRef')}
ref={this.props.forwardedRef}
className={classNames(styles.button, this.props.className)}
/>
);
}

registerRef = (el) => {
this.button = el;
}

focus = () => {
this.button.focus();
}

blur = () => {
this.button.blur();
}
}

Button.propTypes = {
className: string,
type: string
type: string,
forwardedRef: func
};

Button.defaultProps = {
className: null,
type: 'button'
type: 'button',
forwardedRef: null
};

export default React.forwardRef((props, ref) => {
return <Button {...props} forwardedRef={ref} />;
});
33 changes: 14 additions & 19 deletions src/renderer/shared/components/Forms/PrimaryButton/PrimaryButton.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
import React from 'react';
import classNames from 'classnames';
import { string } from 'prop-types';
import { string, func } from 'prop-types';
import { omit } from 'lodash';

import Button from '../Button';
import styles from './PrimaryButton.scss';

export default class PrimaryButton extends React.PureComponent {
class PrimaryButton extends React.PureComponent {
render() {
return ( // eslint-disable-next-line react/button-has-type
return (
<Button
{...this.props}
ref={this.registerRef}
{...omit(this.props, 'forwardedRef')}
ref={this.props.forwardedRef}
className={classNames(styles.primaryButton, this.props.className)}
/>
);
}

registerRef = (el) => {
this.button = el;
}

focus = () => {
this.button.focus();
}

blur = () => {
this.button.blur();
}
}

PrimaryButton.propTypes = {
className: string
className: string,
forwardedRef: func
};

PrimaryButton.defaultProps = {
className: null
className: null,
forwardedRef: null
};

export default React.forwardRef((props, ref) => {
return <PrimaryButton {...props} forwardedRef={ref} />;
});

0 comments on commit dee8b79

Please sign in to comment.