Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] added more examples. #446

Merged
merged 1 commit into from
Jun 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 15 additions & 67 deletions examples/basic/app.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,36 @@
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import Modal from '../../src/index';
import ViewA from './view_a';
import ViewB from './view_b';

const appElement = document.getElementById('example');

Modal.setAppElement('#example');

const heading = firstView => {
if (firstView) {
return "#1. Working with one modal at a time.";
}
return "#2. Working with many modal.";
};

class App extends Component {
constructor(props) {
super(props);
this.state = { modal1: false, modal2: false };
}

toggleModal_1 = () => {
this.setState({ ...this.state, modal1: !this.state.modal1 });
}

toggleModal_2 = event => {
event.preventDefault();
this.setState({ ...this.state, modal2: !this.state.modal2 });
}

handleModalCloseRequest = () => {
// opportunity to validate something and keep the modal open even if it
// requested to be closed
this.setState({ ...this.state, modal1: false });
}

handleInputChange = () => {
this.setState({ ...this.state, foo: 'bar' });
this.state = { firstView: true };
}

handleOnAfterOpenModal = () => {
// when ready, we can access the available refs.
this.refs.title.style.color = '#F00';
toggleView = () => {
this.setState({ ...this.state, firstView: !this.state.firstView });
}

render() {
const { modal1, modal2 } = this.state;
return (
<div>
<button onClick={this.toggleModal_1}>Open Modal A</button>
<button onClick={this.toggleModal_2}>Open Modal B</button>
<Modal
ref="mymodal"
id="test"
closeTimeoutMS={150}
isOpen={modal1}
contentLabel="modalA"
onAfterOpen={this.handleOnAfterOpenModal}
onRequestClose={this.handleModalCloseRequest}>
<h1 ref="title">Hello</h1>
<button onClick={this.toggleModal_1}>close</button>
<div>I am a modal</div>
<form>
<input onChange={this.handleInputChange} />
<input />
<input />
<input />
<input />
<br/>
<button>hi</button>
<button>hi</button>
<button>hi</button>
<button>hi</button>
<button onClick={this.toggleModal_2}>Open Modal B</button>
</form>
</Modal>
<Modal ref="mymodal2"
id="test2"
aria={{
labelledby: "heading",
describedby: "fulldescription"
}}
closeTimeoutMS={150}
contentLabel="modalB"
isOpen={modal2}
onAfterOpen={() => {}}
onRequestClose={this.toggleModal_2}>
<h1 id="heading">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>
<button onClick={this.toggleView}>Click to go to the next example!</button>
<h2>{heading(this.state.firstView)}</h2>
{this.state.firstView ? <ViewA /> : <ViewB />}
</div>
);
}
Expand Down
37 changes: 37 additions & 0 deletions examples/basic/modal_a.js
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>
);
}
83 changes: 83 additions & 0 deletions examples/basic/view_a.js
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>
);
}
}
110 changes: 110 additions & 0 deletions examples/basic/view_b.js
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>
);
}
}