chore: CollapseGroup组件新增enableFieldSetStyle控制样式

This commit is contained in:
lurunze1226 2023-09-12 16:02:18 +08:00
parent 92168782ef
commit 199fa5c5ca
8 changed files with 297 additions and 150 deletions

View File

@ -211,15 +211,62 @@ order: 36
} }
``` ```
## 类FieldSet样式
当Collapse组件作为Form组件的子元素时`enableFieldSetStyle`属性可以控制Collapse组件样式风格开启后转换为类似[FieldSet组件](./form/fieldset)的样式,默认开启。
```schema: scope="body"
{
"type": "form",
"body": [
{
"type": "switch",
"name": "enableFieldSetStyle",
"label": "切换展示方式",
"mode": "horizontal",
"value": true
},
{
"type": "collapse-group",
"activeKey": ["1"],
"enableFieldSetStyle": "${enableFieldSetStyle}",
"body": [
{
"type": "collapse",
"key": "1",
"header": "标题1",
"body": "这里是内容1"
},
{
"type": "collapse",
"key": "2",
"header": "标题2",
"body": "这里是内容2"
},
{
"type": "collapse",
"key": "3",
"header": "标题3",
"body": "这里是内容3"
}
]
}
]
}
```
## CollapseGroup 属性表 ## CollapseGroup 属性表
| 属性名 | 类型 | 默认值 | 说明 | | 属性名 | 类型 | 默认值 | 说明 | 版本 |
| ------------------ | ------------------------------------------------------ | ------------------ | ----------------------------------- | | ------------------ | ------------------------------------------------------ | ------------------ | ----------------------------------- | --- |
| type | `string` | `"collapse-group"` | 指定为 collapse-group 渲染器 | | type | `string` | `"collapse-group"` | 指定为 collapse-group 渲染器 |
| activeKey | `Array<string \| number \| never> \| string \| number` | - | 初始化激活面板的 key | | activeKey | `Array<string \| number \| never> \| string \| number` | - | 初始化激活面板的 key |
| accordion | `boolean` | `false` | 手风琴模式 | | accordion | `boolean` | `false` | 手风琴模式 |
| expandIcon | `SchemaNode` | - | 自定义切换图标 | | expandIcon | `SchemaNode` | - | 自定义切换图标 |
| expandIconPosition | `string` | `"left"` | 设置图标位置,可选值`left \| right` | | expandIconPosition | `string` | `"left"` | 设置图标位置,可选值`left \| right` |
| enableFieldSetStyle | `boolean` | `true` | 当Collapse作为Form组件的子元素时开启该属性后组件样式设置为FieldSet组件的样式默认开启 | `3.5.0` |
## CollapseGroup 事件表 ## CollapseGroup 事件表
@ -297,8 +344,8 @@ order: 36
## Collapse 属性表 ## Collapse 属性表
| 属性名 | 类型 | 默认值 | 说明 | | 属性名 | 类型 | 默认值 | 说明 | 版本 |
| --------- | ---------------------- | ------------ | ---------------------- | | --------- | ---------------------- | ------------ | ---------------------- | --- |
| type | `string` | `"collapse"` | 指定为 collapse 渲染器 | | type | `string` | `"collapse"` | 指定为 collapse 渲染器 |
| disabled | `boolean` | `false` | 禁用 | | disabled | `boolean` | `false` | 禁用 |
| collapsed | `boolean` | `true` | 初始状态是否折叠 | | collapsed | `boolean` | `true` | 初始状态是否折叠 |

View File

@ -170,16 +170,19 @@
//FieldSet Form + Collapse //FieldSet Form + Collapse
.#{$ns}Form { .#{$ns}Form {
.#{$ns}Collapse { .#{$ns}Collapse.#{$ns}Collapse--fieldset {
border: none; border: none;
&-header {
.#{$ns}Collapse-header {
background-color: var(--white); background-color: var(--white);
display: inline-flex; display: inline-flex;
justify-content: flex-end; justify-content: flex-end;
align-items: center;
flex-direction: row-reverse; flex-direction: row-reverse;
border-radius: 0; border-radius: 0;
} }
&-content {
.#{$ns}Collapse-content {
padding: 0; padding: 0;
} }
} }

View File

