Code Monkey home page Code Monkey logo

react-input-switch's Introduction

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

react-input-switch's People

Contributors

strml avatar wangzuo avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

react-input-switch's Issues

ReferenceError: window is not defined

When using with Next.js, in a page component, using server-side rendering, the following error occurs:
ReferenceError: window is not defined

image

I'm using this function to render the Switch:

const renderValue = (val, key) => {
    console.log('@renderValue', val, key, typeof val)
    switch (typeof val) {
      case 'boolean': {
        return (
          <Switch 
            onChange={e => console.log(e.target.value ? 'Sim' : "Nao")}
            value={Boolean(val)}
            name={key}
          />
        )
      }

      case 'string': {
        return (
          <textarea rows={5} className='w-full resize-none border border-gray-300 p-2 rounded-md mt-3' type="text" defaultValue={val} placeholder={key} name={key} />
        )
      }

      case 'number': {
        return (
          <input
            type="number"
            defaultValue={Number(val)}
            placeholder={key}
            name={key}
          />
        )
      }

      case 'object': {
        return (
          <div className='flex flex-col items-start justify-start w-full'>
            {Object.keys(val).map(nKey => renderValue(nKey, val[nKey]))}
          </div>
        )
      }

      default: {
        return <div>{val}</div>
      }
    }
  }

Thanks in advance!

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.