Giter Club home page Giter Club logo

react-native-button's Introduction

apsl-react-native-button

A React Native button component customizable via style props.

Button component screenshot Android Button component screenshot

Renders a TouchableOpacity under iOS and a TouchableNativeFeedback under Android.

Install

apsl-react-native-button>=2.6.0 needs React Native 0.28 or higher. apsl-react-native-button>=2.5.0 needs React Native 0.25 or higher. apsl-react-native-button<=2.4.2 needs React Native 0.16 or higher.

Install the package:

$ npm i apsl-react-native-button --save

Import the Button component:

import Button from 'apsl-react-native-button'

Usage

Provide TouchableWithoutFeedback' props to the component (including style), textStyle's StyleSheet to customize the inner text and a children node to render. You can also provide the isLoading prop that will dim the button and disable it to prevent accidental taps.

<Button style={{backgroundColor: 'red'}} textStyle={{fontSize: 18}}>
  Hello!
</Button>

You can also provide a <Button> element with nested children that are not strings or <Text> elements as long as they are valid React elements or numbers. This helps if your project is using another library that provides easy icon integration utilizing the <i> tag, for instance, as well as various other cases where you are creating visually complex buttons. You may omit the textStyle property and apply your own styles to your child elements as you see fit. Multiple children are allowed.

<Button style={{backgroundColor: 'blue'}}>
  <View style={styles.nestedViewStyle}>
    <Text style={styles.nestedTextStyle}>Nested views!</Text>
  </View>
</Button>

API

Prop Type Description
onPress func Function to execute when the onPress event is triggered.
onPressIn func Function to execute when the onPressIn event is triggered.
onPressOut func Function to execute when the onPressOut event is triggered.
onLongPress func Function to execute when the onLongPress event is triggered.
textStyle TextStylePropTypes The StyleSheet to apply to the inner button text.
disabledStyle TextStylePropTypes The StyleSheet to apply when disabled.
children string, number, React.Element,or array The child nodes to render inside the button. If child is string or number, it will be rendered inside of a <Text> element with textStyle applied if present. Multiple children are allowed (array).
isLoading bool Renders an inactive state dimmed button with a spinner if true.
isDisabled bool Renders an inactive state dimmed button if true.
activeOpacity Number The button onpressing transparency (Usually with a point value between 0 and 1).
activityIndicatorColor string Sets the button of the ActivityIndicatorIOS or ProgressBarAndroid in the loading state.
background TouchableNativeFeedback.propTypes.background Android only. The background prop of TouchableNativeFeedback.
Check the included example for more options.

Similar projects

James Ide's react-native-button https://github.com/ide/react-native-button/

License

MIT.

react-native-button's People

Contributors

alvaromb avatar antt001 avatar brianjd avatar danielweinmann avatar faridsafi avatar k-leon avatar kafaichoi avatar ma-pe avatar mxh-mo avatar sondremare avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-native-button's Issues

Wrapping all children in <Text>

Most mobile sdk's allow for buttons to contain nearly anything that is a "View" or whatever the base UI component on that system is. With this plugin, a decision was made to wrap everything in a <Text> element. What are your thoughts on loosening that up so that we can nest other renderable elements and have them rendered as react-native sees fit instead of wrapping them in <Text>?

Thanks for this plugin and all your work!

Support for testing

The following changes run fine w/ Jest:

...
   textStyle: PropTypes.string,
    disabledStyle: PropTypes.string,
...
   background: PropTypes.any

But the original does not:

 FAIL  src/components/__tests__/FormButton-test.js
Runtime Error
  - TypeError: Cannot read property 'style' of undefined
        at Object.<anonymous> (node_modules/apsl-react-native-button/Button.js:22:25)
        at Runtime._execModule (node_modules/jest-cli/src/Runtime/Runtime.js:261:17)
        at Object.<anonymous> (src/components/FormButton.js:22:12)
        at Object.<anonymous> (src/components/__tests__/FormButton-test.js:27:16)
        at jasmine2 (node_modules/jest-jasmine2/src/index.js:252:16)
        at process._tickCallback (internal/process/next_tick.js:103:7)
1 test suite failed, 0 tests passed (0 total in 1 test suite, run time 6.012s)

I think we've had this discussion before - see pull request #12 - I don't know how to support older Forks in my Repo and update to the newest? I've got this for backward compatibility: https://github.com/bartonhammond/react-native-button.

Is there a way to get this addressed so I don't have to fork it just for testing?

isLoading and isDisabled is not working

Hi,

