6.0.3 • Published 3 years ago

@rexlabs-spicerhaart/text-input v6.0.3

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
3 years ago

TextInput - Heidi Component

TextInput forms the basis of almost all text-based inputs. It is designed to be extremely flexible and extensible, using a pattern I'm calling "adaptive composition".

The core input itself is very simple and has no internal logic. All behaviour is composed into the input via Higher Order Components (HoCs), which themselves adapt to the props passed at the callsite using recompose's branch. Each HoC strongly adheres to the Single Responsibility Principle, keeping all behaviour encapsulated and the core input itself ignorant of these concerns. This enables a new API: uncontrolled, which when applied, causes certain HoCs to switch to stateful versions which manage their own handlers and state, all from a single export.

TextArea is provided as a named export. Additional preset inputs (email, password, search) should be defined in your application using recompose's withProps HoC (e.g: export const EmailInput = withProps({type: EMAIL})(TextInput)). This affords us the best balance between flexibility and ease-of-use.

Custom API

PropDescription
typeSets input type and mobile keyboard
nameUsed as id and name
prefixArbitrary content before the input
suffixArbitrary content after the input
onEnterCalled when uses presses 'Enter' within the input
onTabCalled when uses presses 'Tab' within the input
onBackspaceCalled when uses presses 'Backspace' within the input
setRefReturns the input DOM node
ContainerComponent to render around the input
InputComponent to render as the input
containerPropsProps passed to the Container
charLimitAdds a character limit and displays it below input
showCharLimitControls displaying of charLimit
HoCs
uncontrolledSetting this will trigger stateful/uncontrolled versions of some HoCs
validated
validateFunction to return whether value is valid or not (only for uncontrolled)
dirtyInput has been blurred (valid status will not display until dirty)
validInput value is valid
validSuffixArbitrary content to display after input when value is valid
errorSuffixArbitrary content to display after input when value is invalid
withA11y
isFocusedInput currently has focus
shouldSelectOnFocusFocusing the input should select all text content
appLayerCurrent application UI layer (for modals and such)
layerInput UI layer - when different to appLayer, sets tabIndex to -1
initialized
autoFocusFocus input on mount
rehydrateCall onChange with input value on mount (for server-side rendered views)
masked
maskStrategy (function/regex/preset) to modify value in onChange
withHint
hintText displayed beneath input as context
shouldHideHintWithoutFocusHide hint when input is not focused
withInlineErrors
errorsArray of error messages displayed beneath input
withCosmetic
cosmeticArbitrary content to display instead of input when input is not focused

Development

Install dependencies

$ yarn

Available Commands

$ yarn start              # starts storybook, for visually testing text-input
$ yarn test               # runs all units tests
$ yarn test:watch         # runs unit tests when files change
$ yarn build              # bundles the package for production

Ongoing development and maintenance should follow the following guidelines:

  • Modifications to text-input should be extremely rare and carefully considered
  • Cross-cutting concerns between HoCs should be kept to an absolute minimum (currently only isFocused crosses HoC boundaries), and always cleaned up (omitted from props passed to text-input)
  • Setting the value prop should be considered banned outside of uncontrolled branches (reading from it is ok)
  • HoCs should export all propTypes required for their API (and these should be merged onto the composed input in index)

Legal

Copyright © 2018 Rex Software All Rights Reserved.