1.1.2 • Published 6 years ago

atom-lib v1.1.2

Weekly downloads
2
License
ISC
Repository
github
Last release
6 years ago

ReactJS Container Library

Reusable container components for common React features/functionalities

Install

npm install --save atom-lib


Usage

    import {
    Toggler,
    withToggler, 
    SwitchBoard,
    ... 
    } from "atom-lib";

Container API Reference

Containers are components that have some sort of reusable functionality. Internal state/methods are exposed mainly via children props unless otherwise stated. The children function must always return either a React element or a React component. A corresponding HOC is also provided unless otherwise stated.

§ <Toggler>

Props
NameTypeDefaultDescription
children requiredFuncN/ASee below
on optionalBoolfalseDetermines initial value of toggler
Children Props
PropertyTypeDescription
onBoolThe value of current state
toggleFuncCallback function which toggles on value
<Toggler on>
    {({on, toggle}) => (
        <div>
            <button onClick={toggle}>{on ? "ON" : "OFF"}</button>
        </div>
    )}
</Toggler>

§ withToggler

Will expose the toggle and on values from above as props to the specified component.

withToggler(config)(component)

config

PropertiesTypeDescription
on optionalBooleanInitial value of toggler
import {withToggler} from "atom-lib"

function YourComponent({on, toggle}){
    return (
        // ...
    )
}

export default withToggler({on: false})(YourComponent)

§ <Switchboard>

Props
NameTypeDefaultDescription
children requiredFuncN/ASee below
exclusive optionalBoolfalseDetermines if switchboard values will be mutually exclusive (Any change in one switch value will result in all other switches being set to the opposite value)
flipped optionalBoolfalseInverts the on values for each switch.
switches optionalObject{}Initial values for the switchboard. Key values must be boolean.
Children Props
PropertyTypeDescription
toggleExclusiveFuncToggles the mutually exclusive setting. When set to false, changes to individual switch values won't affect others.
flipBoardFuncInverts the on values for each switch.
exclusiveBoolCurrent exclusion state of switchboard.
flippedBoolCurrent flip state of switchboard.
switchesObjectCurrent switch values for switchboard.
<Switchboard exclusive>
    {({ flipBoard, toggleExclusive, flipped, exclusive, switches }) => (
      <div>
        <Switch id={"a"}>
          {({ on, toggleSwitch }) => (
            <a style={{
              textDecoration: on ? "underline" :
                "none"
            }} onClick={toggleSwitch}>A</a>
          )}
        </Switch>
        <Switch id={"b"}>
          {({ on, toggleSwitch }) => (
            <a style={{
              textDecoration: on ? "underline" :
                "none"
            }} onClick={toggleSwitch}>B</a>
          )}
          <button onClick={toggleExclusive}>{exclusive ? "Set back to Inclusive" : "Set back to exclusive"}</button>
          <button onClick={flipBoard}>{flipped ? "Reset" : "Invert"}</button>
          <p>Currently, {Object.keys(switches).reduce((total, sw) => switches[sw] ? total + 1 : total, 0)} switches are ON</p>
        </Switch>
      </div>
    )}
  </Switchboard>

§ withSwitchboard

Will expose the children prop values from above as props to the specified component.

withSwitchboard(config)(component)

config accepts the same key-value pairs as the props object to Switchboard except for children.

import {withToggler} from "atom-lib"

function YourComponent({on, toggle}){
    return (
        // ...
    )
}

export default withToggler({on: false})(YourComponent)

§ <Switch>

Switch components must be rendered within a Switchboard component. They are responsible for toggling and containing a particular switch value.

Props
NameTypeDefaultDescription
children requiredFuncN/ASee below
id requiredString or NumberN/AIdentifies the particular switch across the switchboard
Children Props
PropertyTypeDescription
onBoolThe value of current switch state
toggleSwitchFuncCallback function which toggles on value
<Switch id="a">
    {({on, toggleSwitch}) => (
        <div>
            <button onClick={toggleSwitch}>{on ? "ON" : "OFF"}</button>
        </div>
    )}
</Switch>

§ withSwitch

Will expose the children prop values from above as props to the specified component. Must be rendered within a Switchboard component.

withSwitch(config)(component)

config Properties | Type | Description --- | --- | --- id required |String or Number | Identifies the particular switch across the switchboard

const linkComponents = links.map((content, i) => (
    createElement(withSwitch({ id: i })(NavLink), { key: i }, content)
  ))

Changelog

  • 10/14 - Added Toggler, withToggler, Switchboard, and withSwitchboard

Upcoming

  • Loader, Error Handler, SortFilter
1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago