|
1 | 1 | import React from 'react'; |
2 | | -import PropTypes from 'prop-types'; |
3 | 2 | import classNames from 'classnames'; |
4 | 3 | import { Cancel, CheckCircleOutline, SpinnerSimple } from '../../icons'; |
5 | 4 | import Button from '../Button'; |
6 | 5 | import Icon from '../Icon'; |
7 | 6 |
|
| 7 | +export interface StatefulButtonProps extends React.ComponentPropsWithoutRef<typeof Button> { |
| 8 | + /** Optionally specify additional CSS classes to give this button. */ |
| 9 | + className?: string; |
| 10 | + /** |
| 11 | + * Each state has: |
| 12 | + * - A label (required) |
| 13 | + * - An icon |
| 14 | + * - an option to be disabled |
| 15 | + * |
| 16 | + * Control the state with the `state` prop. Example usage: |
| 17 | + * |
| 18 | + * ```jsx |
| 19 | + * <StatefulButton |
| 20 | + * state="pending" |
| 21 | + * labels={{ |
| 22 | + * default: 'Download', |
| 23 | + * pending: 'Downloading', |
| 24 | + * complete: 'Downloaded', |
| 25 | + * }} |
| 26 | + * icons={{ |
| 27 | + * default: <Icon className="fa fa-download" />, |
| 28 | + * pending: <Icon className="fa fa-spinner fa-spin" />, |
| 29 | + * complete: <Icon className="fa fa-check" />, |
| 30 | + * }} |
| 31 | + * disabledStates=['pending'] |
| 32 | + * className='btn-primary mr-2' |
| 33 | + * /> |
| 34 | + * ``` |
| 35 | + */ |
| 36 | + state?: string; |
| 37 | + /** Each state has a `label`. Required. */ |
| 38 | + labels: { [key: string]: React.ReactNode }; |
| 39 | + /** Each state has an `icon`. */ |
| 40 | + icons?: { [key: string]: React.ReactNode }; |
| 41 | + /** Each state has a `disabledState` */ |
| 42 | + disabledStates?: string[]; |
| 43 | +} |
| 44 | + |
8 | 45 | function StatefulButton({ |
9 | 46 | className, |
10 | | - state, |
| 47 | + state = 'default', |
11 | 48 | labels, |
12 | | - icons, |
13 | | - disabledStates, |
| 49 | + icons = { |
| 50 | + default: undefined, |
| 51 | + pending: <Icon src={SpinnerSimple} className={classNames('icon-spin')} />, |
| 52 | + complete: <Icon src={CheckCircleOutline} />, |
| 53 | + error: <Icon src={Cancel} />, |
| 54 | + }, |
| 55 | + disabledStates = ['pending', 'complete'], |
14 | 56 | onClick, |
15 | 57 | ...attributes |
16 | | -}) { |
| 58 | +}: StatefulButtonProps) { |
17 | 59 | const isDisabled = disabledStates.indexOf(state) !== -1; |
18 | 60 | const icon = icons[state] !== undefined ? icons[state] : icons.default; |
19 | 61 | const label = labels[state] !== undefined ? labels[state] : labels.default; |
@@ -51,56 +93,4 @@ function StatefulButton({ |
51 | 93 | ); |
52 | 94 | } |
53 | 95 |
|
54 | | -StatefulButton.propTypes = { |
55 | | - className: PropTypes.string, |
56 | | - /** |
57 | | - * Each state has: |
58 | | - * - A label (required) |
59 | | - * - An icon |
60 | | - * - an option to be disabled |
61 | | - * |
62 | | - * Control the state with the `state` prop. Example usage: |
63 | | - * |
64 | | - * ```jsx |
65 | | - * <StatefulButton |
66 | | - * state="pending" |
67 | | - * labels={{ |
68 | | - * default: 'Download', |
69 | | - * pending: 'Downloading', |
70 | | - * complete: 'Downloaded', |
71 | | - * }} |
72 | | - * icons={{ |
73 | | - * default: <Icon className="fa fa-download" />, |
74 | | - * pending: <Icon className="fa fa-spinner fa-spin" />, |
75 | | - * complete: <Icon className="fa fa-check" />, |
76 | | - * }} |
77 | | - * disabledStates=['pending'] |
78 | | - * className='btn-primary mr-2' |
79 | | - * /> |
80 | | - * ``` |
81 | | - */ |
82 | | - state: PropTypes.string, |
83 | | - /** Required. Each state has a `label`. */ |
84 | | - labels: PropTypes.objectOf(PropTypes.node).isRequired, |
85 | | - /** Required. Each state has an `icon`. */ |
86 | | - icons: PropTypes.objectOf(PropTypes.node), |
87 | | - /** Required. Each state has a `disabledState` */ |
88 | | - disabledStates: PropTypes.arrayOf(PropTypes.string), |
89 | | - /** Specifies the callback function when the button is clicked */ |
90 | | - onClick: PropTypes.func, |
91 | | -}; |
92 | | - |
93 | | -StatefulButton.defaultProps = { |
94 | | - className: undefined, |
95 | | - state: 'default', |
96 | | - icons: { |
97 | | - default: undefined, |
98 | | - pending: <Icon src={SpinnerSimple} className={classNames('icon-spin')} />, |
99 | | - complete: <Icon src={CheckCircleOutline} />, |
100 | | - error: <Icon src={Cancel} />, |
101 | | - }, |
102 | | - disabledStates: ['pending', 'complete'], |
103 | | - onClick: undefined, |
104 | | -}; |
105 | | - |
106 | 96 | export default StatefulButton; |
0 commit comments