-
Notifications
You must be signed in to change notification settings - Fork 8
/
TypeWriter.js
133 lines (111 loc) · 2.94 KB
/
TypeWriter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import PropTypes from 'prop-types';
import React from 'react';
import { Text } from 'react-native';
const delayShape = PropTypes.shape({
at: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.instanceOf(RegExp),
]),
delay: PropTypes.number,
});
const propTypes = {
fixed: PropTypes.bool,
delayMap: PropTypes.arrayOf(PropTypes.shape(delayShape)),
typing: PropTypes.oneOf([-1, 0, 1]),
maxDelay: PropTypes.number,
minDelay: PropTypes.number,
onTypingEnd: PropTypes.func,
onTyped: PropTypes.func,
text: PropTypes.string,
typeWriterStyle: Text.propTypes.style,
};
const defaultProps = {
typing: 0,
maxDelay: 100,
minDelay: 20,
};
class TypeWriter extends React.Component {
constructor(props) {
super(props);
this.state = {
visibleChars: 0,
};
this._handleTimeout = this._handleTimeout.bind(this);
}
componentDidMount() {
this._timeoutId = setTimeout(this._handleTimeout, this.props.minDelay);
}
componentWillReceiveProps(nextProps) {
const next = nextProps.typing;
const active = this.props.typing;
if (this.props.text !== nextProps.text) {
this.setState({
visibleChars: 0,
});
} else {
if (active > 0 && next < 0) {
this.setState({
visibleChars: this.state.visibleChars - 1,
});
} else if (active <= 0 && next > 0) {
this.setState({
visibleChars: this.state.visibleChars + 1,
});
}
}
}
shouldComponentUpdate(nextProps, nextState) {
return (
this.state.visibleChars !== nextState.visibleChars ||
this.props.text !== nextProps.text
);
}
componentDidUpdate(prevProps, prevState) {
const {
text,
maxDelay,
minDelay,
delayMap,
onTyped,
onTypingEnd,
} = this.props;
const { visibleChars } = prevState;
const token = text[visibleChars];
const nextToken = text[this.state.visibleChars];
if (token && onTyped) {
onTyped(token, visibleChars);
}
if (nextToken) {
let timeout = ~~(Math.random() * (maxDelay - minDelay) + minDelay);
if (delayMap) {
delayMap.forEach(({ at, delay }) => {
if (at === visibleChars || token.match(at)) {
timeout += delay;
}
});
}
this._timeoutId = setTimeout(this._handleTimeout, timeout);
} else if (onTypingEnd) {
onTypingEnd();
}
}
componentWillUnmount() {
clearInterval(this._timeoutId);
}
_handleTimeout() {
const { typing } = this.props;
const { visibleChars } = this.state;
this.setState({
visibleChars: visibleChars + typing,
});
}
render() {
const { text, typeWriterStyle, ...props } = this.props;
const { visibleChars } = this.state;
return <Text style={typeWriterStyle} {...props}>{text.slice(0, visibleChars)}</Text>;
}
}
TypeWriter.propTypes = propTypes;
TypeWriter.defaultProps = defaultProps;
export default TypeWriter;