The code powering m.abunchtell.com https://m.abunchtell.com
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

91 行
2.1 KiB

  1. import { Motion, spring } from 'react-motion';
  2. import PropTypes from 'prop-types';
  3. class IconButton extends React.PureComponent {
  4. constructor (props, context) {
  5. super(props, context);
  6. this.handleClick = this.handleClick.bind(this);
  7. }
  8. handleClick (e) {
  9. e.preventDefault();
  10. if (!this.props.disabled) {
  11. this.props.onClick(e);
  12. }
  13. }
  14. render () {
  15. let style = {
  16. fontSize: `${this.props.size}px`,
  17. width: `${this.props.size * 1.28571429}px`,
  18. height: `${this.props.size * 1.28571429}px`,
  19. lineHeight: `${this.props.size}px`,
  20. ...this.props.style
  21. };
  22. if (this.props.active) {
  23. style = { ...style, ...this.props.activeStyle };
  24. }
  25. const classes = ['icon-button'];
  26. if (this.props.active) {
  27. classes.push('active');
  28. }
  29. if (this.props.disabled) {
  30. classes.push('disabled');
  31. }
  32. if (this.props.inverted) {
  33. classes.push('inverted');
  34. }
  35. if (this.props.overlay) {
  36. classes.push('overlayed');
  37. }
  38. return (
  39. <Motion defaultStyle={{ rotate: this.props.active ? -360 : 0 }} style={{ rotate: this.props.animate ? spring(this.props.active ? -360 : 0, { stiffness: 120, damping: 7 }) : 0 }}>
  40. {({ rotate }) =>
  41. <button
  42. aria-label={this.props.title}
  43. title={this.props.title}
  44. className={classes.join(' ')}
  45. onClick={this.handleClick}
  46. style={style}>
  47. <i style={{ transform: `rotate(${rotate}deg)` }} className={`fa fa-fw fa-${this.props.icon}`} aria-hidden='true' />
  48. </button>
  49. }
  50. </Motion>
  51. );
  52. }
  53. }
  54. IconButton.propTypes = {
  55. title: PropTypes.string.isRequired,
  56. icon: PropTypes.string.isRequired,
  57. onClick: PropTypes.func,
  58. size: PropTypes.number,
  59. active: PropTypes.bool,
  60. style: PropTypes.object,
  61. activeStyle: PropTypes.object,
  62. disabled: PropTypes.bool,
  63. inverted: PropTypes.bool,
  64. animate: PropTypes.bool,
  65. overlay: PropTypes.bool
  66. };
  67. IconButton.defaultProps = {
  68. size: 18,
  69. active: false,
  70. disabled: false,
  71. animate: false,
  72. overlay: false
  73. };
  74. export default IconButton;