Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/inshadowin/react-wizzard

Wizard for your forms that does exactly that and nothing more
https://github.com/inshadowin/react-wizzard

react wizard wizard-steps

Last synced: about 2 months ago
JSON representation

Wizard for your forms that does exactly that and nothing more

Awesome Lists containing this project

README

        

# react-wizzard-light

Lightweight React-Wizzard package

Serves purpose of control over stages and `onEnter` / `onLeave` events

Component is stateless, you need to provide state / onChange on your own

It's enough to have `[state, onChange] = useState({})` as there are no additional logic there

## Documenation

- `Wizzard` component. Core component that accepts children of `Wizzard.Stage` and `Wizzard.Consumer` type

`` Props:

```js
//
// Types:
//

type WizzardChild =
| React.ReactElement>
| React.ReactElement>;

type WizzardProps = {
defaultStage?: string,
state: T,
onChange: (newState: T) => void,
children: WizzardChild | WizzardChild[],
};

type WizzardStageProps = {
stage: string,
onEnter?: (state?: T, onChange?: (newState: T) => void) => void,
onLeave?: (state?: T, onChange?: (newState: T) => void) => void,
children:
| React.ReactElement>
| ((params: WizzardStageChildrenProps) => JSX.Element),
} ;

type WizzardConsumerProps = {
children:
| React.ReactElement>
| ((params: WizzardStageChildrenProps) => JSX.Element),
};

type WizzardStageChildrenProps = {
state?: T,
onChange?: (newState: T) => void,
stage?: string,
setStage?: (newStage: string) => void,
goBack?: (() => void) | null,
goNext?: (() => void) | null,
};

//
// Props:
//
// Wizzard
{
// Stage for Wizzard start. Defaults to first child stage
defaultStage?: string,

// object that has wizzard data
state: T, // - any type you use

// callback to track data change
onChange: (newState: T) => void

// Components that define stages
children: || // element
}

// WizzardConsumer
{
// Must contain element based on Component with WizzardStageChildrenProps consumption
// or render function
// or just a Node
children:
| React.ReactNode
| React.ReactElement>
| ((params: WizzardStageChildrenProps) => JSX.Element)
}

// WizzardStage
{
// Stage ID for Wizzard stage
stage?: string,

// Effect that takes place on stage enter.
// Second argument allows to consume change function, and do on-enter data change
onEnter?: (state?: T, onChange?: (newState: T) => void) => void,

// Effect that takes place on stage leave.
// Second argument allows to consume change function, and do on-leave data change
onLeave?: (state?: T, onChange?: (newState: T) => void) => void,

// Must contain element based on Component with WizzardStageChildrenProps consumption
// or render function
children:
| React.ReactElement>
| ((params: WizzardStageChildrenProps) => JSX.Element)
}

// WizzardStageChildrenProps
{
// Access to Wizzard state
state?: T,

// Function that allows to change state
onChange?: (newState: T) => void

// Stage ID passed
stage?: string,

// Function to enter any stage
setStage?: (newStage: string) => void,

// Function to move to the previous stage. is null if it's first stage
goBack?: (() => void) | null,

// Function to move to the next stage. is null if it's last stage
goNext?: (() => void) | null,
}
```

## Examples:

We have three stages with side-effects. Stage2Component is an example of component that consumes all props

```tsx
type StateType = {
text1: string;
text2: string;
text3: string;
};

const Stage2Component = ({
state,
onChange,
stage,
goNext,
goBack,
}: StageChildrenProps) => {
return (
<>
Stage #{stage}
onChange?.({ ...state, text2: e.target.value })}
/>
{!!goBack && goBack?.()}>Back}
{!!goNext && goNext?.()}>Next}
>
);
};

const OurWizzardExample = () => {
// component must use outer state
const [state, setState] = useState({
text1: '',
text2: '',
text3: '',
});

return (


{params =>

Stage from Consumer# {params.stage}
}

alert('Entering Stage 1')}>


alert('Leaving Stage 2')}>






{props =>
Just a custom render
}


);
};
```