|
@@ -1,4 +1,4 @@
|
|
|
-# Dragact
|
|
|
+# Dragact 👋
|
|
|
[](https://www.npmjs.com/package/dragact) [](https://www.npmjs.com/package/dragact)
|
|
|
|
|
|
|
|
@@ -7,18 +7,19 @@ Dragact 是一款React组件,他能够使你简单、快速的构建出一款
|
|
|

|
|
|

|
|
|
|
|
|
-# Demo地址
|
|
|
+# Demo地址 ✌️
|
|
|
[Live Demo(预览地址)](http://htmlpreview.github.io/?https://github.com/215566435/React-dragger-layout/blob/master/build/index.html)
|
|
|
|
|
|
-# 特点
|
|
|
+# 特点
|
|
|
|
|
|
- [x] 自动布局的网格系统
|
|
|
- [x] 手机上也可以操作
|
|
|
- [x] 高度自适应
|
|
|
-- [x] 静态组件([Live Demo(预览地址)](http://htmlpreview.github.io/?https://github.com/215566435/React-dragger-layout/blob/master/build/index.html))
|
|
|
-- [x] 拖拽组件([Live Demo(预览地址)](http://htmlpreview.github.io/?https://github.com/215566435/React-dragger-layout/blob/master/build/index.html))
|
|
|
+- [x] 静态组件
|
|
|
+- [x] 拖拽组件
|
|
|
- [x] 自动缩放组件
|
|
|
- [x] 自定义拖拽把手
|
|
|
+- [x] 自定义缩放把手
|
|
|
|
|
|
|
|
|
|
|
@@ -41,9 +42,10 @@ const fakeData = [
|
|
|
{ GridX: 0, GridY: 0, w: 4, h: 2, key: '2' }
|
|
|
]
|
|
|
|
|
|
-const blockStyle = {
|
|
|
- background: 'grey',
|
|
|
- height: '100%'
|
|
|
+const getblockStyle = (isDragging) => {
|
|
|
+ return {
|
|
|
+ background: isDragging ? '#1890ff' : 'white',
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
ReactDOM.render(
|
|
@@ -54,17 +56,27 @@ ReactDOM.render(
|
|
|
rowHeight={40}//必填项
|
|
|
margin={[5, 5]}//必填项
|
|
|
className='plant-layout'//必填项
|
|
|
- style={{ background: '#eee' }}//非必填项
|
|
|
+ style={{ background: '#333' }}//非必填项
|
|
|
placeholder={true}//非必填项
|
|
|
>
|
|
|
- {(item, isDragging) => {
|
|
|
- return <div style={blockStyle}>
|
|
|
- {isDragging ? '正在抓取' : '停放'}
|
|
|
- </div>
|
|
|
+ {(item, provided) => {
|
|
|
+ return (
|
|
|
+ <div
|
|
|
+ {...provided.props}
|
|
|
+ {...provided.dragHandle}
|
|
|
+ style={{
|
|
|
+ ...provided.props.style,
|
|
|
+ ...getblockStyle(provided.isDragging)
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ {provided.isDragging ? '正在抓取' : '停放'}
|
|
|
+ </div>
|
|
|
+ )
|
|
|
}}
|
|
|
</Dragact>,
|
|
|
document.getElementById('root')
|
|
|
);
|
|
|
+
|
|
|
```
|
|
|
|
|
|
|
|
@@ -95,18 +107,27 @@ ReactDOM.render(
|
|
|
```jsx
|
|
|
<Dragact
|
|
|
layout={fakeData}//必填项
|
|
|
- ....
|
|
|
+ {...something}
|
|
|
>
|
|
|
- {(item, isDragging) => {
|
|
|
- return <div style={blockStyle}>
|
|
|
- {isDragging ? '正在抓取' : '停放'}
|
|
|
- </div>
|
|
|
+ {(item, provided) => {
|
|
|
+ return (
|
|
|
+ <div
|
|
|
+ {...provided.props}
|
|
|
+ {...provided.dragHandle}
|
|
|
+ style={{
|
|
|
+ ...provided.props.style,
|
|
|
+ ...getblockStyle(provided.isDragging)
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ {provided.isDragging ? '正在抓取' : '停放'}
|
|
|
+ </div>
|
|
|
+ )
|
|
|
}}
|
|
|
</Dragact>,
|
|
|
```
|
|
|
-现在,我们子元素渲染变成一个小小的**构造函数**,第一个入参是您输入数据的每一项,第二个参数就是**isDragging**,状态监听参数。
|
|
|
+现在,我们子元素渲染变成一个小小的**构造函数**,第一个入参是您输入数据的每一项,第二个参数就是**provided**,提供了所有的拖拽属性。
|
|
|
|
|
|
-这么做,轻易的实现了,组件漂亮,不用写map函数,不用写key,同时更容易监听每一个组件的拖拽状态**isDragging**.
|
|
|
+这么做,轻易的实现了,减少组件层级,组件漂亮,不用写map函数,不用写key,同时更容易监听每一个组件的拖拽状态**provided.isDragging**.
|
|
|
|
|
|
更多的依赖注入思想以及好处,请看我的知乎问答:[知乎,方正的回答:如何设计一款组件库](https://www.zhihu.com/question/266745124/answer/322998960)
|
|
|
|
|
@@ -129,20 +150,15 @@ ReactDOM.render(
|
|
|
通过获取dragact组件的实例,我提供了一个api ```getLayout():DragactLayout;```,用于获取当前的**布局信息**。
|
|
|
|
|
|
|
|
|
-### 4.拖拽把手的设计
|
|
|
-设计一个拖拽把手,必须要两个步骤。
|
|
|
-1. 挂件中拖拽把手拥有一个```id='dragact-handle'```的html tag(```<div id='dragact-handle'>我是把手</div>```);
|
|
|
-2. 在数据中标记是否存在把手
|
|
|
-```ts
|
|
|
-const layout = [
|
|
|
- { GridX: 0, GridY: 0, w: 4, h: 2, key: '0',handle:true },
|
|
|
- { GridX: 0, GridY: 0, w: 4, h: 2, key: '1' },
|
|
|
- { GridX: 0, GridY: 0, w: 4, h: 2, key: '2' }
|
|
|
-]
|
|
|
+### 4.滑动中心
|
|
|
+经过不断的努力和尝试,现在所有的widget移动都是依赖重力中心去移动的。
|
|
|
+
|
|
|
+这意味着当我们拖动一个widget的时候更加得心应手和自然。
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-```
|
|
|
|
|
|
-这样的一个设计,略显繁杂,但是起码能够保证了把手的**自定义**,或许在下个版本中我将设计一款高皆组件,使得书写拖拽把手更加轻便简洁。
|
|
|
|
|
|
|
|
|
|
|
@@ -153,7 +169,7 @@ const layout = [
|
|
|
数据属性指的是我们每一个组件所拥有的属性,类似以下的一组数据
|
|
|
```ts
|
|
|
const layout = [
|
|
|
- { GridX: 0, GridY: 0, w: 4, h: 2, key: '0',canResize:false,static:true,canDrag:false,handle:true },
|
|
|
+ { GridX: 0, GridY: 0, w: 4, h: 2, key: '0' },
|
|
|
{ GridX: 0, GridY: 0, w: 4, h: 2, key: '1' },
|
|
|
{ GridX: 0, GridY: 0, w: 4, h: 2, key: '2' }
|
|
|
]
|
|
@@ -163,17 +179,13 @@ GridY:number//必填,挂件布局中的纵坐标
|
|
|
w:number//必填,挂件布局中宽度,整数
|
|
|
h:number//必填,挂件布局中高度,整数
|
|
|
key:number|string//必填,挂件在布局中的唯一id
|
|
|
-canResize:boolean //非必要,能否调整大小的开关
|
|
|
-static:boolean //非必要,静态组件的开关
|
|
|
-canDrag:boolean //非必要,是否能拖拽的开关
|
|
|
-handle:boolean //非必要,是否有拖拽把手的开关
|
|
|
```
|
|
|
|
|
|
|
|
|
### 组件属性
|
|
|
```ts
|
|
|
-interface DragactProps {
|
|
|
- layout?: DragactLayout[] //暂时不推荐使用
|
|
|
+export interface DragactProps {
|
|
|
+ layout: DragactLayoutItem[]
|
|
|
/**
|
|
|
* 宽度切分比
|
|
|
* 这个参数会把容器的宽度平均分为col等份
|
|
@@ -194,7 +206,7 @@ interface DragactProps {
|
|
|
*/
|
|
|
padding?: number,
|
|
|
|
|
|
- children: any[] | any
|
|
|
+ children: (Item: DragactLayoutItem, provided: GridItemProvided) => any,
|
|
|
|
|
|
|
|
|
//
|
|
@@ -220,7 +232,7 @@ interface DragactProps {
|
|
|
/**
|
|
|
* 拖动结束的回调
|
|
|
*/
|
|
|
- onDragEnd?: (key: number | string) => void
|
|
|
+ onDragEnd?: (event: GridItemEvent) => void
|
|
|
|
|
|
/**
|
|
|
* 每个元素的margin,第一个参数是左右,第二个参数是上下
|
|
@@ -231,8 +243,13 @@ interface DragactProps {
|
|
|
* layout的名字
|
|
|
*/
|
|
|
className: number | string
|
|
|
-}
|
|
|
|
|
|
+ /**是否有placeholder */
|
|
|
+ placeholder?: Boolean
|
|
|
+
|
|
|
+ style?: React.CSSProperties
|
|
|
+
|
|
|
+}
|
|
|
```
|
|
|
|
|
|
# Ref Api
|
|
@@ -269,11 +286,6 @@ npm run test
|
|
|
如果你发现了本项目的Bug,请务必马上告诉我。添加一个issue,并且帮忙给出最最简单重现的例子,这能让我快速定位到Bug帮你解决,谢谢!
|
|
|
|
|
|
|
|
|
+# LICENSE
|
|
|
|
|
|
-
|
|
|
-# TODO-LIST
|
|
|
-- [ ] horizontal swaping
|
|
|
-- [x] resizing
|
|
|
-- [x] touch handle
|
|
|
-- [ ] responsive layout
|
|
|
-- [ ] SSR/server rendering
|
|
|
+MIT
|