Working login on mobile
This commit is contained in:
67
mobile/src/ui/BoundButton.js
Normal file
67
mobile/src/ui/BoundButton.js
Normal file
@@ -0,0 +1,67 @@
|
||||
import React from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
import { View, Text, TouchableHighlight } from 'react-native'
|
||||
import { reactAutoBind } from 'auto-bind2'
|
||||
|
||||
export class BoundButton extends React.Component {
|
||||
static propTypes = {
|
||||
name: PropTypes.string.isRequired,
|
||||
title: PropTypes.string,
|
||||
binder: PropTypes.object.isRequired,
|
||||
submit: PropTypes.string,
|
||||
onPress: PropTypes.func,
|
||||
width: PropTypes.oneOfType([ PropTypes.string, PropTypes.number ]),
|
||||
}
|
||||
|
||||
constructor(props) {
|
||||
super(props)
|
||||
reactAutoBind(this)
|
||||
|
||||
let { name, binder } = this.props
|
||||
|
||||
binder.addListener(name, this.updateValue)
|
||||
this.state = binder.getFieldState(name)
|
||||
}
|
||||
|
||||
updateValue(e) {
|
||||
this.setState(e.state)
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
this.props.binder.removeListener(this.props.name, this.updateValue)
|
||||
}
|
||||
|
||||
componentWillReceiveProps(nextProps) {
|
||||
if (nextProps.binder !== this.props.binder) {
|
||||
this.props.binder.removeListener(this.props.name, this.updateValue)
|
||||
nextProps.binder.addListener(nextProps.name, this.updateValue)
|
||||
this.setState(nextProps.binder.getFieldState(nextProps.name))
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const { name, title, submit, width, onPress } = this.props
|
||||
const { visible, disabled } = this.state
|
||||
|
||||
if (!visible) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (disabled) {
|
||||
return (
|
||||
<View style={{ flexDirection: 'column', justifyContent: 'center', paddingHorizontal: 10, height: 40, width , backgroundColor: '#E0E0E0' }}>
|
||||
<Text style={{ alignSelf: 'center', color: '#AAAAAA' }}>{title}</Text>
|
||||
</View>
|
||||
)
|
||||
} else {
|
||||
return (
|
||||
<TouchableHighlight
|
||||
onPress={onPress}
|
||||
style={{ justifyContent: 'center', paddingHorizontal: 10, height: 40, width, backgroundColor: '#3BB0FD' }}
|
||||
underlayColor='#1A72AC'>
|
||||
<Text style={{ alignSelf: 'center', color: 'black' }}>{title}</Text>
|
||||
</TouchableHighlight>
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
59
mobile/src/ui/BoundInput.js
Normal file
59
mobile/src/ui/BoundInput.js
Normal file
@@ -0,0 +1,59 @@
|
||||
import React from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
import { TextInput, Text, View } from 'react-native'
|
||||
import { reactAutoBind } from 'auto-bind2'
|
||||
|
||||
export class BoundInput extends React.Component {
|
||||
static propTypes = {
|
||||
name: PropTypes.string.isRequired,
|
||||
message: PropTypes.string,
|
||||
label: PropTypes.string,
|
||||
binder: PropTypes.object.isRequired,
|
||||
password: PropTypes.bool,
|
||||
placeholder: PropTypes.string
|
||||
}
|
||||
|
||||
constructor(props) {
|
||||
super(props)
|
||||
reactAutoBind(this)
|
||||
this.state = props.binder.getFieldState(props.name)
|
||||
}
|
||||
|
||||
handleChangeText(newText) {
|
||||
const { binder, name } = this.props
|
||||
const state = binder.getFieldState(name)
|
||||
|
||||
if (!state.readOnly && !state.disabled) {
|
||||
this.setState(binder.updateFieldValue(name, newText))
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const { label, password, name, placeholder, message } = this.props
|
||||
const { visible, disabled, value, valid } = this.state
|
||||
|
||||
if (!visible) {
|
||||
return null
|
||||
}
|
||||
|
||||
// TODO: Disabled
|
||||
|
||||
return (
|
||||
<View style={{ width: '100%' }}>
|
||||
<Text style={{ color: 'black', fontSize: 14, marginBottom: 5 }}>{label}</Text>
|
||||
<TextInput style={{ width: '100%', paddingLeft: 5, paddingRight: 5, height: 40, borderColor: 'gray', borderWidth: 1, fontSize: 16 }}
|
||||
autoCapitalize='none'
|
||||
underlineColorAndroid='white'
|
||||
value={value}
|
||||
secureTextEntry={password}
|
||||
onChangeText={this.handleChangeText}
|
||||
placeholder={placeholder} />
|
||||
<Text style={{
|
||||
fontSize: 12,
|
||||
display: valid ? 'none' : 'flex',
|
||||
color: 'red',
|
||||
}}>{message}</Text>
|
||||
</View>
|
||||
)
|
||||
}
|
||||
}
|
||||
55
mobile/src/ui/BoundSwitch.js
Normal file
55
mobile/src/ui/BoundSwitch.js
Normal file
@@ -0,0 +1,55 @@
|
||||
import React from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
import { View, Switch, Text } from 'react-native'
|
||||
import { reactAutoBind } from 'auto-bind2'
|
||||
|
||||
export class BoundSwitch extends React.Component {
|
||||
static propTypes = {
|
||||
name: PropTypes.string.isRequired,
|
||||
label: PropTypes.string,
|
||||
binder: PropTypes.object.isRequired,
|
||||
}
|
||||
|
||||
constructor(props) {
|
||||
super(props)
|
||||
reactAutoBind(this)
|
||||
this.state = props.binder.getFieldState(props.name)
|
||||
}
|
||||
|
||||
handleValueChange() {
|
||||
const { binder, name } = this.props
|
||||
const state = binder.getFieldState(name)
|
||||
|
||||
if (!state.readOnly && !state.disabled) {
|
||||
this.setState(binder.updateFieldValue(name, !state.value))
|
||||
}
|
||||
}
|
||||
|
||||
componentWillReceiveProps(nextProps) {
|
||||
if (nextProps.binder !== this.props.binder) {
|
||||
this.setState(nextProps.binder.getFieldState(nextProps.name))
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const { name, label } = this.props
|
||||
const { visible, disabled, value } = this.state
|
||||
|
||||
return (
|
||||
<View style={{
|
||||
display: visible ? 'flex' : 'none',
|
||||
flexDirection: 'row',
|
||||
}}>
|
||||
<Switch disabled={disabled} value={value} onValueChange={this.handleValueChange} />
|
||||
<Text style={{
|
||||
color: disabled ? 'gray' : 'black',
|
||||
fontSize: 16,
|
||||
paddingLeft: 8,
|
||||
alignSelf: 'center'
|
||||
}}>
|
||||
{label}
|
||||
</Text>
|
||||
</View>
|
||||
)
|
||||
}
|
||||
}
|
||||
3
mobile/src/ui/index.js
Normal file
3
mobile/src/ui/index.js
Normal file
@@ -0,0 +1,3 @@
|
||||
export { BoundSwitch } from './BoundSwitch'
|
||||
export { BoundInput } from './BoundInput'
|
||||
export { BoundButton } from './BoundButton'
|
||||
Reference in New Issue
Block a user