Skip to content

Commit 1c16025

Browse files
WesSouzaarturbien
authored andcommitted
feat(checkbox): convert to TypeScript and export types
1 parent d42af74 commit 1c16025

File tree

5 files changed

+118
-131
lines changed

5 files changed

+118
-131
lines changed

src/Checkbox/Checkbox.mdx

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ name: Checkbox
33
menu: Components
44
---
55

6-
import Checkbox from './Checkbox'
7-
import Fieldset from '../Fieldset/Fieldset'
8-
import Button from '../Button/Button'
9-
import Cutout from '../Cutout/Cutout'
10-
import List from '../List/List'
11-
import ListItem from '../ListItem/ListItem'
12-
import Divider from '../Divider/Divider'
6+
import { Checkbox } from './Checkbox';
7+
import Fieldset from '../Fieldset/Fieldset';
8+
import Button from '../Button/Button';
9+
import Cutout from '../Cutout/Cutout';
10+
import List from '../List/List';
11+
import ListItem from '../ListItem/ListItem';
12+
import Divider from '../Divider/Divider';
1313

1414
# Checkbox
1515

@@ -19,29 +19,31 @@ import Divider from '../Divider/Divider'
1919

2020
<Playground>
2121
{() => {
22-
const [steak, setSteak] = React.useState(true)
23-
const [tortilla, setTortilla] = React.useState(false)
24-
const [pizza, setPizza] = React.useState(false)
22+
const [steak, setSteak] = React.useState(true);
23+
const [tortilla, setTortilla] = React.useState(false);
24+
const [pizza, setPizza] = React.useState(false);
2525
const handleChange = event => {
26-
const { target: { value } } = event;
26+
const {
27+
target: { value }
28+
} = event;
2729
if (value === 'steak') {
28-
setSteak(!steak)
29-
return
30+
setSteak(!steak);
31+
return;
3032
}
3133
if (value === 'tortilla') {
32-
setTortilla(!tortilla)
33-
return
34+
setTortilla(!tortilla);
35+
return;
3436
}
3537
if (value === 'pizza') {
36-
setPizza(!pizza)
37-
return
38+
setPizza(!pizza);
39+
return;
3840
}
3941
};
4042
const reset = () => {
41-
setSteak(false)
42-
setTortilla(false)
43-
setPizza(false)
44-
}
43+
setSteak(false);
44+
setTortilla(false);
45+
setPizza(false);
46+
};
4547
return (
4648
<div style={{ maxWidth: '250px' }}>
4749
<Fieldset label='Party food'>
@@ -94,11 +96,12 @@ import Divider from '../Divider/Divider'
9496
</Playground>
9597

9698
#### Flat
99+
97100
<Playground>
98101
<Cutout style={{ padding: '1rem', width: '300px' }}>
99102
<p style={{ lineHeight: 1.3 }}>
100-
When you want to add input field on a light background (like
101-
scrollable content), just use the flat variant:
103+
When you want to add input field on a light background (like scrollable
104+
content), just use the flat variant:
102105
</p>
103106
<div style={{ marginTop: '1rem' }}>
104107
<Checkbox
@@ -128,6 +131,7 @@ import Divider from '../Divider/Divider'
128131
</Playground>
129132

130133
#### Menu
134+
131135
<Playground>
132136
<List>
133137
<ListItem size='md'>

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import React from 'react';
21
import { renderWithTheme } from '../../test/utils';
3-
import Checkbox from './Checkbox';
2+
import { Checkbox } from './Checkbox';
43

54
describe('<Checkbox />', () => {
65
describe('label', () => {
@@ -106,7 +105,7 @@ describe('<Checkbox />', () => {
106105
);
107106

108107
rerender(<Checkbox checked />);
109-
const checkbox = getByRole('checkbox');
108+
const checkbox = getByRole('checkbox') as HTMLInputElement;
110109

111110
expect(checkbox.checked).toBe(true);
112111
expect(getByRole('checkbox')).toHaveAttribute('checked');
@@ -119,7 +118,7 @@ describe('<Checkbox />', () => {
119118
it('should uncheck the checkbox', () => {
120119
const { getByRole, rerender } = renderWithTheme(<Checkbox checked />);
121120
rerender(<Checkbox checked={false} />);
122-
const checkbox = getByRole('checkbox');
121+
const checkbox = getByRole('checkbox') as HTMLInputElement;
123122

124123
expect(checkbox.checked).toBe(false);
125124
expect(getByRole('checkbox')).not.toHaveAttribute('checked');
@@ -131,7 +130,7 @@ describe('<Checkbox />', () => {
131130
describe('uncontrolled', () => {
132131
it('can change checked state uncontrolled starting from defaultChecked', () => {
133132
const { getByRole } = renderWithTheme(<Checkbox defaultChecked />);
134-
const checkbox = getByRole('checkbox');
133+
const checkbox = getByRole('checkbox') as HTMLInputElement;
135134

136135
expect(checkbox.checked).toBe(true);
137136

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { useState } from 'react';
22
import styled from 'styled-components';
33

44
import { Checkbox, Fieldset, Cutout, List, ListItem, Divider } from 'react95';
5+
import { ComponentMeta } from '@storybook/react';
56

67
const Wrapper = styled.div`
78
background: ${({ theme }) => theme.material};
@@ -21,7 +22,7 @@ export default {
2122
title: 'Checkbox',
2223
component: Checkbox,
2324
decorators: [story => <Wrapper>{story()}</Wrapper>]
24-
};
25+
} as ComponentMeta<typeof Checkbox>;
2526

2627
export function Default() {
2728
const [state, setState] = useState({
@@ -33,7 +34,7 @@ export function Default() {
3334
const { cheese, bacon, broccoli } = state;
3435
const ingredientsArr = Object.values(state).map(val => (val ? 1 : 0));
3536
const possibleIngredients = Object.keys(state).length;
36-
const chosenIngredients = ingredientsArr.reduce((a, b) => a + b, 0);
37+
const chosenIngredients = ingredientsArr.reduce<number>((a, b) => a + b, 0);
3738

3839
const isIndeterminate = ![0, possibleIngredients].includes(chosenIngredients);
3940

@@ -60,10 +61,8 @@ export function Default() {
6061
}
6162
};
6263

63-
const toggleIngredient = e => {
64-
const {
65-
target: { value }
66-
} = e;
64+
const toggleIngredient = (e: React.ChangeEvent<HTMLInputElement>) => {
65+
const value = e.target.value as 'cheese' | 'bacon' | 'broccoli';
6766
setState({
6867
...state,
6968
[value]: !state[value]
@@ -135,7 +134,7 @@ export function Flat() {
135134
const { cheese, bacon, broccoli } = state;
136135
const ingredientsArr = Object.values(state).map(val => (val ? 1 : 0));
137136
const possibleIngredients = Object.keys(state).length;
138-
const chosenIngredients = ingredientsArr.reduce((a, b) => a + b, 0);
137+
const chosenIngredients = ingredientsArr.reduce<number>((a, b) => a + b, 0);
139138

140139
const isIndeterminate = ![0, possibleIngredients].includes(chosenIngredients);
141140

@@ -162,10 +161,8 @@ export function Flat() {
162161
}
163162
};
164163

165-
const toggleIngredient = e => {
166-
const {
167-
target: { value }
168-
} = e;
164+
const toggleIngredient = (e: React.ChangeEvent<HTMLInputElement>) => {
165+
const value = e.target.value as 'cheese' | 'bacon' | 'broccoli';
169166
setState({
170167
...state,
171168
[value]: !state[value]

0 commit comments

Comments
 (0)