Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions demos/demo3-childrenClassName/Demo.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React, { Component } from 'react';
import { DragDropContext } from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';

import { Nestable } from '../../src/react-dnd-nestable';

class Demo extends Component {
state = {
items: [
{ id: 1, text: 'Item #1', children: [] },
{ id: 2, text: 'Item #2', children: [] },
{ id: 3, text: 'Item #3', children: [] },
{ id: 4, text: 'Item #4', children: [{ id: 5, text: 'Item #5', children: [] }] }
]
};

renderItem = ({ item }) => {
return <div style={ styles.item }>{ item.text }</div>;
};

updateItems = (newItems) => {
this.setState({ items: newItems });
};

componentDidMount(){
require("./demo.css")
}

render() {
return (
<Nestable
items={ this.state.items }
renderItem={ this.renderItem }
onUpdate={ this.updateItems }
childrenClassName="child"
/>
);
}
}

var styles = {
item: {
marginBottom: 5,
padding: 10,
border: '1px solid #000',
background: '#fff'
},
};

export default DragDropContext(HTML5Backend)(Demo);
3 changes: 3 additions & 0 deletions demos/demo3-childrenClassName/demo.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.child {
margin-left: 30px;
}
25 changes: 25 additions & 0 deletions demos/demo3-childrenClassName/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!doctype html>
<html lang="en">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<head>
<meta charset="UTF-8">
<title>Nested List</title>
<style>
body {
margin: 5px;
}
ol {
margin: 0;
padding: 0;
list-style: none;
}
</style>
</head>
<body>

<div id="content"></div>

<script src="./all.js"></script>

</body>
</html>
26 changes: 26 additions & 0 deletions demos/demo3-childrenClassName/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import ReactDOM from 'react-dom';
import Demo from './Demo';
import { AppContainer } from 'react-hot-loader';

const rootEl = document.querySelector('#content');

ReactDOM.render(
<AppContainer>
<Demo />
</AppContainer>,
rootEl
);

if (module.hot) {
module.hot.accept('./Demo', () => {
const NewDemo = require('./Demo').default;

ReactDOM.render(
<AppContainer>
<NewDemo />
</AppContainer>,
rootEl
);
});
}
53 changes: 53 additions & 0 deletions demos/demo4-isDragging/Demo.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React, { Component } from 'react';
import { DragDropContext } from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';

import { Nestable } from '../../src/react-dnd-nestable';

class Demo extends Component {
state = {
items: [
{ id: 1, text: 'Item #1', children: [] },
{ id: 2, text: 'Item #2', children: [] },
{ id: 3, text: 'Item #3', children: [] },
{ id: 4, text: 'Item #4', children: [{ id: 5, text: 'Item #5', children: [] }] }
]
};

renderItem = ({ item, isDragging }) => {
if (isDragging) {
return <div style={{...styles.item, background: 'pink'}}>{item.text}</div>
}
return <div style={ styles.item }>{ item.text }</div>;
};

updateItems = (newItems) => {
this.setState({ items: newItems });
};

componentDidMount(){
require("./demo.css")
}

render() {
return (
<Nestable
items={ this.state.items }
renderItem={ this.renderItem }
onUpdate={ this.updateItems }
childrenClassName="child"
/>
);
}
}

var styles = {
item: {
marginBottom: 5,
padding: 10,
border: '1px solid #000',
background: '#fff'
},
};

export default DragDropContext(HTML5Backend)(Demo);
3 changes: 3 additions & 0 deletions demos/demo4-isDragging/demo.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.child {
margin-left: 30px;
}
25 changes: 25 additions & 0 deletions demos/demo4-isDragging/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!doctype html>
<html lang="en">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<head>
<meta charset="UTF-8">
<title>Nested List</title>
<style>
body {
margin: 5px;
}
ol {
margin: 0;
padding: 0;
list-style: none;
}
</style>
</head>
<body>

<div id="content"></div>

<script src="./all.js"></script>

</body>
</html>
26 changes: 26 additions & 0 deletions demos/demo4-isDragging/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import ReactDOM from 'react-dom';
import Demo from './Demo';
import { AppContainer } from 'react-hot-loader';

const rootEl = document.querySelector('#content');

ReactDOM.render(
<AppContainer>
<Demo />
</AppContainer>,
rootEl
);

