API Reference

August 6, 2018 ยท View on GitHub


<Value>

The <Value> component is the most generic component exposed by react-values, and it is the base for all other components.

It takes either a value or defaultValue and an onChange handler. Depending on whether you pass it value or defaultValue it will either be "controlled" or "uncontrolled", respectively.

<Value
  value={Any|undefined}
  defaultValue={Any|undefined}
  onChange={Function}
  disabled={Boolean}
>
  {({
    value,
    set,
    clear,
    reset,
  }) => (
    ...
  )}
</Value>
PropTypeDescription
valueAnyThe value, for controlled components.
defaultValueAnyThe default value, for uncontrolled components.
onChangeFunction onChange(value)A handler that will be called whenever the current value changes.
disabledBooleanWhether the component is current disabled, ignoring state changes.
Render PropTypeDescription
valueAnyThe state's current value.
disabledBooleanWhether the component is currently disabled or not.
setFunction set(value)Sets the value to a new state.
clearFunction clear()Sets the value to undefined.
resetFunction reset()Resets the value to its initial value/defaultValue state.

<ArrayValue>

A value for an Array.

<ArrayValue
  value={Array|undefined}
  defaultValue={Array|undefined}
  onChange={Function}
  disabled={Boolean}
>
  {({
    value,
    set,
    reset,
    concat,
    fill,
    filter,
    map,
    pop,
    push,
    reverse,
    shift,
    slice,
    sort,
    splice,
    unshift,
  }) => (
    ...
  )}
</ArrayValue>
PropTypeDescription
valueAnyThe value, for controlled components.
defaultValueAnyThe default value, for uncontrolled components.
onChangeFunction onChange(value)A handler that will be called whenever the current value changes.
disabledBooleanWhether the component is current disabled, ignoring state changes.
Render PropTypeDescription
valueArrayThe current array value.
disabledBooleanWhether the component is currently disabled or not.
setFunction set(array)Sets the value to a new state.
clearFunction clear()Sets the value to an [] empty array.
resetFunction reset()Resets the value to its initial value/defaultValue state.
firstAnyThe first element in the current array value.
lastAnyThe last element in the current array value.
concatFunction concat(...values)Calls Array.concat.
fillFunction fill(value)Calls Array.fill.
filterFunction filter(callback)Calls Array.filter.
flatFunction flat(depth)Calls Array.flat.
flatMapFunction flatMap(callback)Calls Array.flatMap.
mapFunction map(callback)Calls Array.map.
popFunction pop()Calls Array.pop.
pushFunction push(...values)Calls Array.push.
reverseFunction reverse()Calls Array.reverse.
shiftFunction shift()Calls Array.shift.
sliceFunction slice(begin, end)Calls Array.slice.
sortFunction sort(comparator)Calls Array.sort.
spliceFunction splice(start, remove, ...values)Calls Array.splice.
unshiftFunction unshift(...values)Calls Array.unshift.

<BooleanValue>

A value for a Boolean.

<BooleanValue
  value={Boolean|undefined}
  defaultValue={Boolean|undefined}
  onChange={Function}
  disabled={Boolean}
>
  {({
    value,
    set,
    clear,
    reset,
    toggle,
  }) => (
    ...
  )}
</BooleanValue>
PropTypeDescription
valueAnyThe value, for controlled components.
defaultValueAnyThe default value, for uncontrolled components.
onChangeFunction onChange(value)A handler that will be called whenever the current value changes.
disabledBooleanWhether the component is current disabled, ignoring state changes.
Render PropTypeDescription
valueBooleanThe current boolean value.
disabledBooleanWhether the component is currently disabled or not.
setFunction set(boolean)Sets the value to a new state.
clearFunction clear()Sets the value to false.
resetFunction reset()Resets the value to its initial value/defaultValue state.
toggleFunction toggle()Sets the boolean to its opposite value.

<DateValue>

A value for a Date.

<DateValue
  value={Date|undefined}
  defaultValue={Date|undefined}
  onChange={Function}
  disabled={Boolean}
