# useadapter

> A React hook to help abstract user input.

Latest version **1.1.1** (published 2022-08-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install useadapter
pnpm add useadapter
yarn add useadapter
bun add useadapter
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2022-08-11 |
| First published | 2022-06-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 232.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ross Esmond |
| Maintainers | ross-esmond |
| Keywords | react, hooks, forms |

## Links

- npm: https://www.npmjs.com/package/useadapter
- Repository: https://github.com/Ross-Esmond/useAdapter
- Homepage: https://github.com/Ross-Esmond/useAdapter#readme
- Issues: https://github.com/Ross-Esmond/useAdapter/issues
- npm.io page: https://npm.io/package/useadapter

## Dependencies (1)

- [fast-deep-equal](https://npm.io/package/fast-deep-equal.md) ^3.1.3

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.1.1 (latest) — 2022-08-11
- 1.1.0 — 2022-06-05
- 1.0.0 — 2022-06-05

## README

# useAdapter

A React hook to help abstract user input.

[![Build status badge](https://img.shields.io/circleci/build/gh/Ross-Esmond/useAdapter)](https://app.circleci.com/pipelines/github/Ross-Esmond/useAdapter?branch=main&filter=all)
[![npm](https://img.shields.io/npm/v/useadapter)](https://www.npmjs.com/package/useadapter)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2FRoss-Esmond%2FuseAdapter%2Fmain)](https://dashboard.stryker-mutator.io/reports/github.com/Ross-Esmond/useAdapter/main)
[![npm bundle size](https://img.shields.io/bundlephobia/minzip/useadapter)](https://bundlephobia.com/package/useadapter)

## Install
With npm.
```bash
npm install useadapter
```
With yarn.
```bash
yarn add useadapter
```

## Usage
```JSX
import useAdapter from 'useadapter'
function PhoneNumberField({ value, onChange }) {
    const [getInput, setInput] = useAdapter(
        // display, turn 8175553356 into 817-555-3356
        digits => {
            if (digits?.length !== 10) return ''
            return `${digits.substr(0,3)}-${digits.substr(3, 3)}-${digits.substr(6, 4)}`
        },
        // parser, turn any formatted phone number like 817-555-3356 into 8175553356
        input => {
            const digits = input.match(/\d+/g).join('')
            if (digits.length !== 10) return null
            return digits
        }
    )

    return (
        <input type="tel"
            // getInput runs the argument through the display function above
            value={getInput(value)}
            // setInput runs the argument through the parser function above
            onChange={e => onChange(setInput(e.target.value))}
        />
    )
}
```
The `setInput` function does three things. First, it stores `e.target.value` in
React state. Next, it runs `e.target.value` through your custom parser function
and stores the result in React state. And finally, it returns the result of the
parse so that you may pass it directly to a callback or a conventional
`setState`.

On the first render, `getInput` will simply run `value` through your custom
display function and return the result. On subsequent renders, however,
`getInput` will check if `value` is equal to the result of parsing the current
input. If the value is equal to parsing the input (which was stored by
`setInput`), `getInput` will simply return the stored input. Equality is checked
by the `fast-deep-equal` npm package, such that objects and arrays with
equivilant structures and equal values are considered equal.

This system allows `PhoneNumberField` to abstract the input value for any
higher-level component without disallowing the user from typing a phone number
in whatever format they see fit. Normally, if a parent component only stores the
digits of a phone number, the user would be incapable of typing formatting
characters like `-` or spaces, as React idioms dictate that the state be passed
as the current input value,
```JSX
<input type="tel" value={number} onChange={ev => setNumber(ev.target.value)} />
```
Conversely, `useAdapter` allows developers to parse values before hoisting them
into React state while still retaining the users precise input as they type.

Of course, you could skip passing the value back to the input, but then your
React app would not be able to programmatically update the phone number if
necessary, as with a reset button.
```JSX
<button onClick={() => onChange(previousNumber)}>Reset</button>
```
In the case of a reset, `getInput` would detect that the `value` has changed,
which would trigger the use of the display function to generate the new input
value.

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