Skip to content

Commit 24bee6e

Browse files
author
akito470
committed
docs for Swatch component
1 parent 2958ca5 commit 24bee6e

File tree

4 files changed

+117
-52
lines changed

4 files changed

+117
-52
lines changed
Lines changed: 97 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,109 @@
1+
/* eslint-disable react/no-unescaped-entities */
2+
import { ArgsTable, Primary, PRIMARY_STORY } from '@storybook/addon-docs'
3+
import { Meta, StoryObj } from '@storybook/react'
4+
import {
5+
DocTable,
6+
DoDont,
7+
Hero,
8+
LiveDemo,
9+
Note,
10+
RelatedComponent,
11+
RelatedComponentContainer,
12+
Section,
13+
StoryHeading,
14+
StoryStage,
15+
TableOfContents,
16+
} from 'pcln-docs-utils'
17+
import { Box, Flex, ISwatchProps, Swatch, Text, ThemeProvider } from '..'
118
import React, { useState } from 'react'
2-
import Swatch from './Swatch'
3-
import { Box, Flex, Text } from '../'
419

5-
export default {
6-
title: 'Swatch',
7-
component: Swatch,
20+
type SwatchStory = StoryObj<ISwatchProps>
21+
22+
export const Playground: SwatchStory = {
23+
render: (args) => <Swatch {...args} />,
24+
args: {
25+
colors: [
26+
'#D50032',
27+
'#1B7742',
28+
'#0033A0',
29+
'#002244',
30+
'#6244BB',
31+
'#31225D',
32+
'#31225D',
33+
'#FFC658',
34+
'#4F6F8F',
35+
],
36+
},
37+
argTypes: {
38+
onClick: () => null,
39+
},
40+
}
41+
42+
export const SingleColor: SwatchStory = {
43+
render: () => (
44+
<StoryStage>
45+
<Swatch colors={['#0033A0']} />
46+
</StoryStage>
47+
),
848
}
949

10-
export const SingleColor = () => <Swatch colors={['#0033A0']} />
11-
12-
export const MultipleColors = () => <Swatch colors={['#D50032', '#1B7742', '#0033A0']} />
13-
14-
export const Wrap = () => (
15-
<Box width='100px'>
16-
<Swatch
17-
colors={[
18-
'#D50032',
19-
'#1B7742',
20-
'#0033A0',
21-
'#002244',
22-
'#6244BB',
23-
'#31225D',
24-
'#31225D',
25-
'#FFC658',
26-
'#4F6F8F',
27-
]}
28-
/>
29-
</Box>
30-
)
31-
32-
export const SelectColor = () => {
50+
export const MultipleColors: SwatchStory = {
51+
render: () => (
52+
<StoryStage>
53+
<Swatch colors={['#D50032', '#1B7742', '#0033A0']} />
54+
</StoryStage>
55+
),
56+
}
57+
58+
const SwatchWithState = () => {
3359
const [color, setColor] = useState('#D50032')
3460
return (
35-
<>
36-
<Swatch colors={['#FFFFFF', '#D50032', '#1B7742', '#0033A0']} onClick={(color) => setColor(color)} />
61+
<StoryStage>
62+
<Swatch
63+
colors={['#FFFFFF', '#D50032', '#1B7742', '#0033A0']}
64+
onClick={(color) => (typeof color === 'string' ? setColor(color) : undefined)}
65+
/>
3766
<Flex mt='3' flexDirection='row' alignItems='center'>
3867
<Text mr={2}>Color:</Text>
3968
<Text>{color}</Text>
4069
</Flex>
41-
</>
70+
</StoryStage>
4271
)
4372
}
73+
74+
export const SelectColor: SwatchStory = {
75+
render: () => <SwatchWithState />,
76+
}
77+
78+
const meta: Meta<typeof Swatch> = {
79+
title: 'Swatch',
80+
component: Swatch,
81+
parameters: {
82+
docs: {
83+
page: () => (
84+
<ThemeProvider>
85+
<Hero name='Swatch' img={null}>
86+
Use the Swatch component to render a group of colors. Clicking one of the color will execute
87+
onClick callback, that can be passed as a prop.
88+
</Hero>
89+
90+
<TableOfContents links={['Overview', 'Props']} />
91+
92+
<Section heading='Overview'>
93+
<Text textStyle='paragraph'>
94+
Use the Swatch component to render a group of colors. Clicking one of the color will execute
95+
onClick callback, that can be passed as a prop.
96+
</Text>
97+
</Section>
98+
99+
<Section heading='Props'>
100+
<Primary />
101+
<ArgsTable story={PRIMARY_STORY} />
102+
</Section>
103+
</ThemeProvider>
104+
),
105+
},
106+
},
107+
}
108+
109+
export default meta
Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import PropTypes, { InferProps } from 'prop-types'
21
import React from 'react'
32
import styled from 'styled-components'
43
import { Flex } from '../Flex'
@@ -15,25 +14,25 @@ const SwatchColor = styled.div`
1514
cursor: ${(props) => (props.onClick ? 'pointer' : 'default')};
1615
`
1716

18-
const propTypes = {
19-
colors: PropTypes.arrayOf(PropTypes.string),
20-
onClick: PropTypes.func,
17+
export interface ISwatchProps {
18+
colors?: string[]
19+
onClick?: (color: string) => void
2120
}
2221

23-
const Swatch: React.FC<InferProps<typeof propTypes>> = ({ colors, onClick, ...props }) => (
24-
<Flex flexWrap='wrap' {...props}>
25-
{colors.map((color, idx) => (
26-
<SwatchColor
27-
data-testid={`${idx}-${color}`}
28-
key={`${idx}-${color}`}
29-
color={color}
30-
onClick={onClick ? () => onClick(color) : undefined}
31-
/>
32-
))}
33-
</Flex>
34-
)
35-
36-
Swatch.propTypes = propTypes
37-
Swatch.displayName = 'Swatch'
22+
const Swatch: React.FC<ISwatchProps> = ((props: ISwatchProps) => {
23+
const { colors, onClick } = props
24+
return (
25+
<Flex flexWrap='wrap' {...props}>
26+
{colors.map((color, idx) => (
27+
<SwatchColor
28+
data-testid={`${idx}-${color}`}
29+
key={`${idx}-${color}`}
30+
color={color}
31+
onClick={onClick ? () => onClick(color) : undefined}
32+
/>
33+
))}
34+
</Flex>
35+
)
36+
})
3837

3938
export default Swatch

packages/core/src/Swatch/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { default as Swatch } from './Swatch'
1+
export { default as Swatch, ISwatchProps } from './Swatch'

packages/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export { Spinner } from './Spinner'
4848
export { SrOnly } from './SrOnly'
4949
export { Stamp } from './Stamp'
5050
export { Stepper } from './Stepper'
51-
export { Swatch } from './Swatch'
51+
export { Swatch, ISwatchProps } from './Swatch'
5252
export { Text } from './Text'
5353
export { TextArea } from './TextArea'
5454
export { Toast } from './Toast'

0 commit comments

Comments
 (0)