From 3db32cb6312795a13fab649d54bd26112448c466 Mon Sep 17 00:00:00 2001 From: hengkx Date: Wed, 10 Jul 2019 10:32:02 +0800 Subject: [PATCH 1/3] feat:Descriptions support colon props --- .../__tests__/__snapshots__/demo.test.js.snap | 88 +++++++++---------- .../__snapshots__/index.test.js.snap | 78 +++++++++++++--- .../descriptions/__tests__/index.test.js | 9 ++ components/descriptions/index.en-US.md | 1 + components/descriptions/index.tsx | 37 ++++++-- components/descriptions/index.zh-CN.md | 1 + components/descriptions/style/index.less | 6 ++ 7 files changed, 155 insertions(+), 65 deletions(-) diff --git a/components/descriptions/__tests__/__snapshots__/demo.test.js.snap b/components/descriptions/__tests__/__snapshots__/demo.test.js.snap index 7ed955a48f..393a8d1772 100644 --- a/components/descriptions/__tests__/__snapshots__/demo.test.js.snap +++ b/components/descriptions/__tests__/__snapshots__/demo.test.js.snap @@ -22,7 +22,7 @@ exports[`renders ./components/descriptions/demo/basic.md correctly 1`] = ` colspan="1" > UserName @@ -37,7 +37,7 @@ exports[`renders ./components/descriptions/demo/basic.md correctly 1`] = ` colspan="1" > Telephone @@ -52,7 +52,7 @@ exports[`renders ./components/descriptions/demo/basic.md correctly 1`] = ` colspan="1" > Live @@ -71,7 +71,7 @@ exports[`renders ./components/descriptions/demo/basic.md correctly 1`] = ` colspan="1" > Remark @@ -86,7 +86,7 @@ exports[`renders ./components/descriptions/demo/basic.md correctly 1`] = ` colspan="2" > Address @@ -121,7 +121,7 @@ exports[`renders ./components/descriptions/demo/border.md correctly 1`] = ` class="ant-descriptions-row" > Product @@ -132,7 +132,7 @@ exports[`renders ./components/descriptions/demo/border.md correctly 1`] = ` Cloud Database Billing Mode @@ -143,7 +143,7 @@ exports[`renders ./components/descriptions/demo/border.md correctly 1`] = ` Prepaid Automatic Renewal @@ -158,7 +158,7 @@ exports[`renders ./components/descriptions/demo/border.md correctly 1`] = ` class="ant-descriptions-row" > Order time @@ -169,7 +169,7 @@ exports[`renders ./components/descriptions/demo/border.md correctly 1`] = ` 2018-04-24 18:00:00 Usage Time @@ -184,7 +184,7 @@ exports[`renders ./components/descriptions/demo/border.md correctly 1`] = ` class="ant-descriptions-row" > Status @@ -210,7 +210,7 @@ exports[`renders ./components/descriptions/demo/border.md correctly 1`] = ` class="ant-descriptions-row" > Negotiated Amount @@ -221,7 +221,7 @@ exports[`renders ./components/descriptions/demo/border.md correctly 1`] = ` $80.00 Discount @@ -232,7 +232,7 @@ exports[`renders ./components/descriptions/demo/border.md correctly 1`] = ` $20.00 Official Receipts @@ -247,7 +247,7 @@ exports[`renders ./components/descriptions/demo/border.md correctly 1`] = ` class="ant-descriptions-row" > Config Info @@ -298,7 +298,7 @@ exports[`renders ./components/descriptions/demo/responsive.md correctly 1`] = ` colspan="1" > Product @@ -313,7 +313,7 @@ exports[`renders ./components/descriptions/demo/responsive.md correctly 1`] = ` colspan="1" > Billing @@ -328,7 +328,7 @@ exports[`renders ./components/descriptions/demo/responsive.md correctly 1`] = ` colspan="1" > time @@ -347,7 +347,7 @@ exports[`renders ./components/descriptions/demo/responsive.md correctly 1`] = ` colspan="1" > Amount @@ -362,7 +362,7 @@ exports[`renders ./components/descriptions/demo/responsive.md correctly 1`] = ` colspan="1" > Discount @@ -377,7 +377,7 @@ exports[`renders ./components/descriptions/demo/responsive.md correctly 1`] = ` colspan="1" > Official @@ -396,7 +396,7 @@ exports[`renders ./components/descriptions/demo/responsive.md correctly 1`] = ` colspan="3" > Config Info @@ -507,7 +507,7 @@ exports[`renders ./components/descriptions/demo/size.md correctly 1`] = ` class="ant-descriptions-row" > Product @@ -518,7 +518,7 @@ exports[`renders ./components/descriptions/demo/size.md correctly 1`] = ` Cloud Database Billing @@ -529,7 +529,7 @@ exports[`renders ./components/descriptions/demo/size.md correctly 1`] = ` Prepaid time @@ -544,7 +544,7 @@ exports[`renders ./components/descriptions/demo/size.md correctly 1`] = ` class="ant-descriptions-row" > Amount @@ -555,7 +555,7 @@ exports[`renders ./components/descriptions/demo/size.md correctly 1`] = ` $80.00 Discount @@ -566,7 +566,7 @@ exports[`renders ./components/descriptions/demo/size.md correctly 1`] = ` $20.00 Official @@ -581,7 +581,7 @@ exports[`renders ./components/descriptions/demo/size.md correctly 1`] = ` class="ant-descriptions-row" > Config Info @@ -632,7 +632,7 @@ exports[`renders ./components/descriptions/demo/vertical.md correctly 1`] = ` colspan="1" > UserName @@ -642,7 +642,7 @@ exports[`renders ./components/descriptions/demo/vertical.md correctly 1`] = ` colspan="1" > Telephone @@ -652,7 +652,7 @@ exports[`renders ./components/descriptions/demo/vertical.md correctly 1`] = ` colspan="1" > Live @@ -700,7 +700,7 @@ exports[`renders ./components/descriptions/demo/vertical.md correctly 1`] = ` colspan="1" > Remark @@ -710,7 +710,7 @@ exports[`renders ./components/descriptions/demo/vertical.md correctly 1`] = ` colspan="2" > Address @@ -764,19 +764,19 @@ exports[`renders ./components/descriptions/demo/vertical-border.md correctly 1`] class="ant-descriptions-row" > Product Billing Mode Automatic Renewal @@ -808,13 +808,13 @@ exports[`renders ./components/descriptions/demo/vertical-border.md correctly 1`] class="ant-descriptions-row" > Order time Usage Time @@ -840,7 +840,7 @@ exports[`renders ./components/descriptions/demo/vertical-border.md correctly 1`] class="ant-descriptions-row" > Status @@ -871,19 +871,19 @@ exports[`renders ./components/descriptions/demo/vertical-border.md correctly 1`] class="ant-descriptions-row" > Negotiated Amount Discount Official Receipts @@ -915,7 +915,7 @@ exports[`renders ./components/descriptions/demo/vertical-border.md correctly 1`] class="ant-descriptions-row" > Config Info diff --git a/components/descriptions/__tests__/__snapshots__/index.test.js.snap b/components/descriptions/__tests__/__snapshots__/index.test.js.snap index e269a348ff..c90513c35c 100644 --- a/components/descriptions/__tests__/__snapshots__/index.test.js.snap +++ b/components/descriptions/__tests__/__snapshots__/index.test.js.snap @@ -1,5 +1,57 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Descriptions Descriptions support colon 1`] = ` + +
+
+ + + + + + +
+ + Product + + + Cloud Database + +
+
+
+
+`; + exports[`Descriptions Descriptions.Item support className 1`] = ` Product @@ -74,7 +126,7 @@ exports[`Descriptions column is number 1`] = ` key=".$.0" > Product @@ -92,7 +144,7 @@ exports[`Descriptions column is number 1`] = ` key=".$.1" > Billing @@ -109,7 +161,7 @@ exports[`Descriptions column is number 1`] = ` colSpan={1} > time @@ -131,7 +183,7 @@ exports[`Descriptions column is number 1`] = ` colSpan={3} > Amount @@ -183,7 +235,7 @@ exports[`Descriptions vertical layout 1`] = ` colSpan={1} > Product @@ -215,7 +267,7 @@ exports[`Descriptions vertical layout 1`] = ` colSpan={1} > Billing @@ -247,7 +299,7 @@ exports[`Descriptions vertical layout 1`] = ` colSpan={1} > time @@ -279,7 +331,7 @@ exports[`Descriptions vertical layout 1`] = ` colSpan={1} > Amount @@ -340,7 +392,7 @@ exports[`Descriptions when item is rendered conditionally 1`] = ` colSpan={1} > Product @@ -362,7 +414,7 @@ exports[`Descriptions when item is rendered conditionally 1`] = ` colSpan={1} > Billing @@ -384,7 +436,7 @@ exports[`Descriptions when item is rendered conditionally 1`] = ` colSpan={1} > time @@ -406,7 +458,7 @@ exports[`Descriptions when item is rendered conditionally 1`] = ` colSpan={1} > Amount diff --git a/components/descriptions/__tests__/index.test.js b/components/descriptions/__tests__/index.test.js index 9c20b5523f..a4d01e046f 100644 --- a/components/descriptions/__tests__/index.test.js +++ b/components/descriptions/__tests__/index.test.js @@ -151,4 +151,13 @@ describe('Descriptions', () => { ); expect(wrapper).toMatchSnapshot(); }); + + it('Descriptions support colon', () => { + const wrapper = mount( + + Cloud Database + , + ); + expect(wrapper).toMatchSnapshot(); + }); }); diff --git a/components/descriptions/index.en-US.md b/components/descriptions/index.en-US.md index e052191dd4..39e091917e 100644 --- a/components/descriptions/index.en-US.md +++ b/components/descriptions/index.en-US.md @@ -22,6 +22,7 @@ Commonly displayed on the details page. | column | the number of `DescriptionItems` in a row,could be a number or a object like `{ xs: 8, sm: 16, md: 24}`,(Only set `bordered={true}` to take effect) | number | 3 | | size | set the size of the list. Can be set to `middle`,`small`, or not filled | `default | middle | small` | false | | layout | Define description layout | `horizontal | vertical` | `horizontal` | +| colon | change default props `colon` value of `Descriptions.Item` | boolean | true | ### DescriptionItem diff --git a/components/descriptions/index.tsx b/components/descriptions/index.tsx index 06cd33365c..13466f2f0a 100644 --- a/components/descriptions/index.tsx +++ b/components/descriptions/index.tsx @@ -29,6 +29,7 @@ export interface DescriptionsProps { title?: string; column?: number | Partial>; layout?: 'horizontal' | 'vertical'; + colon?: boolean; } /** @@ -79,12 +80,17 @@ const generateChildrenRows = ( * {DescriptionsItem.children} * */ -const renderCol = (child: React.ReactElement, bordered: boolean) => { +const renderCol = ( + child: React.ReactElement, + bordered: boolean, + colon: boolean, +) => { const { prefixCls, label, className, children, span = 1 } = child.props; if (bordered) { return [ , bordered: b , bordered: b ); }; -const renderLabelCol = (child: React.ReactElement, bordered: boolean) => { +const renderLabelCol = ( + child: React.ReactElement, + bordered: boolean, + colon: boolean, +) => { const { prefixCls, label, span = 1 } = child.props; if (bordered) { return ( - + {label} ); } return ( - + {label} @@ -159,6 +177,7 @@ const renderRow = ( { prefixCls, column, isLast }: { prefixCls: string; column: number; isLast: boolean }, bordered: boolean, layout: string, + colon: boolean, ) => { // copy children,prevent changes to incoming parameters const childrenArray = [...children]; @@ -174,7 +193,7 @@ const renderRow = ( const cloneLabelChildren = React.Children.map( childrenArray, (childrenItem: React.ReactElement) => { - return renderLabelCol(childrenItem, bordered); + return renderLabelCol(childrenItem, bordered, colon); }, ); const cloneContentChildren = React.Children.map( @@ -186,7 +205,7 @@ const renderRow = ( return [ {cloneLabelChildren} - {renderLabelCol(lastChildren, bordered)} + {renderLabelCol(lastChildren, bordered, colon)} , {cloneContentChildren} @@ -197,13 +216,13 @@ const renderRow = ( const cloneChildren = React.Children.map( childrenArray, (childrenItem: React.ReactElement) => { - return renderCol(childrenItem, bordered); + return renderCol(childrenItem, bordered, colon); }, ); return ( {cloneChildren} - {renderCol(lastChildren, bordered)} + {renderCol(lastChildren, bordered, colon)} ); }; @@ -281,6 +300,7 @@ class Descriptions extends React.Component< children, bordered = false, layout = 'horizontal', + colon = true, } = this.props; const prefixCls = getPrefixCls('descriptions', customizePrefixCls); @@ -322,6 +342,7 @@ class Descriptions extends React.Component< }, bordered, layout, + colon, ), )} diff --git a/components/descriptions/index.zh-CN.md b/components/descriptions/index.zh-CN.md index c200a4dd89..ffd4e5daf0 100644 --- a/components/descriptions/index.zh-CN.md +++ b/components/descriptions/index.zh-CN.md @@ -23,6 +23,7 @@ cols: 1 | column | 一行的 `DescriptionItems` 数量,可以写成像素值或支持响应式的对象写法 `{ xs: 8, sm: 16, md: 24}` | number | 3 | | size | 设置列表的大小。可以设置为 `middle` 、`small`, 或不填(只有设置 `bordered={true}` 生效) | `default | middle | small` | false | | layout | 描述布局 | `horizontal | vertical` | `horizontal` | +| colon | 配置 `Descriptions.Item` 的 `colon` 的默认值 | boolean | true | ### DescriptionItem diff --git a/components/descriptions/style/index.less b/components/descriptions/style/index.less index 6282cd7533..ad7dfdbfae 100644 --- a/components/descriptions/style/index.less +++ b/components/descriptions/style/index.less @@ -47,6 +47,12 @@ position: relative; top: -0.5px; margin: 0 8px 0 2px; + content: ' '; + } + } + + &-item-colon { + &::after { content: ':'; } } From 99a2cc201e40a15a0e409b65c42c06afe4462ba1 Mon Sep 17 00:00:00 2001 From: hengkx Date: Wed, 10 Jul 2019 13:21:56 +0800 Subject: [PATCH 2/3] improve Descriptions code --- components/descriptions/Col.tsx | 75 ++ .../__tests__/__snapshots__/demo.test.js.snap | 40 +- .../__snapshots__/index.test.js.snap | 741 ++++++++++++------ components/descriptions/index.tsx | 158 +--- 4 files changed, 646 insertions(+), 368 deletions(-) create mode 100644 components/descriptions/Col.tsx diff --git a/components/descriptions/Col.tsx b/components/descriptions/Col.tsx new file mode 100644 index 0000000000..e779b60e1c --- /dev/null +++ b/components/descriptions/Col.tsx @@ -0,0 +1,75 @@ +import * as React from 'react'; +import classNames from 'classnames'; +import { DescriptionsItemProps } from './index'; + +interface ColProps { + child: React.ReactElement; + bordered: boolean; + colon: boolean; + type?: 'label' | 'content'; + layout?: 'horizontal' | 'vertical'; +} + +const Col: React.SFC = props => { + const { child, bordered, colon, type, layout } = props; + const { prefixCls, label, className, children, span = 1 } = child.props; + const labelProps: any = { + className: classNames(`${prefixCls}-item-label`, className, { + [`${prefixCls}-item-colon`]: colon, + [`${prefixCls}-item-no-label`]: !label, + }), + key: 'label', + }; + if (layout === 'vertical') { + labelProps.colSpan = span * 2 - 1; + } + + if (bordered) { + if (type === 'label') { + return {label}; + } + return ( + + {children} + + ); + } + if (layout === 'vertical') { + if (type === 'content') { + return ( + + + {children} + + + ); + } + return ( + + + {label} + + + ); + } + return ( + + {label} + + {children} + + + ); +}; + +export default Col; diff --git a/components/descriptions/__tests__/__snapshots__/demo.test.js.snap b/components/descriptions/__tests__/__snapshots__/demo.test.js.snap index 393a8d1772..68791d4000 100644 --- a/components/descriptions/__tests__/__snapshots__/demo.test.js.snap +++ b/components/descriptions/__tests__/__snapshots__/demo.test.js.snap @@ -763,24 +763,24 @@ exports[`renders ./components/descriptions/demo/vertical-border.md correctly 1`] - Product - - + Billing Mode - - + Automatic Renewal - + - Order time - - + Usage Time - + - Status - + - Negotiated Amount - - + Discount - - + Official Receipts - + - Config Info - + - + Cloud Database + + } + colon={false} + key="label-0" + layout="horizontal" + type="label" > - - Product - - - Cloud Database - - + + Product + + + Cloud Database + + + @@ -78,23 +95,41 @@ exports[`Descriptions Descriptions.Item support className 1`] = ` className="ant-descriptions-row" key="0" > - + Cloud Database + + } + colon={true} + key="label-0" + layout="horizontal" + type="label" > - - Product - - - Cloud Database - - + + Product + + + Cloud Database + + + @@ -120,81 +155,144 @@ exports[`Descriptions column is number 1`] = ` className="ant-descriptions-row" key="0" > - + Cloud Database + + } + colon={true} + key="label-0" + layout="horizontal" + type="label" > - - Product - - - Cloud Database - - - + Product + + + Cloud Database + + + + + Prepaid + + } + colon={true} + key="label-1" + layout="horizontal" + type="label" > - - Billing - - - Prepaid - - - + Billing + + + Prepaid + + + + + 18:00:00 + + } + colon={true} + key="label-2" + layout="horizontal" + type="label" > - - time - - - 18:00:00 - - + + time + + + 18:00:00 + + + - + $80.00 + + } + colon={true} + key="label-0" + layout="horizontal" + type="label" > - - Amount - - - $80.00 - - + + Amount + + + $80.00 + + + @@ -230,129 +328,259 @@ exports[`Descriptions vertical layout 1`] = ` className="ant-descriptions-row" key="label-0" > - + Cloud Database + + } + colon={true} + key="label-0" + layout="vertical" + type="label" > - - Product - - + + Product + + + - + Cloud Database + + } + colon={true} + key="content-0" + layout="vertical" + type="content" > - - Cloud Database - - + + Cloud Database + + + - + Prepaid + + } + colon={true} + key="label-0" + layout="vertical" + type="label" > - - Billing - - + + Billing + + + - + Prepaid + + } + colon={true} + key="content-0" + layout="vertical" + type="content" > - - Prepaid - - + + Prepaid + + + - + 18:00:00 + + } + colon={true} + key="label-0" + layout="vertical" + type="label" > - - time - - + + time + + + - + 18:00:00 + + } + colon={true} + key="content-0" + layout="vertical" + type="content" > - - 18:00:00 - - + + 18:00:00 + + + - + $80.00 + + } + colon={true} + key="label-0" + layout="vertical" + type="label" > - - Amount - - + + Amount + + + - + $80.00 + + } + colon={true} + key="content-0" + layout="vertical" + type="content" > - - $80.00 - - + + $80.00 + + + @@ -387,89 +615,154 @@ exports[`Descriptions when item is rendered conditionally 1`] = ` className="ant-descriptions-row" key="0" > - + Cloud Database + + } + colon={true} + key="label-0" + layout="horizontal" + type="label" > - - Product - - - Cloud Database - - + + Product + + + Cloud Database + + + - + Prepaid + + } + colon={true} + key="label-0" + layout="horizontal" + type="label" > - - Billing - - - Prepaid - - + + Billing + + + Prepaid + + + - + 18:00:00 + + } + colon={true} + key="label-0" + layout="horizontal" + type="label" > - - time - - - 18:00:00 - - + + time + + + 18:00:00 + + + - + $80.00 + + } + colon={true} + key="label-0" + layout="horizontal" + type="label" > - - Amount - - - $80.00 - - + + Amount + + + $80.00 + + + diff --git a/components/descriptions/index.tsx b/components/descriptions/index.tsx index 13466f2f0a..88990a6a09 100644 --- a/components/descriptions/index.tsx +++ b/components/descriptions/index.tsx @@ -7,6 +7,7 @@ import ResponsiveObserve, { responsiveArray, } from '../_util/responsiveObserve'; import { ConfigConsumer, ConfigConsumerProps } from '../config-provider'; +import Col from './Col'; export interface DescriptionsItemProps { prefixCls?: string; @@ -70,113 +71,12 @@ const generateChildrenRows = ( return childrenArray; }; -/** - * This code is for handling react15 does not support returning an array, - * It can convert a children into th and td - * @param child DescriptionsItem - * @returns - * <> - * {DescriptionsItem.label} - * {DescriptionsItem.children} - * - */ -const renderCol = ( - child: React.ReactElement, - bordered: boolean, - colon: boolean, -) => { - const { prefixCls, label, className, children, span = 1 } = child.props; - if (bordered) { - return [ - - {label} - , - - {children} - , - ]; - } - return ( - - - {label} - - - {children} - - - ); -}; - -const renderLabelCol = ( - child: React.ReactElement, - bordered: boolean, - colon: boolean, -) => { - const { prefixCls, label, span = 1 } = child.props; - if (bordered) { - return ( - - {label} - - ); - } - return ( - - - {label} - - - ); -}; - -const renderContentCol = (child: React.ReactElement, bordered: boolean) => { - const { prefixCls, children, span = 1 } = child.props; - if (bordered) { - return ( - - {children} - - ); - } - return ( - - - {children} - - - ); -}; - const renderRow = ( children: React.ReactElement[], index: number, { prefixCls, column, isLast }: { prefixCls: string; column: number; isLast: boolean }, bordered: boolean, - layout: string, + layout: 'horizontal' | 'vertical', colon: boolean, ) => { // copy children,prevent changes to incoming parameters @@ -188,41 +88,51 @@ const renderRow = ( span, }); } + childrenArray.push(lastChildren); + + const renderCol = ( + childrenItem: React.ReactElement, + type: 'label' | 'content', + idx: number, + ) => ( + + ); + + const cloneChildren: JSX.Element[] = []; + const cloneContentChildren: JSX.Element[] = []; + React.Children.forEach( + childrenArray, + (childrenItem: React.ReactElement, idx: number) => { + cloneChildren.push(renderCol(childrenItem, 'label', idx)); + if (layout === 'vertical') { + cloneContentChildren.push(renderCol(childrenItem, 'content', idx)); + } else if (bordered) { + cloneChildren.push(renderCol(childrenItem, 'content', idx)); + } + }, + ); if (layout === 'vertical') { - const cloneLabelChildren = React.Children.map( - childrenArray, - (childrenItem: React.ReactElement) => { - return renderLabelCol(childrenItem, bordered, colon); - }, - ); - const cloneContentChildren = React.Children.map( - childrenArray, - (childrenItem: React.ReactElement) => { - return renderContentCol(childrenItem, bordered); - }, - ); return [ - {cloneLabelChildren} - {renderLabelCol(lastChildren, bordered, colon)} + {cloneChildren} , {cloneContentChildren} - {renderContentCol(lastChildren, bordered)} , ]; } - const cloneChildren = React.Children.map( - childrenArray, - (childrenItem: React.ReactElement) => { - return renderCol(childrenItem, bordered, colon); - }, - ); + return ( {cloneChildren} - {renderCol(lastChildren, bordered, colon)} ); }; From 628dbc2d6abdddbe324647de4534551b4136d77e Mon Sep 17 00:00:00 2001 From: hengkx Date: Wed, 10 Jul 2019 13:46:36 +0800 Subject: [PATCH 3/3] improve Descriptions code --- components/descriptions/Col.tsx | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/components/descriptions/Col.tsx b/components/descriptions/Col.tsx index e779b60e1c..f147597dfc 100644 --- a/components/descriptions/Col.tsx +++ b/components/descriptions/Col.tsx @@ -49,10 +49,7 @@ const Col: React.SFC = props => { ); } return ( - +