>
  {({
    value,
    set,
    clear,
    reset,
    date,
    hours,
    milliseconds,
    minutes,
    month,
    seconds,
    year,
    setDate,
    setHours,
    setMilliseconds,
    setMinutes,
    setMonth,
    setSeconds,
    setYear,
    incrementDate,
    incrementHours,
    incrementMilliseconds,
    incrementMinutes,
    incrementMonth,
    incrementSeconds,
    incrementYear,
    decrementDate,
    decrementFullYear,
    decrementHours,
    decrementMilliseconds,
    decrementMinutes,
    decrementMonth,
    decrementSeconds,
  }) => (
    ...
  )}
</DateValue>
PropTypeDescription
valueAnyThe value, for controlled components.
defaultValueAnyThe default value, for uncontrolled components.
onChangeFunction onChange(value)A handler that will be called whenever the current value changes.
disabledBooleanWhether the component is current disabled, ignoring state changes.
Render PropTypeDescription
valueDateThe current date value.
disabledBooleanWhether the component is currently disabled or not.
setFunction set(date)Sets the value to a date.
clearFunction clear()Sets the value to new Date().
resetFunction reset()Resets the value to its initial value/defaultValue state.
yearNumberThe current state of value.getFullYear().
monthNumberThe current state of value.getMonth().
dateNumberThe current state of value.getDate().
hoursNumberThe current state of value.getHours().
minutesNumberThe current state of value.getMinutes().
secondsNumberThe current state of value.getSeconds().
millisecondsNumberThe current state of value.getMilliseconds().
setYearFunction setYear(n)Calls Date.setFullYear.
setMonthFunction setMonth(n)Calls a bug-fixed version of Date.setMonth.
setDateFunction setDate(n)Calls Date.setDate.
setHoursFunction setHours(n)Calls Date.setHours.
setMinutesFunction setMinutes(n)Calls Date.setMinutes.
setSecondsFunction setSeconds(n)Calls Date.setSeconds.
setMillisecondsFunction setMilliseconds(n)Calls Date.setMilliseconds.
incrementYearFunction incrementYear(n = 1)Increments the value's year by n.
incrementMonthFunction incrementMonth(n = 1)Increments the value's month by n.
incrementDateFunction incrementDate(n = 1)Increments the value's date by n.
incrementHoursFunction incrementHours(n = 1)Increments the value's hours by n.
incrementMinutesFunction incrementMinutes(n = 1)Increments the value's minutes by n.
incrementSecondsFunction incrementSeconds(n = 1)Increments the value's seconds by n.
incrementMillisecondsFunction incrementMilliseconds(n = 1)Increments the value's milliseconds by n.
decrementYearFunction decrementYear(n = 1)Decrements the value's year by n.
decrementMonthFunction decrementMonth(n = 1)Decrements the value's month by n.
decrementDateFunction decrementDate(n = 1)Decrements the value's date by n.
decrementHoursFunction decrementHours(n = 1)Decrements the value's hours by n.
decrementMinutesFunction decrementMinutes(n = 1)Decrements the value's minutes by n.
decrementSecondsFunction decrementSeconds(n = 1)Decrements the value's seconds by n.
decrementMillisecondsFunction decrementMilliseconds(n = 1)Decrements the value's milliseconds by n.

<MapValue>

A value for a Map.

<MapValue
  value={Map|undefined}
  defaultValue={Map|undefined}
  onChange={Function}
  disabled={Boolean}
>
  {({
    value,
    set,
    clear,
    reset,
    unset,
    delete,
  }) => (
    ...
  )}
</MapValue>
PropTypeDescription
valueAnyThe value, for controlled components.
defaultValueAnyThe default value, for uncontrolled components.
onChangeFunction onChange(value)A handler that will be called whenever the current value changes.
disabledBooleanWhether the component is current disabled, ignoring state changes.
Render PropTypeDescription
valueAnyThe current map value.
disabledBooleanWhether the component is currently disabled or not.
setFunction set(key, value) or set(map)Calls Map.set, or sets the value to a new map.
clearFunction clear()Calls Map.clear.
resetFunction reset()Resets the value to its initial value/defaultValue state.
deleteFunction delete(key)Calls Map.delete.
unsetFunction unset(key)An alias for Map.delete that's not a reserved word.

<NumberValue>

A value for a Number.

<NumberValue
  value={Number|undefined}
  defaultValue={Number|undefined}
  onChange={Function}
  disabled={Boolean}
  min={Number|undefined}
  max={Number|undefined}
