forked from reactjs/react-modal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fixed] stop propagating ESC key event.
This is now required when nesting modals (the event will trigger both handleKeyDown). closes reactjs#583.
- Loading branch information
Showing
8 changed files
with
156 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import React, { Component } from 'react'; | ||
import Modal from 'react-modal'; | ||
|
||
class Item extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
isOpen: false | ||
}; | ||
} | ||
|
||
toggleModal = index => event => { | ||
console.log("NESTED MODAL ITEM", event); | ||
this.setState({ | ||
itemNumber: !this.state.isOpen ? index : null, | ||
isOpen: !this.state.isOpen | ||
}); | ||
}; | ||
|
||
render() { | ||
const { isOpen, itemNumber } = this.state; | ||
const { number, index } = this.props; | ||
|
||
const toggleModal = this.toggleModal(index); | ||
|
||
return ( | ||
<div key={index} onClick={toggleModal}> | ||
<a href="javascript:void(0)">{number}</a> | ||
<Modal closeTimeoutMS={150} | ||
contentLabel="modalB" | ||
isOpen={isOpen} | ||
onRequestClose={toggleModal} | ||
aria={{ | ||
labelledby: "item_title", | ||
describedby: "item_info" | ||
}}> | ||
<h1 id="item_title">Item: {itemNumber}</h1> | ||
<div id="item_info"> | ||
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur pulvinar varius auctor. Aliquam maximus et justo ut faucibus. Nullam sit amet urna molestie turpis bibendum accumsan a id sem. Proin ullamcorper nisl sapien, gravida dictum nibh congue vel. Vivamus convallis dolor vitae ipsum ultricies, vitae pulvinar justo tincidunt. Maecenas a nunc elit. Phasellus fermentum, tellus ut consectetur scelerisque, eros nunc lacinia eros, aliquet efficitur tellus arcu a nibh. Praesent quis consequat nulla. Etiam dapibus ac sem vel efficitur. Nunc faucibus efficitur leo vitae vulputate. Nunc at quam vitae felis pretium vehicula vel eu quam. Quisque sapien mauris, condimentum eget dictum ut, congue id dolor. Donec vitae varius orci, eu faucibus turpis. Morbi eleifend orci non urna bibendum, ac scelerisque augue efficitur.</p> | ||
</div> | ||
</Modal> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
class List extends Component { | ||
render() { | ||
return this.props.items.map((n, index) => ( | ||
<Item key={index} index={index} number={n} /> | ||
)); | ||
} | ||
} | ||
|
||
|
||
class NestedModals extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
isOpen: false, | ||
currentItem: -1, | ||
loading: false, | ||
items: [] | ||
}; | ||
} | ||
|
||
toggleModal = event => { | ||
event.preventDefault(); | ||
console.log("NESTEDMODAL", event); | ||
this.setState({ | ||
items: [], | ||
isOpen: !this.state.isOpen, | ||
loading: true | ||
}); | ||
} | ||
|
||
handleOnAfterOpenModal = () => { | ||
// when ready, we can access the available refs. | ||
(new Promise((resolve, reject) => { | ||
setTimeout(() => resolve(true), 500); | ||
})).then(res => { | ||
this.setState({ | ||
items: [1, 2, 3, 4, 5].map(x => `Item ${x}`), | ||
loading: false | ||
}); | ||
}); | ||
} | ||
|
||
render() { | ||
const { isOpen } = this.state; | ||
return ( | ||
<div> | ||
<button type="button" className="btn btn-primary" onClick={this.toggleModal}>Open Modal A</button> | ||
<Modal | ||
id="test" | ||
closeTimeoutMS={150} | ||
contentLabel="modalA" | ||
isOpen={isOpen} | ||
onAfterOpen={this.handleOnAfterOpenModal} | ||
onRequestClose={this.toggleModal}> | ||
<h1>List of items</h1> | ||
{this.state.loading ? ( | ||
<p>Loading...</p> | ||
) : ( | ||
<List items={this.state.items} /> | ||
)} | ||
</Modal> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default { | ||
label: "Working with nested modals.", | ||
app: NestedModals | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters