Skip to content

Commit

Permalink
fix: ts number and proptypes enum
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Feb 20, 2020
1 parent 1513ddd commit 4eebe4f
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 6 deletions.
6 changes: 4 additions & 2 deletions core/core/src/smartControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ export const controlFromProps = (
case 'number': {
let value;
try {
if (typeof defaultValue === 'string') {
if (typeof defaultValue === 'number') {
value = defaultValue;
} else if (typeof defaultValue === 'string') {
value = parseFloat(defaultValue);
}
} catch (e) {
Expand All @@ -112,7 +114,7 @@ export const controlFromProps = (
: undefined;
const options = Array.isArray(propDef.type)
? propDef.type
: splitType || (propDef.type as any).value;
: (propDef.type as any).value || splitType;
if (!Array.isArray(options)) {
return null;
}
Expand Down
3 changes: 1 addition & 2 deletions examples/storybook-5/src/components/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ interface ButtonProps {
padding: number,
};

/** Button component description imported from comments inside the component file */
/** Button with react Typescript properties */
export const Button: FC<ButtonProps> = ({ disabled, label, onClick, style, backgroundColor, color, type, padding }) => (
// eslint-disable-next-line react/button-has-type
<button
type={type}
disabled={disabled}
Expand Down
49 changes: 49 additions & 0 deletions examples/storybook-5/src/components/PropTypesButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from 'react';
import { lighten } from 'polished';
import PropTypes from 'prop-types';

/** Button with react PropTypes */
export const Button = ({ disabled, label, onClick, style, backgroundColor, color, type, padding }) => (
<button
type={type}
disabled={disabled}
onClick={onClick}
style={{ ...style, backgroundColor, color: lighten(disabled ? 0.4 : 0, color), padding }}
>
{label}
</button>
);

Button.defaultProps = {
disabled: false,
label: 'default',
onClick: () => {},
style: {},
backgroundColor: '#fefefe',
color: 'black',
type: 'button',
padding: 5,
};

Button.propTypes = {
/** Boolean indicating whether the button should render as disabled */
disabled: PropTypes.bool,
/** button label. */
label: PropTypes.string,
/** onClick handler */
onClick: PropTypes.func,
/** Custom styles */
style: PropTypes.shape({}),

/** Background color */
backgroundColor: PropTypes.string,

/** Text color, default black */
color: PropTypes.string,

/** Button type */
type: PropTypes.oneOf(['button', 'reset', 'submit']),

/** Numeric field type */
padding: PropTypes.number,
};
38 changes: 38 additions & 0 deletions examples/storybook-5/src/stories/smart-prop-type.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react';
import { Button } from '../components/PropTypesButton';

export default {
title: 'Storybook/Smart PropTypes',
parameters: {
component: Button,
},
};

export const allProps = props => <Button {...props} />;

export const onlyColors = props => <Button label="Choose colors" {...props} />;

onlyColors.story = {
parameters: {
addonControls: {
smart: {
include: ['color', 'backgroundColor'],
},
},
},
};

export const noColors = () => <Button label="Choose colors" />;

noColors.story = {
parameters: {
addonControls: {
smart: {
exclude: ['color', 'backgroundColor'],
},
},
},
};

// this story does not have parameters, and smart controls will be disabled for it
export const noProps = () => <Button label="No properties" />;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { Button } from '../components/Button';

export default {
title: 'Storybook/Smart',
title: 'Storybook/Smart Typescript',
parameters: {
component: Button,
},
Expand Down
2 changes: 1 addition & 1 deletion integrations/storybook/src/blocks/BlockContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const BlockContextProvider: React.FC<BlockContextProviderProps> = ({
return null;
}
const story = storyStore.fromId(previewId) || {};
// console.log(myStoryStore);
console.log(myStoryStore);
const source: string | undefined = myStoryStore[previewId]
? myStoryStore[previewId].source
: undefined;
Expand Down

0 comments on commit 4eebe4f

Please sign in to comment.