TrackedInput
Generates a <input>
Element wrapped in a InputContext.
Special cases
As not all inputs behave the same way, we have ad-hoc components for specific ones:
<input type="radio">
: TrackedInputRadio<input type="checkbox">
: TrackedInputCheckbox<select>
: TrackedSelect
TrackedInput: (props: {
objectiv?: {
id?: string,
normalizeId?: boolean,
trackValue?: boolean,
stateless?: boolean,
eventHandler?: 'onBlur' | 'onChange' | 'onClick'
}
}) => <input>
objectiv
prop attributes
type | default value | ||
---|---|---|---|
optional | id | string | inferred from native id |
optional | normalizeId | boolean | true |
optional | trackValue | boolean | false |
optional | stateless | boolean | false |
optional | eventHandler | 'onBlur' | 'onChange' | 'onClick' | onBlur |
info
- When the
id
attribute is omitted, we attempt to infer it from the elements'id
native prop.
If that fails, an error will be logged to the Developer Console and the original Component is rendered.
Returns
ReactElement
Automatic Events
- InputChangeEvent when
eventHandler
triggers. Tracking normally occurs only ifvalue
changed, unless stateless has been set totrue
.
Global Contexts
- InputValueContext with the changed
value
, iftrackValue
is set totrue
.
Usage example
import { TrackedInput } from '@objectiv/tracker-react';
<div>
<header>
<label for={'search'}>Search:</label>
<TrackedInput id={'search'} type={'search'} objectiv={{ trackValue: true }} />
</header>
<main>
<label for={'email'}>Email:</label>
<TrackedInput id={'email'} type={'email'} placeholder={'email address'} />
</main>
<footer>
...
</footer>
</div>
By default, all Tracked Elements automatically normalize their Context identifiers to a kebab-cased format.
This can be disabled via the normalizeId
option:
import { TrackedInput } from '@objectiv/tracker-react';
<div>
<TrackedInput id={'Email Address'} type={'email'} objectiv={{ normalizeId: false }} />
</div>
Did you know ?
TrackedInput
internally uses TrackedInputContext.