Skip to content

Commit b24552b

Browse files
committed
refactor: list to ts
1 parent f6953dc commit b24552b

File tree

4 files changed

+45
-33
lines changed

4 files changed

+45
-33
lines changed

components/list/Item.jsx renamed to components/list/Item.tsx

+14-8
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ import classNames from '../_util/classNames';
33
import { getComponent, isStringElement, isEmptyElement, getSlot } from '../_util/props-util';
44
import { Col } from '../grid';
55
import { defaultConfigProvider } from '../config-provider';
6-
import { ListGridType } from './index';
76
import { cloneElement } from '../_util/vnode';
8-
import { inject } from 'vue';
7+
import { defineComponent, ExtractPropTypes, FunctionalComponent, inject } from 'vue';
98

109
export const ListItemProps = {
1110
prefixCls: PropTypes.string,
1211
extra: PropTypes.any,
1312
actions: PropTypes.array,
14-
grid: ListGridType,
13+
grid: PropTypes.any,
1514
};
1615

1716
export const ListItemMetaProps = {
@@ -21,9 +20,11 @@ export const ListItemMetaProps = {
2120
title: PropTypes.any,
2221
};
2322

24-
export const ListItemMeta = (props, { slots }) => {
23+
export const ListItemMeta: FunctionalComponent<Partial<
24+
ExtractPropTypes<typeof ListItemMetaProps>
25+
>> = (props, { slots }) => {
2526
const configProvider = inject('configProvider', defaultConfigProvider);
26-
const getPrefixCls = configProvider.getPrefixCls;
27+
const { getPrefixCls } = configProvider;
2728
const { prefixCls: customizePrefixCls } = props;
2829
const prefixCls = getPrefixCls('list', customizePrefixCls);
2930
const avatar = props.avatar || slots.avatar?.();
@@ -53,13 +54,18 @@ function getGrid(grid, t) {
5354
return grid[t] && Math.floor(24 / grid[t]);
5455
}
5556

56-
export default {
57+
export interface ListContext {
58+
grid?: any;
59+
itemLayout?: string;
60+
}
61+
62+
export default defineComponent({
5763
name: 'AListItem',
5864
inheritAttrs: false,
5965
Meta: ListItemMeta,
6066
props: ListItemProps,
6167
setup() {
62-
const listContext = inject('listContext', {});
68+
const listContext = inject<ListContext>('listContext', {});
6369
const configProvider = inject('configProvider', defaultConfigProvider);
6470
return {
6571
listContext,
@@ -147,4 +153,4 @@ export default {
147153

148154
return mainContent;
149155
},
150-
};
156+
});

components/list/__tests__/__snapshots__/empty.test.js.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ exports[`List renders empty list 1`] = `
99
<div class="ant-spin-container">
1010
<div class="ant-list-empty-text">
1111
<div class="ant-empty ant-empty-normal">
12-
<div class="ant-empty-image"><svg width="64" height="41" viewBox="0 0 64 41" xmlns="http://www.w3.org/2000/svg">
12+
<div class="ant-empty-image"><svg class="ant-empty-img-default" width="64" height="41" viewBox="0 0 64 41">
1313
<g transform="translate(0 1)" fill="none" fill-rule="evenodd">
14-
<ellipse fill="#F5F5F5" cx="32" cy="33" rx="32" ry="7"></ellipse>
15-
<g fill-rule="nonzero" stroke="#D9D9D9">
14+
<ellipse class="ant-empty-img-default-ellipse" fill="#F5F5F5" cx="32" cy="33" rx="32" ry="7"></ellipse>
15+
<g class="ant-empty-img-default-g" fill-rule="nonzero" stroke="#D9D9D9">
1616
<path d="M55 12.76L44.854 1.258C44.367.474 43.656 0 42.907 0H21.093c-.749 0-1.46.474-1.947 1.257L9 12.761V22h46v-9.24z"></path>
17-
<path d="M41.613 15.931c0-1.605.994-2.93 2.227-2.931H55v18.137C55 33.26 53.68 35 52.05 35h-40.1C10.32 35 9 33.259 9 31.137V13h11.16c1.233 0 2.227 1.323 2.227 2.928v.022c0 1.605 1.005 2.901 2.237 2.901h14.752c1.232 0 2.237-1.308 2.237-2.913v-.007z" fill="#FAFAFA"></path>
17+
<path d="M41.613 15.931c0-1.605.994-2.93 2.227-2.931H55v18.137C55 33.26 53.68 35 52.05 35h-40.1C10.32 35 9 33.259 9 31.137V13h11.16c1.233 0 2.227 1.323 2.227 2.928v.022c0 1.605 1.005 2.901 2.237 2.901h14.752c1.232 0 2.237-1.308 2.237-2.913v-.007z" fill="#FAFAFA" class="ant-empty-img-default-path"></path>
1818
</g>
1919
</g>
2020
</svg></div>

components/list/index.jsx renamed to components/list/index.tsx

+27-21
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import Pagination, { PaginationConfig } from '../pagination';
88
import { Row } from '../grid';
99

1010
import Item from './Item';
11-
import { initDefaultProps, getComponent, getSlot } from '../_util/props-util';
11+
import { getComponent, getSlot } from '../_util/props-util';
12+
import initDefaultProps from '../_util/props-util/initDefaultProps';
1213
import { cloneElement } from '../_util/vnode';
13-
import { provide, inject } from 'vue';
14+
import { provide, inject, defineComponent, App } from 'vue';
15+
import { tuple } from '../_util/type';
1416

1517
export { ListItemProps, ListItemMetaProps } from './Item';
1618

@@ -29,7 +31,7 @@ export const ListGridType = {
2931
xxl: PropTypes.oneOf(ColumnCount),
3032
};
3133

32-
export const ListSize = ['small', 'default', 'large'];
34+
export const ListSize = tuple('small', 'default', 'large');
3335

3436
export const ListProps = () => ({
3537
bordered: PropTypes.looseBool,
@@ -52,7 +54,7 @@ export const ListProps = () => ({
5254
locale: PropTypes.object,
5355
});
5456

55-
const List = {
57+
const List = defineComponent({
5658
inheritAttrs: false,
5759
Item,
5860
name: 'AList',
@@ -65,31 +67,35 @@ const List = {
6567
}),
6668
created() {
6769
provide('listContext', this);
68-
},
69-
setup() {
70-
return {
71-
configProvider: inject('configProvider', defaultConfigProvider),
72-
};
73-
},
74-
75-
data() {
76-
this.keys = [];
7770
this.defaultPaginationProps = {
7871
current: 1,
7972
pageSize: 10,
80-
onChange: (page, pageSize) => {
73+
onChange: (page: number, pageSize: number) => {
8174
const { pagination } = this;
8275
this.paginationCurrent = page;
83-
if (pagination && pagination.onChange) {
84-
pagination.onChange(page, pageSize);
76+
if (pagination && (pagination as any).onChange) {
77+
(pagination as any).onChange(page, pageSize);
8578
}
8679
},
8780
total: 0,
8881
};
8982
this.onPaginationChange = this.triggerPaginationEvent('onChange');
9083
this.onPaginationShowSizeChange = this.triggerPaginationEvent('onShowSizeChange');
84+
},
85+
setup() {
86+
return {
87+
keys: [],
88+
defaultPaginationProps: {},
89+
onPaginationChange: null,
90+
onPaginationShowSizeChange: null,
91+
configProvider: inject('configProvider', defaultConfigProvider),
92+
};
93+
},
94+
95+
data() {
9196
const { pagination } = this.$props;
92-
const paginationObj = pagination && typeof pagination === 'object' ? pagination : {};
97+
const paginationObj: Partial<typeof pagination> =
98+
pagination && typeof pagination === 'object' ? pagination : {};
9399
return {
94100
paginationCurrent: paginationObj.defaultCurrent || 1,
95101
paginationSize: paginationObj.defaultPageSize || 10,
@@ -163,7 +169,7 @@ const List = {
163169
paginationSize,
164170
$attrs,
165171
} = this;
166-
const getPrefixCls = this.configProvider.getPrefixCls;
172+
const { getPrefixCls } = this.configProvider;
167173
const prefixCls = getPrefixCls('list', customizePrefixCls);
168174
const { class: className, ...restAttrs } = $attrs;
169175
const loadMore = getComponent(this, 'loadMore');
@@ -209,7 +215,7 @@ const List = {
209215
total: dataSource.length,
210216
current: paginationCurrent,
211217
pageSize: paginationSize,
212-
...(pagination || {}),
218+
...((pagination as any) || {}),
213219
};
214220
classString;
215221
const largestPage = Math.ceil(paginationProps.total / paginationProps.pageSize);
@@ -276,10 +282,10 @@ const List = {
276282
</div>
277283
);
278284
},
279-
};
285+
});
280286

281287
/* istanbul ignore next */
282-
List.install = function(app) {
288+
List.install = function(app: App) {
283289
app.component(List.name, List);
284290
app.component(List.Item.name, List.Item);
285291
app.component(List.Item.Meta.displayName, List.Item.Meta);
File renamed without changes.

0 commit comments

Comments
 (0)