From 3bf3d688ecd15e7894b6858a0e9b63ed11bcfff6 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Thu, 15 Sep 2016 11:11:56 +0200 Subject: [PATCH] example: Not mutate state and correctly handle selectedIndex [skip ci] --- examples/dyno/app.js | 88 ++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/examples/dyno/app.js b/examples/dyno/app.js index 98601c18ef..9141768311 100644 --- a/examples/dyno/app.js +++ b/examples/dyno/app.js @@ -4,7 +4,6 @@ import Modal from 'react-modal'; import { Tab, Tabs, TabList, TabPanel } from '../../src/main'; Modal.setAppElement(document.getElementById('example')); -Modal.injectCSS(); const App = React.createClass({ getInitialState() { @@ -20,25 +19,57 @@ const App = React.createClass({ }; }, + openModal() { + this.setState({ + isModalOpen: true, + }); + }, + + closeModal() { + this.setState({ + isModalOpen: false, + }); + }, + + addTab() { + const label = this.refs.label.value; + const content = this.refs.content.value; + + this.setState({ + tabs: [ + ...this.state.tabs, + { label, content }, + ], + selectedIndex: this.state.tabs.length, + }); + this.closeModal(); + }, + + removeTab(index) { + this.setState({ + tabs: this.state.tabs.filter((tab, i) => i !== index), + selectedIndex: this.state.selectedIndex - 1, + }); + }, + render() { return (

- + this.setState({ selectedIndex })} + > - {this.state.tabs.map((tab, i) => { - return ( - - {tab.label} - - ); - })} + {this.state.tabs.map((tab, i) => ( + + {tab.label} this.removeTab(i)}>✕ + + ))} - {this.state.tabs.map((tab, i) => { - return {tab.content}; - })} + {this.state.tabs.map((tab, i) => {tab.content})} Label:



-

+