Giter Site home page Giter Site logo

vic2tee4u / react-native-keyboard-aware-view Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vansonleung/react-native-keyboard-aware-view

0.0 1.0 0.0 36 KB

A simple React Native View component that resizes composite children views inside itself when the keyboard appears. You can implement your own ScrollView, ListView, Multiple Views etc... inside the View and set your own view to 'flex: 1'.

License: MIT License

JavaScript 100.00%

react-native-keyboard-aware-view's Introduction

WARNING: DEPRECATED SORRY

0.60 Update:

You may now add prop:

useNativeDriver={true}

to enable to support native driver.

<KeyboardAwareView animated useNativeDriver />

react-native-keyboard-aware-view

npm version NPM

A simple React Native View component that resizes composite children views inside itself when the keyboard appears. You can implement your own ScrollView, ListView, Multiple Views etc... inside the View and set your own view to 'flex: 1'.

For the screen below demonstration, the <TextInput> automatically jumping according to <ScrollView> scroll is NOT related to this library. It is related to my own implementation of <CTextInput>, which is another story & beyond the scope of this plugin.

The problem this plugin aims to solve is to offer easy approach to adjust the view height when keyboard shows / hides. So that you could see the view at the bottom of the screen always sticking above the keyboard. You want to implement a sticky Whatsapp-like message text input box? Set your Chat scrollview to flex: 1, and set your message box view below the scrollview, and then wrap them both inside <KeyboardAwareView>. Here you go. You have implemented a sticky message box view in iOS with no effort at all!

Demo screen

Note: this view only affects iOS. Although still parsable in Android environment, it is treated as an ordinary <View> with "flex: 1" bootstrapped.

Tested: it only affects iOS since Android version of React Native lacks support of this callback, by v0.17:

DeviceEventEmitter.addListener('keyboardWillShow', this.onKeyboardWillShow.bind(this))
DeviceEventEmitter.addListener('keyboardWillHide', this.onKeyboardWillHide.bind(this))

I believe this callback feature will not be landed in the future Android version of React Native. But anyway, Android version has its very own native handling for the keyboards without coding, so this lack is somehow acceptable.

Installation

You can install this component through npm:

npm i react-native-keyboard-aware-view --save

Usage

Import react-native-keyboard-aware-view and wrap your content inside it:

import { KeyboardAwareView } from 'react-native-keyboard-aware-view'

Animated keyboard aware view:

<KeyboardAwareView animated={true}>
	...
</KeyboardAwareView>

Instant (non-animated) keyboard aware view:

<KeyboardAwareView animated={false}>
	...
</KeyboardAwareView>

0.0.8 Updates - Android fix - optionally do not force dismiss keyboard when KeyboardAwareView layout changes:

~~After adopting this plugin, when your Android app needs to undergo indeterminate layout changes THAT would affect the layout of <KeyboardAwareView>, the keyboard would be dismissed. If you do not want the keyboard to be dismissed by this behaviour you just have to set this parameter to true:

<KeyboardAwareView doNotForceDismissKeyboardWhenLayoutChanges={true}>
	...
</KeyboardAwareView>

0.0.12 Updates - Reflecting the fix of 0.0.8, Android no longer accepts "doNotForceDismissKeyboardWhenLayoutChanges" boolean. Instead, the keyboard would never be dismissed when the app layout changes in Android, such that the "doNotForceDismissKeyboardWhenLayoutChanges" parameter would always be treated as "true".

However, iOS still keeps this parameter to control the keyboard:

<KeyboardAwareView doNotForceDismissKeyboardWhenLayoutChanges={true}>
	...
</KeyboardAwareView>

An Example snippet from one of my projects:

  render() {
    return (
        <View style={{flex: 1}}>
          <KeyboardAwareView animated={true}>
            <View style={[styles.bg]}>
              <ScrollView
                  ref={(view) => {this.scrollView = view; }}
                  style={[{flex: 1, alignSelf: 'stretch'}, this.props.style]}
                  keyboardShouldPersistTaps={true}
                  automaticallyAdjustContentInsets={false}
                  onScroll={this.onScroll.bind(this)}
                  scrollEventThrottle={200}
                  onLayout={(e) => {var {x, y, width, height} = e.nativeEvent.layout; console.log(height); }}>
                <View style={{height: 34}} />
                <CTextInput scrollView={this.getScrollView()} placeholder="登記名稱"/>
                <CTextInput scrollView={this.getScrollView()} placeholder="電郵"/>
                <CTextInput scrollView={this.getScrollView()} placeholder="電話"/>
                <CTextInput scrollView={this.getScrollView()} placeholder="登記名稱"/>
                <CTextInput scrollView={this.getScrollView()} placeholder="電郵"/>
                <CTextInput scrollView={this.getScrollView()} placeholder="電話"/>
                <CTextInput scrollView={this.getScrollView()} placeholder="登記名稱"/>
                <CTextInput scrollView={this.getScrollView()} placeholder="電郵"/>
                <CTextInput scrollView={this.getScrollView()} placeholder="電話"/>
                <CTextInput scrollView={this.getScrollView()} placeholder="登記名稱"/>
                <CTextInput scrollView={this.getScrollView()} placeholder="電郵"/>
                <CTextInput scrollView={this.getScrollView()} placeholder="電話"/>
                <CTextInput scrollView={this.getScrollView()} placeholder="登記名稱"/>
                <CTextInput scrollView={this.getScrollView()} placeholder="電郵"/>
                <CTextInput scrollView={this.getScrollView()} placeholder="電話"/>
                <View style={{height: 46}} />
                <Text>或以Facebook登記</Text>
                <Image source={GLOBAL.IMAGE.icon_facebook} style={{width: 50, height: 50}} />
              </ScrollView>
            </View>
            <TouchableOpacity style={{height: 50, backgroundColor: 'transparent', alignItems: 'center', justifyContent: 'center', alignSelf: 'stretch'}}>

              <Text style={{fontSize: 20, color: '#FFFFFF'}}>提交</Text>
              <CTextInput scrollView={this.getScrollView()} placeholder="電話"/>
            </TouchableOpacity>
          </KeyboardAwareView>
        </View>
    );
  }

A Easier Example:

  render() {
    return (
        <View style={{flex: 1}}>
          <KeyboardAwareView animated={true}>
            <View style={{flex: 1}}>
              <ScrollView style={{flex: 1}}>
	              <Text style={{fontSize: 20, color: '#FFFFFF'}}>A</Text>
	              <Text style={{fontSize: 20, color: '#FFFFFF'}}>B</Text>
	              <Text style={{fontSize: 20, color: '#FFFFFF'}}>C</Text>
	              <Text style={{fontSize: 20, color: '#FFFFFF'}}>D</Text>
              </ScrollView>
            </View>
            <TouchableOpacity style={{height: 50, backgroundColor: 'transparent', alignItems: 'center', justifyContent: 'center', alignSelf: 'stretch'}}>
              <Text style={{fontSize: 20, color: '#FFFFFF'}}>Submit</Text>
            </TouchableOpacity>
          </KeyboardAwareView>
        </View>
    );
  }

License

MIT.

react-native-keyboard-aware-view's People

Contributors

vansonleung avatar sharafat avatar mtolly avatar torifat avatar tjbenton avatar dameyerdave avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.