From c7b0cb0732cd200a7a431312c7183c0db44a060c Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Thu, 20 Feb 2020 21:21:09 +0800 Subject: [PATCH] feat: update list --- build/config.js | 2 +- components/_util/props-util.js | 4 + components/list/Item.jsx | 115 +++---- .../__tests__/__snapshots__/demo.test.js.snap | 316 ++++++++---------- .../__snapshots__/empty.test.js.snap | 10 +- components/list/__tests__/index.test.js | 3 + components/list/index.en-US.md | 29 +- components/list/index.jsx | 39 ++- components/list/index.zh-CN.md | 31 +- 9 files changed, 274 insertions(+), 275 deletions(-) diff --git a/build/config.js b/build/config.js index 7f19e215b..65041891a 100644 --- a/build/config.js +++ b/build/config.js @@ -1,5 +1,5 @@ module.exports = { dev: { - componentName: 'layout', // dev components + componentName: 'list', // dev components }, }; diff --git a/components/_util/props-util.js b/components/_util/props-util.js index 4868f59c0..050f634d4 100644 --- a/components/_util/props-util.js +++ b/components/_util/props-util.js @@ -259,6 +259,10 @@ export function isEmptyElement(c) { return !(c.tag || (c.text && c.text.trim() !== '')); } +export function isStringElement(c) { + return !c.tag; +} + export function filterEmpty(children = []) { return children.filter(c => !isEmptyElement(c)); } diff --git a/components/list/Item.jsx b/components/list/Item.jsx index 65de8fb3d..c1f7090bf 100644 --- a/components/list/Item.jsx +++ b/components/list/Item.jsx @@ -3,12 +3,14 @@ import classNames from 'classnames'; import { getSlotOptions, getComponentFromProp, - isEmptyElement, + isStringElement, getListeners, + isEmptyElement, } from '../_util/props-util'; import { Col } from '../grid'; import { ConfigConsumerProps } from '../config-provider'; import { ListGridType } from './index'; +import { cloneElement } from '../_util/vnode'; export const ListItemProps = { prefixCls: PropTypes.string, @@ -68,59 +70,68 @@ export default { listContext: { default: () => ({}) }, configProvider: { default: () => ConfigConsumerProps }, }, + methods: { + isItemContainsTextNodeAndNotSingular() { + const { $slots } = this; + let result; + const children = $slots.default || []; + children.forEach(element => { + if (isStringElement(element) && !isEmptyElement(element)) { + result = true; + } + }); + return result && children.length > 1; + }, + + isFlexMode() { + const extra = getComponentFromProp(this, 'extra'); + const { itemLayout } = this.listContext; + if (itemLayout === 'vertical') { + return !!extra; + } + return !this.isItemContainsTextNodeAndNotSingular(); + }, + }, render() { - const { grid } = this.listContext; + const { grid, itemLayout } = this.listContext; const { prefixCls: customizePrefixCls, $slots } = this; const listeners = getListeners(this); const getPrefixCls = this.configProvider.getPrefixCls; const prefixCls = getPrefixCls('list', customizePrefixCls); - - const classString = `${prefixCls}-item`; const extra = getComponentFromProp(this, 'extra'); const actions = getComponentFromProp(this, 'actions'); - const metaContent = []; - const otherContent = []; - ($slots.default || []).forEach(element => { - if (!isEmptyElement(element)) { - if (getSlotOptions(element).__ANT_LIST_ITEM_META) { - metaContent.push(element); - } else { - otherContent.push(element); - } - } - }); + const actionsContent = actions && actions.length > 0 && ( + + ); - const contentClassString = classNames(`${prefixCls}-item-content`, { - [`${prefixCls}-item-content-single`]: metaContent.length < 1, - }); - const content = - otherContent.length > 0 ?
{otherContent}
: null; - - let actionsContent; - if (actions && actions.length > 0) { - const actionsContentItem = (action, i) => ( -
  • - {action} - {i !== actions.length - 1 && } -
  • - ); - actionsContent = ( - - ); - } - - const extraContent = ( -
    -
    - {metaContent} - {content} - {actionsContent} -
    -
    {extra}
    -
    + const Tag = grid ? 'div' : 'li'; + const itemChildren = ( + + {itemLayout === 'vertical' && extra + ? [ +
    + {$slots.default} + {actionsContent} +
    , +
    + {extra} +
    , + ] + : [$slots.default, actionsContent, cloneElement(extra, { key: 'extra' })]} +
    ); const mainContent = grid ? ( @@ -133,20 +144,10 @@ export default { xl={getGrid(grid, 'xl')} xxl={getGrid(grid, 'xxl')} > -
    - {extra && extraContent} - {!extra && metaContent} - {!extra && content} - {!extra && actionsContent} -
    + {itemChildren} ) : ( -
    - {extra && extraContent} - {!extra && metaContent} - {!extra && content} - {!extra && actionsContent} -
    + itemChildren ); return mainContent; diff --git a/components/list/__tests__/__snapshots__/demo.test.js.snap b/components/list/__tests__/__snapshots__/demo.test.js.snap index 7e0895746..05a73740c 100644 --- a/components/list/__tests__/__snapshots__/demo.test.js.snap +++ b/components/list/__tests__/__snapshots__/demo.test.js.snap @@ -4,42 +4,44 @@ exports[`renders ./components/list/demo/basic.md correctly 1`] = `
    -
    -
    -
    -
    -

    Ant Design Title 1

    -
    Ant Design, a design language for background applications, is refined by Ant UED Team
    +
      +
    • +
      +
      +
      +

      Ant Design Title 1

      +
      Ant Design, a design language for background applications, is refined by Ant UED Team
      +
      -
    -
    -
    -
    -
    -
    -

    Ant Design Title 2

    -
    Ant Design, a design language for background applications, is refined by Ant UED Team
    + +
  • +
    +
    +
    +

    Ant Design Title 2

    +
    Ant Design, a design language for background applications, is refined by Ant UED Team
    +
    -
  • -
    -
    -
    -
    -
    -

    Ant Design Title 3

    -
    Ant Design, a design language for background applications, is refined by Ant UED Team
    + +
  • +
    +
    +
    +

    Ant Design Title 3

    +
    Ant Design, a design language for background applications, is refined by Ant UED Team
    +
    -
  • -
    -
    -
    -
    -
    -

    Ant Design Title 4

    -
    Ant Design, a design language for background applications, is refined by Ant UED Team
    + +
  • +
    +
    +
    +

    Ant Design Title 4

    +
    Ant Design, a design language for background applications, is refined by Ant UED Team
    +
    -
  • -
    + +
    @@ -50,59 +52,51 @@ exports[`renders ./components/list/demo/grid.md correctly 1`] = `
    -
    +
    -
    -
    -
    -
    -
    Title 1
    -
    +
    +
    +
    +
    Title 1
    -
    Card content
    +
    Card content
    -
    +
    -
    -
    -
    -
    -
    Title 2
    -
    +
    +
    +
    +
    Title 2
    -
    Card content
    +
    Card content
    -
    +
    -
    -
    -
    -
    -
    Title 3
    -
    +
    +
    +
    +
    Title 3
    -
    Card content
    +
    Card content
    -
    +
    -
    -
    -
    -
    -
    Title 4
    -
    +
    +
    +
    +
    Title 4
    -
    Card content
    +
    Card content
    @@ -119,7 +113,15 @@ exports[`renders ./components/list/demo/infinite-load.md correctly 1`] = `
    -
    No Data
    +
    + + + + + + + +

    No Data

    @@ -148,7 +150,7 @@ exports[`renders ./components/list/demo/loadmore.md correctly 1`] = `
    -
    +
    @@ -163,87 +165,75 @@ exports[`renders ./components/list/demo/resposive.md correctly 1`] = `
    -
    +
    -
    -
    -
    -
    -
    Title 1
    -
    +
    +
    +
    +
    Title 1
    -
    Card content
    +
    Card content
    -
    +
    -
    -
    -
    -
    -
    Title 2
    -
    +
    +
    +
    +
    Title 2
    -
    Card content
    +
    Card content
    -
    +
    -
    -
    -
    -
    -
    Title 3
    -
    +
    +
    +
    +
    Title 3
    -
    Card content
    +
    Card content
    -
    +
    -
    -
    -
    -
    -
    Title 4
    -
    +
    +
    +
    +
    Title 4
    -
    Card content
    +
    Card content
    -
    +
    -
    -
    -
    -
    -
    Title 5
    -
    +
    +
    +
    +
    Title 5
    -
    Card content
    +
    Card content
    -
    +
    -
    -
    -
    -
    -
    Title 6
    -
    +
    +
    +
    +
    Title 6
    -
    Card content
    +
    Card content
    @@ -262,21 +252,13 @@ exports[`renders ./components/list/demo/simple.md correctly 1`] = `
    -
    -
    Racing car sprays burning fuel into crowd.
    -
    -
    -
    Japanese princess to wed commoner.
    -
    -
    -
    Australian walks 100km after outback crash.
    -
    -
    -
    Man charged over missing wedding girl.
    -
    -
    -
    Los Angeles battles huge wildfires.
    -
    +
      +
    • Racing car sprays burning fuel into crowd.
    • +
    • Japanese princess to wed commoner.
    • +
    • Australian walks 100km after outback crash.
    • +
    • Man charged over missing wedding girl.
    • +
    • Los Angeles battles huge wildfires.
    • +
    -
    -
    Racing car sprays burning fuel into crowd.
    -
    -
    -
    Japanese princess to wed commoner.
    -
    -
    -
    Australian walks 100km after outback crash.
    -
    -
    -
    Man charged over missing wedding girl.
    -
    -
    -
    Los Angeles battles huge wildfires.
    -
    +
      +
    • Racing car sprays burning fuel into crowd.
    • +
    • Japanese princess to wed commoner.
    • +
    • Australian walks 100km after outback crash.
    • +
    • Man charged over missing wedding girl.
    • +
    • Los Angeles battles huge wildfires.
    • +
    -
    -
    Racing car sprays burning fuel into crowd.
    -
    -
    -
    Japanese princess to wed commoner.
    -
    -
    -
    Australian walks 100km after outback crash.
    -
    -
    -
    Man charged over missing wedding girl.
    -
    -
    -
    Los Angeles battles huge wildfires.
    -
    +
      +
    • Racing car sprays burning fuel into crowd.
    • +
    • Japanese princess to wed commoner.
    • +
    • Australian walks 100km after outback crash.
    • +
    • Man charged over missing wedding girl.
    • +
    • Los Angeles battles huge wildfires.
    • +