import React from 'react';
import ReactDOM from 'react-dom';
import InfiniteScroll from 'react-infinite-scroller';
import { DndProvider, DragSource, DropTarget, useDrag, useDrop } from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';
import update from 'immutability-helper';
import { Skeleton, Avatar, Button, ButtonGroup, Spin } from '../../index';
import List from '..';
import AutoSizer from 'react-virtualized/dist/commonjs/AutoSizer';
import VList from 'react-virtualized/dist/commonjs/List';
import InfiniteLoader from 'react-virtualized/dist/commonjs/InfiniteLoader';
const Item = List.Item;
export default {
title: 'List'
}
const data = [
'从明天起,做一个幸福的人',
'喂马,劈柴,周游世界',
'从明天起,关心粮食和蔬菜',
'我有一所房子,面朝大海,春暖花开',
];
const titles = ['示例标题1', '示例标题2', '示例标题3', '示例标题4', '示例标题5', '示例标题6'];
export const BasicList = () => (
Default Size
Header
}
footer={Footer
}
bordered
dataSource={data}
renderItem={item => - {item}
}
/>
Default Size
Header} footer={Footer
} bordered>
- console.log('key 1')}>
从明天起,做一个幸福的人
-
喂马,劈柴,周游世界
-
从明天起,关心粮食和蔬菜'
-
我有一所房子,面朝大海,春暖花开
Small Size
Header}
footer={Footer
}
bordered
onClick={e => console.log(e.target)}
onRightClick={e => console.log(e.target)}
dataSource={data}
renderItem={item => {item}}
/>
Large Size
Header}
footer={Footer
}
bordered
dataSource={data}
renderItem={item => {item}}
/>
);
BasicList.story = {
name: 'basic list',
};
export const EmptyList = () => (
Header
} footer={Footer
} bordered />
);
EmptyList.story = {
name: 'empty list',
};
export const JsxList = () => (
Header}
footer={Footer
}
bordered
split={false}
// layout="horizontal"
// grid
grid={{
gutter: 12,
span: 6,
}}
>
-
从明天起,做一个幸福的人
-
喂马,劈柴,周游世界
-
从明天起,关心粮食和蔬菜'
-
我有一所房子,面朝大海,春暖花开
);
JsxList.story = {
name: 'jsx list',
};
export const NoBorder = () => (
Header
}
footer={Footer
}
dataSource={data}
renderItem={item => {item}}
/>
Header}
footer={Footer
}
split={false}
dataSource={data}
renderItem={item => {item}}
/>
);
NoBorder.story = {
name: 'no border',
};
export const ItemStructure = () => (
(
CA}
main={
示例标题
Semi Design 是由互娱社区前端团队与 UED
团队共同设计开发并维护的设计系统。设计系统包含设计语言以及一整套可复用的前端组件,帮助设计师与开发者更容易地打造高质量的、用户体验一致的、符合设计规范的
Web 应用。
}
extra={
}
/>
)}
/>
);
ItemStructure.story = {
name: 'item structure',
};
export const ItemLayout = () => (
(
CA}
main={
示例标题
Semi Design 是由互娱社区前端团队与 UED
团队共同设计开发并维护的设计系统。设计系统包含设计语言以及一整套可复用的前端组件,帮助设计师与开发者更容易地打造高质量的、用户体验一致的、符合设计规范的
Web 应用。
}
extra={
}
/>
)}
/>
);
ItemLayout.story = {
name: 'item layout',
};
export const ItemLayoutAlign = () => (
(
CA}
main={
示例标题
Semi Design 是由互娱社区前端团队与 UED
团队共同设计开发并维护的设计系统。设计系统包含设计语言以及一整套可复用的前端组件,帮助设计师与开发者更容易地打造高质量的、用户体验一致的、符合设计规范的
Web 应用。
}
align="flex-end"
extra={
}
/>
)}
/>
(
CA}
main={
示例标题
Semi Design 是由互娱社区前端团队与 UED
团队共同设计开发并维护的设计系统。设计系统包含设计语言以及一整套可复用的前端组件,帮助设计师与开发者更容易地打造高质量的、用户体验一致的、符合设计规范的
Web 应用。
}
align="center"
extra={
}
/>
)}
/>
(
CA}
main={
示例标题
Semi Design 是由互娱社区前端团队与 UED
团队共同设计开发并维护的设计系统。设计系统包含设计语言以及一整套可复用的前端组件,帮助设计师与开发者更容易地打造高质量的、用户体验一致的、符合设计规范的
Web 应用。
}
align="baseline"
extra={
}
/>
)}
/>
(
CA}
main={
示例标题
Semi Design 是由互娱社区前端团队与 UED
团队共同设计开发并维护的设计系统。设计系统包含设计语言以及一整套可复用的前端组件,帮助设计师与开发者更容易地打造高质量的、用户体验一致的、符合设计规范的
Web 应用。
}
align="stretch"
extra={
}
/>
)}
/>
);
ItemLayoutAlign.story = {
name: 'item layout align',
};
export const Grid = () => (
);
Grid.story = {
name: 'grid',
};
export const ResponsiveGrid = () => (
(
CA}
main={
示例标题
Semi Design 是由互娱社区前端团队与 UED 团队共同设计开发并维护的设计系统。
}
extra={
}
/>
)}
/>
);
ResponsiveGrid.story = {
name: 'responsive grid',
};
class LoadMoreList extends React.Component {
constructor() {
super();
const count = 3;
const dataList = [];
for (let i = 0; i < 40; i++) {
dataList.push({
color: 'grey',
title: `Semi Design Title ${i}`,
loading: false,
});
}
this.data = dataList;
this.count = 0;
this.fetchData = () => {
let placeholders = [0, 1, 2].map(key => ({ loading: true }));
this.setState({
loading: true,
list: [...this.state.dataSource, ...placeholders],
});
return new Promise((res, rej) => {
setTimeout(() => {
let dataSource = this.data.slice(this.count * count, this.count * count + count);
res(dataSource);
}, 1500);
}).then(dataSource => {
let newData = [...this.state.dataSource, ...dataSource];
this.setState({
loading: false,
dataSource: newData,
list: newData,
noMore: !dataSource.length,
});
});
};
this.state = {
loading: false,
dataSource: [],
list: [],
noMore: false,
};
}
componentDidMount() {
this.fetchData();
}
onLoadMore() {
this.count++;
this.fetchData();
}
render() {
const { loading, list, noMore } = this.state;
const loadMore =
!loading && !noMore ? (
) : null;
const placeholder = (
);
return (
(
SE}
main={
{item.title}
Semi Design
设计系统包含设计语言以及一整套可复用的前端组件,帮助设计师与开发者更容易地打造高质量的、用户体验一致的、符合设计规范的
Web 应用。
}
/>
)}
/>
);
}
}
export const LoadMore = () => ;
LoadMore.story = {
name: 'load more',
};
class ScrollLoad extends React.Component {
constructor() {
super();
const count = 6;
const dataList = [];
for (let i = 0; i < 100; i++) {
dataList.push({
color: 'grey',
title: `Semi Design Title ${i}`,
loading: false,
});
}
this.data = dataList;
this.count = 0;
this.fetchData = () => {
this.setState({
loading: true,
});
return new Promise((res, rej) => {
setTimeout(() => {
let dataSource = this.data.slice(this.count * count, this.count * count + count);
res(dataSource);
}, 1500);
}).then(dataSource => {
let newData = [...this.state.dataSource, ...dataSource];
this.count++;
this.setState({
loading: false,
dataSource: newData,
noMore: !dataSource.length,
});
});
};
this.state = {
loading: false,
dataSource: [],
hasMore: true,
};
}
componentDidMount() {
this.fetchData();
}
render() {
const { loading, dataSource, hasMore } = this.state;
const showLoadMore = this.count % 3 === 0;
const loadMore =
!loading && hasMore && showLoadMore ? (
) : null;
return (
(
SE}
main={
{item.title}
Semi Design
设计系统包含设计语言以及一整套可复用的前端组件,帮助设计师与开发者更容易地打造高质量的、用户体验一致的、符合设计规范的
Web 应用。
}
/>
)}
/>
{this.state.loading && this.state.hasMore && (
)}
);
}
}
export const _ScrollLoad = () => ;
_ScrollLoad.story = {
name: 'scroll load',
};
const listItems = [
{
title: 'Semi Design Title 1',
color: 'red',
},
{
title: 'Semi Design Title 2',
color: 'grey',
},
{
title: 'Semi Design Title 3',
color: 'light-green',
},
{
title: 'Semi Design Title 4',
color: 'light-blue',
},
{
title: 'Semi Design Title 5',
color: 'pink',
},
];
const cardSource = {
beginDrag(props) {
return {
id: props.id,
index: props.index,
};
},
};
const cardTarget = {
hover(props, monitor, component) {
// test demo below is from react-dnd and react-dnd-html5-backend. https://react-dnd.github.io/react-dnd/about
const dragIndex = monitor.getItem().index;
const hoverIndex = props.index;
if (dragIndex === hoverIndex) {
return;
}
const hoverBoundingRect = ReactDOM.findDOMNode(component).getBoundingClientRect();
const hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;
const clientOffset = monitor.getClientOffset();
const hoverClientY = clientOffset.y - hoverBoundingRect.top;
if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) {
return;
}
if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) {
return;
}
monitor.getItem().index = hoverIndex;
props.moveItem(dragIndex, hoverIndex);
},
};
function collectDragSource(connect, monitor) {
return {
connectDragSource: connect.dragSource(),
// isDragging: monitor.isDragging()
draggingItem: monitor.getItem(),
};
}
function collectDropTarget(connect) {
return {
connectDropTarget: connect.dropTarget(),
};
}
class DraggableItem extends React.Component {
render() {
const { component, draggingItem, index, connectDragSource, connectDropTarget } = this.props;
const opacity = draggingItem && draggingItem.index === index ? 0.3 : 1;
const style = {
border: '1px solid var(--semi-color-border)',
marginBottom: 12,
backgroundColor: 'white',
cursor: 'move',
};
return connectDragSource(
connectDropTarget(
(this.node = node)} style={{ ...style, opacity }}>
{component}
)
);
}
}
DraggableItem = DragSource('item', cardSource, collectDragSource)(DraggableItem);
DraggableItem = DropTarget('item', cardTarget, collectDropTarget)(DraggableItem);
class DraggableList extends React.Component {
constructor() {
super();
this.state = {
data: listItems,
};
}
moveItem = (dragIndex, hoverIndex) => {
const { data } = this.state;
const draggingItem = data[dragIndex];
this.setState(
update(this.state, {
data: {
$splice: [
[dragIndex, 1],
[hoverIndex, 0, draggingItem],
],
},
})
);
};
renderDraggable = (item, id) => {
const content = (
SE}
main={
{item.title}
Semi Design
设计系统包含设计语言以及一整套可复用的前端组件,帮助设计师与开发者更容易地打造高质量的、用户体验一致的、符合设计规范的
Web 应用。
}
/>
);
return (
);
};
render() {
const { data } = this.state;
return (
);
}
}
export const Draggable = () => ;
Draggable.story = {
name: 'draggable',
};
class VirtualizedScroll extends React.Component {
constructor() {
super();
const dataList = [];
for (let i = 0; i < 50; i++) {
dataList.push({
color: 'grey',
title: `Semi Design Title ${i}`,
});
}
this.data = dataList;
this.fetchData = (startIndex, stopIndex) => {
return new Promise((res, rej) => {
setTimeout(() => {
let dataSource = this.data.slice(startIndex, stopIndex + 1);
res(dataSource);
}, 1000);
}).then(dataSource => {
let newData = [...this.state.dataSource, ...dataSource];
const { loadedRowsMap, loadingRowCount } = this.state;
const increment = stopIndex - startIndex + 1;
for (let i = startIndex; i <= stopIndex; i++) {
loadedRowsMap[i] = this.statusLoaded;
}
this.setState({
dataSource: newData,
loadedRowsMap,
loadingRowCount: loadingRowCount - increment,
});
});
};
this.state = {
dataSource: [],
loadedRowsMap: {},
loadingRowCount: 0,
};
this.statusLoading = 0;
this.statusLoaded = 1;
this.loadLimit = this.data.length;
this.renderItem = this.renderItem.bind(this);
this.fetchData = this.fetchData.bind(this);
this.handleInfiniteOnLoad = this.handleInfiniteOnLoad.bind(this);
this.isRowLoaded = this.isRowLoaded.bind(this);
}
handleInfiniteOnLoad({ startIndex, stopIndex }) {
let { dataSource, loadedRowsMap, loadingRowCount } = this.state;
const increment = stopIndex - startIndex + 1;
if (stopIndex >= this.loadLimit || loadingRowCount > 0) {
return;
}
for (let i = startIndex; i <= stopIndex; i++) {
loadedRowsMap[i] = this.statusLoading;
}
this.setState({
loadingRowCount: loadingRowCount + increment,
});
return this.fetchData(startIndex, stopIndex);
}
isRowLoaded({ index }) {
const { loadedRowsMap } = this.state;
return !!loadedRowsMap[index];
}
renderItem({ index, key, style }) {
const { dataSource, loadedRowsMap } = this.state;
const item = dataSource[index];
if (!item) {
return;
}
const content = (
SE}
main={
{item.title}
Semi Design
设计系统包含设计语言以及一整套可复用的前端组件,帮助设计师与开发者更容易地打造高质量的、用户体验一致的、符合设计规范的
Web 应用。
}
/>
);
return content;
}
render() {
const { dataSource } = this.state;
const height = 500;
return (
{({ onRowsRendered, registerChild }) => (
{({ width }) => (
)}
)}
);
}
}
export const Virtualized = () => ;
Virtualized.story = {
name: 'virtualized',
};