-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add toggle component used to switch between two elements (String or JSX). Closes DCOS-40043
- Loading branch information
1 parent
29929a6
commit 22dc937
Showing
12 changed files
with
180 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { default as Clickable } from "./clickable"; | ||
export { default as Clickable } from "./components/clickable"; |
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 |
---|---|---|
@@ -1,10 +1,7 @@ | ||
import { css } from "emotion"; | ||
import { coreColors } from "../shared/styles/color"; | ||
|
||
const { white } = coreColors(); | ||
export const outline = css` | ||
&:focus { | ||
box-shadow: 0 0 0 1px ${white}, 0 0 0 3px currentColor; | ||
outline: 0; | ||
} | ||
`; |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { injectGlobalCss } from "./shared/styles/global"; | ||
export { Badge, BadgeButton } from "./badge"; | ||
export { Column, Table, Cell, TextCell, HeaderCell, NumberCell } from "./table"; | ||
export { ToggleContent } from "./toggleContent"; | ||
|
||
injectGlobalCss(); |
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,3 @@ | ||
# Toggle | ||
|
||
Toggle component is used when there is a need to switch between two contents when there is a click from the user. |
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,54 @@ | ||
import * as React from "react"; | ||
import Clickable from "../../clickable/components/clickable"; | ||
import { style } from "../style"; | ||
|
||
export interface IToggleContentState { | ||
isOn: boolean; | ||
} | ||
|
||
export interface IToggleContentProps { | ||
contentOn: JSX.Element | string; | ||
contentOff: JSX.Element | string; | ||
tabIndex?: string | number; | ||
} | ||
|
||
export class ToggleContent extends React.PureComponent< | ||
IToggleContentProps, | ||
IToggleContentState | ||
> { | ||
static defaultProps = { | ||
tabIndex: -1 | ||
}; | ||
|
||
constructor(props) { | ||
super(props); | ||
|
||
this.handleClick = this.handleClick.bind(this); | ||
this.state = { | ||
isOn: true | ||
}; | ||
} | ||
|
||
public handleClick(): void { | ||
this.setState({ | ||
isOn: !this.state.isOn | ||
}); | ||
} | ||
|
||
public render() { | ||
const { tabIndex, contentOn, contentOff } = this.props; | ||
let content = contentOn; | ||
|
||
if (!this.state.isOn) { | ||
content = contentOff; | ||
} | ||
|
||
return ( | ||
<Clickable tabIndex={tabIndex} action={this.handleClick}> | ||
<div className={style}>{content}</div> | ||
</Clickable> | ||
); | ||
} | ||
} | ||
|
||
export default ToggleContent; |
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 @@ | ||
export { default as ToggleContent } from "./components/toggleContent"; |
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,18 @@ | ||
import * as React from "react"; | ||
import { storiesOf } from "@storybook/react"; | ||
import { withReadme } from "storybook-readme"; | ||
import { ToggleContent } from "../../index"; | ||
|
||
const readme = require("../README.md"); | ||
|
||
const primary = () => <div>primary component</div>; | ||
const secondary = () => <div>secondary component</div>; | ||
|
||
storiesOf("Toggle", module) | ||
.addDecorator(withReadme([readme])) | ||
.addWithInfo("string", () => ( | ||
<ToggleContent contentOn="Hello" contentOff="Bye" /> | ||
)) | ||
.addWithInfo("component", () => ( | ||
<ToggleContent contentOn={primary()} contentOff={secondary()} /> | ||
)); |
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,5 @@ | ||
import { css } from "emotion"; | ||
|
||
export const style = css` | ||
cursor: pointer; | ||
`; |
19 changes: 19 additions & 0 deletions
19
packages/toggleContent/tests/__snapshots__/toggleContent.test.tsx.snap
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,19 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`ToggleContent renders a div wrapping the content passed as props 1`] = ` | ||
.emotion-0 { | ||
cursor: pointer; | ||
} | ||
.emotion-0:focus { | ||
outline: 0; | ||
} | ||
<div | ||
class="emotion-0" | ||
role="button" | ||
tabindex="0" | ||
> | ||
Hello | ||
</div> | ||
`; |
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,70 @@ | ||
import React from "react"; | ||
import * as emotion from "emotion"; | ||
import { createSerializer } from "jest-emotion"; | ||
import { mount, render } from "enzyme"; | ||
import toJson from "enzyme-to-json"; | ||
|
||
import { ToggleContent } from "../"; | ||
|
||
expect.addSnapshotSerializer(createSerializer(emotion)); | ||
|
||
const primary = () => <div>primary</div>; | ||
const secondary = () => <div>secondary</div>; | ||
|
||
describe("ToggleContent", () => { | ||
it("renders a div wrapping the content passed as props", () => { | ||
expect( | ||
toJson( | ||
render( | ||
<ToggleContent tabIndex="0" contentOn="Hello" contentOff="Bye" /> | ||
) | ||
) | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it("string content", () => { | ||
const wrapper = mount(<ToggleContent contentOn="Hello" contentOff="Bye" />); | ||
|
||
expect( | ||
wrapper | ||
.last() | ||
.children() | ||
.last() | ||
.text() | ||
).toEqual("Hello"); | ||
|
||
wrapper.simulate("click"); | ||
|
||
expect( | ||
wrapper | ||
.last() | ||
.children() | ||
.last() | ||
.text() | ||
).toEqual("Bye"); | ||
}); | ||
|
||
it("component content", () => { | ||
const wrapper = mount( | ||
<ToggleContent contentOn={primary()} contentOff={secondary()} /> | ||
); | ||
|
||
expect( | ||
wrapper | ||
.last() | ||
.children() | ||
.last() | ||
.text() | ||
).toEqual("primary"); | ||
|
||
wrapper.simulate("click"); | ||
|
||
expect( | ||
wrapper | ||
.last() | ||
.children() | ||
.last() | ||
.text() | ||
).toEqual("secondary"); | ||
}); | ||
}); |