Skip to content

Feat: Add disabled prop to DraggableList #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ following props:
- `onDragStart` is an optional function which is called once a list item starts
being dragged. Receives the dragged item as an argument.
- `onDragEnd` is an optional function which is called once a list item is no longer being dragged. Receives the dragged item as an argument. It differs from `onMoveEnd` in that it's called even if the user does not reorder any items in the lists, like when an item is just picked up and then dropped.
- `disabled` is an optional boolean that defaults to false. If set to true, then the element will not be draggable.

A DraggableList instance has the following methods:

Expand All @@ -85,7 +86,7 @@ The template component is passed the following props:
to 1 when any item is picked up by the user.
- `dragHandleProps` is an object which should be spread as props on the HTML
element to be used as the drag handle. The whole item will be draggable by the
wrapped element. See the
wrapped element. It includes a `disabled` property that indicates whether dragging is disabled or not. See the
[example](https://github.com/StreakYC/react-draggable-list/blob/master/example/Example.tsx)
to see how it should be used.
- `commonProps` will be set to the same value passed as the `commonProps` prop
Expand Down
1 change: 1 addition & 0 deletions example/Example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export default class Example extends React.Component<{}, ExampleState> {
container={() =>
useContainer ? this._container.current! : document.body
}
disabled={false}
/>
</div>
<footer>Footer here.</footer>
Expand Down
10 changes: 7 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export interface Props<I, C, T> {
autoScrollMaxSpeed?: number;
autoScrollRegionSize?: number;
commonProps?: C;
disabled?: boolean;
onDragStart?: (draggedItem: I) => void;
onDragEnd?: (draggedItem: I) => void;
}
Expand Down Expand Up @@ -90,6 +91,7 @@ export default class DraggableList<
autoScrollMaxSpeed: PropTypes.number.isRequired,
autoScrollRegionSize: PropTypes.number.isRequired,
commonProps: PropTypes.object,
disabled: PropTypes.bool
};
public static defaultProps: Partial<Props<any, any, any>> = {
springConfig: { stiffness: 300, damping: 50 },
Expand All @@ -98,6 +100,7 @@ export default class DraggableList<
constrainDrag: false,
autoScrollMaxSpeed: 15,
autoScrollRegionSize: 30,
disabled: false,
};
private readonly _itemRefs: MultiRef<string, MoveContainer<I, any, T>> =
new MultiRef();
Expand Down Expand Up @@ -606,10 +609,11 @@ export default class DraggableList<
...selectedStyle,
};
const makeDragHandleProps = (getY: () => number | undefined): object => ({
onMouseDown: (e: React.MouseEvent) =>
this._handleMouseDown(key, getY(), e),
onMouseDown: (e: React.MouseEvent) =>
!this.props.disabled && this._handleMouseDown(key, getY(), e),
onTouchStart: (e: React.TouchEvent) =>
this._handleTouchStart(key, getY(), e),
!this.props.disabled && this._handleTouchStart(key, getY(), e),
disabled: this.props.disabled
});
const height = this._getItemHeight(key);
return (
Expand Down