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/Knobs: Set input to empty string when value is null #14457

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions addons/knobs/src/components/types/Number.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { styled } from '@storybook/theming';
import { Form } from '@storybook/components';
import { KnobControlConfig, KnobControlProps } from './types';

type NumberTypeKnobValue = number;
type NumberTypeKnobValue = number | null;

export interface NumberTypeKnobOptions {
range?: boolean;
Expand Down Expand Up @@ -95,12 +95,13 @@ export default class NumberType extends Component<NumberTypeProps> {

render() {
const { knob } = this.props;
const inputValue = knob.value === null ? '' : knob.value;

return knob.range ? (
<RangeWrapper>
<RangeLabel>{knob.min}</RangeLabel>
<RangeInput
value={knob.value}
value={inputValue}
type="range"
name={knob.name}
min={knob.min}
Expand All @@ -112,7 +113,7 @@ export default class NumberType extends Component<NumberTypeProps> {
</RangeWrapper>
) : (
<Form.Input
value={knob.value}
value={inputValue}
type="number"
name={knob.name}
min={knob.min}
Expand Down
2 changes: 1 addition & 1 deletion addons/knobs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function boolean(name: string, value: boolean, groupId?: string) {

export function number(
name: string,
value: number,
value: number | null,
options: NumberTypeKnobOptions = {},
groupId?: string
) {
Expand Down
3 changes: 2 additions & 1 deletion lib/components/src/controls/Number.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const NumberControl: FC<NumberProps> = ({
const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
onChange(parse(event.target.value));
};
const inputValue = value === null ? '' : value;

return (
<Wrapper>
Expand All @@ -38,7 +39,7 @@ export const NumberControl: FC<NumberProps> = ({
onChange={handleChange}
size="flex"
placeholder="Adjust number dynamically"
value={value === null ? undefined : value}
value={inputValue}
{...{ name, min, max, step, onFocus, onBlur }}
/>
</Wrapper>
Expand Down
7 changes: 5 additions & 2 deletions lib/components/src/controls/Range.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ const RangeInput = styled.input(({ theme }) => ({
},
'&:focus::-ms-fill-lower': { background: '#dddddd' },
'&:focus::-ms-fill-upper': { background: '#e0e0e0' },
'@supports (-ms-ime-align:auto)': { 'input[type=range]': { margin: '0' } },
'@supports (msImeAlign:auto)': { 'input[type=range]': { margin: '0' } },
Copy link
Member

Choose a reason for hiding this comment

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

Was this intentional?

Copy link
Author

Choose a reason for hiding this comment

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

No, not sure how this snuck in there.

}));

const RangeLabel = styled.span({
Expand Down Expand Up @@ -164,13 +164,16 @@ export const RangeControl: FC<RangeProps> = ({
const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
onChange(parse(event.target.value));
};
const inputValue = value === null ? '' : value;
Copy link
Member

Choose a reason for hiding this comment

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

I would just inline this variable.

Copy link
Author

Choose a reason for hiding this comment

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

Okay, I can do that. I was on the fence about whether or not to inline it.


return (
<RangeWrapper>
<RangeLabel>{min}</RangeLabel>
<RangeInput
type="range"
onChange={handleChange}
{...{ name, value, min, max, step, onFocus, onBlur }}
value={inputValue}
{...{ name, min, max, step, onFocus, onBlur }}
/>
<RangeLabel>{`${value} / ${max}`}</RangeLabel>
</RangeWrapper>
Expand Down