Skip to content

Commit 149557a

Browse files
committed
fix(dnd): add example to demo
1 parent 6732c2e commit 149557a

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

packages/dnd/public/index.tsx

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,62 @@
1-
import FormRenderer from '@data-driven-forms/react-form-renderer/form-renderer';
2-
import React from 'react';
1+
import React, { useReducer } from 'react';
32
import ReactDOM from 'react-dom';
43

5-
const App = () => <span>"dnd"</span>;
4+
import reducer, { initialState } from '../src/reducer';
5+
import Provider from '../src/provider';
6+
import useContainer from '../src/use-container';
7+
import useComponent from '../src/use-component';
8+
import useHandle from '../src/use-handle';
9+
10+
const Component : React.FC<{id: string; container: string}> = ({ id, container }) => {
11+
const { ref } = useComponent({ id });
12+
const handlers = useHandle({ component: id, sourceContainer: container});
13+
14+
return <div ref={ref} {...handlers} style={{
15+
background: 'blue',
16+
minHeight: 100,
17+
minWidth: 308,
18+
margin: 4,
19+
color: 'white',
20+
textAlign: 'center',
21+
fontSize: 48
22+
}}>{id}</div>;
23+
};
24+
25+
const Container = () => {
26+
const { ref, container, id } = useContainer({ isRoot: true });
27+
28+
return <div ref={ref} style={{
29+
background: 'gray',
30+
minHeight: 400,
31+
minWidth: 400,
32+
padding: 4
33+
}}>
34+
{container.children.map((key: string) =>
35+
<Component key={key} id={key} container={id} />
36+
)}
37+
</div>;
38+
};
39+
40+
const App = () => {
41+
const [state, dispatch] = useReducer(reducer, {
42+
...initialState,
43+
components: {
44+
'#1': {},
45+
'#2': {},
46+
'#3': {}
47+
},
48+
containers: {
49+
root: {
50+
children: ['#1', '#2', '#3'],
51+
ref: null
52+
}
53+
},
54+
});
55+
56+
return <Provider state={state} dispatch={dispatch}>
57+
<Container />
58+
</Provider>;
59+
};
660

761
ReactDOM.render(
862
<App />,

packages/dnd/src/reducer/reducer.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ export const clearDrag = {
44
draggingProps: null
55
};
66

7+
export const initialState = {
8+
...clearDrag,
9+
components: {},
10+
containers: {
11+
root: {
12+
children: [],
13+
ref: null
14+
}
15+
},
16+
};
17+
718
export const dragStart = (state: any, action: any) => ({
819
...state,
920
draggingElement: action.component,

0 commit comments

Comments
 (0)