|
| 1 | +import React, {Component} from 'react'; |
| 2 | +import {StyleSheet, ScrollView} from 'react-native'; |
| 3 | +import {Colors, View, Text, Incubator, TouchableOpacity} from 'react-native-ui-lib'; //eslint-disable-line |
| 4 | +import _ from 'lodash'; |
| 5 | + |
| 6 | +const COLOR_OPTIONS: {[key: string]: string} = { |
| 7 | + red: Colors.red30, |
| 8 | + green: Colors.green30, |
| 9 | + yellow: Colors.yellow30, |
| 10 | + blue: Colors.blue30 |
| 11 | +}; |
| 12 | + |
| 13 | +export default class TextFieldScreen extends Component { |
| 14 | + expandableInputRef = React.createRef(); |
| 15 | + expandablePickerRef = React.createRef(); |
| 16 | + |
| 17 | + state = { |
| 18 | + textFieldValueDraft: '', |
| 19 | + textFieldValue: '', |
| 20 | + selectedColor: 'red' |
| 21 | + }; |
| 22 | + |
| 23 | + updateText = (value: string) => this.setState({textFieldValueDraft: value}); |
| 24 | + |
| 25 | + onDone = () => { |
| 26 | + this.setState({textFieldValue: this.state.textFieldValueDraft}); |
| 27 | + this.expandableInputRef.current.closeExpandable(); |
| 28 | + }; |
| 29 | + onCancel = () => { |
| 30 | + this.setState({textFieldValueDraft: this.state.textFieldValue}); |
| 31 | + this.expandableInputRef.current.closeExpandable(); |
| 32 | + }; |
| 33 | + |
| 34 | + onPickItem = ({customValue: color}: {customValue: string}) => { |
| 35 | + this.setState({selectedColor: color}); |
| 36 | + this.expandablePickerRef.current.closeExpandable(); |
| 37 | + }; |
| 38 | + |
| 39 | + renderInputModal = () => { |
| 40 | + const {textFieldValueDraft} = this.state; |
| 41 | + return ( |
| 42 | + <> |
| 43 | + <View bg-white br20 padding-s4> |
| 44 | + <Incubator.TextField |
| 45 | + autoFocus |
| 46 | + preset={null} |
| 47 | + value={textFieldValueDraft} |
| 48 | + multiline |
| 49 | + placeholder="Enter text" |
| 50 | + containerStyle={{minHeight: 300}} |
| 51 | + onChangeText={this.updateText} |
| 52 | + /> |
| 53 | + </View> |
| 54 | + </> |
| 55 | + ); |
| 56 | + }; |
| 57 | + |
| 58 | + renderColorRow = (colorKey: string) => { |
| 59 | + return ( |
| 60 | + <View row centerV height={48}> |
| 61 | + <View width={20} height={20} br100 backgroundColor={COLOR_OPTIONS[colorKey]}/> |
| 62 | + <Text marginL-s2 body style={styles.colorRowText}> |
| 63 | + {colorKey} |
| 64 | + </Text> |
| 65 | + </View> |
| 66 | + ); |
| 67 | + }; |
| 68 | + |
| 69 | + renderPickerContent = () => { |
| 70 | + return ( |
| 71 | + <View bg-white br20 padding-s3 paddingB-60> |
| 72 | + {_.map(COLOR_OPTIONS, (_color, key) => { |
| 73 | + return ( |
| 74 | + <TouchableOpacity key={key} customValue={key} onPress={this.onPickItem}> |
| 75 | + {this.renderColorRow(key)} |
| 76 | + </TouchableOpacity> |
| 77 | + ); |
| 78 | + })} |
| 79 | + </View> |
| 80 | + ); |
| 81 | + }; |
| 82 | + |
| 83 | + renderExpandableFieldExample() { |
| 84 | + const {textFieldValue} = this.state; |
| 85 | + return ( |
| 86 | + <> |
| 87 | + <Text h3 marginB-s4 primary> |
| 88 | + Expandable TextField |
| 89 | + </Text> |
| 90 | + <Incubator.ExpandableOverlay |
| 91 | + ref={this.expandableInputRef} |
| 92 | + modalProps={{animationType: 'slide'}} |
| 93 | + expandableContent={this.renderInputModal()} |
| 94 | + showTopBar |
| 95 | + topBarProps={{title: 'Edit Input', doneLabel: 'Done', onCancel: this.onCancel, onDone: this.onDone}} |
| 96 | + dialogProps={{bottom: true}} |
| 97 | + > |
| 98 | + <Incubator.TextField placeholder="Expandable input" value={textFieldValue}/> |
| 99 | + </Incubator.ExpandableOverlay> |
| 100 | + </> |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + renderExpandablePickerExample() { |
| 105 | + const {selectedColor} = this.state; |
| 106 | + return ( |
| 107 | + <> |
| 108 | + <Text h3 marginB-s4 primary> |
| 109 | + Expandable Picker |
| 110 | + </Text> |
| 111 | + <Incubator.ExpandableOverlay |
| 112 | + ref={this.expandablePickerRef} |
| 113 | + useDialog |
| 114 | + expandableContent={this.renderPickerContent()} |
| 115 | + dialogProps={{bottom: true}} |
| 116 | + > |
| 117 | + {this.renderColorRow(selectedColor)} |
| 118 | + </Incubator.ExpandableOverlay> |
| 119 | + </> |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + render() { |
| 124 | + return ( |
| 125 | + <ScrollView keyboardShouldPersistTaps="always"> |
| 126 | + <View padding-page> |
| 127 | + <Text h2 marginB-s5> |
| 128 | + ExpandableOverlay |
| 129 | + </Text> |
| 130 | + |
| 131 | + {this.renderExpandableFieldExample()} |
| 132 | + {this.renderExpandablePickerExample()} |
| 133 | + </View> |
| 134 | + </ScrollView> |
| 135 | + ); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +const styles = StyleSheet.create({ |
| 140 | + colorRowText: { |
| 141 | + textTransform: 'capitalize' |
| 142 | + } |
| 143 | +}); |
0 commit comments