npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-autofocus

v0.2.0

Published

Simple container & HOC pair to control and auto-focus elements (for example <input> for Web or <TextInput> for React Native or <CustomElement> for both :D)

Downloads

4

Readme

React-Auto-Focus

Especially useful for ReactNative

Hi, I started to think of a simpler way of handling onSubmitEnd => focus next input logic.

However, other implementations had limitations and performance problems.

I have started to create this module which uses React Context to find and subscribe children that have been passed through a high order function to register them to autofocus context.

Here is the initial documentation for the initial version (probably won't work, I've not tested yet)

Example in React

import React from 'react';
import { withAutoFocus, AutoFocusContainer } from 'react-autofocus';
const handleKeyPress = (next, prev) => (e) => {
    if (e.key === 'Enter') {
        if (e.shiftKey) {
            prev();
        } else {
            next();
        }
    }
};
const Input = ({ next, prev, ...props }) => ( 
    <input {...props} onKeyPress={ handleKeyPress(next, prev) }/>
);
const CustomInput = withAutoFocus('focus', ['next'], ['prev'])(Input);
const Button = (props) => <button { ...props } />;
const CustomButton = withAutoFocus('click')(Button);
const Form = (props) => ( // prolly make it stateful in real life
    <AutoFocusContainer>
        <div>
            <label> Your Name: </label>
            <CustomInput type={"text"} autoFocusOrder={1} />
            <label> Your Surname: </label>
            <CustomInput type={"text"} autoFocusOrder={2} /> 
        </div>
        <label>
          Your Favorite Song
        </label>
        <CustomInput type={"text"} autoFocusOrder={3} />
        <CustomButton autoFocusOrder={ 100 }>
            Submit Survey
        </CustomButton>
    </AutoFocusContainer>
);
export default Form;

Example in React Native

import React from 'react';
import { View, Text, TextInput, TouchableOpacity } from 'react-native';
import { withAutoFocus, AutoFocusContainer } from 'react-autofocus';

const CustomInput = withAutoFocus('focus', ['onSubmitEditing'])(TextInput);

const CustomButton = withAutoFocus((elem) => elem.props.onPress)(TouchableOpacity); 

const Form = (props) => ( // prolly make it stateful in real life
      <AutoFocusContainer>
          <View>
              <Text> Your Name: </Text>
              <CustomInput autoFocusOrder={1} />
              <Text> Your Surname: </Text>
              <CustomInput autoFocusOrder={2} /> 
          </View>
          <Text>
            Your Favorite Song
          </Text>
          <CustomInput autoFocusOrder={3} />
          <CustomButton autoFocusOrder={ 100 }>
              Submit Survey
          </CustomButton>
      </AutoFocusContainer>
);
export default Form;

Nesting Also Works BTW

import React from 'react';
import { withAutoFocus, AutoFocusContainer } from 'react-autofocus';
import { CustomInput, CustomButton, CustomLabel, CustomView } from './customs';

const Form = (props) => (
  <AutoFocusContainer>
    <CustomView>
      <CustomLabel>
        Your Name:
      </CustomLabel>
      <CustomInput autoFocusOrder={ 1 }/>
      <CustomLabel>
        Your Surname
      </CustomLabel>
      <CustomInput autoFocusOrder={ 2 }/>
    </CustomView>
    <CustomLabel>
      Your Favorite Song
    </CustomLabel>
    <CustomInput autoFocusOrder={ 3 } />
  </AutoFocusContainer>
);

// Assume that's the Form defined in above examples without submit button, however.

const MultiForms = (props) => ( // bulk 
  <AutoFocusContainer>
    <Form autoFocusOrder={ 1 } />
    <Form autoFocusOrder={ 2 } />
    <Form autoFocusOrder={ 3 } />
    <CustomButton autoFocusOrder={ 100 }>
      Submit All Surveys
    </CustomButton>
  </AutoFocusContainer>
);
export default MultiForms;

withAutoFocus(focusExtractor, nextPropNames, prevPropNames)(Component);

A HOC to use a component with autofocus to it.

Focus extractor can either be a string, (focus = ref[focusExtractor])

or it can be a function, (focus = focusExtractor(ref))

or it can be null (focus = null),

(in that case, focusing into this element would result in skipping it and either focusing on next/previous one)

AutoFocusContainer

Has props next, prev, children, orderComperator = (a, b) => a - b, focusDefaultDirection = 'FORWARD'

No need to provide anything, actually but if you want something like onSubmit behaviour without any buttons, you can add a function as the next prop, that'll be executed when the last focusable element calls its next method. (ex: pressed enter in the last input)

prev is similar to next but in reverse direction, run when previous function is triggered in first focusable element.

children is React Children :D

orderComperator = (a, b) => a - b compare function of AVL tree for ordering focusable components inside.

AutoFocusableComponent = withAutoFocus()(Component)

AutoFocusableComponent expects a number autoFocusOrder to decide in which order the focusing should be handled.

These numbers must be unique within each AutoFocusContainer.

Limit is it should be between MIN_INT_SIZE/1000 and MAX_INT_SIZE/1000

In JS that means between -2^43 and 2^43 since all numbers are floating points.

So, feel free to use up to 12digits :D

There's an AVL tree in autoFocusContainer, handling the order with respect to these numbers.

If you do not supply autoFocusOrder, it will be experimentally guessed. (That might not be what you want.)