Add Definition List component (#10291)
parent
96361872cc
commit
419897cba6
@ -0,0 +1,83 @@ |
||||
import React from 'react' |
||||
import PropTypes from 'prop-types' |
||||
import { omit } from 'lodash' |
||||
import Typography from '../typography' |
||||
import { |
||||
COLORS, |
||||
SIZES, |
||||
TYPOGRAPHY, |
||||
FONT_WEIGHT, |
||||
} from '../../../helpers/constants/design-system' |
||||
import Tooltip from '../tooltip' |
||||
|
||||
const MARGIN_MAP = { |
||||
[SIZES.XS]: 0, |
||||
[SIZES.SM]: 2, |
||||
[SIZES.MD]: 4, |
||||
[SIZES.LG]: 6, |
||||
[SIZES.XL]: 8, |
||||
} |
||||
|
||||
export default function DefinitionList({ |
||||
dictionary, |
||||
termTypography = {}, |
||||
definitionTypography = {}, |
||||
tooltips = {}, |
||||
gapSize = SIZES.SM, |
||||
}) { |
||||
return ( |
||||
<dl className="definition-list"> |
||||
{Object.entries(dictionary).map(([term, definition]) => ( |
||||
<React.Fragment key={`definition-for-${term}`}> |
||||
<Typography |
||||
variant={TYPOGRAPHY.H6} |
||||
fontWeight={FONT_WEIGHT.BOLD} |
||||
{...termTypography} |
||||
boxProps={{ |
||||
marginTop: 0, |
||||
marginBottom: 1, |
||||
}} |
||||
className="definition-list__term" |
||||
tag="dt" |
||||
> |
||||
{term} |
||||
{tooltips[term] && ( |
||||
<Tooltip |
||||
title={tooltips[term]} |
||||
position="top" |
||||
containerClassName="definition-list__tooltip-wrapper" |
||||
> |
||||
<i className="fas fa-info-circle" /> |
||||
</Tooltip> |
||||
)} |
||||
</Typography> |
||||
<Typography |
||||
variant={TYPOGRAPHY.H6} |
||||
color={COLORS.UI4} |
||||
{...definitionTypography} |
||||
boxProps={{ |
||||
marginTop: 0, |
||||
marginBottom: MARGIN_MAP[gapSize], |
||||
}} |
||||
className="definition-list__definition" |
||||
tag="dd" |
||||
> |
||||
{definition} |
||||
</Typography> |
||||
</React.Fragment> |
||||
))} |
||||
</dl> |
||||
) |
||||
} |
||||
|
||||
DefinitionList.propTypes = { |
||||
gapSize: PropTypes.oneOf(Object.values(SIZES)), |
||||
dictionary: PropTypes.objectOf(PropTypes.string), |
||||
tooltips: PropTypes.objectOf(PropTypes.string), |
||||
termTypography: PropTypes.shape({ |
||||
...omit(Typography.propTypes, ['tag', 'className', 'boxProps']), |
||||
}), |
||||
definitionTypography: PropTypes.shape({ |
||||
...omit(Typography.propTypes, ['tag', 'className', 'boxProps']), |
||||
}), |
||||
} |
@ -0,0 +1,19 @@ |
||||
.definition-list { |
||||
$self: &; |
||||
|
||||
&__term { |
||||
display: flex; |
||||
align-items: center; |
||||
|
||||
& i { |
||||
color: $ui-3; |
||||
margin-left: 6px; |
||||
font-size: $font-size-h8; |
||||
} |
||||
|
||||
& #{$self}__tooltip-wrapper { |
||||
display: flex !important; |
||||
align-items: center; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,69 @@ |
||||
import React from 'react' |
||||
import { object, select } from '@storybook/addon-knobs' |
||||
import { |
||||
COLORS, |
||||
SIZES, |
||||
TYPOGRAPHY, |
||||
} from '../../../helpers/constants/design-system' |
||||
import DefinitionList from './definition-list' |
||||
|
||||
export default { |
||||
title: 'Definition List', |
||||
} |
||||
|
||||
const basic = { |
||||
term: |
||||
'a word or phrase used to describe a thing or to express a concept, especially in a particular kind of language or branch of study.', |
||||
definition: |
||||
'a statement of the exact meaning of a word, especially in a dictionary.', |
||||
dl: 'HTML tag denoting a definition list', |
||||
dt: 'HTML tag denoting a definition list term', |
||||
dd: 'HTML tag denoting a definition list definition', |
||||
} |
||||
|
||||
const advanced = { |
||||
'Network Name': 'Ethereum Mainnet', |
||||
'Chain ID': '1', |
||||
Ticker: 'ETH', |
||||
} |
||||
|
||||
const tooltips = { |
||||
'Network Name': 'The name that is associated with this network', |
||||
'Chain ID': 'The numeric value representing the ID of this network', |
||||
Ticker: 'The currency symbol of the primary currency for this network', |
||||
} |
||||
|
||||
export const definitionList = () => ( |
||||
<DefinitionList |
||||
dictionary={object('dictionary', basic)} |
||||
gapSize={select('gapSize', SIZES, SIZES.SM)} |
||||
/> |
||||
) |
||||
|
||||
export const withTooltips = () => ( |
||||
<DefinitionList |
||||
dictionary={object('dictionary', advanced)} |
||||
tooltips={object('tooltips', tooltips)} |
||||
gapSize={select('gapSize', SIZES, SIZES.SM)} |
||||
/> |
||||
) |
||||
|
||||
export const withTypographyControl = () => ( |
||||
<DefinitionList |
||||
dictionary={object('dictionary', advanced)} |
||||
tooltips={object('tooltips', tooltips)} |
||||
gapSize={select('gapSize', SIZES, SIZES.SM)} |
||||
termTypography={{ |
||||
variant: select('termTypography.variant', TYPOGRAPHY, TYPOGRAPHY.H6), |
||||
color: select('termTypography.color', COLORS, COLORS.BLACK), |
||||
}} |
||||
definitionTypography={{ |
||||
variant: select( |
||||
'definitionTypography.variant', |
||||
TYPOGRAPHY, |
||||
TYPOGRAPHY.H6, |
||||
), |
||||
color: select('definitionTypography.color', COLORS, COLORS.BLACK), |
||||
}} |
||||
/> |
||||
) |
@ -0,0 +1 @@ |
||||
export { default } from './definition-list' |
Loading…
Reference in new issue