Skip to content

Commit 8d37b0c

Browse files
WesSouzaarturbien
authored andcommitted
feat(button): convert to TypeScript and export types
1 parent 1c16025 commit 8d37b0c

File tree

13 files changed

+108
-102
lines changed

13 files changed

+108
-102
lines changed

src/AppBar/AppBar.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ menu: Components
55

66
import { AppBar } from './AppBar';
77
import Toolbar from '../Toolbar/Toolbar';
8-
import Button from '../Button/Button';
8+
import { Button } from '../Button/Button';
99
import TextField from '../TextField/TextField';
1010
import List from '../List/List';
1111
import ListItem from '../ListItem/ListItem';

src/Bar/Bar.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ menu: Components
66
import { Bar } from '../Bar/Bar';
77
import { AppBar } from '../AppBar/AppBar.js';
88
import Toolbar from '../Toolbar/Toolbar.js';
9-
import Button from '../Button/Button.js';
9+
import { Button } from '../Button/Button.js';
1010

1111
# Bar
1212

src/Button/Button.mdx

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ name: Button
33
menu: Components
44
---
55

6-
import Button from './Button'
7-
import Window from '../Window/Window'
8-
import WindowContent from '../WindowContent/WindowContent'
9-
import Cutout from '../Cutout/Cutout'
10-
import Toolbar from '../Toolbar/Toolbar'
6+
import { Button } from './Button';
7+
import Window from '../Window/Window';
8+
import WindowContent from '../WindowContent/WindowContent';
9+
import Cutout from '../Cutout/Cutout';
10+
import Toolbar from '../Toolbar/Toolbar';
1111

1212
# Button
1313

@@ -16,7 +16,7 @@ import Toolbar from '../Toolbar/Toolbar'
1616
#### Default
1717

1818
<Playground>
19-
<Button >Default</Button>
19+
<Button>Default</Button>
2020
</Playground>
2121

2222
#### Disabled
@@ -58,15 +58,9 @@ import Toolbar from '../Toolbar/Toolbar'
5858
justifyContent: 'space-between'
5959
}}
6060
>
61-
<Button size='sm'>
62-
small
63-
</Button>
64-
<Button size='md'>
65-
medium
66-
</Button>
67-
<Button size='lg'>
68-
large
69-
</Button>
61+
<Button size='sm'>small</Button>
62+
<Button size='md'>medium</Button>
63+
<Button size='lg'>large</Button>
7064
</div>
7165
</Playground>
7266

@@ -90,14 +84,10 @@ import Toolbar from '../Toolbar/Toolbar'
9084
>
9185
Primary
9286
</Button>
93-
<Button
94-
variant='flat'
95-
fullWidth
96-
style={{ marginRight: '0.5rem' }}
97-
>
87+
<Button variant='flat' fullWidth style={{ marginRight: '0.5rem' }}>
9888
Regular
9989
</Button>
100-
<Button variant='flat' disabled fullWidt>
90+
<Button variant='flat' disabled fullWidt>
10191
Disabled
10292
</Button>
10393
</Toolbar>

src/Button/Button.spec.js renamed to src/Button/Button.spec.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import React from 'react';
21
import { render, fireEvent } from '@testing-library/react';
32

43
import { renderWithTheme, theme } from '../../test/utils';
54
import { blockSizes } from '../common/system';
65

7-
import Button from './Button';
6+
import { Button } from './Button';
87

