Skip to content

Commit

Permalink
feat(checkbox): convert to TypeScript and export types
Browse files Browse the repository at this point in the history
  • Loading branch information
WesSouza authored and arturbien committed Jul 26, 2022
1 parent d42af74 commit 1c16025
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 131 deletions.
50 changes: 27 additions & 23 deletions src/Checkbox/Checkbox.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ name: Checkbox
menu: Components
---

import Checkbox from './Checkbox'
import Fieldset from '../Fieldset/Fieldset'
import Button from '../Button/Button'
import Cutout from '../Cutout/Cutout'
import List from '../List/List'
import ListItem from '../ListItem/ListItem'
import Divider from '../Divider/Divider'
import { Checkbox } from './Checkbox';
import Fieldset from '../Fieldset/Fieldset';
import Button from '../Button/Button';
import Cutout from '../Cutout/Cutout';
import List from '../List/List';
import ListItem from '../ListItem/ListItem';
import Divider from '../Divider/Divider';

# Checkbox

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

<Playground>
{() => {
const [steak, setSteak] = React.useState(true)
const [tortilla, setTortilla] = React.useState(false)
const [pizza, setPizza] = React.useState(false)
const [steak, setSteak] = React.useState(true);
const [tortilla, setTortilla] = React.useState(false);
const [pizza, setPizza] = React.useState(false);
const handleChange = event => {
const { target: { value } } = event;
const {
target: { value }
} = event;
if (value === 'steak') {
setSteak(!steak)
return
setSteak(!steak);
return;
}
if (value === 'tortilla') {
setTortilla(!tortilla)
return
setTortilla(!tortilla);
return;
}
if (value === 'pizza') {
setPizza(!pizza)
return
setPizza(!pizza);
return;
}
};
const reset = () => {
setSteak(false)
setTortilla(false)
setPizza(false)
}
setSteak(false);
setTortilla(false);
setPizza(false);
};
return (
<div style={{ maxWidth: '250px' }}>
<Fieldset label='Party food'>
Expand Down Expand Up @@ -94,11 +96,12 @@ import Divider from '../Divider/Divider'
</Playground>

#### Flat

<Playground>
<Cutout style={{ padding: '1rem', width: '300px' }}>
<p style={{ lineHeight: 1.3 }}>
When you want to add input field on a light background (like
scrollable content), just use the flat variant:
When you want to add input field on a light background (like scrollable
content), just use the flat variant:
</p>
<div style={{ marginTop: '1rem' }}>
<Checkbox
Expand Down Expand Up @@ -128,6 +131,7 @@ import Divider from '../Divider/Divider'
</Playground>

#### Menu

<Playground>
<List>
<ListItem size='md'>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import { renderWithTheme } from '../../test/utils';
import Checkbox from './Checkbox';
import { Checkbox } from './Checkbox';

describe('<Checkbox />', () => {
describe('label', () => {
Expand Down Expand Up @@ -106,7 +105,7 @@ describe('<Checkbox />', () => {
);

rerender(<Checkbox checked />);
const checkbox = getByRole('checkbox');
const checkbox = getByRole('checkbox') as HTMLInputElement;

expect(checkbox.checked).toBe(true);
expect(getByRole('checkbox')).toHaveAttribute('checked');
Expand All @@ -119,7 +118,7 @@ describe('<Checkbox />', () => {
it('should uncheck the checkbox', () => {
const { getByRole, rerender } = renderWithTheme(<Checkbox checked />);
rerender(<Checkbox checked={false} />);
const checkbox = getByRole('checkbox');
const checkbox = getByRole('checkbox') as HTMLInputElement;

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState } from 'react';
import styled from 'styled-components';

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

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

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

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

Expand All @@ -60,10 +61,8 @@ export function Default() {
}
};

const toggleIngredient = e => {
const {
target: { value }
} = e;
const toggleIngredient = (e: React.ChangeEvent<HTMLInputElement>) => {
const value = e.target.value as 'cheese' | 'bacon' | 'broccoli';
setState({
...state,
[value]: !state[value]
Expand Down Expand Up @@ -135,7 +134,7 @@ export function Flat() {
const { cheese, bacon, broccoli } = state;
const ingredientsArr = Object.values(state).map(val => (val ? 1 : 0));
const possibleIngredients = Object.keys(state).length;
const chosenIngredients = ingredientsArr.reduce((a, b) => a + b, 0);
const chosenIngredients = ingredientsArr.reduce<number>((a, b) => a + b, 0);

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

Expand All @@ -162,10 +161,8 @@ export function Flat() {
}
};

const toggleIngredient = e => {
const {
target: { value }
} = e;
const toggleIngredient = (e: React.ChangeEvent<HTMLInputElement>) => {
const value = e.target.value as 'cheese' | 'bacon' | 'broccoli';
setState({
...state,
[value]: !state[value]
Expand Down
Loading

0 comments on commit 1c16025

Please sign in to comment.