Get modal with embedded form working

This commit is contained in:
John Lyon-Smith
2018-02-28 13:15:46 -08:00
parent 6f4330adc5
commit eb54ca0b62
6 changed files with 75 additions and 48 deletions

View File

@@ -71,12 +71,7 @@ export class Login extends React.Component {
} }
} }
}).catch((error) => { }).catch((error) => {
const elems = document.getElementsByName('email')
if (elems) {
elems[0].focus()
}
this.setState({ this.setState({
binder: new FormBinder({ email: this.state.binder.getFieldValue('email').value }, Login.bindings),
waitModal: false, waitModal: false,
messageModal: { icon: 'hold', message: `Unable to login`, detail: error.message } messageModal: { icon: 'hold', message: `Unable to login`, detail: error.message }
}) })
@@ -85,7 +80,16 @@ export class Login extends React.Component {
} }
handleMessageModalDismiss() { handleMessageModalDismiss() {
this.setState({ messageModal: null }) this.setState({
messageModal: null,
binder: new FormBinder({ email: this.state.binder.getFieldValue('email') }, Login.bindings)
})
const elems = document.getElementsByName('password')
if (elems) {
elems[0].focus()
}
} }
render() { render() {
@@ -93,9 +97,9 @@ export class Login extends React.Component {
return ( return (
<Row minHeight='100%'> <Row minHeight='100%'>
<Row.Item grow>&nbsp;</Row.Item> <Row.Item grow />
<Row.Item width='450px'> <Row.Item width='450px'>
<form onSubmit={this.handleSubmit}> <form onSubmit={this.handleSubmit} id='loginForm'>
<Column minHeight='100%'> <Column minHeight='100%'>
<Column.Item> <Column.Item>
<Row> <Row>
@@ -131,7 +135,7 @@ export class Login extends React.Component {
<Row> <Row>
<Row.Item grow /> <Row.Item grow />
<Row.Item> <Row.Item>
<BoundButton name='submit' content='Login' submit binder={this.state.binder} /> <BoundButton name='submit' content='Login' submit='loginForm' binder={this.state.binder} />
</Row.Item> </Row.Item>
</Row> </Row>
</Column.Item> </Column.Item>
@@ -144,7 +148,7 @@ export class Login extends React.Component {
</Column> </Column>
</form> </form>
</Row.Item> </Row.Item>
<Row.Item grow>&nbsp;</Row.Item> <Row.Item grow />
<WaitModal active={waitModal} message='Logging In' /> <WaitModal active={waitModal} message='Logging In' />

View File

@@ -2,6 +2,7 @@ import React from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import { Modal, Button, Column, Row, Text, Icon } from 'ui' import { Modal, Button, Column, Row, Text, Icon } from 'ui'
import { sizeInfo } from 'ui/style' import { sizeInfo } from 'ui/style'
import { reactAutoBind } from 'auto-bind2'
export class MessageModal extends React.Component { export class MessageModal extends React.Component {
static propTypes = { static propTypes = {
@@ -12,11 +13,28 @@ export class MessageModal extends React.Component {
onDismiss: PropTypes.func onDismiss: PropTypes.func
} }
constructor(props) {
super(props)
reactAutoBind(this)
}
onSubmit(e) {
e.preventDefault()
e.stopPropagation()
const { onDismiss } = this.props
if (onDismiss) {
onDismiss()
}
}
render() { render() {
const { onDismiss, open, icon, message, detail } = this.props const { open, icon, message, detail } = this.props
return ( return (
<Modal open={open} width={400} onCancel={onDismiss}> <Modal open={open} width={400}>
<form onSubmit={this.onSubmit} id='messageModal'>
<Row> <Row>
<Row.Item> <Row.Item>
<Icon name={icon} size={150} /> <Icon name={icon} size={150} />
@@ -35,7 +53,7 @@ export class MessageModal extends React.Component {
<Row> <Row>
<Row.Item grow /> <Row.Item grow />
<Row.Item> <Row.Item>
<Button onClick={onDismiss}>OK</Button> <Button submit='messageModal' onClick={this.onSubmit}>OK</Button>
</Row.Item> </Row.Item>
<Row.Item grow /> <Row.Item grow />
</Row> </Row>
@@ -45,6 +63,7 @@ export class MessageModal extends React.Component {
</Row.Item> </Row.Item>
<Row.Item width={10} /> <Row.Item width={10} />
</Row> </Row>
</form>
</Modal> </Modal>
) )
} }

View File

@@ -8,7 +8,7 @@ export default class BoundButton extends React.Component {
name: PropTypes.string.isRequired, name: PropTypes.string.isRequired,
content: PropTypes.string, content: PropTypes.string,
binder: PropTypes.object.isRequired, binder: PropTypes.object.isRequired,
submit: PropTypes.bool, submit: PropTypes.string,
onClick: PropTypes.func onClick: PropTypes.func
} }

View File

@@ -6,19 +6,25 @@ import { sizeInfo } from './style'
class Button extends Component { class Button extends Component {
static propTypes = { static propTypes = {
submit: PropTypes.bool,
children: PropTypes.node, children: PropTypes.node,
visible: PropTypes.bool, visible: PropTypes.bool,
disabled: PropTypes.bool, disabled: PropTypes.bool,
name: PropTypes.string, name: PropTypes.string,
onClick: PropTypes.func, onClick: PropTypes.func,
submit: PropTypes.string,
}
static defaultProps = {
visible: true,
disabled: false,
} }
render() { render() {
const { name, children, submit, visible, disabled, onClick } = this.props const { name, children, submit, visible, disabled, onClick } = this.props
return ( return (
<button name={name} type={!visible ? 'hidden' : submit ? 'submit' : 'button'} disabled={disabled} <button name={name} type={!visible ? 'hidden' : submit ? 'submit' : 'button'}
disabled={disabled} form={submit}
style={[style.base, { minWidth: sizeInfo.minButtonWidth }]} onClick={onClick}> style={[style.base, { minWidth: sizeInfo.minButtonWidth }]} onClick={onClick}>
{children} {children}
</button> </button>

View File

@@ -12,15 +12,16 @@ class Input extends Component {
visible: PropTypes.bool, visible: PropTypes.bool,
disabled: PropTypes.bool, disabled: PropTypes.bool,
name: PropTypes.string, name: PropTypes.string,
value: PropTypes.string,
} }
render() { render() {
let { name, width, password, placeholder, onChange, visible, disabled } = this.props let { name, width, password, placeholder, onChange, visible, disabled, value } = this.props
width = width || '100%' width = width || '100%'
return ( return (
<input name={name} type={!visible ? 'hidden' : password ? 'password' : 'text'} disabled={disabled} <input name={name} value={value} type={!visible ? 'hidden' : password ? 'password' : 'text'} disabled={disabled}
style={[ { width }, style.base ]} placeholder={placeholder} onChange={onChange} /> style={[ { width }, style.base ]} placeholder={placeholder} onChange={onChange} />
) )
} }

View File

@@ -9,15 +9,12 @@ class Modal extends Component {
children: PropTypes.node, children: PropTypes.node,
open: PropTypes.bool, open: PropTypes.bool,
width: PropTypes.oneOfType([ PropTypes.string, PropTypes.number ]), width: PropTypes.oneOfType([ PropTypes.string, PropTypes.number ]),
// TODO: onCancel: PropTypes.func <- for handling ESC & enter key
} }
static defaultProps = { static defaultProps = {
width: '60%', width: '60%',
} }
// TODO: Capture ESC key https://stackoverflow.com/questions/3369593/how-to-detect-escape-key-press-with-pure-js-or-jquery
render() { render() {
const { open, children, width } = this.props const { open, children, width } = this.props