1.0.9 • Published 5 years ago

react-better-inputs v1.0.9

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

React Better Inputs

About

React Better Inputs is an enhanced implementation of DOM inputs for React.

Navigation

Components

Input

Alternative implementation of \ component.

// Uncontrolled input.
<Input value={data.email}/>

ℹ️ Every \ prop is compatible with this component.

ℹ️ Below is a list of parameters that either are specific to this component, or that have a different implementation from the base component.

ParameterTypeRequireddefaultDescription
onChangeFunctiontrue-Change Handler. (1)
tagstring-'input''textarea' (with autosize = true)A custom HTML tag to enforce (example 'textarea').
autosizeboolean-falseLet textarea inputs sync their height automatically with the content inside.
preventPostComputingboolean-falsePrevent post computing on update (such as reseting caret position).
keepOverflowboolean-falseIf content overflows maximum value, it will not be automatically cropped.

(1) onChange implementation is slightly different from the one in the DOM. First, it runs asynchronously, for better sync with your modifications in the DOM. Thus, postComputing operation (such as ensuring correct caret position, which can jump due to React implementation) will only occur once the value successfully updated.

Note it is your duty to ensure that any async operation remains as short as possible, to avoid some lags on user side. Async implementation is only useful to enforce synchronization for short operation that can take a bit longer to fulfill. Avoid operations longer than 50ms (such as network calls).

onChange can be a classic sync function.

Other point of this implementation is that it passes some different arguments to your onChange() handler. Instead of receiving an event, you receive the following arguments:

ArgumentTypeDescription
valuestring or numberEquivalent to evt.target.value .
optionsobjectSome useful information.
options.eventEventThe full evt object.
options.overflowbooleanCustom flag to indicate value was updated above the maximum limit, before being capped.

Note any custom onChange function should return a string, with an updated value.

const onChange = async (value, options) => {
  // Do some stuff.
  
  return newValue;
};

ContentEditable

Advanced contenteditable input component. React has poor contenteditable handling (in regard to the input implementation). This component allows contenteditable to behave almost like a regular input, while handling complex child tree beneath (which is the goal of a contenteditable input).

<ContentEditable
  maxLength={300}
  onChange={this.onChange}
  placeholder={'Enter your text.'}
>
  <Label>selector:</Label>my <span style={{color: 'blue'}}>text</span>
</ContentEditable>
ParameterTypeRequireddefaultDescription
onChangeFunctiontrue-Change Handler. (1)
placeholderHTMLElement--placeholder. (2)
tagstring-'div'A custom HTML tag to enforce.
placeholderCssstring--Custom style for the placeholder. (3)
preventPostComputingboolean-falsePrevent post computing on update (such as reseting caret position).
linearboolean-falseMake the input linear (non linear input behaves like textarea component).
selectionobject--Mount the component with an inner selection range.
selection.selectionStartnumber--Range start.
selection.selectionEndnumber--Range end.
keepOverflowboolean-falseIf content overflows maximum value, it will not be automatically capped.

(1) Receives a value as a parameter and isn't expected to return anything. It can be async, in which case postComputing will be postponed until value gets updated.

const onChange = (value, options) => {
  // Do stuff.
};
ArgumentTypeDescription
valuestring or numberEquivalent to evt.target.value .
optionsobjectSome useful information.
options.eventEventThe full evt object.
options.overflowbooleanCustom flag to indicate value was updated above the maximum limit, before being capped.

(2) Placeholder can be a HTML element (div, etc).

(3) Not that unlike ::placeholder selector on input elements, a contenteditable placeholder has almost every customization options available, like a regular DOM element.

Copyright

2020 Kushuh - MIT license

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago