|
1 | 1 | import { Checkbox } from '@committed/components'
|
2 | 2 | import { action } from '@storybook/addon-actions'
|
3 | 3 | import { Meta, Story } from '@storybook/react'
|
4 |
| -import React from 'react' |
| 4 | +import React, { useState } from 'react' |
5 | 5 | import { useControllableState } from '.'
|
6 | 6 |
|
7 | 7 | export interface UseControllableStateDocsProps<T> {
|
8 | 8 | /** The controlled value (of type T) or undefined for an uncontrolled value */
|
9 | 9 | value: T | undefined
|
10 | 10 | /** The dispatch handler for state changes or undefined for when an uncontrolled value, ignored if uncontrolled*/
|
11 |
| - setValue: React.Dispatch<React.SetStateAction<T>> | undefined |
| 11 | + setValue: |
| 12 | + | React.Dispatch<React.SetStateAction<T>> |
| 13 | + | React.Dispatch<T> |
| 14 | + | undefined |
12 | 15 | /** The initial state value, or state initializer for when uncontrolled, ignored if controlled */
|
13 | 16 | initialState?: T | (() => T | undefined) | undefined
|
14 | 17 | }
|
@@ -60,3 +63,44 @@ Controlled.parameters = {
|
60 | 63 | },
|
61 | 64 | },
|
62 | 65 | }
|
| 66 | + |
| 67 | +export const SetState = Template.bind({}) |
| 68 | +SetState.args = { |
| 69 | + value: true, |
| 70 | + setValue: (value: boolean | ((current: boolean) => void)) => |
| 71 | + action('setState')(value), |
| 72 | +} |
| 73 | +SetState.parameters = { |
| 74 | + docs: { |
| 75 | + description: { |
| 76 | + story: |
| 77 | + 'This is a controlled example, where both set and function set are supported', |
| 78 | + }, |
| 79 | + }, |
| 80 | +} |
| 81 | + |
| 82 | +export const SetValue = Template.bind({}) |
| 83 | +SetValue.args = { |
| 84 | + value: true, |
| 85 | + setValue: (value: boolean) => action('setValue')(value), |
| 86 | +} |
| 87 | +SetValue.parameters = { |
| 88 | + docs: { |
| 89 | + description: { |
| 90 | + story: |
| 91 | + 'This is a controlled example, where only direct value is supported - this is allow but may lead to errors if clients attempt to use the function style set.', |
| 92 | + }, |
| 93 | + }, |
| 94 | +} |
| 95 | + |
| 96 | +export const WrappedExample = () => { |
| 97 | + const [state, setState] = useState(false) |
| 98 | + |
| 99 | + return <Template value={state} setValue={setState} /> |
| 100 | +} |
| 101 | + |
| 102 | +export const WrappedExampleNoFunction = () => { |
| 103 | + const [state, setState] = useState(false) |
| 104 | + |
| 105 | + return <Template value={state} setValue={(value) => setState(value)} /> |
| 106 | +} |
0 commit comments