-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
628 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Description, DoDont, Meta, Props } from 'docsComponents'; | ||
|
||
<Meta title="Toggle" /> | ||
|
||
# Toggle | ||
|
||
<Description of="Toggle" /> | ||
|
||
## Props | ||
|
||
<Props of="Toggle" /> | ||
|
||
## Examples | ||
|
||
### Basic | ||
|
||
A basic usage of Toggle. | ||
|
||
```js live | ||
<Toggle>Children are placed next to the toggle.</Toggle> | ||
``` | ||
|
||
### Disabled | ||
|
||
Toggles can be disabled. | ||
|
||
```js live | ||
<> | ||
<Toggle disabled checked /> | ||
</> | ||
``` | ||
|
||
### Icons | ||
|
||
Toggles can have icons to indicate on and off states. | ||
|
||
```js live | ||
// import { check, x } from 'icon-packs/feather'; | ||
|
||
<Toggle offIcon={x} onIcon={check} /> | ||
``` | ||
|
||
### Size | ||
|
||
Toggles can have different sizes. | ||
|
||
```js live | ||
// import { check, x } from 'icon-packs/feather'; | ||
|
||
<Row gutter={1} align="center"> | ||
<Col span={8}> | ||
<Toggle offIcon={x} onIcon={check} size="sm"> | ||
sm | ||
</Toggle> | ||
</Col> | ||
<Col span={8}> | ||
<Toggle offIcon={x} onIcon={check} size="md"> | ||
md | ||
</Toggle> | ||
</Col> | ||
<Col span={8}> | ||
<Toggle offIcon={x} onIcon={check} size="lg"> | ||
lg | ||
</Toggle> | ||
</Col> | ||
</Row> | ||
``` | ||
|
||
## Design Guidelines | ||
|
||
<DoDont | ||
do={[ | ||
'Use a toggle when the effect of state changes are immediate e.g. switch color theme.', | ||
'Make use of a label when possible otherwise set aria-label.' | ||
]} | ||
dont={[ | ||
'Use a toggle to check multiple items in a list. Checkboxes are used in that case.' | ||
]} | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { render } from '@testing-library/react'; | ||
import { activity } from 'icon-packs/cjs/feather'; | ||
import React, { createRef } from 'react'; | ||
import { Toggle } from './Toggle'; | ||
|
||
describe('Toggle', () => { | ||
it('renders an input element and sets child text', () => { | ||
render(<Toggle>Hello world!</Toggle>); | ||
const container = document.querySelector('.mvn--toggle'); | ||
const input = document.querySelector('input'); | ||
expect(container).toBeInTheDocument(); | ||
expect(input).toBeInTheDocument(); | ||
expect(container).toHaveTextContent('Hello world!'); | ||
}); | ||
|
||
it('sets className', () => { | ||
render(<Toggle className="toggle-class">Hello world!</Toggle>); | ||
const element = document.querySelector('.mvn--toggle'); | ||
expect(element).toHaveClass('toggle-class'); | ||
}); | ||
|
||
it('passes props', () => { | ||
render( | ||
<Toggle id="ToggleId" data-test="toggle-data"> | ||
Hello world! | ||
</Toggle> | ||
); | ||
const element = document.querySelector('input'); | ||
expect(element).toHaveAttribute('id', 'ToggleId'); | ||
expect(element).toHaveAttribute('data-test', 'toggle-data'); | ||
}); | ||
|
||
describe('disabled', () => { | ||
it('is not disabled by default', () => { | ||
render(<Toggle />); | ||
const input = document.querySelector('input'); | ||
expect(input).not.toHaveAttribute('disabled'); | ||
}); | ||
|
||
it('sets disabled', () => { | ||
render(<Toggle disabled />); | ||
const input = document.querySelector('input'); | ||
expect(input).toHaveAttribute('disabled'); | ||
}); | ||
}); | ||
|
||
describe('icons', () => { | ||
it('has no icon by default', () => { | ||
render(<Toggle />); | ||
const svg = document.querySelector('svg'); | ||
const img = document.querySelector('img'); | ||
expect(svg).not.toBeInTheDocument(); | ||
expect(img).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('sets offIcon', () => { | ||
render(<Toggle offIcon={activity} />); | ||
const svg = document.querySelectorAll('svg'); | ||
expect(svg).toHaveLength(1); | ||
}); | ||
|
||
it('sets onIcon', () => { | ||
render(<Toggle onIcon={activity} />); | ||
const svg = document.querySelectorAll('svg'); | ||
expect(svg).toHaveLength(1); | ||
}); | ||
|
||
it('sets both icons', () => { | ||
render(<Toggle offIcon={activity} onIcon={activity} />); | ||
const svg = document.querySelectorAll('svg'); | ||
expect(svg).toHaveLength(2); | ||
}); | ||
}); | ||
|
||
describe('label', () => { | ||
it('has no aria-describedby by default', () => { | ||
render(<Toggle />); | ||
const input = document.querySelector('input'); | ||
expect(input).not.toHaveAttribute('aria-describedby'); | ||
}); | ||
|
||
it('sets aria-describedby by when input has a label', () => { | ||
render(<Toggle label="Label" />); | ||
const input = document.querySelector('input'); | ||
expect(input).toHaveAttribute('aria-describedby'); | ||
}); | ||
}); | ||
|
||
describe('size', () => { | ||
it('is md by default', () => { | ||
render(<Toggle />); | ||
const container = document.querySelector('.mvn--toggle'); | ||
expect(container).toHaveClass('md'); | ||
expect(container).not.toHaveClass('sm'); | ||
expect(container).not.toHaveClass('lg'); | ||
}); | ||
|
||
it('sets sm', () => { | ||
render(<Toggle size="sm" />); | ||
const container = document.querySelector('.mvn--toggle'); | ||
expect(container).toHaveClass('sm'); | ||
}); | ||
|
||
it('sets lg', () => { | ||
render(<Toggle size="lg" />); | ||
const container = document.querySelector('.mvn--toggle'); | ||
expect(container).toHaveClass('lg'); | ||
}); | ||
}); | ||
|
||
describe('forwarding ref', () => { | ||
it('sets ref', () => { | ||
const ref = createRef<HTMLInputElement>(); | ||
render(<Toggle ref={ref} />); | ||
const element = document.querySelector('input'); | ||
expect(ref.current).toBe(element); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.