@storybook/addons#useCallback TypeScript Examples

The following examples show how to use @storybook/addons#useCallback. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: default.tsx    From use-platform with MIT License 6 votes vote down vote up
Default = (args: DefaultArgs) => {
  const { bind, disabled } = args
  const [events, setEvents] = useState<string[]>([])
  const onAction = useCallback(
    (event: KeyboardEvent) => {
      setEvents(events.concat(`Key: ${event.code}, timestamp: ${Date.now()}`))
    },
    [events],
  )
  useKeyBinding({ bind, disabled, onAction: onAction })

  return (
    <div>
      {events.length === 0 && <div>No events were detected yet</div>}
      {events.map((ev) => (
        <div key={ev}>{ev}</div>
      ))}
    </div>
  )
}
Example #2
Source File: default.tsx    From use-platform with MIT License 6 votes vote down vote up
Default = ({ estimate, focusedIndex }: Args) => {
  const items = useMemo(
    () =>
      new Array(100)
        .fill(null)
        .map<DummyItem>((_, index) => ({ text: `Item with index ${index}` })),
    [],
  )
  const {
    containerProps,
    innerProps,
    virtualItems: children,
  } = useVirtualized({
    items,
    renderElement: useCallback(
      ({ element, style }: RenderElementProps<DummyItem>) => (
        <li style={{ ...style, height: estimate, display: 'flex', alignItems: 'center' }}>
          {element.text}
        </li>
      ),
      [estimate],
    ),
    estimate,
    focusedIndex,
  })

  return (
    <div {...containerProps} style={{ ...containerProps.style, maxHeight: 200 }}>
      <ul {...innerProps} style={{ marginTop: 0, ...innerProps.style }}>
        {children}
      </ul>
    </div>
  )
}