@ -53,6 +53,8 @@ export interface CollapseProps extends ThemeProps {
partial?: boolean; partial?: boolean;
children?: React.ReactNode | Array<React.ReactNode>; children?: React.ReactNode | Array<React.ReactNode>;
divideLine?: boolean; divideLine?: boolean;
/** 当Collapse作为Form组件的子元素时开启该属性后组件样式设置为FieldSet组件的样式默认开启 */
enableFieldSetStyle?: boolean;
} }
export interface CollapseState { export interface CollapseState {
@ -72,7 +74,8 @@ export class Collapse extends React.Component<CollapseProps, CollapseState> {
collapsable: true, collapsable: true,
disabled: false, disabled: false,
showArrow: true, showArrow: true,
propsUpdate: false propsUpdate: false,
enableFieldSetStyle: true
}; };
state: CollapseState = { state: CollapseState = {
@ -191,7 +194,8 @@ export class Collapse extends React.Component<CollapseProps, CollapseState> {
expandIcon, expandIcon,
disabled, disabled,
children, children,
mobileUI mobileUI,
enableFieldSetStyle
} = this.props; } = this.props;
const finalHeader = this.state.collapsed const finalHeader = this.state.collapsed
@ -278,7 +282,8 @@ export class Collapse extends React.Component<CollapseProps, CollapseState> {
'is-active': !this.state.collapsed, 'is-active': !this.state.collapsed,
[`Collapse--${size}`]: size, [`Collapse--${size}`]: size,
'Collapse--disabled': disabled, 'Collapse--disabled': disabled,
'Collapse--title-bottom': headerPosition === 'bottom' 'Collapse--title-bottom': headerPosition === 'bottom',
'Collapse--fieldset': enableFieldSetStyle !== false
}, },
className className
)} )}

View File

