-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (90 loc) · 2.61 KB
/
index.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
import React, {Component} from 'react';
import classnames from 'classnames';
import PropTypes from 'prop-types';
import {MDCLineRippleFoundation} from '@material/line-ripple';
export default class LineRipple extends Component {
foundation_ = null;
state = {
classList: new Set(),
style: {},
};
componentDidMount() {
this.foundation_ = new MDCLineRippleFoundation(this.adapter);
this.foundation_.init();
if (this.props.active) {
this.foundation_.activate();
}
}
componentWillReceiveProps(nextProps) {
if (this.props.active !== nextProps.active) {
if (nextProps.active) {
this.foundation_.activate();
} else {
this.foundation_.deactivate();
}
}
if (this.props.rippleCenter !== nextProps.rippleCenter) {
this.foundation_.setRippleCenter(nextProps.rippleCenter);
}
}
componentWillUnmount() {
this.foundation_.destroy();
}
get adapter() {
return {
addClass: (className) =>
this.setState({classList: this.state.classList.add(className)}),
removeClass: (className) => {
const {classList} = this.state;
classList.delete(className);
this.setState({classList});
},
hasClass: (className) => this.state.classList.has(className),
setStyle: this.setStyle,
};
}
get classes() {
const {className} = this.props;
const {classList} = this.state;
return classnames('mdc-line-ripple', Array.from(classList), className);
}
setStyle = (varName, value) => {
const styleName = varName.replace(/-(\w)/g, (_, v) => v.toUpperCase());
const updatedStyle = Object.assign({}, this.state.style);
updatedStyle[styleName] = value;
this.setState({style: updatedStyle});
}
getMergedStyles = () => {
const {style: wrappedStyle} = this.props;
const {style} = this.state;
return Object.assign({}, style, wrappedStyle);
}
render() {
const {
style, // eslint-disable-line no-unused-vars
className, // eslint-disable-line no-unused-vars
active, // eslint-disable-line no-unused-vars
rippleCenter, // eslint-disable-line no-unused-vars
...otherProps
} = this.props;
return (
<div
className={this.classes}
style={this.getMergedStyles()}
onTransitionEnd={(evt) => this.foundation_.handleTransitionEnd(evt)}
{...otherProps}></div>
);
}
}
LineRipple.propTypes = {
className: PropTypes.string,
style: PropTypes.object,
active: PropTypes.bool,
rippleCenter: PropTypes.number,
};
LineRipple.defaultProps = {
className: '',
style: {},
active: false,
rippleCenter: 0,
};