Guest User

react component focus

a guest
Oct 15th, 2016
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //http://stackoverflow.com/questions/40046765/react-native-focusing-on-the-next-textinput-field/40056107?noredirect=1#comment67390590_40056107
  2.  
  3. import React, { PropTypes, Component } from 'react';
  4. import {
  5.   Animated,
  6.   Easing,
  7.   Text,
  8.   TextInput,
  9.   TouchableWithoutFeedback,
  10.   View,
  11.   StyleSheet,
  12. } from 'react-native';
  13.  
  14. import BaseInput from './BaseInput';
  15.  
  16. const PADDING = 16;
  17.  
  18. export default class Kohana extends BaseInput {
  19.  
  20.   static propTypes = {
  21.     /*
  22.      * This is the icon component you are importing from react-native-vector-icons.
  23.      * import FontAwesomeIcon from 'react-native-vector-icons/FontAwesome';
  24.      * iconClass={FontAwesomeIcon}
  25.      */
  26.     iconClass: PropTypes.func.isRequired,
  27.     /*
  28.      * Passed to react-native-vector-icons library as name prop
  29.      */
  30.     iconName: PropTypes.string.isRequired,
  31.     /*
  32.      * Passed to react-native-vector-icons library as color prop
  33.      */
  34.     iconColor: PropTypes.string,
  35.   };
  36.  
  37.   static defaultProps = {
  38.     easing: Easing.bezier(0.2, 1, 0.3, 1),
  39.   };
  40.  
  41.   focus() {
  42.     this.refs.input.focus();
  43.   }
  44.  
  45.   render() {
  46.     const {
  47.       iconClass: Icon,
  48.       iconColor,
  49.       iconName,
  50.       label,
  51.       style: containerStyle,
  52.       inputStyle,
  53.       labelStyle,
  54.     } = this.props;
  55.     const { focusedAnim, value } = this.state;
  56.  
  57.     return (
  58.       <View style={[styles.container, containerStyle]} onLayout={this._onLayout}>
  59.         <TouchableWithoutFeedback onPress={this._focus}>
  60.           <Animated.View style={{
  61.             justifyContent: 'center',
  62.             padding: PADDING,
  63.             marginLeft: focusedAnim.interpolate({
  64.               inputRange: [0, 1],
  65.               outputRange: [-40, 0],
  66.             }),
  67.           }}>
  68.             <Icon
  69.               name={iconName}
  70.               color={iconColor}
  71.               size={25}
  72.             />
  73.           </Animated.View>
  74.         </TouchableWithoutFeedback>
  75.         <TouchableWithoutFeedback onPress={this._focus}>
  76.           <Animated.View style={{
  77.             position: 'absolute',
  78.             top: PADDING,
  79.             left: focusedAnim.interpolate({
  80.               inputRange: [0, 1],
  81.               outputRange: [PADDING, 80],
  82.             }),
  83.             opacity: focusedAnim.interpolate({
  84.               inputRange: [0, 1],
  85.               outputRange: [1, 0],
  86.             }),
  87.           }}>
  88.             <Text style={[styles.label, labelStyle]}>
  89.               {label}
  90.             </Text>
  91.           </Animated.View>
  92.         </TouchableWithoutFeedback>
  93.         <TextInput
  94.           ref="input"
  95.           {...this.props}
  96.           style={[styles.textInput, inputStyle]}
  97.           value={value}
  98.           onBlur={this._onBlur}
  99.           onFocus={this._onFocus}
  100.           onChange={this._onChange}
  101.           underlineColorAndroid={'transparent'}
  102.         />
  103.       </View>
  104.     );
  105.   }
  106. }
  107.  
  108. const styles = StyleSheet.create({
  109.   container: {
  110.     flex: 1,
  111.     flexDirection: 'row',
  112.     backgroundColor: 'white',
  113.     overflow: 'hidden',
  114.   },
  115.   label: {
  116.     fontSize: 18,
  117.     fontFamily: 'Arial',
  118.     fontWeight: 'bold',
  119.     color: '#D2D2D2',
  120.   },
  121.   textInput: {
  122.     flex: 1,
  123.     paddingHorizontal: PADDING,
  124.     paddingVertical: 0,
  125.     color: 'black',
  126.     fontSize: 18,
  127.   },
  128. });
Advertisement
Add Comment
Please, Sign In to add comment