>
  {({
    value,
    set,
    reset,
    increment,
    decrement,
  }) => (
    ...
  )}
</NumberValue>
PropTypeDescription
valueAnyThe value, for controlled components.
defaultValueAnyThe default value, for uncontrolled components.
onChangeFunction onChange(value)A handler that will be called whenever the current value changes.
disabledBooleanWhether the component is current disabled, ignoring state changes.
minNumberAn optional minimum value to not go under.
maxNumberAn optional maximum value to not go over.
Render PropTypeDescription
valueNumberThe current number value.
disabledBooleanWhether the component is currently disabled or not.
setFunction set(number)Sets the value to a new number.
clearFunction clear()Sets the value to 0.
resetFunction reset()Resets the value to its initial value/defaultValue state.
incrementFunction increment(n = 1)Increments the number by n.
decrementFunction decrement(n = 1)Decrements the number by n.

<ObjectValue>

A value for a Object.

<ObjectValue
  value={Object|undefined}
  defaultValue={Object|undefined}
  onChange={Function}
  disabled={Boolean}
>
  {({
    value,
    set,
    clear,
    reset,
    unset,
    delete,
    assign,
  }) => (
    ...
  )}
</ObjectValue>
PropTypeDescription
valueAnyThe value, for controlled components.
defaultValueAnyThe default value, for uncontrolled components.
onChangeFunction onChange(value)A handler that will be called whenever the current value changes.
disabledBooleanWhether the component is current disabled, ignoring state changes.
Render PropTypeDescription
valueAnyThe current object value.
disabledBooleanWhether the component is currently disabled or not.
setFunction set(key, value) or set(object)Set a specific key in to value, or sets the value to a new object.
clearFunction clear()Set the value to a new empty object.
resetFunction reset()Resets the value to its initial value/defaultValue state.
deleteFunction delete(key)Delete a key from the current object.
unsetFunction unset(key)An alias for delete that's not a reserved word.
assignFunction assign(value)Extend the current object with another value using Object.assign.

<SetValue>

A value for a Set.

<SetValue
  value={Set|undefined}
  defaultValue={Set|undefined}
  onChange={Function}
  disabled={Boolean}
>
  {({
    value,
    set,
    clear,
    reset,
    add,
    remove,
    delete,
    toggle,
  }) => (
    ...
  )}
</SetValue>
PropTypeDescription
valueAnyThe value, for controlled components.
defaultValueAnyThe default value, for uncontrolled components.
onChangeFunction onChange(value)A handler that will be called whenever the current value changes.
disabledBooleanWhether the component is current disabled, ignoring state changes.
Render PropTypeDescription
valueSetThe current set value.
disabledBooleanWhether the component is currently disabled or not.
setFunction set(set)Sets the value to a new set.
clearFunction clear()Calls Set.clear.
resetFunction reset()Resets the value to its initial value/defaultValue state.
addFunction add(value)Calls Set.add.
deleteFunction delete(value)Calls Set.delete.
removeFunction remove(value)An alias for Set.delete that's not a reserved word.
toggleFunction toggle(value, boolean)Add or remove a value based on a boolean.

<StringValue>

A value for a String.

<StringValue
  value={String|undefined}
  defaultValue={String|undefined}
  onChange={Function}
  disabled={Boolean}
>
  {({
    value,
    set,
    clear,
    reset,
    concat,
    padEnd,
    padStart,
    repeat,
    replace,
    slice,
    substr,
    substring,
    toLowerCase,
    toUpperCase,
    trim,
    trimEnd,
    trimStart,
  }) => (
    ...
  )}
