0.40.2 • Published 4 years ago

@stardust-ui/react-bindings v0.40.2

Weekly downloads
973
License
MIT
Repository
github
Last release
4 years ago

@stardust-ui/react-bindings

A set of reusable components and hooks to build component libraries and UI kits.

Installation

NPM

npm install --save @stardust-ui/react-bindings

Yarn

yarn add @stardust-ui/react-bindings

Hooks

useStateManager()

A React hook that provides bindings for state managers.

Usage

The examples below assume a component called <Input> will be used this way:

type InputProps = {
  defaultValue?: string;
  value?: string;
  onChange?: (value: string) => void;
};
type InputState = { value: string };
type InputActions = { change: (value: string) => void };

const createInputManager: ManagerFactory<InputState, InputActions> = config =>
  createManager<InputState, InputActions>({
    ...config,
    actions: {
      change: (value: string) => () => ({ value })
    },
    state: { value: "", ...config.state }
  });

const Input: React.FC<InputProps> = props => {
  const [state, actions] = useStateManager(createInputManager, {
    mapPropsToInitialState: () => ({ value: props.defaultValue }),
    mapPropsToState: () => ({ value: props.value })
  });

  return (
    <input
      onChange={e => {
        actions.change(e.target.value);
        if (props.onChange) props.onChange(e.target.value);
      }}
      value={state.value}
    />
  );
};

Reference

const [state, actions] = useStateManager(createInputManager)
const [state, actions] = useStateManager(
  managerFactory: ManagerFactory<State, Actions>, 
  options: UseStateManagerOptions<Props>,
)
  • managerFactory - a factory that implements state manager API
  • options.mapPropsToInitialState - optional, maps component's props to the initial state
  • options.mapPropsToState - optional, maps component's props to the state, should be used if your component implements controlled mode.
0.40.7

4 years ago

0.40.6

4 years ago

0.40.5

4 years ago

0.40.2

5 years ago

0.40.0

5 years ago

0.39.0-2

5 years ago

0.39.0-1

5 years ago

0.39.0-0

5 years ago

0.38.0-6

5 years ago

0.38.0-5

5 years ago

0.38.0-4

5 years ago

0.38.0

5 years ago

0.38.0-2

5 years ago

0.38.1-alpha.14

5 years ago