1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00

Fix form-field for new Storybook format (#12756)

* form-field

* add proptype
This commit is contained in:
Etienne Dusseault 2021-12-09 04:32:27 +08:00 committed by GitHub
parent f4a9c57728
commit 08ed32dfe6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 119 additions and 23 deletions

View File

@ -0,0 +1,31 @@
import { Story, Canvas, ArgsTable } from '@storybook/addon-docs';
import FormField from '.';
# Form Field
Various data fields available for forms and pages.
<Canvas>
<Story id="ui-components-ui-form-field-form-field-stories-js--default-story" />
</Canvas>
## Component API
<ArgsTable of={FormField} />
### Title detail
Show form fields with title detail on the left of the title
<Canvas>
<Story id="ui-components-ui-form-field-form-field-stories-js--form-field-with-title-detail" />
</Canvas>
### Error
Show form fields with error state
<Canvas>
<Story id="ui-components-ui-form-field-form-field-stories-js--form-field-with-error" />
</Canvas>

View File

@ -113,19 +113,61 @@ export default function FormField({
} }
FormField.propTypes = { FormField.propTypes = {
/**
* Identifier for testing purpose
*/
dataTestId: PropTypes.string, dataTestId: PropTypes.string,
/**
* Form Fields Title
*/
titleText: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), titleText: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
/**
* Show unit (eg. ETH)
*/
titleUnit: PropTypes.string, titleUnit: PropTypes.string,
/**
* Add Tooltip and text content
*/
tooltipText: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), tooltipText: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
/**
* Show content (text, image, component) in title
*/
titleDetail: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), titleDetail: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
/**
* Show error message
*/
error: PropTypes.string, error: PropTypes.string,
/**
* Handler when fields change
*/
onChange: PropTypes.func, onChange: PropTypes.func,
/**
* Field value
*/
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/**
* Show detail text if field mode is numeric
*/
detailText: PropTypes.string, detailText: PropTypes.string,
/**
* Set autofocus on render
*/
autoFocus: PropTypes.bool, autoFocus: PropTypes.bool,
/**
* Set numeric mode, the default is text
*/
numeric: PropTypes.bool, numeric: PropTypes.bool,
/**
* Set password mode
*/
password: PropTypes.bool, password: PropTypes.bool,
/**
* Allow decimals on the field
*/
allowDecimals: PropTypes.bool, allowDecimals: PropTypes.bool,
/**
* Check if the form disabled
*/
disabled: PropTypes.bool, disabled: PropTypes.bool,
}; };

View File

@ -1,33 +1,54 @@
/* eslint-disable react/prop-types */ /* eslint-disable react/prop-types */
import React, { useState } from 'react'; import React, { useState } from 'react';
import { select } from '@storybook/addon-knobs'; import README from './README.mdx';
import FormField from '.'; import FormField from '.';
export default { export default {
title: 'Components/UI/FormField', title: 'Components/UI/FormField',
id: __filename, id: __filename,
component: FormField,
parameters: {
docs: {
page: README,
},
},
argTypes: {
titleText: { control: 'text' },
titleUnit: { control: 'text' },
tooltipText: { control: 'text' },
titleDetail: {
options: ['text', 'button', 'checkmark'],
control: { type: 'select' },
},
error: { control: 'text' },
onChange: { action: 'onChange' },
value: { control: 'number' },
detailText: { control: 'text' },
autoFocus: { control: 'boolean' },
numeric: { control: 'boolean' },
password: { control: 'boolean' },
allowDecimals: { control: 'boolean' },
disabled: { control: 'boolean' },
},
}; };
export const DefaultStory = ({ ...props }) => { export const DefaultStory = (args) => {
const options = { text: false, numeric: true };
const [value, setValue] = useState(''); const [value, setValue] = useState('');
return ( return (
<div style={{ width: '600px' }}> <div style={{ width: '600px' }}>
<FormField <FormField {...args} onChange={setValue} value={value} />
onChange={setValue}
titleText="Title"
value={value}
numeric={select('text or numeric', options, options.text)}
{...props}
/>
</div> </div>
); );
}; };
DefaultStory.storyName = 'Default'; DefaultStory.storyName = 'Default';
DefaultStory.args = {
numeric: false,
titleText: 'Title',
};
export const FormFieldWithTitleDetail = () => { export const FormFieldWithTitleDetail = (args) => {
const [clicked, setClicked] = useState(false); const [clicked, setClicked] = useState(false);
const detailOptions = { const detailOptions = {
text: <div style={{ fontSize: '12px' }}>Detail</div>, text: <div style={{ fontSize: '12px' }}>Detail</div>,
@ -41,18 +62,20 @@ export const FormFieldWithTitleDetail = () => {
), ),
checkmark: <i className="fas fa-check" />, checkmark: <i className="fas fa-check" />,
}; };
return (
<DefaultStory return <FormField {...args} titleDetail={detailOptions[args.titleDetail]} />;
titleText="Title"
titleDetail={
detailOptions[
select('detailType', ['text', 'button', 'checkmark'], 'text')
]
}
/>
);
}; };
export const FormFieldWithError = () => { FormFieldWithTitleDetail.args = {
return <DefaultStory titleText="Title" error="Incorrect Format" />; titleText: 'Title',
titleDetail: 'text',
};
export const FormFieldWithError = (args) => {
return <FormField {...args} />;
};
FormFieldWithError.args = {
titleText: 'Title',
error: 'Incorrect Format',
}; };