98
const defaultProps = {
109
children: 'click me'

src/Button/Button.stories.js renamed to src/Button/Button.stories.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import React from 'react';
2-
import styled from 'styled-components';
3-
1+
import { ComponentMeta } from '@storybook/react';
2+
import { useState } from 'react';
43
import {
54
Button,
6-
Window,
7-
WindowHeader,
8-
WindowContent,
5+
Cutout,
6+
Divider,
97
List,
108
ListItem,
11-
Divider,
12-
Cutout,
13-
Toolbar
9+
Toolbar,
10+
Window,
11+
WindowContent,
12+
WindowHeader
1413
} from 'react95';
14+
import styled from 'styled-components';
1515

1616
const Wrapper = styled.div`
1717
padding: 5rem;
@@ -32,7 +32,7 @@ export default {
3232
title: 'Button',
3333
component: Button,
3434
decorators: [story => <Wrapper>{story()}</Wrapper>]
35-
};
35+
} as ComponentMeta<typeof Button>;
3636

3737
export function Default() {
3838
return (
@@ -66,7 +66,7 @@ Default.story = {
6666
const imageSrc =
6767
'https://image.freepik.com/foto-gratuito/la-frutta-fresca-del-kiwi-tagliata-a-meta-con-la-decorazione-completa-del-pezzo-e-bella-sulla-tavola-di-legno_47436-1.jpg';
6868
export function Menu() {
69-
const [open, setOpen] = React.useState(false);
69+
const [open, setOpen] = useState(false);
7070

7171
return (
7272
<Window style={{ maxWidth: '250px' }}>
Lines changed: 72 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,61 @@
11
/* eslint-disable no-nested-ternary */
22

3-
import React from 'react';
4-
import propTypes from 'prop-types';
5-
3+
import React, { forwardRef } from 'react';
64
import styled, { css } from 'styled-components';
75
import {
86
createBorderStyles,
9-
createWellBorderStyles,
107
createBoxStyles,
11-
createFlatBoxStyles,
128
createDisabledTextStyles,
9+
createFlatBoxStyles,
1310
createHatchedBackground,
11+
createWellBorderStyles,
1412
focusOutline
1513
} from '../common';
16-
import { noOp } from '../common/utils';
1714
import { blockSizes } from '../common/system';
15+
import { noOp } from '../common/utils';
16+
import { CommonStyledProps, Sizes } from '../types';
1817

19-
const commonButtonStyles = css`
18+
type ButtonProps = {
19+
active?: boolean;
20+
children?: React.ReactNode;
21+
disabled?: boolean;
22+
fullWidth?: boolean;
23+
onClick?: React.ButtonHTMLAttributes<HTMLButtonElement>['onClick'];
24+
onTouchStart?: React.ButtonHTMLAttributes<HTMLButtonElement>['onTouchStart'];
25+
primary?: boolean;
26+
size?: Sizes;
27+
square?: boolean;
28+
type?: string;
29+
variant?: 'default' | 'menu' | 'flat';
30+
} & React.ButtonHTMLAttributes<HTMLButtonElement> &
31+
CommonStyledProps;
32+
33+
type StyledButtonProps = Pick<
34+
ButtonProps,
35+
| 'active'
36+
| 'disabled'
37+
| 'fullWidth'
38+
| 'primary'
39+
| 'size'
40+
| 'square'
41+
| 'variant'
42+
>;
43+
44+
const commonButtonStyles = css<StyledButtonProps>`
2045
position: relative;
2146
display: inline-flex;
2247
align-items: center;
2348
justify-content: center;
24-
height: ${({ size }) => blockSizes[size]};
25-
width: ${({ fullWidth, square, size }) =>
49+
height: ${({ size = 'md' }) => blockSizes[size]};
50+
width: ${({ fullWidth, size = 'md', square }) =>
2651
fullWidth ? '100%' : square ? blockSizes[size] : 'auto'};
2752
padding: ${({ square }) => (square ? 0 : `0 10px`)};
2853
font-size: 1rem;
2954
user-select: none;
3055
&:active {
31-
padding-top: ${({ isDisabled }) => !isDisabled && '2px'};
56+
padding-top: ${({ disabled }) => !disabled && '2px'};
3257
}
33-
padding-top: ${({ active, isDisabled }) => active && !isDisabled && '2px'};
58+
padding-top: ${({ active, disabled }) => active && !disabled && '2px'};
3459
&:after {
3560
content: '';
3661
position: absolute;
@@ -46,8 +71,8 @@ const commonButtonStyles = css`
4671
font-family: inherit;
4772
`;
4873

49-
export const StyledButton = styled.button`
50-
${({ variant, theme, active, isDisabled, primary }) =>
74+
export const StyledButton = styled.button<StyledButtonProps>`
75+
${({ active, disabled, primary, theme, variant }) =>
5176
variant === 'flat'
5277
? css`
5378
${createFlatBoxStyles()}
@@ -63,7 +88,7 @@ export const StyledButton = styled.button`
6388
outline-offset: -4px;
6489
`}
6590
&:focus:after, &:active:after {
66-
${!active && !isDisabled && focusOutline}
91+
${!active && !disabled && focusOutline}
6792
outline-offset: -4px;
6893
}
6994
`
@@ -73,18 +98,18 @@ export const StyledButton = styled.button`
7398
border: 2px solid transparent;
7499
&:hover,
75100
&:focus {
76-
${!isDisabled && !active && createWellBorderStyles(false)}
101+
${!disabled && !active && createWellBorderStyles(false)}
77102
}
78103
&:active {
79-
${!isDisabled && createWellBorderStyles(true)}
104+
${!disabled && createWellBorderStyles(true)}
80105
}
81106
${active && createWellBorderStyles(true)}
82-
${isDisabled && createDisabledTextStyles()}
107+
${disabled && createDisabledTextStyles()}
83108
`
84109
: css`
85110
${createBoxStyles()};
86111
border: none;
87-
${isDisabled && createDisabledTextStyles()}
112+
${disabled && createDisabledTextStyles()}
88113
${active
89114
? createHatchedBackground({
90115
mainColor: theme.material,
@@ -115,11 +140,11 @@ export const StyledButton = styled.button`
115140
: createBorderStyles({ invert: false })}
116141
}
117142
&:active:before {
118-
${!isDisabled && createBorderStyles({ invert: true })}
143+
${!disabled && createBorderStyles({ invert: true })}
119144
}
120145
&:focus:after,
121146
&:active:after {
122-
${!active && !isDisabled && focusOutline}
147+
${!active && !disabled && focusOutline}
123148
outline-offset: -8px;
124149
}
125150
&:active:focus:after,
@@ -130,49 +155,41 @@ export const StyledButton = styled.button`
130155
${commonButtonStyles}
131156
`;
132157

133-
const Button = React.forwardRef(function Button(props, ref) {
134-
const { onClick, disabled, children, ...otherProps } = props;
135-
158+
const Button = forwardRef<HTMLButtonElement, ButtonProps>(function Button(
159+
{
160+
onClick,
161+
disabled = false,
162+
children,
163+
type = 'button',
164+
fullWidth = false,
165+
size = 'md',
166+
square = false,
167+
active = false,
168+
onTouchStart = noOp,
169+
primary = false,
170+
variant = 'default',
171+
...otherProps
172+
},
173+
ref
174+
) {
136175
return (
137176
<StyledButton
138-
onClick={disabled ? undefined : onClick}
177+
active={active}
139178
disabled={disabled}
140-
isDisabled={disabled}
179+
fullWidth={fullWidth}
180+
onClick={disabled ? undefined : onClick}
181+
onTouchStart={onTouchStart}
182+
primary={primary}
141183
ref={ref}
184+
size={size}
185+
square={square}
186+
type={type}
187+
variant={variant}
142188
{...otherProps}
143189
>
144190
{children}
145191
</StyledButton>
146192
);
147193
});
148194

149-
Button.defaultProps = {
150-
type: 'button',
151-
onClick: null,
152-
disabled: false,
153-
fullWidth: false,
154-
size: 'md',
155-
square: false,
156-
active: false,
157-
// onTouchStart below to enable button :active style on iOS
158-
onTouchStart: noOp,
159-
primary: false,
160-
variant: 'default'
161-
};
162-
163-
Button.propTypes = {
164-
type: propTypes.string,
165-
onClick: propTypes.func,
166-
disabled: propTypes.bool,
167-
fullWidth: propTypes.bool,
168-
size: propTypes.oneOf(['sm', 'md', 'lg']),
169-
square: propTypes.bool,
170-
active: propTypes.bool,
171-
onTouchStart: propTypes.func,
172-
primary: propTypes.bool,
173-
variant: propTypes.oneOf(['default', 'menu', 'flat']),
174-
// eslint-disable-next-line react/require-default-props
175-
children: propTypes.node
176-
};
177-
178-
export default Button;
195+
export { Button, ButtonProps };

src/Checkbox/Checkbox.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ menu: Components
55

66
import { Checkbox } from './Checkbox';
77
import Fieldset from '../Fieldset/Fieldset';
8-
import Button from '../Button/Button';
8+
import { Button } from '../Button/Button';
99
import Cutout from '../Cutout/Cutout';
1010
import List from '../List/List';
1111
import ListItem from '../ListItem/ListItem';

src/DatePicker/DatePicker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import WindowContent from '../WindowContent/WindowContent';
99
import Select from '../Select/Select';
1010
import NumberField from '../NumberField/NumberField';
1111
import Cutout from '../Cutout/Cutout';
12-
import Button from '../Button/Button';
12+
import { Button } from '../Button/Button';
1313
import Toolbar from '../Toolbar/Toolbar';
1414

1515
const Calendar = styled(Cutout)`

src/NumberField/NumberField.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import styled, { css } from 'styled-components';
55
import useControlledOrUncontrolled from '../common/hooks/useControlledOrUncontrolled';
66
import { clamp } from '../common/utils';
77

8-
import Button from '../Button/Button';
8+
import { Button } from '../Button/Button';
99
import { blockSizes } from '../common/system';
1010
import TextField from '../TextField/TextField';
1111

src/Tooltip/Tooltip.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ menu: Components
44
---
55

66
import Tooltip from './Tooltip';
7-
import Button from '../Button/Button';
7+
import { Button } from '../Button/Button';
88

99
# Tooltip
1010

src/Tree/Tree.stories.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useState, useCallback } from 'react';
22
import styled from 'styled-components';
33

44
import { Tree, Fieldset } from 'react95';
5-
import Button from '../Button/Button';
5+
import { Button } from '../Button/Button';
66

77
const Wrapper = styled.div`
88
background: ${({ theme }) => theme.material};

0 commit comments

Comments
 (0)