</StringValue>
PropTypeDescription
valueAnyThe value, for controlled components.
defaultValueAnyThe default value, for uncontrolled components.
onChangeFunction onChange(value)A handler that will be called whenever the current value changes.
disabledBooleanWhether the component is current disabled, ignoring state changes.
Render PropTypeDescription
valueStringThe current string value.
disabledBooleanWhether the component is currently disabled or not.
setFunction set(string)Sets the value to a new string.
clearFunction clear()Sets the value to an '' empty string.
resetFunction reset()Resets the value to its initial value/defaultValue state.
concatFunction concat(value)Calls String.concat.
padEndFunction padEnd(value)Calls String.padEnd.
padStartFunction padStart(value)Calls String.padStart.
repeatFunction repeat(value)Calls String.repeat.
replaceFunction replace(value)Calls String.replace.
sliceFunction slice(value)Calls String.slice.
substrFunction substr(value)Calls String.substr.
substringFunction substring(value)Calls String.substring.
toLowerCaseFunction toLowerCase(value)Calls String.toLowerCase.
toUpperCaseFunction toUpperCase(value)Calls String.toUpperCase.
trimFunction trim(value)Calls String.trim.
trimEndFunction trimEnd(value)Calls String.trimEnd.
trimStartFunction trimStart(value)Calls String.trimStart.

createValue

The createValue factory creates a new <Value> component with the exact same API, but that shares its state between multiple places in your app's render tree.

It takes either a defaultValue and an optional set of defaultProps, and returns a React component. The ConnectedValue constructor also has all of the transforms exposed as static methods, so you can update the connected value without even rendering it.

const ConnectedValue = createValue('default')

<ConnectedValue>
  {({ value, ... }) => (
    ...
  )}
</ConnectedValue>
ArgumentTypeDescription
defaultValueAnyThe default connected value.
defaultPropsObjectAn optional set of default props.

createArrayValue

Create a connected <ArrayValue> component.

const ConnectedArrayValue = createArrayValue([0, 1, 2])

<ConnectedArrayValue>
  {({ value, push, pop, slice, ... }) => (
    ...
  )}
</ConnectedArrayValue>
ArgumentTypeDescription
defaultValueArrayThe default connected value.
defaultPropsObjectAn optional set of default props.

createBooleanValue

Create a connected <BooleanValue> component.

const ConnectedBooleanValue = createBooleanValue(true)

<ConnectedBooleanValue>
  {({ value, set, toggle, ... }) => (
    ...
  )}
</ConnectedBooleanValue>
ArgumentTypeDescription
defaultValueBooleanThe default connected value.
defaultPropsObjectAn optional set of default props.

createDateValue

Create a connected <DateValue> component.

const ConnectedDateValue = createDateValue(new Date())

<ConnectedDateValue>
  {({ value, date, month, year, ... }) => (
    ...
  )}
</ConnectedDateValue>
ArgumentTypeDescription
defaultValueDateThe default connected value.
defaultPropsObjectAn optional set of default props.

createMapValue

Create a connected <MapValue> component.

const ConnectedMapValue = createMapValue(new Map([['a', 1]]))

<ConnectedMapValue>
  {({ value, set, unset, ... }) => (
    ...
  )}
</ConnectedMapValue>
ArgumentTypeDescription
defaultValueMapThe default connected value.
defaultPropsObjectAn optional set of default props.

createNumberValue

Create a connected <NumberValue> component.

const ConnectedNumberValue = createNumberValue(42)

<ConnectedNumberValue>
  {({ value, increment, decrement, ... }) => (
    ...
  )}
</ConnectedNumberValue>
ArgumentTypeDescription
defaultValueNumberThe default connected value.
defaultPropsObjectAn optional set of default props.

createObjectValue

Create a connected <ObjectValue> component.

const ConnectedObjectValue = createObjectValue({ a: 1 })

<ConnectedObjectValue>
  {({ value, set, unset, ... }) => (
    ...
  )}
</ConnectedObjectValue>
ArgumentTypeDescription
defaultValueObjectThe default connected value.
defaultPropsObjectAn optional set of default props.

createSetValue

Create a connected <SetValue> component.

const ConnectedSetValue = createSetValue(new Set([0, 1, 2]))

<ConnectedSetValue>
  {({ value, add, remove, ... }) => (
    ...
  )}
</ConnectedSetValue>
ArgumentTypeDescription
defaultValueSetThe default connected value.
defaultPropsObjectAn optional set of default props.

createStringValue

Create a connected <StringValue> component.

const ConnectedStringValue = createStringValue('default')

<ConnectedStringValue>
  {({ value, repeat, slice, ... }) => (
    ...
  )}
</ConnectedStringValue>
ArgumentTypeDescription
defaultValueStringThe default connected value.
defaultPropsObjectAn optional set of default props.