Skip to content

Commit

Permalink
[PLAY-784] Converting Toggle kit to Typescript (#2510)
Browse files Browse the repository at this point in the history
**What does this PR do?**
Converting Toggle kit to Typescript.

[Runway
Ticket](https://nitro.powerhrg.com/runway/backlog_items/PLAY-784)

**How to test?**
1. Go to Toggle kit on Playbook docs
2. Ensure all the examples still work

#### Checklist:
- [x] **LABELS** Add a label: `enhancement`, `bug`, `improvement`, `new
kit`, `deprecated`, or `breaking`. See [Changelog &
Labels](https://github.com/powerhome/playbook/wiki/Changelog-&-Labels)
for details.
- [x] **DEPLOY** I have added the `milano` label to show I'm ready for a
review.
- [x] **TESTS** I have added test coverage to my code.

---------

Co-authored-by: Nida Ghuman <[email protected]>
  • Loading branch information
carloslimasd and nidaqg authored May 3, 2023
1 parent 434c25f commit d130ce0
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @flow

import React from 'react'
import classnames from 'classnames'
import type { InputCallback } from '../types'
Expand All @@ -10,20 +8,20 @@ import {
buildDataProps,
} from '../utilities/props'

import { globalProps } from '../utilities/globalProps'
import { GlobalProps, globalProps } from '../utilities/globalProps'

type Props = {
aria?: object,
aria?: { [key: string]: string },
checked?: boolean,
children?: React.Node,
children?: React.ReactNode | React.ReactNode[],
className?: string,
data?: object,
data?: { [key: string]: string },
id?: string,
name?: string,
onChange?: InputCallback<HTMLInputElement>,
size?: "sm" | "md",
value?: string,
}
} & GlobalProps

const Toggle = ({
aria = {},
Expand All @@ -33,7 +31,7 @@ const Toggle = ({
data = {},
id,
name,
onChange = () => {},
onChange = () => { },
size = 'sm',
value,
...props
Expand All @@ -51,24 +49,24 @@ const Toggle = ({

return (
<div
{...ariaProps}
{...dataProps}
className={classnames(css, globalProps(props), className)}
id={id}
{...ariaProps}
{...dataProps}
className={classnames(css, globalProps(props), className)}
id={id}
>
<label className="pb_toggle_wrapper">
<If condition={children}>
{children}
<Else />
{children && children}

{!children &&
<input
{...props}
defaultChecked={checked}
name={name}
onChange={onChange}
type="checkbox"
value={value}
{...props}
defaultChecked={checked}
name={name}
onChange={onChange}
type="checkbox"
value={value}
/>
</If>
}
<div className="pb_toggle_control" />
</label>
</div>
Expand Down
67 changes: 67 additions & 0 deletions playbook/app/pb_kits/playbook/pb_toggle/toggle.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React from 'react'
import { render, screen } from '../utilities/test-utils'

import Toggle from './_toggle'

const testId = 'toggle'
const className = 'custom-class-name'

const ToggleDefault = (props) => (
<>
<Toggle
aria={{ label: testId }}
className={className}
data={{ testid: testId }}
id={testId}
{...props}
/>
</>
)

test('should pass data prop', () => {
render(<ToggleDefault />)
const kit = screen.getByTestId(testId)
expect(kit).toBeInTheDocument()
})

test('should pass className prop', () => {
render(<ToggleDefault />)
const kit = screen.getByTestId(testId)
expect(kit).toHaveClass(className)
})

test('should pass aria prop', () => {
render(<ToggleDefault />)
const kit = screen.getByTestId(testId)
expect(kit).toHaveAttribute('aria-label', testId)
})

test('should pass id prop', () => {
render(<ToggleDefault />)
const kit = screen.getByTestId(testId)
expect(kit).toHaveProperty('id', testId)
})

test('should not be checked by default', () => {
render(<ToggleDefault />)
const kit = screen.getByTestId(testId)
expect(kit).toHaveClass('pb_toggle_kit_sm_off')
})

test('should pass checked prop', () => {
render(<ToggleDefault checked />)
const kit = screen.getByTestId(testId)
expect(kit).toHaveClass('pb_toggle_kit_sm_on')
})

test('should have sm size by default', () => {
render(<ToggleDefault />)
const kit = screen.getByTestId(testId)
expect(kit).toHaveClass('pb_toggle_kit_sm_off')
})

test('should pass size prop', () => {
render(<ToggleDefault size='md' />)
const kit = screen.getByTestId(testId)
expect(kit).toHaveClass('pb_toggle_kit_md_off')
})

0 comments on commit d130ce0

Please sign in to comment.