@ -6,6 +6,7 @@
* 2. accordion * 2. accordion
* 3. * 3.
* 4. disabled * 4. disabled
* 5. enableFieldSetStyle属性控制CollapseGroup组件在Form中的样式
*/ */
import {render, fireEvent, waitFor} from '@testing-library/react'; import {render, fireEvent, waitFor} from '@testing-library/react';
import '../../src'; import '../../src';
@ -13,7 +14,7 @@ import {render as amisRender} from '../../src';
import {makeEnv, wait} from '../helper'; import {makeEnv, wait} from '../helper';
// 1. 基本用法 // 1. 基本用法
test('Renderer:Collapse', async () => { test('1. Renderer:Collapse', async () => {
const {container} = render( const {container} = render(
amisRender( amisRender(
{ {
@ -61,7 +62,7 @@ test('Renderer:Collapse', async () => {
}); });
// 2. accordion 手风琴模式 // 2. accordion 手风琴模式
test('Renderer:Collapse with accordion', async () => { test('2. Renderer:Collapse with accordion', async () => {
const {container} = render( const {container} = render(
amisRender( amisRender(
{ {
@ -110,7 +111,7 @@ test('Renderer:Collapse with accordion', async () => {
}); });
// 3. 自定义图标 // 3. 自定义图标
test('Renderer:Collapse with expandIcon & expandIconPosition & showArrow', async () => { test('3. Renderer:Collapse with expandIcon & expandIconPosition & showArrow', async () => {
const schema = { const schema = {
type: 'collapse-group', type: 'collapse-group',
activeKey: ['1'], activeKey: ['1'],
@ -163,7 +164,7 @@ test('Renderer:Collapse with expandIcon & expandIconPosition & showArrow', async
}); });
// 4. disabled 禁用 和 面板嵌套 // 4. disabled 禁用 和 面板嵌套
test('Renderer:Collapse with disabled & panel nesting', async () => { test('4. Renderer:Collapse with disabled & panel nesting', async () => {
const schema = { const schema = {
type: 'collapse-group', type: 'collapse-group',
activeKey: ['1'], activeKey: ['1'],
@ -205,3 +206,62 @@ test('Renderer:Collapse with disabled & panel nesting', async () => {
'cxd-Collapse--disabled' 'cxd-Collapse--disabled'
); );
}); });
test('5. enableFieldSetStyle属性控制CollapseGroup组件在Form中的样式', async () => {
const {container} = render(
amisRender(
{
"type": "form",
"body": [
{
"type": "collapse-group",
"body": [
{
"type": "collapse",
"key": "1",
"header": "标题1",
"body": "这里是内容1"
},
{
"type": "collapse",
"key": "2",
"header": "标题2",
"body": "这里是内容2"
}
]
},
{
"type": "collapse-group",
"enableFieldSetStyle": false,
"body": [
{
"type": "collapse",
"key": "1",
"header": "标题1",
"body": "这里是内容1"
},
{
"type": "collapse",
"key": "2",
"header": "标题2",
"body": "这里是内容2"
}
]
}
]
},
{},
makeEnv({})
)
);
const totalCollections = container.querySelectorAll(
'.cxd-CollapseGroup > .cxd-Collapse'
);
const fieldsetStyledCollections = container.querySelectorAll(
'.cxd-CollapseGroup > .cxd-Collapse.cxd-Collapse--fieldset'
);
expect(totalCollections.length).toBe(4);
expect(fieldsetStyledCollections.length).toBe(2);
})

View File

@ -33,7 +33,7 @@ exports[`Renderer:fieldSet 1`] = `
type="submit" type="submit"
/> />
<fieldset <fieldset
class="cxd-Collapse is-active no-border" class="cxd-Collapse is-active cxd-Collapse--fieldset no-border"
> >
<div <div
class="cxd-Collapse-contentWrapper" class="cxd-Collapse-contentWrapper"
@ -161,7 +161,7 @@ exports[`Renderer:fieldSet with mode 1`] = `
type="submit" type="submit"
/> />
<fieldset <fieldset
class="cxd-Collapse is-active" class="cxd-Collapse is-active cxd-Collapse--fieldset"
> >
<div <div
class="cxd-Collapse-contentWrapper" class="cxd-Collapse-contentWrapper"
@ -336,7 +336,7 @@ exports[`Renderer:fieldSet with mode 2`] = `
type="submit" type="submit"
/> />
<fieldset <fieldset
class="cxd-Collapse is-active" class="cxd-Collapse is-active cxd-Collapse--fieldset"
> >
<div <div
class="cxd-Collapse-contentWrapper" class="cxd-Collapse-contentWrapper"
@ -511,7 +511,7 @@ exports[`Renderer:fieldSet with mode 3`] = `
type="submit" type="submit"
/> />
<fieldset <fieldset
class="cxd-Collapse is-active" class="cxd-Collapse is-active cxd-Collapse--fieldset"
> >
<div <div
class="cxd-Collapse-contentWrapper" class="cxd-Collapse-contentWrapper"
@ -678,7 +678,7 @@ exports[`Renderer:fieldSet with titlePosition & collapseTitle & title 1`] = `
type="submit" type="submit"
/> />
<fieldset <fieldset
class="cxd-Collapse is-active cxd-Collapse--title-bottom" class="cxd-Collapse is-active cxd-Collapse--title-bottom cxd-Collapse--fieldset"
> >
<div <div
class="cxd-Collapse-contentWrapper" class="cxd-Collapse-contentWrapper"

View File

@ -1,12 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Renderer:Collapse 1`] = ` exports[`1. Renderer:Collapse 1`] = `
<div> <div>
<div <div
class="cxd-CollapseGroup" class="cxd-CollapseGroup"
> >
<div <div
class="cxd-Collapse is-active" class="cxd-Collapse is-active cxd-Collapse--fieldset"
> >
<div <div
class="cxd-Collapse-header" class="cxd-Collapse-header"
@ -48,7 +48,7 @@ exports[`Renderer:Collapse 1`] = `
</div> </div>
</div> </div>
<div <div
class="cxd-Collapse is-active" class="cxd-Collapse is-active cxd-Collapse--fieldset"
> >
<div <div
class="cxd-Collapse-header" class="cxd-Collapse-header"
@ -90,7 +90,7 @@ exports[`Renderer:Collapse 1`] = `
</div> </div>
</div> </div>
<div <div
class="cxd-Collapse is-active" class="cxd-Collapse is-active cxd-Collapse--fieldset"
> >
<div <div
class="cxd-Collapse-header" class="cxd-Collapse-header"
@ -136,13 +136,130 @@ exports[`Renderer:Collapse 1`] = `
</div> </div>
`; `;
exports[`Renderer:Collapse with disabled & panel nesting 1`] = ` exports[`3. Renderer:Collapse with expandIcon & expandIconPosition & showArrow 1`] = `
<div>
<div
class="cxd-CollapseGroup icon-position-right"
>
<div
class="cxd-Collapse is-active cxd-Collapse--fieldset"
>
<div
class="cxd-Collapse-header"
>
<i
class="caret-right cxd-Collapse-icon-tranform cxd-Collapse-icon-tranform fa fa-caret-right"
/>
<span
class="cxd-TplField"
>
<span>
标题1
</span>
</span>
</div>
<div
class="cxd-Collapse-contentWrapper"
>
<div
class="cxd-Collapse-body"
>
<div
class="cxd-Collapse-content"
>
<span
class="cxd-TplField"
>
<span>
这里是内容1
</span>
</span>
</div>
</div>
</div>
</div>
<div
class="cxd-Collapse cxd-Collapse--fieldset"
>
<div
class="cxd-Collapse-header"
>
<i
class="caret-right cxd-Collapse-icon-tranform cxd-Collapse-icon-tranform fa fa-caret-right"
/>
<span
class="cxd-TplField"
>
<span>
标题2
</span>
</span>
</div>
<div
class="cxd-Collapse-contentWrapper out"
>
<div
class="cxd-Collapse-body"
>
<div
class="cxd-Collapse-content"
>
<span
class="cxd-TplField"
>
<span>
这里是内容2
</span>
</span>
</div>
</div>
</div>
</div>
<div
class="cxd-Collapse cxd-Collapse--fieldset"
>
<div
class="cxd-Collapse-header"
>
<span
class="cxd-TplField"
>
<span>
标题3
</span>
</span>
</div>
<div
class="cxd-Collapse-contentWrapper out"
>
<div
class="cxd-Collapse-body"
>
<div
class="cxd-Collapse-content"
>
<span
class="cxd-TplField"
>
<span>
这里是内容3
</span>
</span>
</div>
</div>
</div>
</div>
</div>
</div>
`;
exports[`4. Renderer:Collapse with disabled & panel nesting 1`] = `
<div> <div>
<div <div
class="cxd-CollapseGroup" class="cxd-CollapseGroup"
> >
<div <div
class="cxd-Collapse is-active" class="cxd-Collapse is-active cxd-Collapse--fieldset"
> >
<div <div
class="cxd-Collapse-header" class="cxd-Collapse-header"
@ -176,7 +293,7 @@ exports[`Renderer:Collapse with disabled & panel nesting 1`] = `
class="cxd-CollapseGroup" class="cxd-CollapseGroup"
> >
<div <div
class="cxd-Collapse is-active" class="cxd-Collapse is-active cxd-Collapse--fieldset"
> >
<div <div
class="cxd-Collapse-header" class="cxd-Collapse-header"
@ -223,7 +340,7 @@ exports[`Renderer:Collapse with disabled & panel nesting 1`] = `
</div> </div>
</div> </div>
<div <div
class="cxd-Collapse cxd-Collapse--disabled lastOne" class="cxd-Collapse cxd-Collapse--disabled cxd-Collapse--fieldset lastOne"
> >
<div <div
class="cxd-Collapse-header" class="cxd-Collapse-header"
@ -267,120 +384,3 @@ exports[`Renderer:Collapse with disabled & panel nesting 1`] = `
</div> </div>
</div> </div>
`; `;
exports[`Renderer:Collapse with expandIcon & expandIconPosition & showArrow 1`] = `
<div>
<div
class="cxd-CollapseGroup icon-position-right"
>
<div
class="cxd-Collapse is-active"
>
<div
class="cxd-Collapse-header"
>
<i
class="caret-right cxd-Collapse-icon-tranform cxd-Collapse-icon-tranform fa fa-caret-right"
/>
<span
class="cxd-TplField"
>
<span>
标题1
</span>
</span>
</div>
<div
class="cxd-Collapse-contentWrapper"
>
<div
class="cxd-Collapse-body"
>
<div
class="cxd-Collapse-content"
>
<span
class="cxd-TplField"
>
<span>
这里是内容1
</span>
</span>
</div>
</div>
</div>
</div>
<div
class="cxd-Collapse"
>
<div
class="cxd-Collapse-header"
>
<i
class="caret-right cxd-Collapse-icon-tranform cxd-Collapse-icon-tranform fa fa-caret-right"
/>
<span
class="cxd-TplField"
>
<span>
标题2
</span>
</span>
</div>
<div
class="cxd-Collapse-contentWrapper out"
>
<div
class="cxd-Collapse-body"
>
<div
class="cxd-Collapse-content"
>
<span
class="cxd-TplField"
>
<span>
这里是内容2
</span>
</span>
</div>
</div>
</div>
</div>
<div
class="cxd-Collapse"
>
<div
class="cxd-Collapse-header"
>
<span
class="cxd-TplField"
>
<span>
标题3
</span>
</span>
</div>
<div
class="cxd-Collapse-contentWrapper out"
>
<div
class="cxd-Collapse-body"
>
<div
class="cxd-Collapse-content"
>
<span
class="cxd-TplField"
>
<span>
这里是内容3
</span>
</span>
</div>
</div>
</div>
</div>
</div>
</div>
`;

View File

@ -6,7 +6,9 @@ import {
RendererProps, RendererProps,
ScopedContext, ScopedContext,
autobind, autobind,
resolveEventData resolveEventData,
isPureVariable,
resolveVariableAndFilter
} from 'amis-core'; } from 'amis-core';
import {Collapse as BasicCollapse, Icon} from 'amis-ui'; import {Collapse as BasicCollapse, Icon} from 'amis-ui';
import {BaseSchema, SchemaCollection, SchemaTpl, SchemaObject} from '../Schema'; import {BaseSchema, SchemaCollection, SchemaTpl, SchemaObject} from '../Schema';
@ -109,6 +111,8 @@ export interface CollapseProps
// 内容口子 // 内容口子
children?: JSX.Element | ((props?: any) => JSX.Element); children?: JSX.Element | ((props?: any) => JSX.Element);
/** 当Collapse作为Form组件的子元素时开启该属性后组件样式设置为FieldSet组件的样式默认开启 */
enableFieldSetStyle?: boolean;
} }
export default class Collapse extends React.Component<CollapseProps, {}> { export default class Collapse extends React.Component<CollapseProps, {}> {
@ -194,9 +198,9 @@ export default class Collapse extends React.Component<CollapseProps, {}> {
collapsed, collapsed,
propsUpdate, propsUpdate,
mobileUI, mobileUI,
divideLine divideLine,
enableFieldSetStyle
} = this.props; } = this.props;
const heading = title || header || ''; const heading = title || header || '';
return ( return (
@ -253,6 +257,7 @@ export default class Collapse extends React.Component<CollapseProps, {}> {
mobileUI={mobileUI} mobileUI={mobileUI}
onCollapse={this.handleCollapseChange} onCollapse={this.handleCollapseChange}
divideLine={divideLine} divideLine={divideLine}
enableFieldSetStyle={enableFieldSetStyle}
></BasicCollapse> ></BasicCollapse>
); );
} }

View File

@ -1,5 +1,12 @@
import React from 'react'; import React from 'react';
import {Renderer, RendererProps, autobind, resolveEventData} from 'amis-core'; import {
Renderer,
RendererProps,
autobind,
resolveEventData,
isPureVariable,
resolveVariableAndFilter
} from 'amis-core';
import {BaseSchema, SchemaCollection, SchemaObject} from '../Schema'; import {BaseSchema, SchemaCollection, SchemaObject} from '../Schema';
import {CollapseGroup} from 'amis-ui'; import {CollapseGroup} from 'amis-ui';
@ -37,6 +44,11 @@ export interface CollapseGroupSchema extends BaseSchema {
* *
*/ */
body?: SchemaCollection; body?: SchemaCollection;
/**
* Collapse作为Form组件的子元素时FieldSet组件的样式
*/
enableFieldSetStyle?: boolean;
} }
export interface CollapseGroupProps export interface CollapseGroupProps
extends RendererProps, extends RendererProps,
@ -49,6 +61,10 @@ export class CollapseGroupRender extends React.Component<
CollapseGroupProps, CollapseGroupProps,
{} {}
> { > {
static defaultProps = {
enableFieldSetStyle: true
};
constructor(props: CollapseGroupProps) { constructor(props: CollapseGroupProps) {
super(props); super(props);
} }
@ -84,8 +100,19 @@ export class CollapseGroupRender extends React.Component<
className, className,
style, style,
render, render,
mobileUI mobileUI,
data
} = this.props; } = this.props;
let enableFieldSetStyle = this.props.enableFieldSetStyle;
if (isPureVariable(enableFieldSetStyle)) {
enableFieldSetStyle = resolveVariableAndFilter(
enableFieldSetStyle,
data,
'| raw'
);
}
return ( return (
<CollapseGroup <CollapseGroup
defaultActiveKey={defaultActiveKey} defaultActiveKey={defaultActiveKey}
@ -97,7 +124,7 @@ export class CollapseGroupRender extends React.Component<
mobileUI={mobileUI} mobileUI={mobileUI}
onCollapseChange={this.handleCollapseChange} onCollapseChange={this.handleCollapseChange}
> >
{render('body', body || '')} {render('body', body || '', {enableFieldSetStyle})}
</CollapseGroup> </CollapseGroup>
); );
} }