Skip to content

Commit 6cd3531

Browse files
committed
feat(list): add basic List component
1 parent 699f96a commit 6cd3531

File tree

7 files changed

+114
-62
lines changed

7 files changed

+114
-62
lines changed

src/common/style.ts

Whitespace-only changes.

src/components/list/index.tsx

+2-53
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,3 @@
11
import './style.scss';
2-
import * as React from 'react';
3-
import { prefixClaName, classNames } from 'mo/common/className';
4-
5-
export interface IItem<T = any> {
6-
id?: string;
7-
name?: string;
8-
title?: string;
9-
icon?: string;
10-
disabled?: boolean;
11-
data?: T;
12-
className?: string;
13-
onClick?(event: React.MouseEvent, item: IItem): void;
14-
}
15-
16-
export interface IList<T = any> {
17-
className?: string;
18-
/**
19-
* Default is vertical direction
20-
*/
21-
direction?: 'horizontal' | 'vertical';
22-
}
23-
24-
export function Item(props: React.PropsWithChildren<IItem>) {
25-
const { id, onClick, disabled, className, children, ...others } = props;
26-
const click = (e: React.MouseEvent) => {
27-
if (onClick) {
28-
onClick(e, props);
29-
}
30-
};
31-
return (
32-
<li
33-
className={classNames('list-item', className, disabled)}
34-
key={`${id}`}
35-
>
36-
<a className={'list-label'} onClick={click} {...others}>
37-
{children}
38-
</a>
39-
</li>
40-
);
41-
}
42-
43-
export function List<T = any>(props: React.PropsWithChildren<IList<T>>) {
44-
const { children, className, direction = 'vertical', ...others } = props;
45-
46-
return (
47-
<ul
48-
className={classNames(prefixClaName('list', className), direction)}
49-
{...others}
50-
>
51-
{children}
52-
</ul>
53-
);
54-
}
2+
export * from './list';
3+
export * from './item';

src/components/list/item.tsx

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as React from 'react';
2+
import { classNames } from 'mo/common/className';
3+
import { IList } from './list';
4+
5+
export interface IItem<T = any> extends IList {
6+
icon?: string;
7+
disabled?: boolean;
8+
data?: T;
9+
}
10+
11+
export function Item(props: React.PropsWithChildren<IItem>) {
12+
const { id, onClick, disabled, active, className, children, ...others } = props;
13+
const click = (e: React.MouseEvent) => {
14+
if (onClick) {
15+
onClick(e, props);
16+
}
17+
};
18+
const claNames = classNames('list-item', className, disabled, active === id ? 'active' : '');
19+
return (
20+
<li
21+
className={claNames}
22+
key={`${id}`}
23+
>
24+
<a className={'item-container'} onClick={click} {...others as any}>
25+
{children}
26+
</a>
27+
</li>
28+
);
29+
}

src/components/list/list.tsx

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import './style.scss';
2+
import * as React from 'react';
3+
import { prefixClaName, classNames } from 'mo/common/className';
4+
import { ComponentProps } from 'react';
5+
import { IItem } from './item';
6+
import { cloneReactChildren } from 'mo/react';
7+
8+
export interface IList<T = any> extends ComponentProps<any> {
9+
/**
10+
* Default is vertical mode
11+
*/
12+
mode?: 'horizontal' | 'vertical';
13+
/**
14+
* Current active
15+
*/
16+
active?: string;
17+
onClick?(event: React.MouseEvent, item?: IItem): void;
18+
}
19+
20+
export function List<T = any>(props: React.PropsWithChildren<IList<T>>) {
21+
const { children, active, onClick, className, mode = 'vertical', ...others } = props;
22+
const claNames = classNames(prefixClaName('list'), className, mode);
23+
return (
24+
<ul
25+
{...others}
26+
className={claNames}
27+
>
28+
{
29+
cloneReactChildren<IList>(children, { active, onClick })
30+
}
31+
</ul>
32+
);
33+
}

src/components/list/style.scss

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
@import 'mo/style/const';
1+
@import 'mo/style/common';
22
$list: 'list';
33

44
#{prefix($list)} {
55
display: flex;
6+
height: 100%;
67
margin: 0 auto;
78
padding: 0;
89
width: 100%;
@@ -14,23 +15,42 @@ $list: 'list';
1415
justify-content: left;
1516
list-style: none;
1617
position: relative;
18+
19+
// &.active {
20+
21+
// }
22+
}
23+
24+
.list-item:hover {
25+
opacity: 0.8;
1726
}
1827

1928
&.vertical {
2029
flex-direction: column;
30+
31+
.list-item {
32+
height: 50px;
33+
width: 100%;
34+
}
2135
}
2236

2337
&.horizontal {
2438
flex-direction: row;
39+
40+
.list-item {
41+
margin-left: 10px;
42+
}
2543
}
2644

27-
.list-label {
45+
.item-container {
2846
align-items: center;
2947
background-position: center center;
3048
background-repeat: no-repeat;
3149
background-size: 16px;
3250
display: flex;
51+
height: 100%;
3352
justify-content: left;
3453
text-decoration: none;
54+
width: 100%;
3555
}
3656
}

src/style/theme.scss

+13
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,16 @@
167167
background: rgba(0, 0, 0, 0.1);
168168
}
169169
}
170+
171+
// =============== List =============== //
172+
173+
#{prefix($list)} {
174+
.list-item {
175+
background-color: #252526;
176+
color: #bbb;
177+
178+
&.active {
179+
background-color: #316ac5;
180+
}
181+
}
182+
}

stories/components/9-List.stories.tsx

+15-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { withKnobs } from '@storybook/addon-knobs';
44
import { propsTable } from '../common/propsTable';
55

66
import { List, Item } from 'mo/components/list';
7+
import { useState } from 'react';
78

89
const stories = storiesOf('List', module);
910
stories.addDecorator(withKnobs);
@@ -21,25 +22,32 @@ const propDefinitions = [
2122
stories.add(
2223
'Basic Usage',
2324
() => {
25+
26+
const [active, setActive] = useState('1');
27+
28+
const click = (e, item) => {
29+
console.log('item.', e, item);
30+
setActive(item.id);
31+
}
32+
2433
return (
2534
<div>
2635
<h2>简述</h2>
2736
<p>
28-
ContextView
29-
组件主要是提供了一个可根据指定锚点位置、渲染内容的视图容器。
37+
List component.
3038
</p>
3139
<h2>使用示例</h2>
3240
<div>
3341
<h3>Direction - vertical</h3>
34-
<List direction="vertical">
35-
<Item>Item 1</Item>
36-
<Item>Item 1</Item>
37-
<Item>Item 1</Item>
42+
<List className="custom-list-1" mode="vertical" onClick={click} active={active}>
43+
<Item id="1">Item 1</Item>
44+
<Item id="2">Item 1</Item>
45+
<Item id="3">Item 1</Item>
3846
</List>
3947
</div>
4048
<div>
4149
<h3>Direction - horizontal</h3>
42-
<List direction="horizontal">
50+
<List className="custom-list-2" mode="horizontal">
4351
<Item>Item 1</Item>
4452
<Item>Item 1</Item>
4553
<Item>Item 1</Item>

0 commit comments

Comments
 (0)