Skip to content

Commit bd3d041

Browse files
author
Clauderic Demers
committed
fix: updated the behaviour of disabled elements
1 parent 866b41f commit bd3d041

File tree

4 files changed

+71
-44
lines changed

4 files changed

+71
-44
lines changed

src/.stories/Storybook.scss

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646
font-weight: 400;
4747
}
4848

49+
.disabled {
50+
cursor: not-allowed;
51+
opacity: 0.5;
52+
}
53+
4954
// Drag handle
5055
.handle {
5156
display: block;

src/.stories/index.js

+41-17
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ const Handle = SortableHandle(() => <div className={style.handle} />);
3232
const Item = SortableElement((props) => {
3333
return (
3434
<div
35-
className={props.className}
35+
className={classNames(
36+
props.className,
37+
props.isDisabled && style.disabled,
38+
)}
3639
style={{
3740
height: props.height,
3841
...props.style,
@@ -47,19 +50,25 @@ const Item = SortableElement((props) => {
4750
});
4851

4952
const SortableList = SortableContainer(
50-
({className, items, itemClass, shouldUseDragHandle}) => {
53+
({className, items, disabledItems = [], itemClass, shouldUseDragHandle}) => {
5154
return (
5255
<div className={className}>
53-
{items.map(({value, height}, index) => (
54-
<Item
55-
key={`item-${value}`}
56-
className={itemClass}
57-
index={index}
58-
value={value}
59-
height={height}
60-
shouldUseDragHandle={shouldUseDragHandle}
61-
/>
62-
))}
56+
{items.map(({value, height}, index) => {
57+
const disabled = disabledItems.includes(value);
58+
59+
return (
60+
<Item
61+
key={`item-${value}`}
62+
disabled={disabled}
63+
isDisabled={disabled}
64+
className={itemClass}
65+
index={index}
66+
value={value}
67+
height={height}
68+
shouldUseDragHandle={shouldUseDragHandle}
69+
/>
70+
);
71+
})}
6372
</div>
6473
);
6574
},
@@ -119,6 +128,7 @@ class ListWrapper extends Component {
119128
onSortEnd: PropTypes.func,
120129
component: PropTypes.func,
121130
shouldUseDragHandle: PropTypes.bool,
131+
disabledItems: PropTypes.arrayOf(PropTypes.string),
122132
};
123133

124134
static defaultProps = {
@@ -367,6 +377,18 @@ storiesOf('Basic Configuration', module)
367377
</div>
368378
);
369379
})
380+
.add('Disabled items', () => {
381+
return (
382+
<div className={style.root}>
383+
<ListWrapper
384+
component={SortableList}
385+
items={getItems(10, 59)}
386+
helperClass={style.stylizedHelper}
387+
disabledItems={[2, 3, 7]}
388+
/>
389+
</div>
390+
);
391+
})
370392
.add('Elements that shrink', () => {
371393
const getHelperDimensions = ({node}) => ({
372394
height: 20,
@@ -480,11 +502,13 @@ storiesOf('Advanced', module)
480502
})
481503
.add('Custom sortable helper container', () => {
482504
return (
483-
<ListWrapper
484-
component={SortableListWithCustomContainer}
485-
items={getItems(50, 59)}
486-
helperClass={style.stylizedHelper}
487-
/>
505+
<div className={style.root}>
506+
<ListWrapper
507+
component={SortableListWithCustomContainer}
508+
items={getItems(50, 59)}
509+
helperClass={style.stylizedHelper}
510+
/>
511+
</div>
488512
);
489513
});
490514

src/SortableContainer/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,11 @@ export default function sortableContainer(
190190
!this.state.sorting
191191
) {
192192
const {useDragHandle} = this.props;
193-
const {index, collection} = node.sortableInfo;
193+
const {index, collection, disabled} = node.sortableInfo;
194+
195+
if (disabled) {
196+
return;
197+
}
194198

195199
if (
196200
useDragHandle &&
@@ -596,7 +600,7 @@ export default function sortableContainer(
596600

597601
for (let i = 0, len = nodes.length; i < len; i++) {
598602
const {node} = nodes[i];
599-
const index = node.sortableInfo.index;
603+
const {index} = node.sortableInfo;
600604
const width = node.offsetWidth;
601605
const height = node.offsetHeight;
602606
const offset = {

src/SortableElement/index.js

+19-25
Original file line numberDiff line numberDiff line change
@@ -30,54 +30,48 @@ export default function sortableElement(
3030
};
3131

3232
componentDidMount() {
33-
const {collection, disabled, index} = this.props;
34-
35-
if (!disabled) {
36-
this.setDraggable(collection, index);
37-
}
33+
this.register();
3834
}
3935

40-
componentWillReceiveProps(nextProps) {
41-
if (this.props.index !== nextProps.index && this.node) {
42-
this.node.sortableInfo.index = nextProps.index;
43-
}
36+
componentDidUpdate(prevProps) {
37+
if (this.node) {
38+
if (prevProps.index !== this.props.index) {
39+
this.node.sortableInfo.index = this.props.index;
40+
}
4441

45-
if (this.props.disabled !== nextProps.disabled) {
46-
const {collection, disabled, index} = nextProps;
47-
if (disabled) {
48-
this.removeDraggable(collection);
49-
} else {
50-
this.setDraggable(collection, index);
42+
if (prevProps.disabled !== this.props.disabled) {
43+
this.node.sortableInfo.disabled = this.props.disabled;
5144
}
52-
} else if (this.props.collection !== nextProps.collection) {
53-
this.removeDraggable(this.props.collection);
54-
this.setDraggable(nextProps.collection, nextProps.index);
45+
}
46+
47+
if (prevProps.collection !== this.props.collection) {
48+
this.unregister(prevProps.collection);
49+
this.register();
5550
}
5651
}
5752

5853
componentWillUnmount() {
59-
const {collection, disabled} = this.props;
60-
61-
if (!disabled) {
62-
this.removeDraggable(collection);
63-
}
54+
this.unregister();
6455
}
6556

66-
setDraggable(collection, index) {
57+
register() {
58+
const {collection, disabled, index} = this.props;
6759
const node = findDOMNode(this);
6860

6961
node.sortableInfo = {
7062
index,
7163
collection,
64+
disabled,
7265
manager: this.context.manager,
7366
};
7467

7568
this.node = node;
7669
this.ref = {node};
70+
7771
this.context.manager.add(collection, this.ref);
7872
}
7973

80-
removeDraggable(collection) {
74+
unregister(collection = this.props.collection) {
8175
this.context.manager.remove(collection, this.ref);
8276
}
8377

0 commit comments

Comments
 (0)