From ef07749a7c23261811575fc71be3caa8fa1c19a2 Mon Sep 17 00:00:00 2001 From: Rayhan Yulanda Date: Thu, 22 Aug 2019 13:25:43 +0700 Subject: [PATCH 1/2] Add some feature Adding defaultUri and editable in config --- lib/ImageFactory.js | 466 +++++++++++++++++++++++--------------------- 1 file changed, 241 insertions(+), 225 deletions(-) diff --git a/lib/ImageFactory.js b/lib/ImageFactory.js index 53db913..bca18e5 100644 --- a/lib/ImageFactory.js +++ b/lib/ImageFactory.js @@ -2,13 +2,13 @@ import React from 'react'; import { - ActionSheetIOS, - View, - Text, - Button, - Animated, - StyleSheet, - Platform + ActionSheetIOS, + View,Image, + Text, + Button, + Animated, + StyleSheet, + Platform } from 'react-native'; import ImagePicker from 'react-native-image-crop-picker'; import SimpleLineIcons from 'react-native-vector-icons/SimpleLineIcons'; @@ -17,237 +17,253 @@ import BottomSheet from 'react-native-js-bottom-sheet'; import MaterialIcons from 'react-native-vector-icons/MaterialIcons'; type Props = { - title: string + title: string }; type State = { - image: ?string + image: ?string }; const Component = t.form.Component; class ImageFactory extends Component { - bottomSheet: BottomSheet; - - constructor(props: Props) { - super(props); - this.state = { - image: undefined, - height: new Animated.Value(0), - overflow: 'visible' - }; - } - - _onPressImage = () => { - const options = this.props.options.config.options || [ - 'Open camera', - 'Select from the gallery', - 'Cancel' - ]; - ActionSheetIOS.showActionSheetWithOptions( - { - options, - cancelButtonIndex: 2 - }, - (buttonIndex: number) => { - if (buttonIndex === 0) { - ImagePicker.openCamera({}).then((image: Object) => - this._getImageFromStorage(image.path) - ); - } else if (buttonIndex === 1) { - ImagePicker.openPicker({}).then((image: Object) => - this._getImageFromStorage(image.path) - ); - } - } - ); - }; - - shouldComponentUpdate(nextProps: Props, nextState: State) { - return true; - } - - _startAnimation = () => { - Animated.sequence([ - Animated.timing(this.state.height, { - toValue: 0, - duration: 250 - }), - Animated.timing(this.state.height, { - toValue: 150, - duration: 500, - delay: 75 - }) - ]).start(); - }; - - _renderOptions = (): Array => { - const options = this.props.options.config.options || [ - 'Open camera', - 'Select from the gallery', - 'Cancel' - ]; - - return [ - { - title: options[0], - onPress: () => - ImagePicker.openCamera({}).then((image: Object) => - this._getImageFromStorage(image.path) - ) && this.bottomSheet.close(), - icon: ( - - ) - }, - { - title: options[1], - onPress: () => - ImagePicker.openPicker({}).then((image: Object) => - this._getImageFromStorage(image.path) - ) && this.bottomSheet.close(), - icon: ( - - ) - } - ]; - }; - - _getImageFromStorage = (path: string) => { - this.setState({ image: path, overflow: 'hidden' }, () => - this._startAnimation() - ); - super.getLocals().onChange(path); - }; - - getTemplate() { - return (locals: Object) => { - const stylesheet = locals.stylesheet; - let formGroupStyle = stylesheet.formGroup.normal; - let controlLabelStyle = stylesheet.controlLabel.normal; - let textboxStyle = stylesheet.textbox.normal; - let helpBlockStyle = stylesheet.helpBlock.normal; - let errorBlockStyle = stylesheet.errorBlock; - let topContainer = stylesheet.imagePicker - ? stylesheet.imagePicker.topContainer - : styles.topContainer; - let container = stylesheet.imagePicker - ? stylesheet.imagePicker.container - : styles.container; - let cameraColor = - locals.config && locals.config.cameraColor - ? locals.config.cameraColor - : '#333'; - let buttonTextColor = - locals.config && locals.config.buttonTextColor - ? locals.config.buttonTextColor - : '#333'; - - if (locals.hasError) { - controlLabelStyle = stylesheet.controlLabel.error; - formGroupStyle = stylesheet.formGroup.error; - textboxStyle = stylesheet.textbox.error; - helpBlockStyle = stylesheet.helpBlock.error; - } - - const style: Object = this.props.options.config.style || {}; - return ( - - {locals.label ? ( - - {locals.label} - - ) : null} - - { + const options = this.props.options.config.options || [ + 'Open camera', + 'Select from the gallery', + 'Cancel' + ]; + ActionSheetIOS.showActionSheetWithOptions( + { + options, + cancelButtonIndex: 2 + }, + (buttonIndex: number) => { + if (buttonIndex === 0) { + ImagePicker.openCamera({}).then((image: Object) => + this._getImageFromStorage(image.path) + ); + } else if (buttonIndex === 1) { + ImagePicker.openPicker({}).then((image: Object) => + this._getImageFromStorage(image.path) + ); } - ]} - /> - - - - -