0.3.0 • Published 9 months ago

freeform-input v0.3.0

Weekly downloads
1
License
MIT
Repository
-
Last release
9 months ago

React Freeform Input

A controlled <input> posing as an uncontrolled input.

Install

$ yarn add freeform-input

Purpose

React form controls fall in two categories: controlled and uncontrolled. The exact details are well documented elsewhere, but it doesn't matter, because the dichotomy is a lie. You don't have to choose! There exists a perfect middleground tradeoff, the Hybrid Controllable Input pattern.

An HCI behaves like a typical controlled input until it gains focus, then it enters a freeform edit mode. Once the user is finished making changes, a single change event is emitted and it snaps back to a declarative controlled mode.

This is particularly useful with a centralized state management library like Redux. You can sync your input state without needing a dispatch on every keystroke, state changes won't wipe out your user's changes, and intermediate edits don't violate validation rules.

API

This library is functionally identical to <input> save for one exception: it overrides onChange:

const [inputState, updateState] = useState('input value')

<FreeformInput
  value={inputState}
  onChange={updateState}
/>

The handler emits a value, not an event. This is due to a limitation in React where they deviate from browser behavior. React doesn't have a real onChange event.

0.3.0

9 months ago

0.2.0

3 years ago

0.1.0

3 years ago