thx for this great Button. I have a question about the functionality of "isDisabled" and "isLoading". It won't work for me when I'm doing this:

...
var isLoading = false;
export default class extends React.Component
...
return (
            <View style={styles.container}>
                <View style={styles.input}>
                    ...
                    <Button
                        onPress={this._onLoginPressed.bind(this)}
                        isDisabled={isLoading}
                        isLoading={isLoading}>
                        Login
                    </Button>
                </View>
            </View>
        );
    }

If I press the button, my variable "isLoading" gets true but the button is still enabled. What am I doing wrong here?

Can we lossen up props.children check?

Currently it will show warning if you provided anything else than text. But in react-native it's possible to nest text one into another.

My use case is to show icon next to text

<Button>
    <Icon name="sign-in"/><Text> Sign up </Text>
</Button>

Solution for hiding a disabled button

Maybe this is useful to others as well. Its a style that I use to hide buttons that are disabled. I use it for disabledStyle.
The hidden button will still take a little space though. Not sure why.

    buttonDisabled: {
      width: 1, 
      height: 1,
      margin: 0, 
      borderColor: 'rgba(0,0,0,0)', // transparent
      backgroundColor: 'rgba(0,0,0,0)'
    }

Default margin bottom

Why does it have a marginBottom: 10 set by default? I think it makes sense to add margin by the user instead of library defining an arbitrary number. I have to set marginBottom to 0 everytime.

The button layout is skewed when wrap with view component

The button layout is skewed when wrap with view component.
Is this a specification??

OK

<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF' }}>
  <Button style={{ backgroundColor: 'red'}} textStyle={{fontSize: 18}}>
    Hello!
  </Button>
</View>

2016-11-15 23 58 56

NG

<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF' }}>
  <View>
    <Button style={{ backgroundColor: 'red'}} textStyle={{fontSize: 18}}>
      Hello!
    </Button>
  </View>
</View>

2016-11-16 0 06 07

Unable to import module (with babel)

I'm using the react-native-webpack-starter-kit with babel

I get the following error while importing :

ERROR in ./~/apsl-react-native-button/Button.js
Module parse failed: /Users/shivekkhurana/Documents/WIP/Cfel/LifeU/node_modules/apsl-react-native-button/Button.js Unexpected token (53:10)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (53:10)
    at Parser.pp.raise (/Users/shivekkhurana/Documents/WIP/Cfel/LifeU/node_modules/webpack/node_modules/acorn/dist/acorn.js:923:13)
    at Parser.pp.unexpected (/Users/shivekkhurana/Documents/WIP/Cfel/LifeU/node_modules/webpack/node_modules/acorn/dist/acorn.js:1490:8)
    at Parser.pp.parseExprAtom (/Users/shivekkhurana/Documents/WIP/Cfel/LifeU/node_modules/webpack/node_modules/acorn/dist/acorn.js:333:12)
    at Parser.pp.parseExprSubscripts (/Users/shivekkhurana/Documents/WIP/Cfel/LifeU/node_modules/webpack/node_modules/acorn/dist/acorn.js:228:19)
    at Parser.pp.parseMaybeUnary (/Users/shivekkhurana/Documents/WIP/Cfel/LifeU/node_modules/webpack/node_modules/acorn/dist/acorn.js:207:17)
    at Parser.pp.parseExprOps (/Users/shivekkhurana/Documents/WIP/Cfel/LifeU/node_modules/webpack/node_modules/acorn/dist/acorn.js:154:19)
    at Parser.pp.parseMaybeConditional (/Users/shivekkhurana/Documents/WIP/Cfel/LifeU/node_modules/webpack/node_modules/acorn/dist/acorn.js:136:19)
    at Parser.pp.parseMaybeAssign (/Users/shivekkhurana/Documents/WIP/Cfel/LifeU/node_modules/webpack/node_modules/acorn/dist/acorn.js:112:19)
    at Parser.pp.parseParenAndDistinguishExpression (/Users/shivekkhurana/Documents/WIP/Cfel/LifeU/node_modules/webpack/node_modules/acorn/dist/acorn.js:376:28)
    at Parser.pp.parseExprAtom (/Users/shivekkhurana/Documents/WIP/Cfel/LifeU/node_modules/webpack/node_modules/acorn/dist/acorn.js:307:19)
    at Parser.pp.parseExprSubscripts (/Users/shivekkhurana/Documents/WIP/Cfel/LifeU/node_modules/webpack/node_modules/acorn/dist/acorn.js:228:19)
    at Parser.pp.parseMaybeUnary (/Users/shivekkhurana/Documents/WIP/Cfel/LifeU/node_modules/webpack/node_modules/acorn/dist/acorn.js:207:17)
    at Parser.pp.parseExprOps (/Users/shivekkhurana/Documents/WIP/Cfel/LifeU/node_modules/webpack/node_modules/acorn/dist/acorn.js:154:19)
    at Parser.pp.parseMaybeConditional (/Users/shivekkhurana/Documents/WIP/Cfel/LifeU/node_modules/webpack/node_modules/acorn/dist/acorn.js:136:19)
    at Parser.pp.parseMaybeAssign (/Users/shivekkhurana/Documents/WIP/Cfel/LifeU/node_modules/webpack/node_modules/acorn/dist/acorn.js:112:19)
    at Parser.pp.parseVar (/Users/shivekkhurana/Documents/WIP/Cfel/LifeU/node_modules/webpack/node_modules/acorn/dist/acorn.js:2060:24)
 @ ./src/components/auth/Login.js 4:27-62
  • "apsl-react-native-button": "^3.0.0"
  • "react": "^15.2.1"
  • "react-native": "^0.28.0"
  • "babel-core": "^6.10.4"
  • "babel-loader": "^6.2.4"
  • "babel-preset-es2015": "^6.9.0"

