Giter VIP home page Giter VIP logo

react-bootstrap-toggle's Introduction

React-Bootstrap-Toggle

Build Status Coverage Status

This is a react component of the http://www.bootstraptoggle.com/ project.

Usage

You need to include the bootstrap css file and also the bootstrap2-toggle css file in your app.

If you are using the npm version then you can include it from the module like this.

<link rel="stylesheet" href="node_modules/lib/bootstrap2-toggle.css">

Or you could import it to your SASS or LESS build.

@import "node_modules/lib/bootstrap2-toggle.css";
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import Toggle from 'react-bootstrap-toggle';

class Form extends Component {
  constructor() {
    super();
    this.state = { toggleActive: false };
    this.onToggle = this.onToggle.bind(this);
  }

  onToggle() {
    this.setState({ toggleActive: !this.state.toggleActive });
  }

  render() {
    return (
      <form>
        <SomeInput something={someProp} />
        .....
        <Toggle
          onClick={this.onToggle}
          on={<h2>ON</h2>}
          off={<h2>OFF</h2>}
          size="xs"
          offstyle="danger"
          active={this.state.toggleActive}
        />
      </form>
    )
  }

}

Install via NPM

npm install react-bootstrap-toggle --save

Props

Name Type Default Description
active boolean true Sets the initial state of the toggle
on string/html "On" Text of the on toggle
off string/html "Off" Text of the off toggle
size string null Size of the toggle. Possible values are lg, sm, xs.
onstyle string "primary" Style of the on toggle. Possible values are default, primary, success, info, warning, danger
onClassName string null additional classname to put on the on button
offstyle string "default" Style of the off toggle. Possible values are default, primary, success, info, warning, danger
offClassName string null additional classname to put on the off button
handlestyle string "default" Style of the handle. Possible values are default, primary, success, info, warning, danger
handleClassName string null additional classname to put on the handle button
width integer null Sets the width of the toggle. if set to null, width will be responsive.
height integer null Sets the height of the toggle. if set to null, height will be responsive.
disabled bool false Render the toggle as disabled
style object null If you want to add additional style to the root div
recalculateOnResize bool false If the toggle should recalculate it's dimensions when visibility or dimensions change

onClick

A callback function that returns the state, the parent node, and the event

onClick(state, node, evt);

Using Bootstrap2 class names

If you want to use bootstrap2 class names you can import the component like this

import { Bootstrap2Toggle } from 'react-bootstrap-toggle';

Now the component will use large instead of lg and etc..

Bootstrap2Toggle Props

Name Type Default Description
size string null Size of the toggle. Possible values are large, small, tiny.

react-bootstrap-toggle's People

Contributors

arnthor3 avatar danosaure avatar dependabot[bot] avatar jeffvandyke avatar ledsoft avatar pfischer1290 avatar thevasya avatar winstonewert 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

Watchers

 avatar  avatar  avatar

react-bootstrap-toggle's Issues

`className` prop being ignored

In https://github.com/arnthor3/react-bootstrap-toggle/blob/master/src/react-bootstrap-toggle.jsx a className property is defined, and this property is applied to the outermost div inside the render method.

However this property is not initialised inside the defaultProps and does not seem to be applied at all when I use it like so:

import React, { Component, PropTypes } from 'react';
import Toggle from 'react-bootstrap-toggle';

import 'react-bootstrap-toggle/lib/bootstrap2-toggle.css';

