|
1 | 1 | import React, { Component } from 'react';
|
2 | 2 | import Modal from 'react-modal';
|
3 |
| -import MyModal from './modal'; |
4 | 3 |
|
5 | 4 | const MODAL_A = 'modal_a';
|
6 | 5 | const MODAL_B = 'modal_b';
|
7 | 6 |
|
8 | 7 | const DEFAULT_TITLE = 'Default title';
|
9 | 8 |
|
10 |
| -class SimpleUsage extends Component { |
| 9 | +class Forms extends Component { |
11 | 10 | constructor(props) {
|
12 | 11 | super(props);
|
13 |
| - this.state = { |
14 |
| - title1: DEFAULT_TITLE, |
15 |
| - currentModal: null |
16 |
| - }; |
17 |
| - } |
18 |
| - |
19 |
| - toggleModal = key => event => { |
20 |
| - event.preventDefault(); |
21 |
| - if (this.state.currentModal) { |
22 |
| - this.handleModalCloseRequest(); |
23 |
| - return; |
24 |
| - } |
25 |
| - |
26 |
| - this.setState({ |
27 |
| - ...this.state, |
28 |
| - currentModal: key, |
29 |
| - title1: DEFAULT_TITLE |
30 |
| - }); |
31 |
| - } |
32 |
| - |
33 |
| - handleModalCloseRequest = () => { |
34 |
| - // opportunity to validate something and keep the modal open even if it |
35 |
| - // requested to be closed |
36 |
| - this.setState({ |
37 |
| - ...this.state, |
38 |
| - currentModal: null |
39 |
| - }); |
40 |
| - } |
41 | 12 |
|
42 |
| - handleInputChange = e => { |
43 |
| - let text = e.target.value; |
44 |
| - if (text == '') { |
45 |
| - text = DEFAULT_TITLE; |
46 |
| - } |
47 |
| - this.setState({ ...this.state, title1: text }); |
| 13 | + this.state = { isOpen: false }; |
48 | 14 | }
|
49 | 15 |
|
50 |
| - handleOnAfterOpenModal = () => { |
51 |
| - // when ready, we can access the available refs. |
52 |
| - this.heading && (this.heading.style.color = '#F00'); |
| 16 | + toggleModal = event => { |
| 17 | + console.log(event); |
| 18 | + const { isOpen } = this.state; |
| 19 | + this.setState({ isOpen: !isOpen }); |
53 | 20 | }
|
54 | 21 |
|
55 |
| - headingRef = h1 => this.heading = h1; |
56 |
| - |
57 | 22 | render() {
|
58 |
| - const { currentModal } = this.state; |
| 23 | + const { isOpen } = this.state; |
59 | 24 |
|
60 | 25 | return (
|
61 | 26 | <div>
|
62 |
| - <button onClick={this.toggleModal(MODAL_A)}>Open Modal A</button> |
63 |
| - <button onClick={this.toggleModal(MODAL_B)}>Open Modal B</button> |
64 |
| - <MyModal |
65 |
| - title={this.state.title1} |
66 |
| - isOpen={currentModal == MODAL_A} |
67 |
| - onAfterOpen={this.handleOnAfterOpenModal} |
68 |
| - onRequestClose={this.handleModalCloseRequest} |
69 |
| - askToClose={this.toggleModal(MODAL_A)} |
70 |
| - onChangeInput={this.handleInputChange} /> |
| 27 | + <button className="btn btn-primary" onClick={this.toggleModal}>Open Modal</button> |
71 | 28 | <Modal
|
72 |
| - ref="mymodal2" |
73 |
| - id="test2" |
| 29 | + id="modal_with_forms" |
| 30 | + isOpen={isOpen} |
| 31 | + closeTimeoutMS={150} |
| 32 | + contentLabel="modalB" |
| 33 | + shouldCloseOnOverlayClick={true} |
| 34 | + onRequestClose={this.toggleModal} |
74 | 35 | aria={{
|
75 | 36 | labelledby: "heading",
|
76 | 37 | describedby: "fulldescription"
|
77 |
| - }} |
78 |
| - closeTimeoutMS={150} |
79 |
| - contentLabel="modalB" |
80 |
| - isOpen={currentModal == MODAL_B} |
81 |
| - onAfterOpen={this.handleOnAfterOpenModal} |
82 |
| - onRequestClose={this.toggleModal(MODAL_B)}> |
83 |
| - <h1 id="heading" ref={headingRef}>This is the modal 2!</h1> |
| 38 | + }}> |
| 39 | + <h1 id="heading">Forms!</h1> |
84 | 40 | <div id="fulldescription" tabIndex="0" role="document">
|
85 | 41 | <p>This is a description of what it does: nothing :)</p>
|
86 |
| - </div>p |
| 42 | + |
| 43 | + <form> |
| 44 | + <fieldset> |
| 45 | + <input type="text" /> |
| 46 | + <input type="text" /> |
| 47 | + </fieldset> |
| 48 | + <fieldset> |
| 49 | + <legend>Radio buttons</legend> |
| 50 | + <label> |
| 51 | + <input id="radio-a" name="radios" type="radio" /> A |
| 52 | + </label> |
| 53 | + <label> |
| 54 | + <input id="radio-b" name="radios" type="radio" /> B |
| 55 | + </label> |
| 56 | + </fieldset> |
| 57 | + <fieldset> |
| 58 | + <legend>Checkbox buttons</legend> |
| 59 | + <label> |
| 60 | + <input id="checkbox-a" name="checkbox-a" type="checkbox" /> A |
| 61 | + </label> |
| 62 | + <label> |
| 63 | + <input id="checkbox-b" name="checkbox-b" type="checkbox" /> B |
| 64 | + </label> |
| 65 | + </fieldset> |
| 66 | + <input type="text" /> |
| 67 | + </form> |
| 68 | + </div> |
87 | 69 | </Modal>
|
88 | 70 | </div>
|
89 | 71 | );
|
90 | 72 | }
|
91 | 73 | }
|
92 | 74 |
|
93 | 75 | export default {
|
94 |
| - label: "#1. Working with one modal at a time.", |
95 |
| - app: SimpleUsage |
| 76 | + label: "#3. Modal with forms fields.", |
| 77 | + app: Forms |
96 | 78 | };
|
0 commit comments