diff --git a/cookbook-react/src/recipes/tab-bars/tab-bar-1/__snapshots__/index.test.tsx.snap b/cookbook-react/src/recipes/tab-bars/tab-bar-1/__snapshots__/index.test.tsx.snap new file mode 100644 index 00000000..285d734c --- /dev/null +++ b/cookbook-react/src/recipes/tab-bars/tab-bar-1/__snapshots__/index.test.tsx.snap @@ -0,0 +1,40 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Tab-bar-1 match snapshot 1`] = ` +
+
+
+ +
+ + + +
+
+
+
+`; diff --git a/cookbook-react/src/recipes/tab-bars/tab-bar-1/index.test.tsx b/cookbook-react/src/recipes/tab-bars/tab-bar-1/index.test.tsx new file mode 100644 index 00000000..1bc2c086 --- /dev/null +++ b/cookbook-react/src/recipes/tab-bars/tab-bar-1/index.test.tsx @@ -0,0 +1,27 @@ +import React from 'react'; +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; + +import TabBar from './index'; + +describe('Tab-bar-1', () => { + const options = [ + { id: 1, label: 'Sección 1' }, + { id: 2, label: 'Sección 2' }, + { id: 3, label: 'Sección 3' } + ]; + + test('match snapshot ', () => { + const { container } = render(); + expect(container).toMatchSnapshot(); + }); + + test('select second tab', () => { + render(); + const tabElement = screen.getByText('Sección 2'); + + userEvent.click(tabElement); + + expect(tabElement).toHaveClass('selected'); + }); +}); diff --git a/cookbook-react/src/recipes/tab-bars/tab-bar-2/__snapshots__/tab-bar-2.test.tsx.snap b/cookbook-react/src/recipes/tab-bars/tab-bar-2/__snapshots__/tab-bar-2.test.tsx.snap new file mode 100644 index 00000000..dafe351a --- /dev/null +++ b/cookbook-react/src/recipes/tab-bars/tab-bar-2/__snapshots__/tab-bar-2.test.tsx.snap @@ -0,0 +1,44 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`test tab-bar component match to the snapshot 1`] = ` +
+
+ + +
+
+`; diff --git a/cookbook-react/src/recipes/tab-bars/tab-bar-2/cookbook.json b/cookbook-react/src/recipes/tab-bars/tab-bar-2/cookbook.json new file mode 100644 index 00000000..5df0a82e --- /dev/null +++ b/cookbook-react/src/recipes/tab-bars/tab-bar-2/cookbook.json @@ -0,0 +1,11 @@ +{ + "thumbnail": { + "type": "iframe", + "url": "https://cookbook-react.now.sh/?category=tab-bars&component=tab-bar-2&compact=true" + }, + "detail": { + "type": "iframe", + "url": "https://cookbook-react.now.sh/?category=tab-bars&component=tab-bar-2&compact=true" + } + } + \ No newline at end of file diff --git a/cookbook-react/src/recipes/tab-bars/tab-bar-2/index.tsx b/cookbook-react/src/recipes/tab-bars/tab-bar-2/index.tsx new file mode 100644 index 00000000..1471a27c --- /dev/null +++ b/cookbook-react/src/recipes/tab-bars/tab-bar-2/index.tsx @@ -0,0 +1,54 @@ +import React from 'react'; +import cn from 'classnames'; + +import styles from './styles.module.scss'; + +interface Tab { + icon?: string; + label?: string; + id: number; +} + +interface Props { + tabs?: Tab[]; + active?: null | number; + handleChange: (id: number) => void; +} + +function TabBar({ tabs, active, handleChange }: Props) { + return ( +
+ {tabs?.map((tab) => ( + + ))} +
+ ); +} + +export default TabBar; + +TabBar.defaultProps = { + active: null, + tabs: [ + { id: 1, label: 'Primera tab', icon: '' }, + { id: 2, label: 'Segunda tab', icon: '' } + ] +}; diff --git a/cookbook-react/src/recipes/tab-bars/tab-bar-2/styles.module.scss b/cookbook-react/src/recipes/tab-bars/tab-bar-2/styles.module.scss new file mode 100644 index 00000000..f4b3cbf1 --- /dev/null +++ b/cookbook-react/src/recipes/tab-bars/tab-bar-2/styles.module.scss @@ -0,0 +1,39 @@ +$color-blue: #003DD5; +$color-black: #000; +$color-white: #FFF; +$text-color: #525256; +$tabs: 2; +$height: 40px; +$width: ( 60 / $tabs ) * 1%; + +.tabs-container { + display: flex; + justify-content: space-evenly; + + .radio-label { + align-items: center; + border-radius: 40px; + color: $text-color; + cursor: pointer; + display: flex; + font-weight: 600; + flex: 0 0 $width; + height: $height; + justify-content: center; + transition: background-color 0.5s ease; + + .icon { + font-size: 14px; + margin-right: 10px; + } + + .tab-input { + display: none; + } + + &.active { + background-color: $color-blue; + color: $color-white; + } + } +} diff --git a/cookbook-react/src/recipes/tab-bars/tab-bar-2/tab-bar-2.test.tsx b/cookbook-react/src/recipes/tab-bars/tab-bar-2/tab-bar-2.test.tsx new file mode 100644 index 00000000..28bfda40 --- /dev/null +++ b/cookbook-react/src/recipes/tab-bars/tab-bar-2/tab-bar-2.test.tsx @@ -0,0 +1,38 @@ +import React from 'react'; +import { render, screen, fireEvent } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; + +import TabBar from './index'; + +const tabs = [ + { id: 1, label: 'Primera tab', icon: ':D' }, + { id: 2, label: 'Segunda tab', icon: ':C' } +]; +const active = null; +const handleChange = jest.fn(); + +describe('test tab-bar component', () => { + test('match to the snapshot', () => { + const { container } = render(); + expect(container).toMatchSnapshot(); + }); + + test('call handleChange function with the tab id', () => { + render(); + userEvent.click(screen.getByLabelText('Primera tab')); + expect(handleChange).toHaveBeenCalledWith(1); + }); + + test('show first tab icon', () => { + render(); + const icon = screen.getByRole('icon-1'); + expect(icon).toBeInTheDocument(); + }); + + test('select second tab', () => { + render(); + const tabElement = screen.getByLabelText('Segunda tab') as HTMLInputElement; + fireEvent.change(tabElement, { target: { value: 2 } }); + expect(tabElement.value).toBe('2'); + }); +});