0.1.0 • Published 6 years ago

react-controllable-renderless v0.1.0

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

react-controllable-renderless

Inspired by react-powerplug, a set of 'pluggable' renderless components that provide logic for your 'dumb' components.

Unlike react-powerplug the state of these renderless components can also be optionally controlled.

Also:

  • A Filter component.
  • The Focus component provides a focus function to focus on the target (using refs).

Quick example

import { State } from 'react-controllable-renderless';
import { Pagination } from './components';

// State works exactly like react-powerplug
const StateExample = props =>
  <State
    { ...props }
    initial={{ offset: 0, limit: 10, totalCount: 200 }}
    render={ ({state, setState}) =>
      <Pagination {...state} onChange={offset => setState({ offset })} />
    }
  />

// But we can also control parts of its state, e.g. limit, by adding props
const ControlledExample = props =>
  <StateExample
    limit={props.limit}
    onStateChange={props.onStateChange}
  />

Renderless components

See react-powerplug for an introduction to using renderless components.

Note: You must use a render prop not the children prop.

Install

yarn add react-controllable-renderless
npm i react-controllable-renderless

Examples

See the storybook.

Components

State

Props

proptypedefaultdescription
initialobjectundefinedThe initial state.
onStateChangefunction(changes: object, state: object)noopThis function is called any time the state is changed.
renderfunction(renderProps: object)requiredThe render function. See below.

In addition any part of the state can be controlled by passing a prop with the same name. E.g. to control state.age, pass in an age prop, <State age={} ... />, and use onStateChange to detect when age is trying to be changed.

Render props

proptypedescription
stateobjectThe current state.
setStatefunctionState setter, same as setState from React.Component.

Filter

Filters an array of items using a filterFunc that takes a query and items as arguments and return the filteredItems.

The query is updated by passing a new query to the refine render prop function.

In addition, query can be optionally controlled by passing a query prop and using onQueryChange to detect internal changes to the query.

Props

proptypedefaultdescription
defaultQueryany''The default query.
filterFuncfunction(items: array, query: any)requiredA function to filter the items.
itemsarray(any)[]The items to filter.
queryanyundefinedoptional control prop
onQueryChangefunction(newQuery: any)noopThis function is called when the query is changed.
renderfunction(renderProps: object)requiredThe render function. See below.

Render props

proptypedescription
filteredItemsarray(any)The filtered items.
queryanyThe current query.
refinefunction(newQuery: any)This function takes a new query and updates the filteredItems.

Focus

Like react-powerplug's Focus but also has focus and blur methods that use refs internally to allow a target element's focus to be controlled.

Props

proptypedefaultdescription
onBlurfunction(event: SyntheticEvent)noopThis function is called when the target is blurred.
onFocusfunction(event: SyntheticEvent)noopThis function is called when the target is focused.
renderfunction(renderProps: object)requiredThe render function. See below.
targetReffunction(element)noopAn optional property to pass a ref callback to the target. Useful for chaining Focus components.

Render props

proptypedescription
blurfunctionThis function blurs the target.
focusfunctionThis function will cause the target element to gain focus.
focusedbooleanThe current focus state of the target.
getTargetPropsfunction(additionalProps={})A function that returns the props for the target (the element we wish to focus). Takes an optional object argument of extra props to be merge in.Example: <input { ...getTargetProps({onFocus: <handleFocus>}) } />See this blog post on 'prop getters'.

Toggle

Props

proptypedefaultdescription
initialbooleanfalseThe initial/default state of the toggle.
onboolundefinedoptional control prop
onTogglefunction(newState: bool)This function is called when a toggle occurs.
renderfunction(renderProps: object)requiredThe render function. See below.

Render props

proptypedescription
onboolThe current state of the toggle.
togglefunctionFunction that toggles the state.

TODO

  • Tests
  • Add children prop functionality
  • Add compose functionality
  • Add other react-powerplug components

Credits

I initially learnt about the power of renderless components and the 'render prop' pattern from downshift as well as the 'prop getter' pattern used in getTargetProps prop of the Focus component.

I also learnt more about these patterns from articles and courses by Michael Jackson and Ryan Florence.

And, of course, react-powerplug.

Thank you.