Skip to content

Commit c9dc9e8

Browse files
author
guohui.deng
committed
修改参数传入位置,并更改readme
1 parent 69b8bd8 commit c9dc9e8

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ Then you can use it like this:
5757

5858
That's it, you're ready to go!
5959

60+
show a toast, and execute callback function when toast close it:
61+
62+
```javascript
63+
this.refs.toast.show('hello world!', 500, () => {
64+
// something you want to do at close
65+
});
66+
```
67+
6068
Show a toast forever until you manually close it:
6169
```javascript
6270
this.refs.toast.show('hello world!', DURATION.FOREVER);
@@ -65,11 +73,14 @@ Show a toast forever until you manually close it:
6573
this.refs.toast.close('hello world!');
6674
```
6775

76+
77+
6878
Optional you can pass a delay in seconds to the close()-method:
6979
```javascript
7080
this.refs.toast.close('hello world!', 500);
7181
```
7282

83+
7384
Currently, the default delay for close() in FOREVER-mode is set to 250 ms (or this.props.defaultCloseDelay, which you can pass with)
7485

7586
```jsx
@@ -148,7 +159,7 @@ textStyle | View.propTypes.style | true | {color:'white'} | Custom style te
148159

149160
Method | Type | Optional | Description
150161
----------------- | -------- | -------- | -----------
151-
show(text, duration) | function | false | show a toast,unit is millisecond
162+
show(text, duration, callback) | function | false | show a toast,unit is millisecond,and do callback
152163
close() | function | - | start the close timer
153164

154165

index.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import {
1515
Text,
1616
} from 'react-native'
1717

18-
import PropTypes from 'prop-types';
19-
2018
export const DURATION = {
2119
LENGTH_LONG: 2000,
2220
LENGTH_SHORT: 500,
@@ -37,9 +35,10 @@ export default class Toast extends Component {
3735
}
3836
}
3937

40-
show(text, callback, duration = DURATION.LENGTH_SHORT, ) {
38+
show(text, duration, callback) {
4139

42-
let delay = duration;
40+
this.duration = typeof duration === 'number' ? duration : DURATION.LENGTH_SHORT;
41+
this.callback = callback;
4342
this.setState({
4443
isShow: true,
4544
text: text,
@@ -57,9 +56,9 @@ export default class Toast extends Component {
5756
});
5857
}
5958

60-
close(callback, duration = DURATION.LENGTH_SHORT) {
59+
close(duration) {
6160

62-
let delay = duration;
61+
let delay = typeof duration === 'undefined' ? this.duration : duration;
6362
if(delay === DURATION.FOREVER) delay = this.props.defaultCloseDelay || 250;
6463

6564
if (!this.isShow && !this.state.isShow) return;
@@ -72,13 +71,12 @@ export default class Toast extends Component {
7271
duration: this.props.fadeOutDuration,
7372
}
7473
).start(() => {
75-
7674
this.setState({
7775
isShow: false,
7876
});
7977
this.isShow = false;
80-
if(typeof callback === 'function') {
81-
callback();
78+
if(typeof this.callback === 'function') {
79+
this.callback();
8280
}
8381
});
8482
}, delay);
@@ -135,17 +133,17 @@ const styles = StyleSheet.create({
135133
});
136134

137135
Toast.propTypes = {
138-
style: PropTypes.object,
139-
position: PropTypes.oneOf([
136+
style: View.propTypes.style,
137+
position: React.PropTypes.oneOf([
140138
'top',
141139
'center',
142140
'bottom',
143141
]),
144-
textStyle: PropTypes.object,
145-
positionValue: PropTypes.number,
146-
fadeInDuration: PropTypes.number,
147-
fadeOutDuration: PropTypes.number,
148-
opacity: PropTypes.number
142+
textStyle: Text.propTypes.style,
143+
positionValue: React.PropTypes.number,
144+
fadeInDuration: React.PropTypes.number,
145+
fadeOutDuration: React.PropTypes.number,
146+
opacity: React.PropTypes.number
149147
}
150148

151149
Toast.defaultProps = {

0 commit comments

Comments
 (0)