Skip to content

Commit 0c52a1c

Browse files
committed
feat(list): initial list component
1 parent dfaf376 commit 0c52a1c

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed

src/components/list/index.tsx

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
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+
}

src/components/list/style.scss

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
@import 'mo/style/const';
2+
$list: 'list';
3+
4+
#{prefix($list)} {
5+
display: flex;
6+
margin: 0 auto;
7+
padding: 0;
8+
width: 100%;
9+
10+
.list-item {
11+
align-items: center;
12+
cursor: pointer;
13+
display: flex;
14+
justify-content: left;
15+
list-style: none;
16+
position: relative;
17+
}
18+
19+
&.vertical {
20+
flex-direction: column;
21+
}
22+
23+
&.horizontal {
24+
flex-direction: row;
25+
}
26+
27+
.list-label {
28+
align-items: center;
29+
background-position: center center;
30+
background-repeat: no-repeat;
31+
background-size: 16px;
32+
display: flex;
33+
justify-content: left;
34+
text-decoration: none;
35+
}
36+
}

stories/components/9-List.stories.tsx

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import * as React from 'react';
2+
import { storiesOf } from '@storybook/react';
3+
import { withKnobs } from '@storybook/addon-knobs';
4+
import { propsTable } from '../common/propsTable';
5+
6+
import { List, Item } from 'mo/components/list';
7+
8+
const stories = storiesOf('List', module);
9+
stories.addDecorator(withKnobs);
10+
11+
const propDefinitions = [
12+
{
13+
property: 'render',
14+
propType: '() => React.ReactNode',
15+
required: false,
16+
description: 'Default render content',
17+
defaultValue: null,
18+
},
19+
];
20+
21+
stories.add(
22+
'Basic Usage',
23+
() => {
24+
return (
25+
<div>
26+
<h2>简述</h2>
27+
<p>
28+
ContextView
29+
组件主要是提供了一个可根据指定锚点位置、渲染内容的视图容器。
30+
</p>
31+
<h2>使用示例</h2>
32+
<div>
33+
<h3>Direction - vertical</h3>
34+
<List direction="vertical">
35+
<Item>Item 1</Item>
36+
<Item>Item 1</Item>
37+
<Item>Item 1</Item>
38+
</List>
39+
</div>
40+
<div>
41+
<h3>Direction - horizontal</h3>
42+
<List direction="horizontal">
43+
<Item>Item 1</Item>
44+
<Item>Item 1</Item>
45+
<Item>Item 1</Item>
46+
</List>
47+
</div>
48+
</div>
49+
);
50+
},
51+
{
52+
info: {
53+
inline: true,
54+
TableComponent: () => propsTable({ propDefinitions }),
55+
// propTablesExclude: [],
56+
text: `
57+
代码示例:
58+
~~~js
59+
import { useContextView } from 'mo/components/contextview';
60+
61+
const contextView = useContextView();
62+
63+
const mouseMove = (event: React.MouseEvent): void => {
64+
contextView.show({
65+
x: event.clientX,
66+
y: event.clientY,
67+
}, () => {
68+
return (
69+
<h1>Hello World</h1>
70+
);
71+
});
72+
};
73+
74+
return (
75+
<div>
76+
<div id="topLeft"
77+
onMouseMove={mouseMove}
78+
style={
79+
{
80+
position: 'absolute',
81+
width: 200,
82+
height: 200,
83+
top: 0,
84+
left: 0,
85+
right: 0,
86+
bottom: 0,
87+
background: '#dddddd',
88+
}
89+
}>
90+
Hover me!
91+
</div>
92+
</div>
93+
);
94+
~~
95+
`,
96+
},
97+
}
98+
);

0 commit comments

Comments
 (0)