mirror of
https://github.com/storybookjs/storybook.git
synced 2025-03-20 05:02:37 +08:00
adding currentBackground for @addon/background
This commit is contained in:
parent
2675c19144
commit
5b6d0a7738
@ -72,11 +72,11 @@ export default class BackgroundPanel extends Component {
|
||||
this.channel = addons.getChannel();
|
||||
}
|
||||
|
||||
this.state = { backgrounds: [] };
|
||||
this.state = { backgrounds: [], currentBackground: null };
|
||||
|
||||
this.channel.on('background-set', backgrounds => {
|
||||
this.setState({ backgrounds });
|
||||
const currentBackground = api.getQueryParam('background');
|
||||
const currentBackground = api.getQueryParam('background') || this.state.currentBackground;
|
||||
|
||||
if (currentBackground) {
|
||||
this.updateIframe(currentBackground);
|
||||
@ -106,6 +106,7 @@ export default class BackgroundPanel extends Component {
|
||||
}
|
||||
|
||||
setBackgroundFromSwatch = background => {
|
||||
this.updateCurrentBackground(background);
|
||||
this.updateIframe(background);
|
||||
this.props.api.setQueryParams({ background });
|
||||
};
|
||||
@ -114,6 +115,12 @@ export default class BackgroundPanel extends Component {
|
||||
this.iframe.style.background = background;
|
||||
}
|
||||
|
||||
updateCurrentBackground(background) {
|
||||
this.setState({
|
||||
currentBackground: background,
|
||||
});
|
||||
}
|
||||
|
||||
render() {
|
||||
const backgrounds = [...this.state.backgrounds];
|
||||
|
||||
|
@ -129,4 +129,19 @@ describe('Background Panel', () => {
|
||||
background: backgrounds[0].value,
|
||||
});
|
||||
});
|
||||
|
||||
it('should persist the current background color in state', () => {
|
||||
const SpiedChannel = new EventEmitter();
|
||||
const backgroundPanel = mount(<BackgroundPanel channel={SpiedChannel} api={mockedApi} />);
|
||||
backgroundPanel.setState({ backgrounds }); // force re-render
|
||||
|
||||
expect(backgroundPanel.state('currentBackground')).toEqual(null);
|
||||
|
||||
backgroundPanel
|
||||
.find('h4')
|
||||
.first()
|
||||
.simulate('click');
|
||||
|
||||
expect(backgroundPanel.state('currentBackground')).toEqual(backgrounds[0].value);
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user