Skip to content

Commit c7b0cb0

Browse files
committed
feat: update list
1 parent 45ff670 commit c7b0cb0

File tree

9 files changed

+274
-275
lines changed

9 files changed

+274
-275
lines changed

build/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
22
dev: {
3-
componentName: 'layout', // dev components
3+
componentName: 'list', // dev components
44
},
55
};

components/_util/props-util.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ export function isEmptyElement(c) {
259259
return !(c.tag || (c.text && c.text.trim() !== ''));
260260
}
261261

262+
export function isStringElement(c) {
263+
return !c.tag;
264+
}
265+
262266
export function filterEmpty(children = []) {
263267
return children.filter(c => !isEmptyElement(c));
264268
}

components/list/Item.jsx

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import classNames from 'classnames';
33
import {
44
getSlotOptions,
55
getComponentFromProp,
6-
isEmptyElement,
6+
isStringElement,
77
getListeners,
8+
isEmptyElement,
89
} from '../_util/props-util';
910
import { Col } from '../grid';
1011
import { ConfigConsumerProps } from '../config-provider';
1112
import { ListGridType } from './index';
13+
import { cloneElement } from '../_util/vnode';
1214

1315
export const ListItemProps = {
1416
prefixCls: PropTypes.string,
@@ -68,59 +70,68 @@ export default {
6870
listContext: { default: () => ({}) },
6971
configProvider: { default: () => ConfigConsumerProps },
7072
},
73+
methods: {
74+
isItemContainsTextNodeAndNotSingular() {
75+
const { $slots } = this;
76+
let result;
77+
const children = $slots.default || [];
78+
children.forEach(element => {
79+
if (isStringElement(element) && !isEmptyElement(element)) {
80+
result = true;
81+
}
82+
});
83+
return result && children.length > 1;
84+
},
85+
86+
isFlexMode() {
87+
const extra = getComponentFromProp(this, 'extra');
88+
const { itemLayout } = this.listContext;
89+
if (itemLayout === 'vertical') {
90+
return !!extra;
91+
}
92+
return !this.isItemContainsTextNodeAndNotSingular();
93+
},
94+
},
7195
render() {
72-
const { grid } = this.listContext;
96+
const { grid, itemLayout } = this.listContext;
7397
const { prefixCls: customizePrefixCls, $slots } = this;
7498
const listeners = getListeners(this);
7599
const getPrefixCls = this.configProvider.getPrefixCls;
76100
const prefixCls = getPrefixCls('list', customizePrefixCls);
77-
78-
const classString = `${prefixCls}-item`;
79101
const extra = getComponentFromProp(this, 'extra');
80102
const actions = getComponentFromProp(this, 'actions');
81-
const metaContent = [];
82-
const otherContent = [];
83103

84-
($slots.default || []).forEach(element => {
85-
if (!isEmptyElement(element)) {
86-
if (getSlotOptions(element).__ANT_LIST_ITEM_META) {
87-
metaContent.push(element);
88-
} else {
89-
otherContent.push(element);
90-
}
91-
}
92-
});
93-
94-
const contentClassString = classNames(`${prefixCls}-item-content`, {
95-
[`${prefixCls}-item-content-single`]: metaContent.length < 1,
96-
});
97-
const content =
98-
otherContent.length > 0 ? <div class={contentClassString}>{otherContent}</div> : null;
99-
100-
let actionsContent;
101-
if (actions && actions.length > 0) {
102-
const actionsContentItem = (action, i) => (
103-
<li key={`${prefixCls}-item-action-${i}`}>
104-
{action}
105-
{i !== actions.length - 1 && <em class={`${prefixCls}-item-action-split`} />}
106-
</li>
107-
);
108-
actionsContent = (
109-
<ul class={`${prefixCls}-item-action`}>
110-
{actions.map((action, i) => actionsContentItem(action, i))}
111-
</ul>
112-
);
113-
}
104+
const actionsContent = actions && actions.length > 0 && (
105+
<ul class={`${prefixCls}-item-action`} key="actions">
106+
{actions.map((action, i) => (
107+
<li key={`${prefixCls}-item-action-${i}`}>
108+
{action}
109+
{i !== actions.length - 1 && <em class={`${prefixCls}-item-action-split`} />}
110+
</li>
111+
))}
112+
</ul>
113+
);
114114

115-
const extraContent = (
116-
<div class={`${prefixCls}-item-extra-wrap`}>
117-
<div class={`${prefixCls}-item-main`}>
118-
{metaContent}
119-
{content}
120-
{actionsContent}
121-
</div>
122-
<div class={`${prefixCls}-item-extra`}>{extra}</div>
123-
</div>
115+
const Tag = grid ? 'div' : 'li';
116+
const itemChildren = (
117+
<Tag
118+
{...{ on: listeners }}
119+
class={classNames(`${prefixCls}-item`, {
120+
[`${prefixCls}-item-no-flex`]: !this.isFlexMode(),
121+
})}
122+
>
123+
{itemLayout === 'vertical' && extra
124+
? [
125+
<div class={`${prefixCls}-item-main`} key="content">
126+
{$slots.default}
127+
{actionsContent}
128+
</div>,
129+
<div class={`${prefixCls}-item-extra`} key="extra">
130+
{extra}
131+
</div>,
132+
]
133+
: [$slots.default, actionsContent, cloneElement(extra, { key: 'extra' })]}
134+
</Tag>
124135
);
125136

126137
const mainContent = grid ? (
@@ -133,20 +144,10 @@ export default {
133144
xl={getGrid(grid, 'xl')}
134145
xxl={getGrid(grid, 'xxl')}
135146
>
136-
<div {...{ on: listeners }} class={classString}>
137-
{extra && extraContent}
138-
{!extra && metaContent}
139-
{!extra && content}
140-
{!extra && actionsContent}
141-
</div>
147+
{itemChildren}
142148
</Col>
143149
) : (
144-
<div {...{ on: listeners }} class={classString}>
145-
{extra && extraContent}
146-
{!extra && metaContent}
147-
{!extra && content}
148-
{!extra && actionsContent}
149-
</div>
150+
itemChildren
150151
);
151152

152153
return mainContent;

0 commit comments

Comments
 (0)