if (module.hot) {
module.hot.accept('./Demo', () => {
const NewDemo = require('./Demo').default;

ReactDOM.render(
<AppContainer>
<NewDemo />
</AppContainer>,
rootEl
);
});
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-1": "^6.5.0",
"css-loader": "^0.28.7",
"enzyme": "^2.2.0",
"eslint": "^2.9.0",
"eslint-friendly-formatter": "^2.0.2",
Expand All @@ -43,6 +44,7 @@
"react-dom": "^15.0.2",
"react-hot-loader": "^3.0.0-beta.2",
"react-transform-catch-errors": "^1.0.2",
"style-loader": "^0.18.2",
"webpack": "^1.13.0",
"webpack-dev-server": "^1.14.1"
},
Expand Down
4 changes: 3 additions & 1 deletion src/Container.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ class Container extends Component {
const {
items,
parentPosition,
childrenClassName,
childrenProperty,
childrenStyle,
topLevel
} = this.props;

return (
<ol style={ topLevel ? {} : childrenStyle }>
<ol style={ topLevel ? {} : childrenStyle } className={ topLevel ? '' : childrenClassName }>
{ items.map((item, i) => {
const position = parentPosition.concat([i]);
const children = item[childrenProperty];
Expand All @@ -54,6 +55,7 @@ class Container extends Component {
parentPosition={ position }
childrenProperty={ childrenProperty }
childrenStyle={ childrenStyle }
childrenClassName={childrenClassName}
/>
: null
}
Expand Down
18 changes: 17 additions & 1 deletion src/Item.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,23 @@ function decreaseHorizontalLevel(prevPosition) {

const cardSource = {
isDragging(props, monitor) {
return props.id === monitor.getItem().id;
let draggedItem = monitor.getItem();


let ids = [];
const flattenId = (item) => {
ids.push(item.id)
if (item.children && item.children.length) {
item.children.forEach(child => flattenId(child));
}
}

flattenId(draggedItem.data)

if (ids.indexOf(props.id) > -1){
return true
}

},
beginDrag(props, monitor, component) {
const node = findDOMNode(component);
Expand Down
10 changes: 7 additions & 3 deletions src/Nestable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class Nestable extends Component {
renderItem: () => { throw new Error('Nestable: You must supply a renderItem prop.'); },
useDragHandle: false,
maxDepth: Infinity,
threshold: 30
threshold: 30,
childrenClassName: ''
};

static childContextTypes = {
Expand All @@ -69,7 +70,8 @@ class Nestable extends Component {
threshold: PropTypes.number.isRequired,
renderItem: PropTypes.func.isRequired,
moveItem: PropTypes.func.isRequired,
dropItem: PropTypes.func.isRequired
dropItem: PropTypes.func.isRequired,
childrenClassName: PropTypes.string
};

state = {
Expand Down Expand Up @@ -139,7 +141,7 @@ class Nestable extends Component {

render() {
const { items } = this.state;
const { renderItem, childrenProperty, childrenStyle } = this.props;
const { renderItem, childrenProperty, childrenStyle, childrenClassName } = this.props;

return (
<div>
Expand All @@ -148,12 +150,14 @@ class Nestable extends Component {
parentPosition={ [] }
childrenProperty={ childrenProperty }
childrenStyle={ childrenStyle }
childrenClassName={ childrenClassName }
topLevel={ true }
/>
<CustomDragLayer
renderItem={ renderItem }
childrenProperty={ childrenProperty }
childrenStyle={ childrenStyle }
childrenClassName={ childrenClassName }
/>
</div>
);
Expand Down
7 changes: 5 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ var plugins = [
var entry = {
'demo0-flat-list': './demos/demo0-flat-list/index.jsx',
'demo1-nested-list': './demos/demo1-nested-list/index.jsx',
'demo2-drag-handles': './demos/demo2-drag-handles/index.jsx'
'demo2-drag-handles': './demos/demo2-drag-handles/index.jsx',
'demo3-childrenClassName': './demos/demo3-childrenClassName/index.jsx',
'demo4-isDragging': './demos/demo4-isDragging/index.jsx'
};

if (process.env.NODE_ENV === 'development') {
Expand Down Expand Up @@ -56,7 +58,8 @@ module.exports = {
test: /\.jsx?$/,
exclude: /build|node_modules/,
loaders: ['babel']
}
},
{ test: /\.css$/, loader: "style-loader!css-loader" }
]
},
resolve: {
Expand Down