WithValue.tsx 475 B

12345678910111213141516
  1. import { action } from '@storybook/addon-actions'
  2. import { ReactElement, useState } from 'react'
  3. export interface WithValueProps<T> {
  4. initial: T
  5. actionName?: string
  6. children: (value: T, setValue: (value: T) => void) => ReactElement
  7. }
  8. export function WithValue<T>(props: WithValueProps<T>) {
  9. const [value, setValue] = useState<T>(props.initial)
  10. return props.children(value, (value) => {
  11. action(props.actionName || 'setValue')(value)
  12. setValue(value)
  13. })
  14. }