Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-stepper-primitive

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-stepper-primitive

A React primitive for building a stepper component

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

react-stepper-primitive Build Status

React primitives for a "stepper" component.

So you can build this:

ReactDOM.render(
  <Stepper
    min={1}
    max={100}
    render={({
      getFormProps,
      getInputProps,
      getIncrementProps,
      getDecrementProps
    }) =>
      <form {...getFormProps()}>
        <button className='my-button' {...getDecrementProps()}>
          <img src='/assets/svg/minus.svg' />
        </button>
        <input className='my-step-input' {...getInputProps()} />
        <button className='my-button' {...getIncrementProps()}>
          <img src='/assets/svg/plus.svg' />
        </button>
      </form>}
  />,
  document.body
)

Why?

Because a stepper (minus button, input, plus button) is non-trivial. There's a lot to manage: there's a minimum and maximum. There's the input displaying the current value. There's the input allowing free-type while the user focuses the input, then interpreting the user's value once they blur it.

These primitives manage the data manipulation for you so you only have to worry about the styling.

Install

$ npm install --save react-stepper-primitive

API

<Stepper>

Props
defaultValue

number | default 0 | optional

The initial value.

onChange

function | optional

Called when the value changes, with the new value as the only argument.

value

number | optional

The value. If no value is passed in, the stepper will manage its value via its own internal state.

If value is passed in, the stepper becomes a "controlled component".

The onChange function passed in will be called whenever value changes, whether you pass it in or not.

Note: This is very similar to how normal controlled components work elsewhere in react (like <input />).

min

number | optional, no default

The value cannot go below this minimum.

max

number | optional, no default

The value cannot go above this maximum.

step

number | default 1 | optional

Every click on the increment or decrement button increases the value by step.

render

function() | required

<Stepper render={() => <div />} />

The render prop function is called with the following object:

propertycategorytypedescription
valuestatenumberThe current value of the stepper
focusedstatebooleanWhether the input is currently focused.
getFormPropsprop getterfunctionReturns the props you should apply to a form element (for submit handling)
getInputPropsprop getterfunctionReturns the props you should apply to an input element (for displaying and free-form modification of the current value)
getDecrementPropsprop getterfunctionReturns the props you should apply to a decrement button
getIncrementPropsprop getterfunctionReturns the props you should apply to an increment button
incrementsetterfunctionIncrement the value by one. Value cannot go under props.min.
decrementsetterfunctionDecrement the value by one. Value cannot go over props.max.
setValuesetterfunctionSet a new value. Value is coerced to stay between props.min and props.max.
enableReinitialize

boolean | default false | optional

Control whether the current value (if unchanged) will update to the new default if defaultValue changes.

Thanks to Kent C Dodds for formalizing the "prop getters" idea in downshift. And for the readme formatting, which I've stolen.

License

MIT © Andrew Joslin

Keywords

FAQs

Package last updated on 17 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc