afc163
8 years ago
13 changed files with 356 additions and 132 deletions
@ -0,0 +1,33 @@ |
|||
import React from 'react'; |
|||
import classNames from 'classnames'; |
|||
import splitObject from '../_util/splitObject'; |
|||
|
|||
export interface CheckableTagProps { |
|||
prefixCls?: string; |
|||
className?: string; |
|||
checked: boolean; |
|||
onChange?: (checked) => void; |
|||
} |
|||
|
|||
export default class CheckableTag extends React.Component<CheckableTagProps, any> { |
|||
handleClick = () => { |
|||
const { checked, onChange } = this.props; |
|||
if (onChange) { |
|||
onChange(!checked); |
|||
} |
|||
} |
|||
render() { |
|||
const [{ prefixCls = 'ant-tag', className = '', checked }, restProps ] = splitObject( |
|||
this.props, ['prefixCls', 'className', 'checked'] |
|||
); |
|||
const cls = classNames({ |
|||
[`${prefixCls}`]: true, |
|||
[`${prefixCls}-checkable`]: true, |
|||
[`${prefixCls}-checkable-checked`]: checked, |
|||
[className]: className, |
|||
}); |
|||
|
|||
delete restProps.onChange; |
|||
return <div {...restProps} className={cls} onClick={this.handleClick} />; |
|||
} |
|||
} |
@ -1,28 +1,32 @@ |
|||
--- |
|||
order: 0 |
|||
title: |
|||
title: |
|||
zh-CN: 基本 |
|||
en-US: Basic |
|||
--- |
|||
|
|||
## zh-CN |
|||
|
|||
简单的标签展示,添加 closable 表示可关闭。 |
|||
基本标签的用法,可以通过添加 `closable` 变为可关闭标签。可关闭标签具有 `onClose` `afterClose` 两个事件。 |
|||
|
|||
## en-US |
|||
|
|||
Simple demo of tag, 'closable' property represents which can be closed. |
|||
Usage of basic Tag, and it could be closable by set `closable` property. Closable Tag supports `onClose` `afterClose` events. |
|||
|
|||
````jsx |
|||
import { Tag } from 'antd'; |
|||
|
|||
function onClose(e) { |
|||
function log(e) { |
|||
console.log(e); |
|||
} |
|||
|
|||
ReactDOM.render(<div> |
|||
<Tag>Tag 1</Tag> |
|||
<Tag>Tag 2</Tag> |
|||
<Tag closable onClose={onClose}>Tag 3</Tag> |
|||
</div>, mountNode); |
|||
ReactDOM.render( |
|||
<div> |
|||
<Tag>Tag 1</Tag> |
|||
<Tag closable onClose={log}>Tag 2</Tag> |
|||
<Tag closable afterClose={log}>Tag 3</Tag> |
|||
<Tag closable><a href="https://github.com/ant-design/ant-design/issues/1862">Link</a></Tag> |
|||
</div>, |
|||
mountNode |
|||
); |
|||
```` |
|||
|
@ -0,0 +1,39 @@ |
|||
--- |
|||
order: 3 |
|||
title: |
|||
zh-CN: 可选择 |
|||
en-US: Checkable |
|||
--- |
|||
|
|||
## zh-CN |
|||
|
|||
可通过 Tag.CheckableTag 实现类似 Checkbox 的效果,该组件为完全受控组件,不支持非受控用法。 |
|||
|
|||
## en-US |
|||
|
|||
Tag.CheckableTag works like Checkbox, and it is an absolute controlled component and has no uncontrolled mode. |
|||
|
|||
````jsx |
|||
import { Tag } from 'antd'; |
|||
const CheckableTag = Tag.CheckableTag; |
|||
|
|||
class UncontrolledCheckableTag extends React.Component { |
|||
state = { checked: false }; |
|||
handleChange = (checked) => { |
|||
this.setState({ checked }); |
|||
} |
|||
|
|||
render() { |
|||
return <CheckableTag {...this.props} checked={this.state.checked} onChange={this.handleChange} />; |
|||
} |
|||
} |
|||
|
|||
ReactDOM.render( |
|||
<div> |
|||
<CheckableTag>Unchecked</CheckableTag> |
|||
<CheckableTag checked>Checked</CheckableTag> |
|||
<UncontrolledCheckableTag>Uncontrolled</UncontrolledCheckableTag> |
|||
</div>, |
|||
mountNode |
|||
); |
|||
```` |
@ -0,0 +1,56 @@ |
|||
--- |
|||
order: 4 |
|||
title: |
|||
zh-CN: 热门标签 |
|||
en-US: Hot Tags |
|||
--- |
|||
|
|||
## zh-CN |
|||
|
|||
选择你感兴趣的话题。 |
|||
|
|||
## en-US |
|||
|
|||
Select your favourite topics. |
|||
|
|||
````jsx |
|||
import { Tag } from 'antd'; |
|||
const CheckableTag = Tag.CheckableTag; |
|||
|
|||
const tagsFromServer = ['Movie', 'Books', 'Music']; |
|||
|
|||
class HotTags extends React.Component { |
|||
state = { |
|||
selectedTags: [], |
|||
}; |
|||
|
|||
handleChange(tag, checked) { |
|||
const { selectedTags } = this.state; |
|||
const nextSelectedTags = checked ? |
|||
[...selectedTags, tag] : |
|||
selectedTags.filter(t => t !== tag); |
|||
console.log('You are interested in: ', nextSelectedTags); |
|||
this.setState({ selectedTags: nextSelectedTags }); |
|||
} |
|||
|
|||
render() { |
|||
const { selectedTags } = this.state; |
|||
return ( |
|||
<div> |
|||
<strong>Hots: </strong> |
|||
{tagsFromServer.map(tag => ( |
|||
<CheckableTag |
|||
key={tag} |
|||
checked={selectedTags.indexOf(tag) > -1} |
|||
onChange={checked => this.handleChange(tag, checked)} |
|||
> |
|||
{tag} |
|||
</CheckableTag> |
|||
))} |
|||
</div> |
|||
); |
|||
} |
|||
} |
|||
|
|||
ReactDOM.render(<HotTags />, mountNode); |
|||
```` |
Loading…
Reference in new issue