4 | react-progress-button example
5 |
6 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/example/index-promises.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | react-progress-button example
5 |
6 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 Mathieu Dutour
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/example/src/app.promise.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import ReactDOM from 'react-dom'
3 | import ProgressButton from '../../lib/index'
4 | import createReactClass from 'create-react-class'
5 |
6 | const App = createReactClass({
7 | render () {
8 | return (
9 |
41 | )
42 | },
43 |
44 | handleClick () {
45 | this.setState({buttonState: 'loading'})
46 | // make asynchronous call
47 | setTimeout(() => {
48 | this.setState({buttonState: 'success'})
49 | }, 3000)
50 | }
51 | })
52 | ```
53 |
54 | [Source](https://github.com/mathieudutour/react-progress-button/blob/master/example/index.html)
55 |
56 | ### Using Promises:
57 |
58 | If the function passed in via the `onClick` prop return a Promise or if a promise
59 | is passed as an argument of the `loading` method,
60 | the component will automatically transition to its success or error
61 | states based on the outcome of the Promise without the need for
62 | external manipulation of state using a ref.
63 |
64 | ```javascript
65 | import ProgressButton from 'react-progress-button'
66 |
67 | const App = React.createClass({
68 | render () {
69 | return (
70 |
71 |
72 | Go!
73 |
74 |
75 | )
76 | },
77 |
78 | handleClick() {
79 | return new Promise(function(resolve, reject) {
80 | setTimeout(resolve, 3000)
81 | })
82 | }
83 | });
84 | ```
85 |
86 | [Source](https://github.com/mathieudutour/react-progress-button/blob/master/example/index-promises.html)
87 |
88 | ## API
89 |
90 | ### Props
91 |
92 | All props are optional. All props other than that will be passed to the top element.
93 |
94 | ##### controlled
95 |
96 | `true` if you control the button state (by providing `props.state` and `props.onClick`).`false` to let the button manage its state with Promises.
97 |
98 | ##### classNamespace
99 |
100 | Namespace for CSS classes, default is `pb-` i.e CSS classes are `pb-button`.
101 |
102 | ##### durationError
103 |
104 | Duration (ms) before going back to normal state when an error occurs,
105 | default is 1200
106 |
107 | ##### durationSuccess
108 |
109 | Duration (ms) before going back to normal state when an success occurs,
110 | default is 500
111 |
112 | ##### onClick
113 |
114 | Function to call when the button is clicked; if it returns a Promise
115 | then the component will transition to the success/error state based on
116 | the outcome of the Promise
117 |
118 | ##### onError
119 |
120 | Function to call when going back to the normal state after an error
121 |
122 | ##### onSuccess
123 |
124 | Function to call when going back to the normal state after a success
125 |
126 | ##### state
127 |
128 | State of the button if you do not want to use the functions. Can be `''`, `loading`, `success`, `error` or `disabled`.
129 |
130 | ##### type
131 |
132 | Type of the button (can be 'submit' for example).
133 |
134 | ##### form
135 |
136 | Id of the form to submit (useful if the button is not directly inside the form).
137 |
138 | ##### shouldAllowClickOnLoading
139 |
140 | Whether click event should bubble when in loading state
141 |
142 | ### Methods
143 |
144 | ##### loading()
145 |
146 | Put the button in the loading state.
147 |
148 | ##### disable()
149 |
150 | Put the button in the disabled state.
151 |
152 | ##### notLoading(), enable()
153 |
154 | Put the button in the normal state.
155 |
156 | ##### success([callback, dontGoBackToNormal])
157 |
158 | Put the button in the success state. Call the callback or the onSuccess prop when going back to the normal state.
159 |
160 | ##### error([callback])
161 |
162 | Put the button in the error state. Call the callback or the onError prop when going back to the normal state.
163 |
164 | ## Styles
165 |
166 | Look at [react-progress-button.css](https://github.com/mathieudutour/react-progress-button/blob/master/react-progress-button.css) for an idea on how to style this component.
167 |
168 | If you are using webpack, you'll need to have ```css-loader``` installed and include
169 | ```
170 | {
171 | test: /\.css$/,
172 | loader: "style!css"
173 | }
174 | ```
175 |
176 | in your webpack config. In your jsx file you can then import the CSS with ```import "../node_modules/react-progress-button/react-progress-button.css";``` although the path depends on how deeply nested your jsx is. If you wish to theme it yourself, copy the CSS to a convenient location and point the import path at the copy, which is part of your repo, unlike the original in ```node_modules```.
177 |
178 | ## License
179 |
180 | MIT
181 |
--------------------------------------------------------------------------------