Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 58cfc6f

Browse files
mgr34Matt Goo
authored and
Matt Goo
committed
fix(top-app-bar): foundation should be destroyed and reinitialized on variant change (#519)
1 parent e3bb701 commit 58cfc6f

File tree

5 files changed

+186
-3
lines changed

5 files changed

+186
-3
lines changed

packages/top-app-bar/index.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,25 @@ export default class TopAppBar extends React.Component {
7171
this.foundation_.destroy();
7272
}
7373

74+
componentDidUpdate(prevProps) {
75+
const foundationChanged = ['short', 'shortCollapsed', 'fixed']
76+
.some((variant) => this.props[variant] !== prevProps[variant] );
77+
78+
if (foundationChanged) {
79+
this.initializeFoundation();
80+
}
81+
}
82+
83+
7484
initializeFoundation = () => {
75-
if (this.props.short) {
85+
const {short, shortCollapsed, fixed} = this.props;
86+
if (this.foundation_) {
87+
this.foundation_.destroy();
88+
}
89+
90+
if (short || shortCollapsed) {
7691
this.foundation_ = new MDCShortTopAppBarFoundation(this.adapter);
77-
} else if (this.props.fixed) {
92+
} else if (fixed) {
7893
this.foundation_ = new MDCFixedTopAppBarFoundation(this.adapter);
7994
} else {
8095
this.foundation_ = new MDCTopAppBarFoundation(this.adapter);
@@ -83,6 +98,7 @@ export default class TopAppBar extends React.Component {
8398
this.foundation_.init();
8499
}
85100

101+
86102
addClassesToElement(classes, element) {
87103
const updatedProps = {
88104
className: classnames(classes, element.props.className),

test/screenshot/golden.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"top-app-bar/standard": "7a2dd6318d62ac2eabd66f1b28100db7c15840ccb753660065fa9524db6435d6",
3333
"top-app-bar/standardNoActionItems": "6d361edb994cafcc6ac720336d12ee7d7114745993e16abd6e6b00f078424ff2",
3434
"top-app-bar/standardWithNavigationIconElement": "95afd559c35dede805e4d4b51ad1fabd82b4117c358a8679e3166b88e059bf68",
35+
"top-app-bar/prominentToShortCollapsed": "ce23a6060af19f93ee0255e45ebe51c7243ff12c14b2c09d070501ea7806e316",
3536
"top-app-bar/dense": "e273e6c777f168248f5757c1f00a74206f4cce51c484d91cc7d36dc16de7d0de",
3637
"top-app-bar/prominentDense": "cc8af934f9187ffd8f250834ef7c73e5c53c5ace10126bb855f74878ba125149",
3738
"drawer/permanentBelowTopAppBar": "587ee2605c4b3e3f0408c6130b824b58587e05cedf9b964e14fc481b9de1e4c2",
@@ -41,4 +42,4 @@
4142
"drawer/modal": "da83487c9349b253f7d4de01f92d442de55aab92a8028b77ff1a48cfaa265b72",
4243
"drawer/permanentToModal": "6355905c2241b5e6fdddc2e25119a1cc3b062577375a88b59e6750c4b76e4561",
4344
"typography": "c5e87d672d8c05ca3b61c0df4971eabe3c6a6a1f24a9b98f71f55a23360c498a"
44-
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import React from 'react';
2+
import TopAppBar from '../../../packages/top-app-bar';
3+
import MaterialIcon from '../../../packages/material-icon';
4+
import MainTopAppBarContent from './mainContent';
5+
6+
class TopAppBarProminentToShortCollapsedScreenshotTest extends React.Component {
7+
state = {
8+
isPhone: window.innerWidth < 599,
9+
};
10+
11+
componentDidMount() {
12+
window.addEventListener('resize', this.updateTopAppBarVariant);
13+
}
14+
15+
shouldComponentUpdate(_, nextState) {
16+
return nextState.isPhone !== this.state.isPhone;
17+
}
18+
19+
componentWillUnmount() {
20+
window.removeEventListener('resize', this.updateTopAppBarVariant);
21+
}
22+
23+
updateTopAppBarVariant = () => {
24+
const isPhone = window.innerWidth < 599;
25+
this.setState({isPhone});
26+
}
27+
28+
render() {
29+
if (this.state.isPhone) {
30+
return (
31+
<div>
32+
<TopAppBar
33+
shortCollapsed
34+
navigationIcon={<MaterialIcon
35+
icon='menu'
36+
onClick={() => console.log('click')}
37+
/>}
38+
/>
39+
<MainTopAppBarContent />
40+
</div>
41+
);
42+
}
43+
44+
return (
45+
<div>
46+
<TopAppBar
47+
prominent
48+
title={'Annie, I\'m a Hawk'}
49+
navigationIcon={<MaterialIcon
50+
icon='menu'
51+
onClick={() => console.log('click')}
52+
/>}
53+
/>
54+
<MainTopAppBarContent />
55+
</div>
56+
);
57+
}
58+
}
59+
60+
export default TopAppBarProminentToShortCollapsedScreenshotTest;

test/screenshot/top-app-bar/variants.js

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export default [
88
'standard',
99
'standardNoActionItems',
1010
'standardWithNavigationIconElement',
11+
'prominentToShortCollapsed',
1112
];

test/unit/top-app-bar/index.test.js

+105
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,111 @@ test('#enableRippleOnElement throws error if a native element', () => {
215215
);
216216
});
217217

218+
test('when changes from short to fixed the foundation changes', () => {
219+
const wrapper = shallow(<TopAppBar short />);
220+
const originalFoundation = wrapper.instance().foundation_;
221+
wrapper.setProps({fixed: true, short: false});
222+
assert.notEqual(wrapper.instance().foundation_, originalFoundation);
223+
assert.exists(wrapper.instance().foundation_);
224+
});
225+
226+
test('when changes from short to fixed the foundation changes', () => {
227+
const wrapper = shallow(<TopAppBar short />);
228+
const originalFoundation = wrapper.instance().foundation_;
229+
wrapper.setProps({fixed: true, short: false});
230+
assert.notEqual(wrapper.instance().foundation_, originalFoundation);
231+
assert.exists(wrapper.instance().foundation_);
232+
});
233+
234+
test('when changes from short to standard the foundation changes', () => {
235+
const wrapper = shallow(<TopAppBar short />);
236+
const originalFoundation = wrapper.instance().foundation_;
237+
wrapper.setProps({short: false});
238+
assert.notEqual(wrapper.instance().foundation_, originalFoundation);
239+
assert.exists(wrapper.instance().foundation_);
240+
});
241+
242+
test('when changes from short to prominent the foundation changes', () => {
243+
const wrapper = shallow(<TopAppBar short />);
244+
const originalFoundation = wrapper.instance().foundation_;
245+
wrapper.setProps({short: false, prominent: true});
246+
assert.notEqual(wrapper.instance().foundation_, originalFoundation);
247+
assert.exists(wrapper.instance().foundation_);
248+
});
249+
250+
test('when changes from short to shortCollpased the foundation changes', () => {
251+
const wrapper = shallow(<TopAppBar short />);
252+
const originalFoundation = wrapper.instance().foundation_;
253+
wrapper.setProps({shortCollapsed: true});
254+
assert.notEqual(wrapper.instance().foundation_, originalFoundation);
255+
assert.exists(wrapper.instance().foundation_);
256+
});
257+
258+
test('when changes from fixed to prominent the foundation changes', () => {
259+
const wrapper = shallow(<TopAppBar fixed/>);
260+
const originalFoundation = wrapper.instance().foundation_;
261+
wrapper.setProps({fixed: false, prominent: true});
262+
assert.notEqual(wrapper.instance().foundation_, originalFoundation);
263+
assert.exists(wrapper.instance().foundation_);
264+
});
265+
266+
test('when changes from fixed to short the foundation changes', () => {
267+
const wrapper = shallow(<TopAppBar fixed/>);
268+
const originalFoundation = wrapper.instance().foundation_;
269+
wrapper.setProps({fixed: false, short: true});
270+
assert.notEqual(wrapper.instance().foundation_, originalFoundation);
271+
assert.exists(wrapper.instance().foundation_);
272+
});
273+
274+
test('when changes from fixed to shortCollpased the foundation changes', () => {
275+
const wrapper = shallow(<TopAppBar fixed/>);
276+
const originalFoundation = wrapper.instance().foundation_;
277+
wrapper.setProps({fixed: false, shortCollapsed: true});
278+
assert.notEqual(wrapper.instance().foundation_, originalFoundation);
279+
assert.exists(wrapper.instance().foundation_);
280+
});
281+
282+
test('when changes from fixed to standard the foundation changes', () => {
283+
const wrapper = shallow(<TopAppBar fixed/>);
284+
const originalFoundation = wrapper.instance().foundation_;
285+
wrapper.setProps({fixed: false});
286+
assert.notEqual(wrapper.instance().foundation_, originalFoundation);
287+
assert.exists(wrapper.instance().foundation_);
288+
});
289+
290+
test('when changes from standard to fixed the foundation changes', () => {
291+
const wrapper = shallow(<TopAppBar />);
292+
const originalFoundation = wrapper.instance().foundation_;
293+
wrapper.setProps({fixed: true});
294+
assert.notEqual(wrapper.instance().foundation_, originalFoundation);
295+
assert.exists(wrapper.instance().foundation_);
296+
});
297+
298+
test('when changes from standard to short the foundation changes', () => {
299+
const wrapper = shallow(<TopAppBar />);
300+
const originalFoundation = wrapper.instance().foundation_;
301+
wrapper.setProps({short: true});
302+
assert.notEqual(wrapper.instance().foundation_, originalFoundation);
303+
assert.exists(wrapper.instance().foundation_);
304+
});
305+
306+
test('when changes from standard to shortCollapsed the foundation changes', () => {
307+
const wrapper = shallow(<TopAppBar />);
308+
const originalFoundation = wrapper.instance().foundation_;
309+
wrapper.setProps({shortCollapsed: true});
310+
assert.notEqual(wrapper.instance().foundation_, originalFoundation);
311+
assert.exists(wrapper.instance().foundation_);
312+
});
313+
314+
test('when changes from standard to prominent the foundation does not ' +
315+
'change', () => {
316+
const wrapper = shallow(<TopAppBar />);
317+
const originalFoundation = wrapper.instance().foundation_;
318+
wrapper.setProps({prominent: true});
319+
assert.equal(wrapper.instance().foundation_, originalFoundation);
320+
assert.exists(wrapper.instance().foundation_);
321+
});
322+
218323
test('#componentWillUnmount destroys foundation', () => {
219324
const wrapper = shallow(<TopAppBar />);
220325
const foundation = wrapper.instance().foundation_;

0 commit comments

Comments
 (0)