-
Notifications
You must be signed in to change notification settings - Fork 67
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
1 parent
c83558d
commit ce98d42
Showing
9 changed files
with
250 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from "react"; | ||
import cls from "classnames"; | ||
import PropTypes from "prop-types"; | ||
|
||
export default class Collapse extends React.PureComponent { | ||
static defaultProps = { | ||
prefixCls: "cuke-collapse" | ||
}; | ||
static propTypes = { | ||
prefixCls: PropTypes.string.isRequired | ||
}; | ||
render() { | ||
const { className, prefixCls, children, ...attr } = this.props; | ||
|
||
const items = React.Children.map(children, (element, index) => { | ||
return React.cloneElement(element, { | ||
key: index | ||
}); | ||
}); | ||
|
||
return ( | ||
<div className={cls(prefixCls, className)} {...attr}> | ||
{items} | ||
</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,49 @@ | ||
import React from "react"; | ||
import cls from "classnames"; | ||
import PropTypes from "prop-types"; | ||
import { ArrowRightIcon } from "../icon"; | ||
|
||
export default class CollapseItem extends React.PureComponent { | ||
state = { | ||
visible: false | ||
}; | ||
static defaultProps = { | ||
prefixCls: "cuke-collapse-item" | ||
}; | ||
static propTypes = { | ||
prefixCls: PropTypes.string.isRequired | ||
}; | ||
toggleContentPanel = () => { | ||
this.setState({ | ||
visible: !this.state.visible | ||
}); | ||
}; | ||
render() { | ||
const { title, children, className, prefixCls, ...attr } = this.props; | ||
const { visible } = this.state; | ||
return ( | ||
<div className={cls(prefixCls, className)} {...attr}> | ||
<div | ||
className={cls(`${prefixCls}-header`)} | ||
onClick={this.toggleContentPanel} | ||
> | ||
<span | ||
className={cls(`${prefixCls}-arrow`, { | ||
[`${prefixCls}-arrow-active`]: visible | ||
})} | ||
> | ||
<ArrowRightIcon /> | ||
</span> | ||
{title} | ||
</div> | ||
<div | ||
className={cls(`${prefixCls}-content`, { | ||
[`${prefixCls}-hide`]: !visible | ||
})} | ||
> | ||
{children} | ||
</div> | ||
</div> | ||
); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
components/collapse/__tests__/__snapshots__/index.test.js.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,74 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`<Collapse/> should render Collapse 1`] = ` | ||
<div | ||
class="cuke-collapse" | ||
> | ||
<div | ||
class="cuke-collapse-item" | ||
> | ||
<div | ||
class="cuke-collapse-item-header" | ||
> | ||
<span | ||
class="cuke-collapse-item-arrow" | ||
> | ||
<svg | ||
attr="[object Object]" | ||
fill="none" | ||
height="1em" | ||
stroke="currentColor" | ||
stroke-linecap="round" | ||
stroke-linejoin="round" | ||
stroke-width="2" | ||
viewBox="0 0 24 24" | ||
width="1em" | ||
> | ||
<polyline | ||
points="9 18 15 12 9 6" | ||
/> | ||
</svg> | ||
</span> | ||
标题1 | ||
</div> | ||
<div | ||
class="cuke-collapse-item-content cuke-collapse-item-hide" | ||
> | ||
内容1 | ||
</div> | ||
</div> | ||
<div | ||
class="cuke-collapse-item" | ||
> | ||
<div | ||
class="cuke-collapse-item-header" | ||
> | ||
<span | ||
class="cuke-collapse-item-arrow" | ||
> | ||
<svg | ||
attr="[object Object]" | ||
fill="none" | ||
height="1em" | ||
stroke="currentColor" | ||
stroke-linecap="round" | ||
stroke-linejoin="round" | ||
stroke-width="2" | ||
viewBox="0 0 24 24" | ||
width="1em" | ||
> | ||
<polyline | ||
points="9 18 15 12 9 6" | ||
/> | ||
</svg> | ||
</span> | ||
标题2 | ||
</div> | ||
<div | ||
class="cuke-collapse-item-content cuke-collapse-item-hide" | ||
> | ||
内容2 | ||
</div> | ||
</div> | ||
</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,27 @@ | ||
import React from "react"; | ||
import { render, shallow } from "enzyme"; | ||
import assert from "power-assert"; | ||
import toJson from "enzyme-to-json"; | ||
import Collapse from "../index"; | ||
|
||
describe("<Collapse/>", () => { | ||
it("should render Collapse", () => { | ||
const wrapper = render( | ||
<Collapse> | ||
<Collapse.Item title="标题1">内容1</Collapse.Item> | ||
<Collapse.Item title="标题2">内容2</Collapse.Item> | ||
</Collapse> | ||
); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
|
||
it("should find cuke-collapse class name", () => { | ||
const wrapper = shallow( | ||
<Collapse> | ||
<Collapse.Item title="标题1">内容1</Collapse.Item> | ||
<Collapse.Item title="标题2">内容2</Collapse.Item> | ||
</Collapse> | ||
); | ||
assert(wrapper.find(".cuke-collapse").length === 1); | ||
}); | ||
}); |
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,6 @@ | ||
import Collapse from "./Collapse"; | ||
import CollapseItem from "./CollapseItem"; | ||
|
||
Collapse.Item = CollapseItem; | ||
|
||
export default Collapse; |
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,46 @@ | ||
@import "../styles/vars.less"; | ||
|
||
|
||
@prefixCls : cuke-collapse; | ||
.@{prefixCls} { | ||
border: 1px solid @border-color; | ||
border-radius: @border-radius; | ||
font-size: @font-size; | ||
box-shadow: @default-shadow; | ||
position: relative; | ||
|
||
&-item { | ||
border-bottom: 1px solid @border-color; | ||
|
||
&-arrow { | ||
font-size: 16px; | ||
vertical-align: middle; | ||
margin-right: 10px; | ||
svg { | ||
transition: @default-transition; | ||
} | ||
} | ||
|
||
&-arrow-active { | ||
svg { | ||
transform: rotate(90deg) | ||
} | ||
} | ||
&-header { | ||
padding: 15px 20px; | ||
cursor: pointer; | ||
color: fade(@font-color,90%); | ||
background-color: @bg-color; | ||
} | ||
&-content { | ||
padding: 20px; | ||
color: @font-color; | ||
overflow: hidden; | ||
transition: all @default-transition; | ||
} | ||
|
||
&-hide { | ||
display: none; | ||
} | ||
} | ||
} |
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