1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict'
|
7 |
|
8 | import React, {PropTypes as types} from 'react'
|
9 | import classnames from 'classnames'
|
10 |
|
11 | import {ApCheckbox} from 'apeman-react-checkbox'
|
12 | import {ApButton} from 'apeman-react-button'
|
13 | import {ApPureMixin} from 'apeman-react-mixin-pure'
|
14 | import ApDialog from './ap_dialog'
|
15 | import uuid from 'uuid'
|
16 |
|
17 |
|
18 | const ApConfirmDialog = React.createClass({
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | propTypes: {
|
25 | present: types.bool.isRequired,
|
26 |
|
27 | onSubmit: types.func,
|
28 |
|
29 | onCancel: types.func,
|
30 |
|
31 | title: types.string,
|
32 |
|
33 | message: types.string,
|
34 |
|
35 | checkboxId: types.string,
|
36 |
|
37 | checkboxName: types.string,
|
38 |
|
39 | checkboxText: types.string,
|
40 |
|
41 | errorText: types.string,
|
42 |
|
43 | submitText: types.string
|
44 | },
|
45 |
|
46 | mixins: [
|
47 | ApPureMixin
|
48 | ],
|
49 |
|
50 | statics: {},
|
51 |
|
52 | getInitialState () {
|
53 | return {
|
54 | checked: false,
|
55 | errored: false
|
56 | }
|
57 | },
|
58 |
|
59 | getDefaultProps () {
|
60 | let id = uuid.v4().replace(/\-/g, '')
|
61 | return {
|
62 | onSubmit: null,
|
63 | onCancel: null,
|
64 | checkboxId: `ap-confirm-dialog-checkbox-${id}`,
|
65 | checkboxName: `ap-confirm-check-${id}`,
|
66 | checkboxText: null,
|
67 | submitText: 'submit',
|
68 | errorText: 'Needs check before submit.',
|
69 | message: 'Once destroyed, there is no going back. Please be certain.'
|
70 | }
|
71 | },
|
72 |
|
73 | render () {
|
74 | const s = this
|
75 | let { state, props } = s
|
76 | if (!props.present) {
|
77 | return null
|
78 | }
|
79 | return (
|
80 | <ApDialog className={ classnames('ap-confirm-dialog', props.className) }
|
81 | style={ Object.assign({}, props.style) }
|
82 | onClose={ s.handleCancel }
|
83 | { ...props }
|
84 | >
|
85 | <p className="ap-confirm-dialog-message">
|
86 | { props.message }
|
87 | </p>
|
88 | <div>{ props.children }</div>
|
89 | <div className="ap-confirm-dialog-control">
|
90 | { s._renderError(state.errored) }
|
91 | { s._renderCheckbox(state.checked) }
|
92 | { s._renderSubmitButton(state.checked) }
|
93 | </div>
|
94 | </ApDialog>
|
95 | )
|
96 | },
|
97 |
|
98 | toggleCheckbox() {
|
99 | const s = this
|
100 | let state = s.state
|
101 | s.setState({
|
102 | checked: !state.checked,
|
103 | errored: false
|
104 | })
|
105 | },
|
106 |
|
107 | handleSubmit(e) {
|
108 | const s = this
|
109 | let { state, props } = s
|
110 | if (!state.checked) {
|
111 | s.setState({
|
112 | errored: true
|
113 | })
|
114 | return
|
115 | }
|
116 | s.setState({
|
117 | errored: false
|
118 | })
|
119 | if (props.onSubmit) {
|
120 | props.onSubmit(e)
|
121 | }
|
122 | },
|
123 |
|
124 | handleCancel(e) {
|
125 | const s = this,
|
126 | { props } = s
|
127 | s.setState({ errored: false })
|
128 | if (props.onCancel) {
|
129 | props.onCancel(e)
|
130 | }
|
131 | },
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | _renderError(errored) {
|
137 | const s = this
|
138 | let { props } = s
|
139 | if (!errored) {
|
140 | return null
|
141 | }
|
142 | return (
|
143 | <div className="ap-confirm-dialog-err">
|
144 | <span>{ props.errorText }</span>
|
145 | </div>
|
146 | )
|
147 | },
|
148 | _renderCheckbox (checked) {
|
149 | const s = this
|
150 | let { props } = s
|
151 | return (
|
152 | <div>
|
153 | <ApCheckbox className="ap-confirm-dialog-checkbox"
|
154 | checked={ checked }
|
155 | name={ props.checkboxName }
|
156 | id={ props.checkboxId }
|
157 | title={ props.checkboxText }
|
158 | onChange={ s.toggleCheckbox }
|
159 | value="YES"
|
160 |
|
161 | />
|
162 | </div>
|
163 | )
|
164 | },
|
165 | _renderSubmitButton (checked) {
|
166 | const s = this
|
167 | let { props } = s
|
168 | return (
|
169 | <ApButton onTap={ s.handleSubmit }
|
170 | disabled={ false }
|
171 | primary={ true }
|
172 | className={ classnames('ap-confirm-dialog-button', {
|
173 | 'ap-confirm-dialog-button-disabled': !checked
|
174 | }) }>
|
175 | <span className="ap-confirm-dialog-button-text">{ props.submitText }</span>
|
176 | </ApButton>
|
177 | )
|
178 | }
|
179 |
|
180 | })
|
181 |
|
182 | export default ApConfirmDialog
|