UNPKG

@storybook/addon-ondevice-controls

Version:

Display storybook controls on your device.

60 lines (59 loc) 2.32 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { includeConditionalArg, } from 'storybook/internal/csf'; import React, { useCallback, useState } from 'react'; import NoControlsWarning from './NoControlsWarning'; import PropForm from './PropForm'; import { useArgs } from './hooks'; function shouldIncludeArg(argType, args) { try { return includeConditionalArg(argType, args, {}); } catch { return true; } } const ControlsPanel = ({ api }) => { const store = api.store(); const storyId = store.getSelection()?.storyId; const [isPristine, setIsPristine] = useState(true); const [argsFromHook, updateArgs, resetArgs] = useArgs(storyId, store); const { argsObject, argTypes, parameters } = React.useMemo(() => { const { argTypes: storyArgTypes, parameters: storyParameters } = store.fromId(storyId); const storyArgsObject = Object.entries(storyArgTypes).reduce((prev, [key, argType]) => { const isControl = Boolean(argType?.control); const shouldInclude = shouldIncludeArg(argType, argsFromHook); return isControl && shouldInclude ? { ...prev, [key]: { ...argType, name: key, type: argType?.control?.type, value: argsFromHook[key], }, } : prev; }, {}); return { argTypes: storyArgTypes, parameters: storyParameters, argsObject: storyArgsObject, }; }, [store, storyId, argsFromHook]); const hasControls = Object.keys(argTypes).length > 0; const isArgsStory = parameters.__isArgsStory; const showWarning = !(hasControls && isArgsStory); const updateArgsOnFieldChange = useCallback((args) => { updateArgs(args); setIsPristine(false); }, [updateArgs]); const handleReset = useCallback(() => { resetArgs(); setIsPristine(true); }, [resetArgs]); if (showWarning) { return _jsx(NoControlsWarning, {}); } return (_jsx(PropForm, { args: argsObject, isPristine: isPristine, onFieldChange: updateArgsOnFieldChange, onReset: handleReset })); }; export default ControlsPanel;