2.0.2 • Published 3 years ago

react-focuser v2.0.2

Weekly downloads
1
License
UNLICENSED
Repository
github
Last release
3 years ago

react-focuser

npm

Focus switcher for React and Preact components.

Installation

yarn add react-focuser

Demo

Demo / Source

Usage

Select React or Preact somewhere in root of your app

import 'react-focuser/useReact'
// or 
import 'react-focuser/usePreact'

Make inputs and forms

import React, { Fragment } from 'react'
import { FocusableProvider, Focusable } from 'react-focuser'

const Input = props => (
  <Focusable>
    { ({ focusableRef, focusNextInput }) => (
      <input
        {...props}
        ref={focusableRef}
        onKeyPress={(ev) => {
          if (ev.key === 'Enter') focusNextInput()
        }}
      />
    ) }
  </Focusable>
)

export default () => (
  <FocusableProvider>
    <Fragment>
      <Input placeholder="#1 Focus next input on Enter" />
      <Input placeholder="#2 Focus next input on Enter" />
      <Input placeholder="#3" />
    </Fragment>
  </FocusableProvider>
)

API

Object passed to Focusable children

  • focusableRef
  • focusInput(index)
  • focusNextInput()
  • index - override input index to change focus order
2.0.2

3 years ago

2.0.1

4 years ago

2.0.0

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago