|
18 | 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
19 | 19 | // THE SOFTWARE. |
20 | 20 |
|
21 | | -import React from 'react'; |
| 21 | +import React, {useState, useEffect, useRef} from 'react'; |
22 | 22 | import window from 'global/window'; |
23 | 23 |
|
24 | 24 | import XYPlot from 'plot/xy-plot'; |
25 | 25 | import {getDOMNode} from 'utils/react-utils'; |
26 | 26 |
|
27 | | -const CONTAINER_REF = 'container'; |
28 | | - |
29 | 27 | // As a performance enhancement, we want to only listen once |
30 | 28 | const resizeSubscribers = []; |
31 | 29 | const DEBOUNCE_DURATION = 100; |
@@ -101,79 +99,61 @@ function getDisplayName(Component) { |
101 | 99 | */ |
102 | 100 |
|
103 | 101 | function makeFlexible(Component, isWidthFlexible, isHeightFlexible) { |
104 | | - const ResultClass = class extends React.Component { |
105 | | - static get propTypes() { |
106 | | - const {height, width, ...otherPropTypes} = Component.propTypes; // eslint-disable-line no-unused-vars |
107 | | - return otherPropTypes; |
108 | | - } |
109 | | - |
110 | | - constructor(props) { |
111 | | - super(props); |
112 | | - this.state = { |
113 | | - height: 0, |
114 | | - width: 0 |
115 | | - }; |
116 | | - } |
117 | | - |
118 | | - /** |
119 | | - * Get the width of the container and assign the width. |
120 | | - * @private |
121 | | - */ |
122 | | - _onResize = () => { |
123 | | - const containerElement = getDOMNode(this[CONTAINER_REF]); |
124 | | - const {offsetHeight, offsetWidth} = containerElement; |
| 102 | + const ResultFunctionalComponent = function(props) { |
| 103 | + const containerRef = useRef(); |
125 | 104 |
|
126 | | - const newHeight = |
127 | | - this.state.height === offsetHeight ? {} : {height: offsetHeight}; |
| 105 | + const [size, setSize] = useState({height: 0, width: 0}); |
128 | 106 |
|
129 | | - const newWidth = |
130 | | - this.state.width === offsetWidth ? {} : {width: offsetWidth}; |
| 107 | + useEffect(() => { |
| 108 | + function _onResize() { |
| 109 | + const containerElement = getDOMNode(containerRef.current); |
| 110 | + const {offsetHeight, offsetWidth} = containerElement; |
131 | 111 |
|
132 | | - this.setState({ |
133 | | - ...newHeight, |
134 | | - ...newWidth |
135 | | - }); |
136 | | - }; |
| 112 | + const newHeight = |
| 113 | + size.height === offsetHeight ? {} : {height: offsetHeight}; |
137 | 114 |
|
138 | | - componentDidMount() { |
139 | | - this._onResize(); |
140 | | - this.cancelSubscription = subscribeToDebouncedResize(this._onResize); |
141 | | - } |
| 115 | + const newWidth = size.width === offsetWidth ? {} : {width: offsetWidth}; |
142 | 116 |
|
143 | | - UNSAFE_componentWillReceiveProps() { |
144 | | - this._onResize(); |
145 | | - } |
| 117 | + setSize(prevSize => ({ |
| 118 | + ...prevSize, |
| 119 | + ...newHeight, |
| 120 | + ...newWidth |
| 121 | + })); |
| 122 | + } |
146 | 123 |
|
147 | | - componentWillUnmount() { |
148 | | - this.cancelSubscription(); |
149 | | - } |
| 124 | + const cancelSubscription = subscribeToDebouncedResize(_onResize); |
150 | 125 |
|
151 | | - render() { |
152 | | - const {height, width} = this.state; |
153 | | - const props = { |
154 | | - ...this.props, |
155 | | - animation: height === 0 && width === 0 ? null : this.props.animation |
| 126 | + return () => { |
| 127 | + cancelSubscription(); |
156 | 128 | }; |
| 129 | + }, [size.width, size.height]); |
157 | 130 |
|
158 | | - const updatedDimensions = { |
159 | | - ...(isHeightFlexible ? {height} : {}), |
160 | | - ...(isWidthFlexible ? {width} : {}) |
161 | | - }; |
| 131 | + const {height, width} = size; |
| 132 | + const componentProps = { |
| 133 | + ...props, |
| 134 | + animation: height === 0 && width === 0 ? null : props.animation |
| 135 | + }; |
162 | 136 |
|
163 | | - return ( |
164 | | - <div |
165 | | - ref={ref => (this[CONTAINER_REF] = ref)} |
166 | | - style={{width: '100%', height: '100%'}} |
167 | | - > |
168 | | - <Component {...updatedDimensions} {...props} /> |
169 | | - </div> |
170 | | - ); |
171 | | - } |
| 137 | + const updatedDimensions = { |
| 138 | + ...(isHeightFlexible ? {height} : {}), |
| 139 | + ...(isWidthFlexible ? {width} : {}) |
| 140 | + }; |
| 141 | + |
| 142 | + return ( |
| 143 | + <div ref={containerRef} style={{width: '100%', height: '100%'}}> |
| 144 | + <Component {...updatedDimensions} {...componentProps} /> |
| 145 | + </div> |
| 146 | + ); |
172 | 147 | }; |
173 | 148 |
|
174 | | - ResultClass.displayName = `Flexible${getDisplayName(Component)}`; |
| 149 | + const {height, width, ...otherPropTypes} = Component.propTypes || {}; // eslint-disable-line no-unused-vars |
| 150 | + ResultFunctionalComponent.propTypes = otherPropTypes; |
| 151 | + |
| 152 | + ResultFunctionalComponent.displayName = `Flexible${getDisplayName( |
| 153 | + Component |
| 154 | + )}`; |
175 | 155 |
|
176 | | - return ResultClass; |
| 156 | + return ResultFunctionalComponent; |
177 | 157 | } |
178 | 158 |
|
179 | 159 | export function makeHeightFlexible(component) { |
|
0 commit comments