Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controls: Clean up arg unboxing and switch statements #14394

Merged
merged 5 commits into from Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 24 additions & 33 deletions lib/components/src/blocks/ArgsTable/ArgControl.tsx
Expand Up @@ -18,22 +18,39 @@ export interface ArgControlProps {
updateArgs: (args: Args) => void;
}

const Controls: Record<string, FC> = {
array: ObjectControl,
object: ObjectControl,
boolean: BooleanControl,
color: ColorControl,
date: DateControl,
number: NumberControl,
check: OptionsControl,
'inline-check': OptionsControl,
radio: OptionsControl,
'inline-radio': OptionsControl,
select: OptionsControl,
'multi-select': OptionsControl,
range: RangeControl,
text: TextControl,
file: FilesControl,
};

const NoControl = () => <>-</>;

export const ArgControl: FC<ArgControlProps> = ({ row, arg, updateArgs }) => {
const { key, control } = row;

const [isFocused, setFocused] = useState(false);
// box because arg can be a fn (e.g. actions) and useState calls fn's
const [boxedValue, setBoxedValue] = useState({ value: arg });
const [value, setValue] = useState(() => arg);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the point of doing this:

useState(() => arg);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I "boxed" the arg because if the arg was a function it was getting executed, but the boxed version fixes that. However, the function is better than boxing because it doesn't allocate extra memory

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please re-add a comment explaining why we're doing "lazy" state initialization here.

Copy link
Member

@shilman shilman Jul 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const [value, setValue] = useState(() => arg);
// done this way because the value of `arg` can be a function
const [value, setValue] = useState(() => arg);


useEffect(() => {
if (!isFocused) setBoxedValue({ value: arg });
if (!isFocused) setValue(arg);
}, [isFocused, arg]);

const onChange = useCallback(
(argVal: any) => {
setBoxedValue({ value: argVal });
setValue(argVal);
updateArgs({ [key]: argVal });
return argVal;
},
Expand All @@ -47,33 +64,7 @@ export const ArgControl: FC<ArgControlProps> = ({ row, arg, updateArgs }) => {

// row.name is a display name and not a suitable DOM input id or name - i might contain whitespace etc.
// row.key is a hash key and therefore a much safer choice
const props = { name: key, argType: row, value: boxedValue.value, onChange, onBlur, onFocus };
switch (control.type) {
case 'array':
case 'object':
return <ObjectControl {...props} {...control} />;
case 'boolean':
return <BooleanControl {...props} {...control} />;
case 'color':
return <ColorControl {...props} {...control} />;
case 'date':
return <DateControl {...props} {...control} />;
case 'number':
return <NumberControl {...props} {...control} />;
case 'check':
case 'inline-check':
case 'radio':
case 'inline-radio':
case 'select':
case 'multi-select':
return <OptionsControl {...props} {...control} controlType={control.type} />;
case 'range':
return <RangeControl {...props} {...control} />;
case 'text':
return <TextControl {...props} {...control} />;
case 'file':
return <FilesControl {...props} {...control} />;
default:
return <NoControl />;
}
const props = { name: key, argType: row, value, onChange, onBlur, onFocus };
const Control = Controls[control.type] || NoControl;
return <Control {...props} {...control} controlType={control.type} />;
};
34 changes: 21 additions & 13 deletions lib/components/src/controls/options/Options.tsx
Expand Up @@ -28,28 +28,36 @@ const normalizeOptions = (options: Options, labels?: Record<any, string>) => {
return options;
};

const Controls: Record<string, FC> = {
check: CheckboxControl,
'inline-check': CheckboxControl,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shilman I think in the future we might want to rename these to check-inline, minor DX improvement maybe. Not 100% sure if it's worth it, but could be an addition so no breaking change, WDYT?

radio: RadioControl,
'inline-radio': RadioControl,
select: SelectControl,
'multi-select': SelectControl,
};

export type OptionsProps = ControlProps<OptionsSelection> & OptionsConfig;
export const OptionsControl: FC<OptionsProps> = (props) => {
const { type = 'select', options, labels, argType } = props;
const normalized = { ...props, options: normalizeOptions(options || argType.options, labels) };
const normalized = {
...props,
options: normalizeOptions(options || argType.options, labels),
isInline: type.includes('inline'),
isMulti: type.includes('multi'),
};

if (options) {
once.warn(dedent`
'control.options' is deprecated and will be removed in Storybook 7.0. Define 'options' directly on the argType instead, and use 'control.labels' for custom labels.

More info: https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-controloptions
`);
}
switch (type) {
case 'check':
case 'inline-check':
return <CheckboxControl {...normalized} isInline={type === 'inline-check'} />;
case 'radio':
case 'inline-radio':
return <RadioControl {...normalized} isInline={type === 'inline-radio'} />;
case 'select':
case 'multi-select':
return <SelectControl {...normalized} isMulti={type === 'multi-select'} />;
default:
throw new Error(`Unknown options type: ${type}`);

const Control = Controls[type];
if (Control) {
return <Control {...normalized} />;
}
throw new Error(`Unknown options type: ${type}`);
};