-
Notifications
You must be signed in to change notification settings - Fork 51
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
Removable items by moving them out of bounds #16
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { | ||
Examples, | ||
getTestUrl, | ||
trackMouse, | ||
untrackMouse, | ||
addFontStyles, | ||
getListItems | ||
} from './utils'; | ||
|
||
jest.setTimeout(10000); | ||
|
||
beforeEach(async () => { | ||
await page.goto(getTestUrl(Examples.REMOVABLE)); | ||
await page.setViewport({ width: 1030, height: 800 }); | ||
await addFontStyles(page); | ||
}); | ||
|
||
test('dnd the second item out the bounds to be removed', async () => { | ||
await trackMouse(page); | ||
await page.mouse.move(517, 275); | ||
await page.mouse.down(); | ||
await page.mouse.move(828, 222); | ||
await page.mouse.up(); | ||
// make sure that originally dragged item is visible (rendered) | ||
// in a new place | ||
await page.waitForSelector(`#root li:nth-child(1)`, { | ||
visible: true | ||
}); | ||
expect(await getListItems(page)).toEqual([ | ||
'You can remove items by moving them far left or right. Also, onChange always gives you the getBoundingClientRect of the dropped item.', | ||
'Item 3', | ||
'Item 4', | ||
'Item 5', | ||
'Item 6' | ||
]); | ||
await untrackMouse(page); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import * as React from 'react'; | ||
import { List, arrayMove, arrayRemove } from '../src/index'; | ||
|
||
const initialState = { | ||
items: [ | ||
'You can remove items by moving them far left or right. Also, onChange always gives you the getBoundingClientRect of the dropped item.', | ||
'Note, that newIndex = -1 means that item is out of bounds and should be removed.', | ||
'Item 3', | ||
'Item 4', | ||
'Item 5', | ||
'Item 6' | ||
] | ||
}; | ||
|
||
class RemovableByMove extends React.Component<{}, { items: string[] }> { | ||
state = initialState; | ||
render() { | ||
return ( | ||
<div | ||
style={{ | ||
maxWidth: '300px', | ||
margin: '0px auto', | ||
backgroundColor: '#F7F7F7', | ||
padding: '3em', | ||
textAlign: 'center' | ||
}} | ||
> | ||
<List | ||
removableByMove | ||
values={this.state.items} | ||
onChange={({ oldIndex, newIndex, targetRect }) => { | ||
console.log(oldIndex, newIndex, targetRect); | ||
this.setState((prevState: { items: string[] }) => ({ | ||
items: | ||
newIndex === -1 | ||
? arrayRemove(prevState.items, oldIndex) | ||
: arrayMove(prevState.items, oldIndex, newIndex) | ||
})); | ||
}} | ||
renderList={({ children, props, isDragged }) => ( | ||
<ul | ||
{...props} | ||
style={{ | ||
padding: '0em 0em 1em 0em', | ||
cursor: isDragged ? 'grabbing' : undefined | ||
}} | ||
> | ||
{children} | ||
</ul> | ||
)} | ||
renderItem={({ | ||
value, | ||
props, | ||
isDragged, | ||
isSelected, | ||
isOutOfBounds | ||
}) => ( | ||
<li | ||
{...props} | ||
style={{ | ||
...props.style, | ||
padding: '1.5em', | ||
textAlign: 'left', | ||
margin: '0.5em 0em', | ||
listStyleType: 'none', | ||
cursor: isDragged ? 'grabbing' : 'grab', | ||
border: '2px solid #CCC', | ||
boxShadow: '3px 3px #AAA', | ||
color: '#333', | ||
borderRadius: '5px', | ||
fontFamily: 'Arial, "Helvetica Neue", Helvetica, sans-serif', | ||
backgroundColor: | ||
isDragged || isSelected | ||
? isOutOfBounds | ||
? '#F08080' | ||
: '#EEE' | ||
: '#FFF' | ||
}} | ||
> | ||
{value} | ||
</li> | ||
)} | ||
/> | ||
<button onClick={() => this.setState(initialState)}>Reset</button> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default RemovableByMove; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was a bit unsure why state is reset here. calling
isDraggedItemOutOfBounds
a second time is required to get back to default? My first instinct would be to reset after this value is consumed. Interested to learn why this was positioned hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isDraggedItemOutOfBounds
is constantly called when an item is being moved around and then one more time when dropped. In both cases,this.state.isDraggedItemOutOfBounds
can be reseted so that's why I put it directly into this method so it can be done only in one place.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an item is being moved around, it can be be flipped to
> -1
and back multiple times.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!