@gm-pc/sortable 1.24.9-beta.11 → 1.24.9-beta.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,177 +1,177 @@
1
- // @ts-nocheck
2
- /* eslint-disable */
3
- import React, { useEffect, useRef } from 'react'
4
- import { findDOMNode } from 'react-dom'
5
- import { storiesOf } from '@storybook/react'
6
- import { observer } from 'mobx-react'
7
- import { observable } from 'mobx'
8
- import { List, Flex } from '@gm-pc/react'
9
- import SortableJS from 'sortablejs'
10
- import _ from 'lodash'
11
-
12
- const store = observable({
13
- data: [
14
- {
15
- value: 0,
16
- text: '大白菜',
17
- },
18
- {
19
- value: 1,
20
- text: '牛肉',
21
- },
22
- {
23
- value: '2',
24
- text: '鸡肉',
25
- },
26
- {
27
- value: 3,
28
- text: '鸭肉',
29
- },
30
- {
31
- value: 4,
32
- text: '大闸蟹',
33
- },
34
- ],
35
- setData(data) {
36
- console.log(data)
37
- this.data = data
38
- },
39
- })
40
-
41
- const Wrap = observer(({ data, onSortChange }) => {
42
- const refList = useRef(null)
43
-
44
- useEffect(() => {
45
- const map = {}
46
-
47
- const target = findDOMNode(refList.current).childNodes[0]
48
- const sortable = new SortableJS(target, {
49
- animation: 150,
50
- onUpdate: () => {
51
- const newIds = sortable.toArray()
52
- const newData = _.map(newIds.slice(1), (v) => map[v])
53
- onSortChange(newData)
54
- },
55
- })
56
-
57
- // 第一个忽略,不知道为啥
58
- _.each(sortable.toArray().slice(1), (v, i) => {
59
- map[v] = data[i]
60
- })
61
-
62
- return () => {
63
- sortable.destroy()
64
- }
65
- }, [data])
66
-
67
- return <List ref={refList} data={data} />
68
- })
69
-
70
- const Custom = () => {
71
- const refList1 = useRef(null)
72
- const refList2 = useRef(null)
73
-
74
- useEffect(() => {
75
- const target = findDOMNode(refList1.current)
76
- const sortable = new SortableJS(target, {
77
- swap: true,
78
- swapClass: 'gm-back-bg',
79
- animation: 150,
80
- group: 'group',
81
- onUpdate: () => {
82
- const newIds = sortable.toArray()
83
- console.log(newIds)
84
- },
85
- })
86
-
87
- const target2 = findDOMNode(refList2.current)
88
- const sortable2 = new SortableJS(target2, {
89
- swap: true,
90
- swapClass: 'gm-back-bg',
91
- animation: 150,
92
- group: 'group',
93
- onUpdate: () => {
94
- const newIds = sortable.toArray()
95
- console.log(newIds)
96
- },
97
- })
98
- }, [])
99
-
100
- return (
101
- <div>
102
- <Flex ref={refList1}>
103
- <div
104
- className='gm-padding-10 gm-border'
105
- style={{
106
- width: '100px',
107
- height: '100px',
108
- }}
109
- >
110
- 1
111
- </div>
112
- <div
113
- className='gm-padding-10 gm-border'
114
- style={{
115
- width: '100px',
116
- height: '100px',
117
- }}
118
- >
119
- 2
120
- </div>
121
-
122
- <div
123
- className='gm-padding-10 gm-border'
124
- style={{
125
- width: '100px',
126
- height: '100px',
127
- }}
128
- >
129
- 3
130
- </div>
131
- </Flex>
132
- <div
133
- disabled
134
- className='disabled'
135
- style={{ width: '100%', margin: '10px', border: '1px solid black' }}
136
- />
137
- <Flex ref={refList2}>
138
- <div
139
- className='gm-padding-10 gm-border'
140
- style={{
141
- width: '100px',
142
- height: '100px',
143
- }}
144
- >
145
- 4
146
- </div>
147
- <div
148
- className='gm-padding-10 gm-border'
149
- style={{
150
- width: '100px',
151
- height: '100px',
152
- }}
153
- >
154
- 5
155
- </div>
156
- </Flex>
157
- </div>
158
- )
159
- }
160
-
161
- storiesOf('Sortable/JS', module)
162
- .add(
163
- 'default',
164
- () => <Wrap data={store.data.slice()} onSortChange={(data) => store.setData(data)} />,
165
- {
166
- info: {
167
- text: `
168
- 复杂情况实例,比如让 List 具有 sortable:
169
- - List 需能用上 ref,如不支持可通过 id 去识别。
170
- - 在 useEffect 里 new SortableJS 使其具备 sortable。 useEffect 第二个参数监听 data 的变动。记得销毁 destroy
171
- - sortable.toArray 获得拖拽后的顺序,并和一开始的样子做比较获得排序后的 data
172
- - toArray 需 slice(1),具体原因未知
173
- `,
174
- },
175
- }
176
- )
177
- .add('custom', () => <Custom />)
1
+ // @ts-nocheck
2
+ /* eslint-disable */
3
+ import React, { useEffect, useRef } from 'react'
4
+ import { findDOMNode } from 'react-dom'
5
+ import { storiesOf } from '@storybook/react'
6
+ import { observer } from 'mobx-react'
7
+ import { observable } from 'mobx'
8
+ import { List, Flex } from '@gm-pc/react'
9
+ import SortableJS from 'sortablejs'
10
+ import _ from 'lodash'
11
+
12
+ const store = observable({
13
+ data: [
14
+ {
15
+ value: 0,
16
+ text: '大白菜',
17
+ },
18
+ {
19
+ value: 1,
20
+ text: '牛肉',
21
+ },
22
+ {
23
+ value: '2',
24
+ text: '鸡肉',
25
+ },
26
+ {
27
+ value: 3,
28
+ text: '鸭肉',
29
+ },
30
+ {
31
+ value: 4,
32
+ text: '大闸蟹',
33
+ },
34
+ ],
35
+ setData(data) {
36
+ console.log(data)
37
+ this.data = data
38
+ },
39
+ })
40
+
41
+ const Wrap = observer(({ data, onSortChange }) => {
42
+ const refList = useRef(null)
43
+
44
+ useEffect(() => {
45
+ const map = {}
46
+
47
+ const target = findDOMNode(refList.current).childNodes[0]
48
+ const sortable = new SortableJS(target, {
49
+ animation: 150,
50
+ onUpdate: () => {
51
+ const newIds = sortable.toArray()
52
+ const newData = _.map(newIds.slice(1), (v) => map[v])
53
+ onSortChange(newData)
54
+ },
55
+ })
56
+
57
+ // 第一个忽略,不知道为啥
58
+ _.each(sortable.toArray().slice(1), (v, i) => {
59
+ map[v] = data[i]
60
+ })
61
+
62
+ return () => {
63
+ sortable.destroy()
64
+ }
65
+ }, [data])
66
+
67
+ return <List ref={refList} data={data} />
68
+ })
69
+
70
+ const Custom = () => {
71
+ const refList1 = useRef(null)
72
+ const refList2 = useRef(null)
73
+
74
+ useEffect(() => {
75
+ const target = findDOMNode(refList1.current)
76
+ const sortable = new SortableJS(target, {
77
+ swap: true,
78
+ swapClass: 'gm-back-bg',
79
+ animation: 150,
80
+ group: 'group',
81
+ onUpdate: () => {
82
+ const newIds = sortable.toArray()
83
+ console.log(newIds)
84
+ },
85
+ })
86
+
87
+ const target2 = findDOMNode(refList2.current)
88
+ const sortable2 = new SortableJS(target2, {
89
+ swap: true,
90
+ swapClass: 'gm-back-bg',
91
+ animation: 150,
92
+ group: 'group',
93
+ onUpdate: () => {
94
+ const newIds = sortable.toArray()
95
+ console.log(newIds)
96
+ },
97
+ })
98
+ }, [])
99
+
100
+ return (
101
+ <div>
102
+ <Flex ref={refList1}>
103
+ <div
104
+ className='gm-padding-10 gm-border'
105
+ style={{
106
+ width: '100px',
107
+ height: '100px',
108
+ }}
109
+ >
110
+ 1
111
+ </div>
112
+ <div
113
+ className='gm-padding-10 gm-border'
114
+ style={{
115
+ width: '100px',
116
+ height: '100px',
117
+ }}
118
+ >
119
+ 2
120
+ </div>
121
+
122
+ <div
123
+ className='gm-padding-10 gm-border'
124
+ style={{
125
+ width: '100px',
126
+ height: '100px',
127
+ }}
128
+ >
129
+ 3
130
+ </div>
131
+ </Flex>
132
+ <div
133
+ disabled
134
+ className='disabled'
135
+ style={{ width: '100%', margin: '10px', border: '1px solid black' }}
136
+ />
137
+ <Flex ref={refList2}>
138
+ <div
139
+ className='gm-padding-10 gm-border'
140
+ style={{
141
+ width: '100px',
142
+ height: '100px',
143
+ }}
144
+ >
145
+ 4
146
+ </div>
147
+ <div
148
+ className='gm-padding-10 gm-border'
149
+ style={{
150
+ width: '100px',
151
+ height: '100px',
152
+ }}
153
+ >
154
+ 5
155
+ </div>
156
+ </Flex>
157
+ </div>
158
+ )
159
+ }
160
+
161
+ storiesOf('Sortable/JS', module)
162
+ .add(
163
+ 'default',
164
+ () => <Wrap data={store.data.slice()} onSortChange={(data) => store.setData(data)} />,
165
+ {
166
+ info: {
167
+ text: `
168
+ 复杂情况实例,比如让 List 具有 sortable:
169
+ - List 需能用上 ref,如不支持可通过 id 去识别。
170
+ - 在 useEffect 里 new SortableJS 使其具备 sortable。 useEffect 第二个参数监听 data 的变动。记得销毁 destroy
171
+ - sortable.toArray 获得拖拽后的顺序,并和一开始的样子做比较获得排序后的 data
172
+ - toArray 需 slice(1),具体原因未知
173
+ `,
174
+ },
175
+ }
176
+ )
177
+ .add('custom', () => <Custom />)