# react-native-form-focus-management

> Tools to help managing focus across a set of inputs in react-native.

Latest version **0.0.1** (published 2020-04-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-native-form-focus-management
pnpm add react-native-form-focus-management
yarn add react-native-form-focus-management
bun add react-native-form-focus-management
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2020-04-21 |
| First published | 2020-04-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.0.0 |
| Dependencies | 0 |
| Unpacked size | 27.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Sector Labs |
| Maintainers | sectorlabs |

## Links

- npm: https://www.npmjs.com/package/react-native-form-focus-management
- Repository: https://github.com/SectorLabs/react-native-form-focus-management
- Homepage: https://github.com/SectorLabs/react-native-form-focus-management#readme
- Issues: https://github.com/SectorLabs/react-native-form-focus-management/issues
- npm.io page: https://npm.io/package/react-native-form-focus-management

## Recent versions

- 0.0.1 (latest) — 2020-04-21

## README

# react-native-form-focus-management

[![License](https://img.shields.io/:license-mit-blue.svg)](http://doge.mit-license.org)
![test](https://github.com/SectorLabs/react-native-form-focus-management/workflows/test/badge.svg)
[![Strict TypeScript Checked](https://badgen.net/badge/TS/Strict "Strict TypeScript Checked")](https://www.typescriptlang.org)

Did you ever have to...

* Automatically focus the first input
* Submit the form when editing finishes on the last form
* Move focus to the next input when the `next` button is tapped
* De-focus all inputs

Then you know how much of a painful experience this is when working with a list or group of inputs in a form. react-native provides no existing abstraction to manage focus when working with multiple inputs at once.

`react-native-form-focus-management` gives you a set of tools to make this less painful. Less hacking with `refs` and a convenient way of manging focus in a form.

```tsx
import { TextInput } from "react-native";

import { FocusableGroup } from "react-native-form-focus-management";

const MyForm = () => (
    <FocusableGroup
        onMount={context => {
            if (!context.isAnyFocused()) {
                // focus first input on mount
                context.focusables[0].focus();
            }

            // nah, changed my mind
            context.blurAll();
        }}
        onChildFocus={({ focusable, index }, context) => {
            console.log(`input at index ${index} got focus`);
        }}
        onChildBlur={({ focusable, index }, context) => {
            console.log(`input at index ${index} lost focus`);
        }}
        onChildSubmitEditing={({ focusable, index }, context) => {
            console.log(`input at index ${index} submitted`);

            // move focus to next
            context.focusables[index + 1].focus();
        }}
        onChildEndEditing={({ focusable, index }, context) => {
            console.log(`input at index ${index} finished edited`);
        }}
    >
        <TextInput />
        <TextInput />
        <TextInput />
    </FocusableGroup>
);
```

## Other tools

### `AutoManagedFormFocus`

`AutoManagedFormFocus` sits on top of `FocusableGroup` and implements some of the most common focus-related work in a form:

* Let the user navigate to the next field using the `returnKey` on the keyboard.
* Let the user submit the form using the `returnkey` on the keyboard.
* Automatically focus the first input.

```tsx
import { TextInput } from "react-native";

import { AutoManagedFormFocus } from "react-native-form-focus-management";


const MyForm = () => {
    const [hasValidationErrors, setHasValidationErrors] = React.useState(false);

    const onKeyboardSubmit = React.useCallback(() => {
        console.log("user submitted form using keyboard!");
    }, []);

    return (
        <AutoManagedFormFocus
            nextReturnKeyType="next"
            submitReturnKeyType="go"
            doneReturnKeyType="done"
            isSubmittable={hasValidationErrors}
            onSubmit={onKeyboardSubmit}
        >
            <TextInput />
            <TextInput />
            <TextInput />
            <TextInput />
        </AutoManagedFormFocus>
    );
};
```

---
_Source: https://npm.io/package/react-native-form-focus-management · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