On browsing through closed solutions, I found #2, but adding whitelist to web pack config doesn't solve the issue.

...
      loaders: [
        {
          test: /\.(js|jsx|es6)$/,
          include: /node_modules\/react-native/,
          loader: 'babel',
          query: {
            cacheDirectory: true,
            presets: ['es2015', 'stage-1', 'react']
          },
          whitelist: 'es6.modules'
        },
...

How to go about this ?

Thank you.

Align problem on RN 0.37.0

The button text on Android is align on the top left, i think the cause is the Text element that wrap the other Text children.

Default Jest test is failing

This is default test for view which is generated by react-native init ProjectName command:

import 'react-native';
import React from 'react';
import Index from '../index.ios.js';

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

it('renders correctly', () => {
  const tree = renderer.create(
    <Index />
  );
});

In my index file I started to use <Button> component and it works awesomely fine in simulator. However when I ran test command I had a failed test with exception:

 dmitri@MacBook-Pro-Dmitri  ~/github/fanburst/FanburstApp   master  npm test

> [email protected] test /Users/dmitri/github/fanburst/FanburstApp
> jest

 PASS  __tests__/index.android.js (6.547s)
 FAIL  __tests__/index.ios.js
  ● Test suite failed to run

    /Users/dmitri/github/fanburst/FanburstApp/node_modules/apsl-react-native-button/Button.js:53
              <Text
              ^
    SyntaxError: Unexpected token <

      at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:284:10)
      at Object.<anonymous> (index.ios.js:17:28)
      at Object.<anonymous> (__tests__/index.ios.js:3:15)

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        9.343s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

Any ideas?

Support Babel 6

React Native 0.16.0-rc uses Babel 6 so this would be nice. I'm not sure how much work that is to do.

Add "activeOpacity" attribute

As requested on the following #25, is it possible to add the attribute? On Android if you press rapidly you don't have any feedback.

Thanks,
Matteo

Is it possible to show loading indicator and text together

Hi,

When setting isLoading = { true } a loading indicator will be shown, but the text will be gone in the same time. In popular apps, I usually see the indicator and text are shown in parallel, is it possible to support this?

Thanks.

TouchableOpacity

I want use TouchableOpacity instead of TouchableNativeFeedback for android. How can i make this?

ES6 doesn't work on react-native

I was wondering how did you get

import Button from 'apsl-react-native-button'; to work. I could only use es5 syntax of importing modules.

don't take effet when set flex

when i set the button's father view's style flexDirection:'column', aliginItems:'center'
this button not center ,is left

when using The props or button loading will get a error , and other is work.

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undifined. Check the render method of 'Button'.

<Button
isLoading={true}
style={styles.buttonStyle7} textStyle={styles.textStyle6}
onPress={() => {
console.log('world!')
}}>
Hello

it can"t work.

<Button
style={styles.buttonStyle6} textStyle={styles.textStyle}
onPress={() => {
console.log('world!')
}}>
Hello

it can work

Show spinner when the user presses the button?

I have a button that i would like to only submit once.

Ideally I could show a spinner and disable the button after it is pressed. Is this possible? I tried the code below but it didnt seem to work.

<Button onPress={() => { this.isDisabled = {true} }}>My Button</Button>

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.