You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.3 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import classNames from 'classnames';
  4. export default class Button extends React.PureComponent {
  5. static propTypes = {
  6. text: PropTypes.node,
  7. onClick: PropTypes.func,
  8. disabled: PropTypes.bool,
  9. block: PropTypes.bool,
  10. secondary: PropTypes.bool,
  11. size: PropTypes.number,
  12. className: PropTypes.string,
  13. style: PropTypes.object,
  14. children: PropTypes.node,
  15. title: PropTypes.string,
  16. };
  17. static defaultProps = {
  18. size: 36,
  19. };
  20. handleClick = (e) => {
  21. if (!this.props.disabled) {
  22. this.props.onClick(e);
  23. }
  24. }
  25. setRef = (c) => {
  26. this.node = c;
  27. }
  28. focus() {
  29. this.node.focus();
  30. }
  31. render () {
  32. let attrs = {
  33. className: classNames('button', this.props.className, {
  34. 'button-secondary': this.props.secondary,
  35. 'button--block': this.props.block,
  36. }),
  37. disabled: this.props.disabled,
  38. onClick: this.handleClick,
  39. ref: this.setRef,
  40. style: {
  41. padding: `0 ${this.props.size / 2.25}px`,
  42. height: `${this.props.size}px`,
  43. lineHeight: `${this.props.size}px`,
  44. ...this.props.style,
  45. },
  46. };
  47. if (this.props.title) attrs.title = this.props.title;
  48. return (
  49. <button {...attrs}>
  50. {this.props.text || this.props.children}
  51. </button>
  52. );
  53. }
  54. }