|
| 1 | +// @flow |
| 2 | +import React from 'react' |
| 3 | +import {StyleSheet, Text, Platform, TextInput} from 'react-native' |
| 4 | +import {Cell} from 'react-native-tableview-simple' |
| 5 | +import {Row} from '../../components/layout' |
| 6 | +import * as c from '../../components/colors' |
| 7 | + |
| 8 | +const styles = StyleSheet.create({ |
| 9 | + label: { |
| 10 | + width: 90, |
| 11 | + fontSize: 16, |
| 12 | + marginTop: Platform.OS === 'ios' ? -2 : 0, // lines the label up with the text on iOS |
| 13 | + alignSelf: 'center', |
| 14 | + }, |
| 15 | + hiddenLabel: { |
| 16 | + width: 0, |
| 17 | + }, |
| 18 | + customTextInput: { |
| 19 | + flex: 1, |
| 20 | + }, |
| 21 | + loginCell: { |
| 22 | + height: Platform.OS === 'android' ? 65 : 44, |
| 23 | + alignItems: 'stretch', |
| 24 | + paddingTop: 0, |
| 25 | + paddingBottom: 0, |
| 26 | + }, |
| 27 | +}) |
| 28 | + |
| 29 | +export class CellTextField extends React.Component { |
| 30 | + static defaultProps = { |
| 31 | + disabled: false, |
| 32 | + placeholder: '', |
| 33 | + _ref: () => {}, |
| 34 | + returnKeyType: 'default', |
| 35 | + secureTextEntry: false, |
| 36 | + } |
| 37 | + |
| 38 | + props: { |
| 39 | + label?: string, |
| 40 | + _ref: () => any, |
| 41 | + disabled: boolean, |
| 42 | + onChangeText: string => any, |
| 43 | + onSubmitEditing: string => any, |
| 44 | + placeholder: string, |
| 45 | + returnKeyType: 'done' | 'next' | 'default', |
| 46 | + secureTextEntry: boolean, |
| 47 | + value: string, |
| 48 | + labelWidth?: number, |
| 49 | + } |
| 50 | + |
| 51 | + _input: any |
| 52 | + focusInput = () => this._input.focus() |
| 53 | + |
| 54 | + cacheRef = (ref: any) => { |
| 55 | + this._input = ref |
| 56 | + this.props._ref(ref) |
| 57 | + } |
| 58 | + |
| 59 | + onSubmit = () => { |
| 60 | + this.props.onSubmitEditing(this.props.value) |
| 61 | + } |
| 62 | + |
| 63 | + render() { |
| 64 | + const labelWidthStyle = this.props.labelWidth !== null && |
| 65 | + this.props.labelWidth !== undefined |
| 66 | + ? {width: this.props.labelWidth} |
| 67 | + : null |
| 68 | + |
| 69 | + const label = this.props.label |
| 70 | + ? <Text onPress={this.focusInput} style={[styles.label, labelWidthStyle]}> |
| 71 | + {this.props.label} |
| 72 | + </Text> |
| 73 | + : <Text style={styles.hiddenLabel} /> |
| 74 | + |
| 75 | + return ( |
| 76 | + <Cell |
| 77 | + contentContainerStyle={styles.loginCell} |
| 78 | + cellContentView={label} |
| 79 | + cellAccessoryView={ |
| 80 | + <TextInput |
| 81 | + ref={this.cacheRef} |
| 82 | + autoCapitalize="none" |
| 83 | + autoCorrect={false} |
| 84 | + clearButtonMode="while-editing" |
| 85 | + disabled={this.props.disabled} |
| 86 | + onChangeText={this.props.onChangeText} |
| 87 | + onSubmitEditing={this.onSubmit} |
| 88 | + placeholder={this.props.placeholder} |
| 89 | + placeholderTextColor={c.iosPlaceholderText} |
| 90 | + returnKeyType={this.props.returnKeyType} |
| 91 | + secureTextEntry={this.props.secureTextEntry} |
| 92 | + style={[styles.customTextInput]} |
| 93 | + value={this.props.value} |
| 94 | + /> |
| 95 | + } |
| 96 | + /> |
| 97 | + ) |
| 98 | + } |
| 99 | +} |
0 commit comments