Skip to content

swiftcarrot/react-input-switch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

May 7, 2020
b74f5cd · May 7, 2020

History

50 Commits
Oct 28, 2019
Dec 1, 2018
Dec 1, 2018
Jun 17, 2015
Sep 7, 2019
May 15, 2019
Dec 1, 2018
May 7, 2020
May 7, 2020
May 7, 2020

Repository files navigation

react-input-switch

npm npm Build Status codecov styled with prettier

React toggle switch component

Installation

npm install react-input-switch --save
yarn add react-input-switch

Demo

https://swiftcarrot.dev/react-input-switch/

Custom styles

<Switch
  styles={{
    track: {
      backgroundColor: 'blue'
    },
    trackChecked: {
      backgroundColor: 'red'
    },
    button: {
      backgroundColor: 'yellow'
    },
    buttonChecked: {
      backgroundColor: 'blue'
    }
  }}
/>

Controlled example (with hook)

import React, { useState } from 'react';
import Switch from 'react-input-switch';

const App = () => {
  const [value, setValue] = useState(0);

  return <Switch value={value} onChange={setValue} />;
};

Custom on/off value

The default on/off value is 1/0 and default value is 1. This component will also render a hidden input (<input type="hidden"/>) with current value and the name prop.

import React, { useState } from 'react';
import Switch from 'react-input-switch';

const App = () => {
  const [value, setValue] = useState('yes');

  return <Switch on="yes" off="no" value={value} onChange={setValue} />;
};

License

MIT