Skip to content

Commit 286eff4

Browse files
author
Claudéric Demers
authored
feat: added helperContainer prop
1 parent 845c0a0 commit 286eff4

File tree

6 files changed

+177
-86
lines changed

6 files changed

+177
-86
lines changed

README.md

+23-22
Large diffs are not rendered by default.

package.json

+7-5
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
},
5858
"devDependencies": {
5959
"@babel/core": "^7.2.2",
60+
"@storybook/addon-options": "^4.1.4",
6061
"@storybook/react": "^4.1.4",
6162
"autoprefixer": "^6.3.6",
6263
"babel-loader": "^8.0.5",
@@ -69,16 +70,17 @@
6970
"husky": "^1.3.1",
7071
"lodash": "^4.12.0",
7172
"microbundle": "^0.9.0",
72-
"node-sass": "^3.7.0",
73-
"postcss-loader": "^0.9.1",
73+
"node-sass": "^4.11.0",
74+
"postcss": "^7.0.7",
75+
"postcss-loader": "^3.0.0",
7476
"prettier": "^1.15.3",
7577
"pretty-quick": "^1.8.0",
76-
"react": "^15.4.2",
78+
"react": "^16.7.0",
7779
"react-addons-pure-render-mixin": "^15.0.2",
7880
"react-addons-shallow-compare": "^15.1.0",
7981
"react-addons-test-utils": "^15.1.0",
80-
"react-dom": "^15.4.2",
81-
"react-infinite": "^0.9.2",
82+
"react-dom": "^16.7.0",
83+
"react-infinite": "^0.13.0",
8284
"react-tiny-virtual-list": "^2.0.1",
8385
"react-virtualized": "^9.2.2",
8486
"sass-loader": "^7.1.0",

src/.stories/index.js

+38-7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ const SortableList = SortableContainer(
6565
},
6666
);
6767

68+
class SortableListWithCustomContainer extends React.Component {
69+
state = {
70+
container: null,
71+
};
72+
73+
render() {
74+
const {container} = this.state;
75+
76+
return (
77+
<div id="CustomHelperContainer" ref={this.setContainerNode}>
78+
<SortableList {...this.props} helperContainer={container} />
79+
</div>
80+
);
81+
}
82+
83+
setContainerNode = (node) => {
84+
this.setState({container: node});
85+
};
86+
}
87+
6888
const Category = SortableElement((props) => {
6989
return (
7090
<div className={style.category}>
@@ -84,13 +104,11 @@ const Category = SortableElement((props) => {
84104
});
85105

86106
class ListWrapper extends Component {
87-
constructor({items}) {
88-
super();
89-
this.state = {
90-
items,
91-
isSorting: false,
92-
};
93-
}
107+
state = {
108+
items: this.props.items,
109+
isSorting: false,
110+
};
111+
94112
static propTypes = {
95113
items: PropTypes.array,
96114
className: PropTypes.string,
@@ -102,12 +120,14 @@ class ListWrapper extends Component {
102120
component: PropTypes.func,
103121
shouldUseDragHandle: PropTypes.bool,
104122
};
123+
105124
static defaultProps = {
106125
className: classNames(style.list, style.stylizedList),
107126
itemClass: classNames(style.item, style.stylizedItem),
108127
width: 400,
109128
height: 600,
110129
};
130+
111131
onSortStart = () => {
112132
const {onSortStart} = this.props;
113133
this.setState({isSorting: true});
@@ -116,6 +136,7 @@ class ListWrapper extends Component {
116136
onSortStart(this.refs.component);
117137
}
118138
};
139+
119140
onSortEnd = ({oldIndex, newIndex}) => {
120141
const {onSortEnd} = this.props;
121142
const {items} = this.state;
@@ -129,6 +150,7 @@ class ListWrapper extends Component {
129150
onSortEnd(this.refs.component);
130151
}
131152
};
153+
132154
render() {
133155
const Component = this.props.component;
134156
const {items, isSorting} = this.state;
@@ -455,6 +477,15 @@ storiesOf('Advanced', module)
455477
helperClass={style.stylizedHelper}
456478
/>
457479
);
480+
})
481+
.add('Custom sortable helper container', () => {
482+
return (
483+
<ListWrapper
484+
component={SortableListWithCustomContainer}
485+
items={getItems(50, 59)}
486+
helperClass={style.stylizedHelper}
487+
/>
488+
);
458489
});
459490

460491
storiesOf('Customization', module)

src/SortableContainer/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export default function sortableContainer(
105105
]),
106106
getContainer: PropTypes.func,
107107
getHelperDimensions: PropTypes.func,
108+
helperContainer: PropTypes.instanceOf(HTMLElement),
108109
};
109110

110111
static childContextTypes = {
@@ -350,7 +351,7 @@ export default function sortableContainer(
350351
}
351352
});
352353

353-
this.helper = this.document.body.appendChild(clonedNode);
354+
this.helper = this.helperContainer.appendChild(clonedNode);
354355

355356
this.helper.style.position = 'fixed';
356357
this.helper.style.top = `${this.boundingClientRect.top - margin.top}px`;
@@ -877,5 +878,9 @@ export default function sortableContainer(
877878
/>
878879
);
879880
}
881+
882+
get helperContainer() {
883+
return this.props.helperContainer || this.document.body;
884+
}
880885
};
881886
}

types/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export interface SortableContainerProps {
6969
lockOffset?: Offset | [Offset, Offset];
7070
getContainer?: ContainerGetter;
7171
getHelperDimensions?: (sort: SortStart) => Dimensions;
72+
helperContainer?: HTMLElement;
7273
}
7374

7475
export interface SortableElementProps {

0 commit comments

Comments
 (0)