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.
- Loading branch information
Showing
4 changed files
with
245 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React from 'react'; | ||
import Modal from '../../src/index'; | ||
|
||
// This way you can provide a correct interface | ||
// for anyone that will use this modal. | ||
// | ||
// NOTE: Code style is just to show the interface. | ||
// Prefer comment your api. | ||
export default function ModalA( | ||
{ | ||
title, isOpen, onAfterOpen, | ||
onRequestClose, askToClose, onChangeInput | ||
} | ||
) { | ||
return ( | ||
<Modal | ||
id="test" | ||
contentLabel="modalA" | ||
closeTimeoutMS={150} | ||
isOpen={isOpen} | ||
onAfterOpen={onAfterOpen} | ||
onRequestClose={onRequestClose}> | ||
<h1>{title}</h1> | ||
<button onClick={askToClose}>close</button> | ||
<div>I am a modal. Use the first input to change the modal's title.</div> | ||
<form> | ||
<input onChange={onChangeInput} /> | ||
<input /> | ||
<br /> | ||
<button>Button A</button> | ||
<button>Button B</button> | ||
<button>Button C</button> | ||
<button>Button D</button> | ||
</form> | ||
</Modal> | ||
); | ||
} |
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,83 @@ | ||
import React, { Component } from 'react'; | ||
import Modal from '../../src/index'; | ||
import ModalA from './modal_a'; | ||
|
||
const MODAL_A = 'modal_a'; | ||
const MODAL_B = 'modal_b'; | ||
|
||
const DEFAULT_TITLE = 'Default title'; | ||
|
||
export default class ViewA extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
title1: DEFAULT_TITLE, | ||
currentModal: null | ||
}; | ||
} | ||
|
||
toggleModal = key => event => { | ||
event.preventDefault(); | ||
if (this.state.currentModal) { | ||
this.handleModalCloseRequest(); | ||
return; | ||
} | ||
this.setState({ ...this.state, currentModal: key, title1: DEFAULT_TITLE }); | ||
} | ||
|
||
handleModalCloseRequest = () => { | ||
// opportunity to validate something and keep the modal open even if it | ||
// requested to be closed | ||
this.setState({ | ||
...this.state, | ||
currentModal: null | ||
}); | ||
} | ||
|
||
handleInputChange = (e) => { | ||
let text = e.target.value; | ||
if (text == '') { | ||
text = DEFAULT_TITLE; | ||
} | ||
this.setState({ ...this.state, title1: text }); | ||
} | ||
|
||
handleOnAfterOpenModal = () => { | ||
// when ready, we can access the available refs. | ||
this.heading && (this.heading.style.color = '#F00'); | ||
} | ||
|
||
render() { | ||
const { currentModal } = this.state; | ||
return ( | ||
<div> | ||
<button onClick={this.toggleModal(MODAL_A)}>Open Modal A</button> | ||
<button onClick={this.toggleModal(MODAL_B)}>Open Modal B</button> | ||
<ModalA | ||
title={this.state.title1} | ||
isOpen={currentModal == MODAL_A} | ||
onAfterOpen={this.handleOnAfterOpenModal} | ||
onRequestClose={this.handleModalCloseRequest} | ||
askToClose={this.toggleModal(MODAL_A)} | ||
onChangeInput={this.handleInputChange} /> | ||
<Modal | ||
ref="mymodal2" | ||
id="test2" | ||
aria={{ | ||
labelledby: "heading", | ||
describedby: "fulldescription" | ||
}} | ||
closeTimeoutMS={150} | ||
contentLabel="modalB" | ||
isOpen={currentModal == MODAL_B} | ||
onAfterOpen={this.handleOnAfterOpenModal} | ||
onRequestClose={this.toggleModal(MODAL_B)}> | ||
<h1 id="heading" ref={h1 => this.heading = h1}>This is the modal 2!</h1> | ||
<div id="fulldescription" tabIndex="0" role="document"> | ||
<p>This is a description of what it does: nothing :)</p> | ||
</div> | ||
</Modal> | ||
</div> | ||
); | ||
} | ||
} |
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,110 @@ | ||
import React, { Component } from 'react'; | ||
import Modal from '../../src/index'; | ||
import ModalA from './modal_a'; | ||
|
||
function List(props) { | ||
return ( | ||
<div> | ||
{props.items.map( | ||
(x, i) => <div key={i} onClick={props.onItemClick(i)}><a href="javascript:void(0)">{x}</a></div> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
export default class ViewB extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
listItemsIsOpen: false, | ||
currentItem: -1, | ||
loading: false, | ||
items: [] | ||
}; | ||
} | ||
|
||
toggleModal = event => { | ||
event.preventDefault(); | ||
if (this.state.listItemsIsOpen) { | ||
this.handleModalCloseRequest(); | ||
return; | ||
} | ||
this.setState({ | ||
...this.state, | ||
items: [], | ||
listItemsIsOpen: true, | ||
loading: true | ||
}); | ||
} | ||
|
||
handleModalCloseRequest = () => { | ||
// opportunity to validate something and keep the modal open even if it | ||
// requested to be closed | ||
this.setState({ | ||
...this.state, | ||
listItemsIsOpen: false, | ||
loading: false | ||
}); | ||
} | ||
|
||
handleOnAfterOpenModal = () => { | ||
// when ready, we can access the available refs. | ||
(new Promise((resolve, reject) => { | ||
setTimeout(() => resolve(true), 1000); | ||
})).then(res => { | ||
this.setState({ | ||
...this.state, | ||
items: [1, 2, 3, 4, 5].map(x => `Item ${x}`), | ||
loading: false | ||
}); | ||
}); | ||
} | ||
|
||
onItemClick = index => event => { | ||
this.setState({ ...this.state, currentItem: index }); | ||
} | ||
|
||
cleanCurrentItem = () => { | ||
this.setState({ ...this.state, currentItem: -1 }); | ||
} | ||
|
||
render() { | ||
const { listItemsIsOpen } = this.state; | ||
return ( | ||
<div> | ||
<button onClick={this.toggleModal}>Open Modal A</button> | ||
<Modal | ||
id="test" | ||
closeTimeoutMS={150} | ||
contentLabel="modalA" | ||
isOpen={listItemsIsOpen} | ||
onAfterOpen={this.handleOnAfterOpenModal} | ||
onRequestClose={this.toggleModal}> | ||
<h1>List of items</h1> | ||
{this.state.loading ? ( | ||
<p>Loading...</p> | ||
) : ( | ||
<List onItemClick={this.onItemClick} items={this.state.items} /> | ||
)} | ||
</Modal> | ||
<Modal | ||
id="test2" | ||
closeTimeoutMS={150} | ||
contentLabel="modalB" | ||
isOpen={this.state.currentItem > -1} | ||
onRequestClose={this.cleanCurrentItem} | ||
aria={{ | ||
labelledby: "item_title", | ||
describedby: "item_info" | ||
}}> | ||
<h1 id="item_title">Item: {this.state.items[this.state.currentItem]}</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> | ||
|
||
<p>Maecenas justo justo, laoreet vitae odio quis, lacinia porttitor arcu. Nunc nisl est, ultricies sed laoreet eu, semper in nisi. Phasellus lacinia porta purus, eu luctus neque. Nullam quis mi malesuada, vestibulum sem id, rhoncus purus. Aliquam erat volutpat. Duis nec turpis mi. Pellentesque eleifend nisl sed risus aliquet, eu feugiat elit auctor. Suspendisse ac neque vitae ligula consequat aliquam. Vivamus sit amet eros et ante mollis porta.</p> | ||
</div> | ||
</Modal> | ||
</div> | ||
); | ||
} | ||
} |