Skip to content

Commit 88d6270

Browse files
committed
fix(usecontrollablestate): allow use of simple dispatch function in useControllableState
1 parent a8163a3 commit 88d6270

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/useControllableState/useControllableState.stories.tsx

+46-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { Checkbox } from '@committed/components'
22
import { action } from '@storybook/addon-actions'
33
import { Meta, Story } from '@storybook/react'
4-
import React from 'react'
4+
import React, { useState } from 'react'
55
import { useControllableState } from '.'
66

77
export interface UseControllableStateDocsProps<T> {
88
/** The controlled value (of type T) or undefined for an uncontrolled value */
99
value: T | undefined
1010
/** 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
1215
/** The initial state value, or state initializer for when uncontrolled, ignored if controlled */
1316
initialState?: T | (() => T | undefined) | undefined
1417
}
@@ -60,3 +63,44 @@ Controlled.parameters = {
6063
},
6164
},
6265
}
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+
}

src/useControllableState/useControllableState.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ function noop() {}
2222
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2323
export function useControllableState<T = any>(
2424
value: T | undefined,
25-
setValue: React.Dispatch<React.SetStateAction<T>> | undefined,
25+
setValue:
26+
| React.Dispatch<React.SetStateAction<T>>
27+
| React.Dispatch<T>
28+
| undefined,
2629
initialState?: T | (() => T | undefined) | undefined
2730
): [T, React.Dispatch<React.SetStateAction<T>>] {
2831
const { current: wasControlled } = useRef(value !== undefined)

0 commit comments

Comments
 (0)