@render-props/choices
v0.1.19
Published
A state container which provides an interface for making selections from a group of choices. The `Choices` component itself is a context provider which can be used with the `Choice` and `ChoicesConsumer` components for deep-tree selections. It does not ha
Downloads
17
Maintainers
Readme
Choices
A state container which provides an interface for making selections from
a group of choices. The Choices
component itself is a context provider which
can be used with the Choice
and ChoicesConsumer
components for deep-tree
selections. It does not have to be used with these components, however.
Installation
yarn add @render-props/choices
or npm i @render-props/choices
Contents
Choices
- This main component which must be used
Choice
- This is an optional convenience component for deep-tree selections. It
is a context consumer of
Choices
. It provides the following props to its child component:{select, deselect, toggle, isSelected}
- This is an optional convenience component for deep-tree selections. It
is a context consumer of
ChoicesConsumer
- This is the Consumer for the
Choices
Provider which provides an object as its value with this shape:{selections, choices, select, deselect, toggle, setSelections, clearSelections, isSelected, addChoice, deleteChoice, setChoices, clearChoices, isChoice}
- This is the Consumer for the
Usage
import Choices, {Choice} from '@render-props/choices'
const FavoritePets = props => (
<Choices
initialChoices={new Set(['cat', 'dog', 'turtle'])}
initialSelections={new Set(['cat'])}
minChoices={1}
minSelections={1}
maxSelections={2}
onBoundMaxSelections={
function ({selections, select, deselect}) {
selections = Array.from(selections)
deselect(selections.shift())
select(selections.pop())
}
}
>
{PetsControl}
</Choices>
)
const PetsControl = ({
addChoice,
deleteChoice,
setChoices,
clearChoices,
isChoice,
select,
deselect,
setSelections,
clearSelections,
isSelected,
selections,
choices
}) => (
<div style={{borderWidth: 1}}>
<span>
Number of favorites: {selections.size}
</span>
{Array.from(choices).map(pet => (
<Choice key={pet} value={pet}>
{function ({select, deselect, toggle, isSelected}) {
return (
<button
onClick={toggle}
style={
isSelected
? {backgroundColor: 'green'}
: {backgroundColor: 'grey'}
}
>
{pet}
</button>
)
}}
</Choice>
))}
</div>
)
Choices
Props
initialChoices {array|set}
- the initial choices that selections may be made from
initialSelections {array|set}
- the initial selections. These must also be members of
@initialChoices
- the initial selections. These must also be members of
minChoices {integer}
- the minimum number of
choices
that should remain in the state
- the minimum number of
maxChoices {integer}
- the maximum number of
choices
that can be added to the state
- the maximum number of
onBoundMinChoices {function ({choices <array|set>, addChoice <function>, deleteChoice <function>})}
- called when the minimum bound of
choices
has been reached. Callback should include one argument for object:{choices, addChoice, deleteChoice}
wherechoices
is the set of items which triggered the bounding error
- called when the minimum bound of
onBoundMaxChoices {function ({choices <array|set>, addChoice <function>, deleteChoice <function>})}
- called when the maximum bound of
choices
has been reached. Callback should include one argument for object:{choices, addChoice, deleteChoice}
wherechoices
is the set of items which triggered the bounding error
- called when the maximum bound of
onChoicesChange {function (items <array|set>)}
- called whenever an item is added or removed from the set of
choices
. Receives one argument forchoices
which reflects the latest state
- called whenever an item is added or removed from the set of
onAddChoice {function (items <array|set>)}
- called whenever an item is added to the
choices
set. Receives one argument forchoices
which reflects the latest state.
- called whenever an item is added to the
onDeleteChoice {function (items <array|set>)}
- called whenever an item is removed from the
choices
set. Receives one argument forchoices
which reflects the latest state
- called whenever an item is removed from the
minSelections {integer}
- the minimum number of
selections
that should remain in the state
- the minimum number of
maxSelections {integer}
- the maximum number of
selections
that can be added to the state
- the maximum number of
onBoundMinSelections {function ({selections <array|set>, select <function>, deselect <function>})}
- called when the minimum bound of
selections
has been reached. Callback should include one argument for object:{selections, select, deselect}
whereselections
is the set of items which triggered the bounding error
- called when the minimum bound of
onBoundMaxSelections {function ({selections <array|set>, select <function>, deselect <function>})}
- called when the maximum bound of
selections
has been reached. Callback should include one argument for object:{selections, select, deselect}
whereselections
is the set of items which triggered the bounding error
- called when the maximum bound of
onChange {function (items <array|set>)}
- called whenever an item is added or removed from the set of
selections
. Receives one argument forselections
which reflects the latest state
- called whenever an item is added or removed from the set of
onSelect {function (items <array|set>)}
- called whenever an item is added to the
selections
set. Receives one argument forselections
which reflects the latest state.
- called whenever an item is added to the
onDeselect {function (items <array|set>)}
- called whenever an item is removed from the
selections
set. Receives one argument forselections
which reflects the latest state
- called whenever an item is removed from the
Render Props
Methods
select
(...items <any choice>)
- adds one or several selections if they are available in
choices
- adds one or several selections if they are available in
deselect
(...items <any selection>)
- removes one or several
selections
- removes one or several
toggle
(item <any choice>)
- removes an item if it is in selections, otherwise adds it to selections
setSelections
(items <array|set>)
- sets
selections
to whatever the value of@items
is
- sets
clearSelections
()
- clears all selections
isSelected
(item <any>)
- returns
true
if@item
is inselections
- returns
addChoice
(...items <any choice>)
- adds one or several
choices
- adds one or several
deleteChoice
(...items <any selection>)
- removes one or several
choices
- removes one or several
setChoices
(items <array|set>)
- sets
choices
to whatever the value of@items
is
- sets
clearChoices
()
- clears all choices
isChoice
(item <any>)
- returns
true
if@item
is inchoices
- returns
State
selections {array|set}
- the selections currently in the state of the component. Is an
Array
ifinitialSelections
was anArray
and aSet
ifinitialSelections
was aSet
- the selections currently in the state of the component. Is an
choices {array|set}
- the choices currently in the state of the component. Is an
Array
ifinitialChoices
was anArray
and aSet
ifinitialChoices
was aSet
- the choices currently in the state of the component. Is an
Choice
Props
value
the value of the choice from the
Set
/Array
of choices in theChoices
component
Render Props
Methods
select
()
- selects this choice from
choices
- selects this choice from
deselect
()
- deselects this choice from
selections
- deselects this choice from
toggle
()
- selects if
isSelected
, otherwise deselects
- selects if
delete
()
- deletes this choice from
choices
andselections
- deletes this choice from
State
isSelected {bool>}
- returns
true
if the propvalue
provided to this component resides inChoices.selections
- returns
ChoicesConsumer
Render Props
See Choices render props