class MyComponent extends Component {
    ...
    render() {
        return (
            <Toggle size={`xs`} on={`Yes`} off={`No`} onstyle={`success`} offstyle={`danger`} className={`span3`} />
        );
    }
...

The value that I enter for className appears to be thrown away, I also noticed that it is not mentioned in the readme file as a prop that can be set - is this a mistake or am I using it incorrectly?

Does not support FontAwesomeIcons in labels

image

This error is returned when a font awesome icon is placed in labels.
If it's the error is thrown when you click on the button.
If it's the error is thrown at page loading.

There's no Toggle component

Only Bootstrap2Toggle:

$ node
> var t = require('react-bootstrap-toggle')
Warning: Accessing PropTypes via the main React package is deprecated. Use the prop-types package from npm instead.
undefined
> t.
t.__defineGetter__      t.__defineSetter__      t.__lookupGetter__      t.__lookupSetter__      t.__proto__
t.constructor           t.hasOwnProperty        t.isPrototypeOf         t.propertyIsEnumerable  t.toLocaleString
t.toString              t.valueOf               

t.Bootstrap2Toggle      t.__esModule            t.default               

> t.Bootstrap2Toggle
[Function: ReactBootstrap2Toggle]

Impossibility of define an ID for React bootstrap toggle

When trying to define an ID for the react bootstrap toggle (passing the usual id property for JSX tag), the rendered element doesn't have an id property. After inspecting the generated html element on the browser, we can see that the defined id is ignored. As this approach of defining a custom id for elements is very important for later identifying the element, I would like to know if it is in the roadmap of this project to support the receiving of the id.

Enzyme hangs when trying to mount a component with "ToggleButton" in it

I found that one of my unit test suites caused my Jest test runner to hang indefinitely. I traced it down to trying to mount the component in the unit tests for that suite. I investigated further and found the issue was caused by using "ToggleButton" in the React component under test.

<span>
    <ToggleButton onstyle='danger' height='34px' active={true}/>
</span>

Which is imported with:

import ToggleButton from 'react-bootstrap-toggle';

Environment

Component should set className in render based on this.props.active and not this.state.active.

Hi,

Good job on your component.

There is a small issue when I am using your component in my project. The problem is the component should always infer active value from props and not from state.

So it should not be

className={this.state.active ? activeClass : inactiveClass} 

Rather it should be

className={this.props.active ? activeClass : inactiveClass} 

The reason is, we make API calls to get some object and the object's bool value should determine the active state. The objects bool valus is passed through this.props.

Remove dev files when publish

Can you add all dev files in your .npmignore so when published, we are not stuck with files like .babelrc which impacts our building process?

It's probably should only contain the dist/ folder and package.json and some readme, but nothing like .??* nor the src/ folder.

Also, if your package is also compatible with react@15, would appreciate if the peerDependencies be adjusted so too. Right now, we are trying to use the 2.2.3 because it's for our project... or just publish a tag react-15 would be appreciated.

Thanks,

Disabled is not working

<Toggle on={<h5>Enabled</h5>} off={<h5>Disabled</h5>} size="xs" offstyle="danger" active={this.state.status} onClick={this.statusChangeSwitch.bind(this)} disabled />

Disable the toggle is not working

babel transform class properties plugin is not found

./~/react-bootstrap-toggle/dist/index.js
Module build failed: ReferenceError: Unknown plugin "transform-class-properties" specified in "/storage/work/frontend/node_modules/react-bootstrap-toggle/.babelrc" at 0, attempted to resolve relative to "/storage/work/frontend/node_modules/react-bootstrap-toggle"
    at /storage/work/frontend/node_modules/babel-core/lib/transformation/file/options/option-manager.js:180:17
    at Array.map (native)
    at Function.normalisePlugins (/storage/work/frontend/node_modules/babel-core/lib/transformation/file/options/option-manager.js:158:20)
    at OptionManager.mergeOptions (/storage/work/frontend/node_modules/babel-core/lib/transformation/file/options/option-manager.js:234:36)
    at OptionManager.init (/storage/work/frontend/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
    at File.initOptions (/storage/work/frontend/node_modules/babel-core/lib/transformation/file/index.js:212:65)
    at new File (/storage/work/frontend/node_modules/babel-core/lib/transformation/file/index.js:135:24)
    at Pipeline.transform (/storage/work/frontend/node_modules/babel-core/lib/transformation/pipeline.js:46:16)
    at transpile (/storage/work/frontend/node_modules/babel-loader/lib/index.js:46:20)
    at Object.module.exports (/storage/work/frontend/node_modules/babel-loader/lib/index.js:163:20)
 @ ./app/components/App.js 13:13-46
 @ ./app/index.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./app/index.js

PropTypes warnings

I'm getting a series of React warnings from this library:

Warning: You are manually calling a React.PropTypes validation function for the `onstyle` prop on `ReactBootstrapToggle`. This is deprecated and will not work in production with the next major version. You may be seeing this warning due to a third-party PropTypes library. See https://fb.me/react-warning-dont-call-proptypes for details.

(One for each of the PropTypes defined in the lib)
I can't tell why though, it appears your use of PropTypes should still be valid..

react v16

hello,
npm complains because react-bootstrap-toggle demands a 15.* version of react as a peer dependency.
as far as i can see it works fine with v16.2, could to update the peer dependency requirements in the package.json?

thx!

onClick callback

Can the onClick callback pass back more than just the new value?
Like the event, id, data value?

The reason for this would be code reusability ... can reuse the same toggle method to set the state for multiple Toggle's on the same component.

My current example works for all my onChange's except Toggle's 'onClick'
onChange = (event) => { this.setState({ [event.target.attributes["data-config-key"].value]: event.target.value }) }

Event would be best in my opinion because the user can get whatever information they want from the dom if they need it.

Thanks,
Matthew Struensee

Any disable api?

The disable props on the button is kinda buggy.
Any other solution?

Warning: Accessing PropTypes via the main React package is deprecated

The compiled sources use PropTypes from react library directly and React.PropTypes is deprecated as of React v15.5. We have to use the prop-types library instead.

var _react = require('react');
var eitherStringOrInteger = _react.PropTypes.oneOfType([_react.PropTypes.string, _react.PropTypes.number]);

broken npm package

hi,
our CI picked up the latest version (2.2.6, published 14h ago) and it seems to be broken. thats the content of the downloaded package:
image

after switching to 2.2.5 it works again

Discrepancies with CSS button sizes classes

The component appears to use the classes btn-xs, btn-sm, btn-lg, which I think are the bootstrap3 classes, but the docs and the included bootstrap2-toggle css file references bootstrap2, which use the old classes (btn-large, btn-small, btn-mini). Is there a good way to still use the bootstrap2 classes?

Version 1.2.15 doesn't compile lib

Version 1.2.15, which updated peerDependencies, seems to install fine, without warning:

$ npm install react react-dom [email protected]
├── [email protected] 
├── [email protected] 
└── [email protected] 

But the src doesn't get compiled into lib:

$ ls node_modules/react-bootstrap-toggle/
gulpfile.js  package.json readme.md    src

Running npm install a second time still won't compile it, so it doesn't seem to be npm/npm#10727

Version 1.2.14 does give warnings:

$ npm install react react-dom [email protected]
├─┬ UNMET PEER DEPENDENCY [email protected]
│ ├─┬ [email protected] 
│ │ ├── [email protected] 
│ │ ├── [email protected] 
│ │ ├─┬ [email protected] 
│ │ │ ├─┬ [email protected] 
│ │ │ │ ├─┬ [email protected] 
│ │ │ │ │ └── [email protected] 
│ │ │ │ └── [email protected] 
│ │ │ └── [email protected] 
│ │ ├─┬ [email protected] 
│ │ │ └── [email protected] 
│ │ └── [email protected] 
│ ├─┬ [email protected] 
│ │ └── [email protected] 
│ └── [email protected] 
├── [email protected] 
└── UNMET PEER DEPENDENCY [email protected]

But it gets compiled correctly:

$ ls node_modules/react-bootstrap-toggle/
gulpfile.js  lib          package.json readme.md

Node version is v0.12.3 and npm is 3.7.3.

Warning about invalid prop in prop type

I'm getting some warnings about using a type number in width and height props (documentation says these are integer props).
Warning: Failed prop type: Invalid prop heightof typenumbersupplied toReactBootstrapToggle, expected string``
It seems that these props are having check as string type.
I'm currently using [email protected]

Jest infinite loop

Get a infinite render loop when using jest for testing if I use this component:

[Invariant Violation: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.]

To work around for the moment, I remove the this.setDimensions(); from componentDidUpdate, but not sure if this is the correct fix as I dont know the Toggle code, just verifying if this seems to be an ok fix by the author?

How to run unit tests using Mocha and Chai

Greetings,
When I try to unit test the component using Mocha and Chai I get a strange reference error "getComputedStyle is not defined". Is there a way to solve this?

Thank you very much

deprecations in react 15.5.0

I have warning
Warning: Accessing PropTypes via the main React package is deprecated. Use the prop-types package from npm instead.

It's about https://facebook.github.io/react/blog/2017/04/07/react-v15.5.0.html

It seams you use deprecated PropTypes
https://github.com/arnthor3/react-bootstrap-toggle/blob/master/src/react-bootstrap-toggle.jsx#L1

I have this error from dist/react-bootstrap-toggle.js:27
var eitherStringOrInteger = _react.PropTypes.oneOfType([_react.PropTypes.string, _react.PropTypes.number]);

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.