TrackedInputCheckbox
Generates a <input type="checkbox">
Element wrapped in a InputContext.
Checkboxes are tracked with this strategy:
- We monitor the
checked
attribute instead of thevalue
attribute, as the latter never changes. - We don't listen to
onBlur
events, but trackonChange
events instead. - The identifier is optional, because we attempt to assign one by reading the
value
attribute. - When
trackValues
is set we generate an InputValueContext with0
or1
as value, representing the checked state.
TrackedInputCheckbox: (props: {
objectiv?: {
id?: string,
normalizeId?: boolean,
trackValue?: boolean,
stateless?: boolean,
eventHandler?: 'onBlur' | 'onChange' | 'onClick'
}
}) => <input type="checkbox">
objectiv
prop attributes
type | default value | ||
---|---|---|---|
optional | id | string | inferred from native id , name or value |
optional | normalizeId | boolean | true |
optional | trackValue | boolean | false |
optional | stateless | boolean | false |
optional | eventHandler | 'onBlur' | 'onChange' | 'onClick' | onChange |
info
- When the
id
attribute is omitted, we attempt to infer it from the elements'id
,name
orvalue
native props.
If that fails, an error will be logged to the Developer Console and the original Component is rendered.
Returns
<input type="checkbox">
Automatic Events
- InputChangeEvent when
eventHandler
triggers.
Global Contexts
- InputValueContext with the
checked
value, iftrackValue
is set totrue
.
Usage example
track selection
import { TrackedInputCheckbox } from '@objectiv/tracker-react';
<div>
<main>
<TrackedInputCheckbox value={'a'} />
<TrackedInputCheckbox value={'b'} />
<TrackedInputCheckbox value={'c'} />
</main>
</div>
track also the selected value
import { TrackedInputCheckbox } from '@objectiv/tracker-react';
<div>
<main>
<TrackedInputCheckbox value={'a'} defaultChecked objectiv={{ trackValue: true }} />
<TrackedInputCheckbox value={'b'} objectiv={{ trackValue: true }} />
<TrackedInputCheckbox value={'c'} objectiv={{ trackValue: true }} />
</main>
</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 { TrackedInputCheckbox } from '@objectiv/tracker-react';
<div>
<TrackedInputCheckbox value={'some internal id'} objectiv={{ normalizeId: false }} />
</div>
Did you know ?
TrackedInputCheckbox
internally uses TrackedInputContext.