feat: update checkbox && collapse

pull/666/head
wangxueliang 2019-03-15 11:20:37 +08:00
parent 64b3ab1a55
commit 52b3883cf2
8 changed files with 110 additions and 27 deletions

View File

@ -2,6 +2,7 @@ import PropTypes from '../_util/vue-types';
import classNames from 'classnames'; import classNames from 'classnames';
import VcCheckbox from '../vc-checkbox'; import VcCheckbox from '../vc-checkbox';
import { getOptionProps, getAttrs } from '../_util/props-util'; import { getOptionProps, getAttrs } from '../_util/props-util';
import { ConfigConsumerProps } from '../config-provider';
function noop() {} function noop() {}
export default { export default {
@ -11,22 +12,20 @@ export default {
prop: 'checked', prop: 'checked',
}, },
props: { props: {
prefixCls: { prefixCls: PropTypes.string,
default: 'ant-checkbox',
type: String,
},
defaultChecked: PropTypes.bool, defaultChecked: PropTypes.bool,
checked: PropTypes.bool, checked: PropTypes.bool,
disabled: PropTypes.bool, disabled: PropTypes.bool,
isGroup: Boolean, isGroup: PropTypes.bool,
value: PropTypes.any, value: PropTypes.any,
name: String, name: PropTypes.string,
id: String, id: PropTypes.string,
indeterminate: Boolean, indeterminate: PropTypes.bool,
type: PropTypes.string.def('checkbox'), type: PropTypes.string.def('checkbox'),
autoFocus: Boolean, autoFocus: PropTypes.bool,
}, },
inject: { inject: {
configProvider: { default: () => ({}) },
checkboxGroupContext: { default: () => null }, checkboxGroupContext: { default: () => null },
}, },
methods: { methods: {
@ -48,7 +47,10 @@ export default {
const props = getOptionProps(this); const props = getOptionProps(this);
const children = $slots.default; const children = $slots.default;
const { mouseenter = noop, mouseleave = noop, ...restListeners } = $listeners; const { mouseenter = noop, mouseleave = noop, ...restListeners } = $listeners;
const { prefixCls, indeterminate, ...restProps } = props; const { prefixCls: customizePrefixCls, indeterminate, ...restProps } = props;
const getPrefixCls = this.configProvider.getPrefixCls || ConfigConsumerProps.getPrefixCls;
const prefixCls = getPrefixCls('checkbox', customizePrefixCls);
const checkboxProps = { const checkboxProps = {
props: { ...restProps, prefixCls }, props: { ...restProps, prefixCls },
on: restListeners, on: restListeners,

View File

@ -1,5 +1,7 @@
import Checkbox from './Checkbox'; import Checkbox from './Checkbox';
import hasProp from '../_util/props-util'; import hasProp from '../_util/props-util';
import { ConfigConsumerProps } from '../config-provider';
function noop() {} function noop() {}
export default { export default {
name: 'ACheckboxGroup', name: 'ACheckboxGroup',
@ -8,7 +10,6 @@ export default {
}, },
props: { props: {
prefixCls: { prefixCls: {
default: 'ant-checkbox',
type: String, type: String,
}, },
defaultValue: { defaultValue: {
@ -30,6 +31,9 @@ export default {
checkboxGroupContext: this, checkboxGroupContext: this,
}; };
}, },
inject: {
configProvider: { default: () => ({}) },
},
data() { data() {
const { value, defaultValue } = this; const { value, defaultValue } = this;
return { return {
@ -75,7 +79,10 @@ export default {
}, },
render() { render() {
const { $props: props, $data: state, $slots } = this; const { $props: props, $data: state, $slots } = this;
const { prefixCls, options } = props; const { prefixCls: customizePrefixCls, options } = props;
const getPrefixCls = this.configProvider.getPrefixCls || ConfigConsumerProps.getPrefixCls;
const prefixCls = getPrefixCls('checkbox', customizePrefixCls);
let children = $slots.default; let children = $slots.default;
const groupPrefixCls = `${prefixCls}-group`; const groupPrefixCls = `${prefixCls}-group`;
if (options && options.length > 0) { if (options && options.length > 0) {

View File

@ -1,7 +1,10 @@
import animation from '../_util/openAnimation'; import animation from '../_util/openAnimation';
import { getOptionProps, initDefaultProps } from '../_util/props-util'; import { getOptionProps, initDefaultProps, getComponentFromProp, isValidElement } from '../_util/props-util';
import { cloneElement } from '../_util/vnode';
import VcCollapse, { collapseProps } from '../vc-collapse'; import VcCollapse, { collapseProps } from '../vc-collapse';
import Icon from '../icon'; import Icon from '../icon';
import { ConfigConsumerProps } from '../config-provider';
export default { export default {
name: 'ACollapse', name: 'ACollapse',
model: { model: {
@ -13,20 +16,33 @@ export default {
bordered: true, bordered: true,
openAnimation: animation, openAnimation: animation,
}), }),
inject: {
configProvider: { default: () => ({}) },
},
methods: { methods: {
renderExpandIcon() { renderExpandIcon(panelProps, prefixCls) {
return <Icon type="right" class="arrow" />; const expandIcon = getComponentFromProp(this, 'expandIcon', panelProps);
const icon = expandIcon || <Icon type="right" rotate={panelProps.isActive ? 90 : undefined} />;
return isValidElement(expandIcon ? icon[0] : icon)
? cloneElement(icon, {
class: `${prefixCls}-arrow`,
})
: icon;
}, },
}, },
render() { render() {
const { prefixCls, bordered, $listeners } = this; const { prefixCls: customizePrefixCls, bordered, $listeners } = this;
const getPrefixCls = this.configProvider.getPrefixCls || ConfigConsumerProps.getPrefixCls;
const prefixCls = getPrefixCls('collapse', customizePrefixCls);
const collapseClassName = { const collapseClassName = {
[`${prefixCls}-borderless`]: !bordered, [`${prefixCls}-borderless`]: !bordered,
}; };
const rcCollapeProps = { const rcCollapeProps = {
props: { props: {
...getOptionProps(this), ...getOptionProps(this),
expandIcon: this.renderExpandIcon, prefixCls,
expandIcon: (panelProps) => this.renderExpandIcon(panelProps, prefixCls),
}, },
class: collapseClassName, class: collapseClassName,
on: $listeners, on: $listeners,

View File

@ -1,19 +1,27 @@
import { getOptionProps, getComponentFromProp } from '../_util/props-util'; import { getOptionProps, getComponentFromProp } from '../_util/props-util';
import VcCollapse, { panelProps } from '../vc-collapse'; import VcCollapse, { panelProps } from '../vc-collapse';
import { ConfigConsumerProps } from '../config-provider';
export default { export default {
name: 'ACollapsePanel', name: 'ACollapsePanel',
props: { props: {
...panelProps(), ...panelProps(),
}, },
inject: {
configProvider: { default: () => ({}) },
},
render() { render() {
const { prefixCls, showArrow = true, $listeners } = this; const { prefixCls: customizePrefixCls, showArrow = true, $listeners } = this;
const getPrefixCls = this.configProvider.getPrefixCls || ConfigConsumerProps.getPrefixCls;
const prefixCls = getPrefixCls('collapse', customizePrefixCls);
const collapsePanelClassName = { const collapsePanelClassName = {
[`${prefixCls}-no-arrow`]: !showArrow, [`${prefixCls}-no-arrow`]: !showArrow,
}; };
const rcCollapePanelProps = { const rcCollapePanelProps = {
props: { props: {
...getOptionProps(this), ...getOptionProps(this),
prefixCls,
}, },
class: collapsePanelClassName, class: collapsePanelClassName,
on: $listeners, on: $listeners,

View File

@ -0,0 +1,17 @@
import { mount } from '@vue/test-utils';
import Collapse from '..';
describe('Collapse', () => {
it('should support remove expandIcon', () => {
const wrapper = mount({
render() {
return (
<Collapse expandIcon={() => null}>
<Collapse.Panel header="header" />
</Collapse>
);
},
});
expect(wrapper.html()).toMatchSnapshot();
});
});

View File

@ -1,21 +1,21 @@
<cn> <cn>
#### 自定义面板 #### 自定义面板
自定义各个面板的背景色、圆角和边距 自定义各个面板的背景色、圆角、边距和图标
</cn> </cn>
<us> <us>
#### Custom Panel #### Custom Panel
Customize the background, border and margin styles for each panel. Customize the background, border and margin styles and icon for each panel.
</us> </us>
```html ```html
<template> <template>
<div> <div>
<a-collapse defaultActiveKey="1" :bordered="false"> <a-collapse defaultActiveKey="1" :bordered="false">
<a-collapse-panel key="1" :style="customStyle"> <template v-slot:expandIcon="props">
<template slot="header"> <a-icon type="caret-right" rotate="{isActive ? 90 : 0}" />
This is panel header 1<a-icon type="question-circle-o" /> </template>
</template> <a-collapse-panel header="This is panel header 1" key="1" :style="customStyle">
<p>{{text}}</p> <p>{{text}}</p>
</a-collapse-panel> </a-collapse-panel>
<a-collapse-panel header="This is panel header 2" key="2" :style="customStyle"> <a-collapse-panel header="This is panel header 2" key="2" :style="customStyle">

View File

@ -4,12 +4,14 @@
| Property | Description | Type | Default | | Property | Description | Type | Default |
| -------- | ----------- | ---- | ------- | | -------- | ----------- | ---- | ------- |
| accordion | If `true`, `Collapse` renders as `Accordion` | boolean | `false` | | activeKey | Key of the active panel | string\[]\|string | No default value. In `accordion` mode, it's the key of the first panel. |
| activeKey(v-model) | Key of the active panel | string\[]\|string | No default value. In `accordion` mode, it's the key of the first panel. |
| bordered | Toggles rendering of the border around the collapse block | boolean | `true` |
| defaultActiveKey | Key of the initial active panel | string | - | | defaultActiveKey | Key of the initial active panel | string | - |
| bordered | Toggles rendering of the border around the collapse block | boolean | `true` |
| accordion | If `true`, `Collapse` renders as `Accordion` | boolean | `false` |
| expandIcon | allow to customize collapse icon | Function(props):VNode \| slot="expandIcon" slot-scope="props"\|v-slot:expandIcon="props" |
| destroyInactivePanel | Destroy Inactive Panel | boolean | `false` | | destroyInactivePanel | Destroy Inactive Panel | boolean | `false` |
### events ### events
| Events Name | Description | Arguments | | Events Name | Description | Arguments |
| --- | --- | --- | | --- | --- | --- |
@ -24,3 +26,16 @@
| header | Title of the panel | string | - | | header | Title of the panel | string | - |
| key | Unique key identifying the panel from among its siblings | string | - | | key | Unique key identifying the panel from among its siblings | string | - |
| showArrow | If `false`, panel will not show arrow icon | boolean | `true` | | showArrow | If `false`, panel will not show arrow icon | boolean | `true` |
## FAQ
### How to let the arrow to be on the right?
You can adjust style of the arrow:
```
.ant-collapse .ant-collapse-item .ant-collapse-header .anticon {
left: initial;
right: 16px;
}
```

View File

@ -6,6 +6,10 @@
| --- | --- | --- | --- | | --- | --- | --- | --- |
| activeKey(v-model) | 当前激活 tab 面板的 key | string\[]\|string | 默认无accordion模式下默认第一个元素 | | activeKey(v-model) | 当前激活 tab 面板的 key | string\[]\|string | 默认无accordion模式下默认第一个元素 |
| defaultActiveKey | 初始化选中面板的 key | string | 无 | | defaultActiveKey | 初始化选中面板的 key | string | 无 |
| bordered | 带边框风格的折叠面板 | boolean | `true` |
| accordion | 手风琴模式 | boolean | `false` |
| expandIcon | 自定义切换图标 | Function(props):VNode \| slot="expandIcon" slot-scope="props"\|v-slot:expandIcon="props" |
| destroyInactivePanel | 销毁折叠隐藏的面板 | boolean | `false` |
### 事件 ### 事件
| 事件名称 | 说明 | 回调参数 | | 事件名称 | 说明 | 回调参数 |
@ -20,3 +24,17 @@
| forceRender | 被隐藏时是否渲染 DOM 结构 | boolean | false | | forceRender | 被隐藏时是否渲染 DOM 结构 | boolean | false |
| header | 面板头内容 | string\|slot | 无 | | header | 面板头内容 | string\|slot | 无 |
| key | 对应 activeKey | string | 无 | | key | 对应 activeKey | string | 无 |
| showArrow | 是否展示当前面板上的箭头 | boolean | `true` |
## FAQ
### 我希望箭头在右边,怎么做?
通过样式调整,将箭头放到右边就行啦
```
.ant-collapse .ant-collapse-item .ant-collapse-header .anticon {
left: initial;
right: 16px;
}
```