|
| 1 | +/* @flow */ |
| 2 | + |
| 3 | +import React from 'react' |
| 4 | +import { |
| 5 | + ActionSheetIOS, |
| 6 | + View, |
| 7 | + Text, |
| 8 | + Button, |
| 9 | + Animated, |
| 10 | + StyleSheet |
| 11 | +} from 'react-native' |
| 12 | +import ImagePicker from 'react-native-image-crop-picker' |
| 13 | +import SimpleLineIcons from 'react-native-vector-icons/SimpleLineIcons' |
| 14 | +import t from 'tcomb-form-native' |
| 15 | + |
| 16 | +type Props = { |
| 17 | + title: string |
| 18 | +} |
| 19 | +type State = { |
| 20 | + image: ?string |
| 21 | +} |
| 22 | + |
| 23 | +const Component = t.form.Component |
| 24 | + |
| 25 | +class ImageFactory extends Component<Props, State> { |
| 26 | + constructor(props: Props) { |
| 27 | + super(props) |
| 28 | + this.state = { |
| 29 | + image: undefined, |
| 30 | + height: new Animated.Value(0), |
| 31 | + overflow: 'visible' |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + _onPressImage = () => { |
| 36 | + const options = this.props.options.config.options || [ |
| 37 | + 'Open camera', |
| 38 | + 'Select from the gallery', |
| 39 | + 'Cancel' |
| 40 | + ] |
| 41 | + ActionSheetIOS.showActionSheetWithOptions( |
| 42 | + { |
| 43 | + options, |
| 44 | + cancelButtonIndex: 2 |
| 45 | + }, |
| 46 | + (buttonIndex: number) => { |
| 47 | + if (buttonIndex === 0) { |
| 48 | + ImagePicker.openCamera({}).then((image: Object) => |
| 49 | + this._getImageFromStorage(image.path) |
| 50 | + ) |
| 51 | + } else if (buttonIndex === 1) { |
| 52 | + ImagePicker.openPicker({}).then((image: Object) => |
| 53 | + this._getImageFromStorage(image.path) |
| 54 | + ) |
| 55 | + } |
| 56 | + } |
| 57 | + ) |
| 58 | + } |
| 59 | + |
| 60 | + shouldComponentUpdate(nextProps: Props, nextState: State) { |
| 61 | + return true |
| 62 | + } |
| 63 | + |
| 64 | + _startAnimation = () => { |
| 65 | + Animated.sequence([ |
| 66 | + Animated.timing(this.state.height, { |
| 67 | + toValue: 0, |
| 68 | + duration: 250 |
| 69 | + }), |
| 70 | + Animated.timing(this.state.height, { |
| 71 | + toValue: 150, |
| 72 | + duration: 500, |
| 73 | + delay: 75 |
| 74 | + }) |
| 75 | + ]).start() |
| 76 | + } |
| 77 | + |
| 78 | + _getImageFromStorage = (path: string) => { |
| 79 | + this.setState({ image: path, overflow: 'hidden' }, () => |
| 80 | + this._startAnimation() |
| 81 | + ) |
| 82 | + super.getLocals().onChange(path) |
| 83 | + } |
| 84 | + |
| 85 | + getTemplate() { |
| 86 | + return (locals: Object) => { |
| 87 | + const stylesheet = locals.stylesheet |
| 88 | + let formGroupStyle = stylesheet.formGroup.normal |
| 89 | + let controlLabelStyle = stylesheet.controlLabel.normal |
| 90 | + let textboxStyle = stylesheet.textbox.normal |
| 91 | + let helpBlockStyle = stylesheet.helpBlock.normal |
| 92 | + let errorBlockStyle = stylesheet.errorBlock |
| 93 | + |
| 94 | + if (locals.hasError) { |
| 95 | + controlLabelStyle = stylesheet.controlLabel.error |
| 96 | + formGroupStyle = stylesheet.formGroup.error |
| 97 | + textboxStyle = stylesheet.textbox.error |
| 98 | + helpBlockStyle = stylesheet.helpBlock.error |
| 99 | + } |
| 100 | + |
| 101 | + return ( |
| 102 | + <View> |
| 103 | + {locals.label ? ( |
| 104 | + <Text |
| 105 | + style={[ |
| 106 | + controlLabelStyle, |
| 107 | + { |
| 108 | + color: locals.error ? '#a94442' : 'black' |
| 109 | + } |
| 110 | + ]}> |
| 111 | + {locals.label} |
| 112 | + </Text> |
| 113 | + ) : null} |
| 114 | + <View |
| 115 | + style={[ |
| 116 | + styles.topContainer, |
| 117 | + { |
| 118 | + borderColor: locals.hasError ? '#a94442' : 'grey' |
| 119 | + } |
| 120 | + ]}> |
| 121 | + <Animated.Image |
| 122 | + resizeMode="cover" |
| 123 | + source={{ |
| 124 | + uri: this.state.image |
| 125 | + }} |
| 126 | + style={[ |
| 127 | + styles.image, |
| 128 | + { |
| 129 | + height: this.state.height |
| 130 | + } |
| 131 | + ]} |
| 132 | + /> |
| 133 | + <View |
| 134 | + style={[ |
| 135 | + styles.container, |
| 136 | + { |
| 137 | + overflow: this.state.overflow, |
| 138 | + backgroundColor: locals.hasError ? '#E28E8E' : '#e6e6e6' |
| 139 | + } |
| 140 | + ]}> |
| 141 | + <SimpleLineIcons name={'camera'} size={28} style={styles.icon} /> |
| 142 | + </View> |
| 143 | + </View> |
| 144 | + <Button onPress={this._onPressImage} title={locals.config.title} /> |
| 145 | + {locals.help || locals.config.help ? ( |
| 146 | + <Text style={helpBlockStyle}> |
| 147 | + {locals.help || locals.config.help} |
| 148 | + </Text> |
| 149 | + ) : null} |
| 150 | + </View> |
| 151 | + ) |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +const styles = StyleSheet.create({ |
| 157 | + topContainer: { |
| 158 | + overflow: 'hidden', |
| 159 | + borderRadius: 4, |
| 160 | + marginBottom: 12, |
| 161 | + height: 150, |
| 162 | + borderColor: 'grey', |
| 163 | + borderWidth: 1 |
| 164 | + }, |
| 165 | + container: { |
| 166 | + flex: 1, |
| 167 | + alignItems: 'center', |
| 168 | + justifyContent: 'center', |
| 169 | + backgroundColor: '#e6e6e6', |
| 170 | + |
| 171 | + height: 100, |
| 172 | + borderRadius: 4 |
| 173 | + }, |
| 174 | + icon: { |
| 175 | + textAlign: 'center', |
| 176 | + textAlignVertical: 'center' |
| 177 | + }, |
| 178 | + image: { |
| 179 | + height: 150 |
| 180 | + } |
| 181 | +}) |
| 182 | + |
| 183 | +export default ImageFactory |
0 commit comments