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
Implement carousel with basic functionality #74
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d656af4
Add alias expansion for tests
slargman 9444bc0
Add prop-types package
slargman 9bb1ef8
Create basic carousel component
slargman 46112ec
Remove unnecessary range function
slargman bb40414
Clean up App
slargman 074dd93
Remove empty Carousel test file
slargman c682d1b
Add Carousel size test
slargman 94504c4
Rearrange test setup
slargman e3765ce
Achieve proper variable scoping
slargman 9d61b1b
Remove empty test files
slargman af381ec
Test Carousel scrolling
slargman de09ad3
Add Carousel documentation
slargman File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 was deleted.
Oops, something went wrong.
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,41 @@ | ||
import React, { useState } from "react"; | ||
import PropTypes from "prop-types"; | ||
|
||
/** | ||
* Display a set of item with buttons to scroll through them | ||
* @param {Array} items The items to be displayed | ||
* @param {Number} size How many items to display at once | ||
*/ | ||
|
||
function Carousel({ items, size }) { | ||
const [start, setStart] = useState(0); | ||
|
||
const decrement = (event) => { | ||
event.preventDefault(); | ||
setStart((oldStart) => Math.max(0, oldStart - size)); | ||
}; | ||
|
||
const increment = (event) => { | ||
event.preventDefault(); | ||
setStart((oldStart) => Math.min(items.length - size + 1, oldStart + size)); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<button type="button" onClick={decrement}> | ||
< | ||
</button> | ||
{items.slice(start, start + size)} | ||
<button type="button" onClick={increment}> | ||
> | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
Carousel.propTypes = { | ||
items: PropTypes.arrayOf(PropTypes.element).isRequired, | ||
size: PropTypes.number.isRequired, | ||
}; | ||
|
||
export default Carousel; |
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,62 @@ | ||
import React from 'react'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import Carousel from 'App/Carousel.jsx'; | ||
|
||
describe('Carousel', () => { | ||
|
||
const range = (start, end) => ( | ||
Array(end - start + 1).fill().map((_, i) => i + start) | ||
); | ||
|
||
const numbers = range(0, 9); | ||
const size = 4; | ||
|
||
const getScreenItems = () => { | ||
const screenItems = []; | ||
numbers.forEach(i => { | ||
const screenItem = screen.queryByText(new RegExp(`^${i}$`)); | ||
if (screenItem) { | ||
screenItems.push(screenItem); | ||
} | ||
}); | ||
return screenItems; | ||
}; | ||
|
||
const checkScreenItems = (bounds) => { | ||
numbers.forEach(i => { | ||
const screenItem = screen.queryByText(new RegExp(`^${i}$`)); | ||
if (bounds.includes(i)) { | ||
expect(screenItem).toBeVisible(); | ||
} else { | ||
expect(screenItem).toBeNull(); | ||
} | ||
}); | ||
}; | ||
|
||
beforeEach(() => { | ||
const items = numbers.map(i => ( | ||
<div key={i}>{i}</div> | ||
)); | ||
|
||
render( | ||
<Carousel items={items} size={size} /> | ||
); | ||
}); | ||
|
||
it('should show the correct number of items', () => { | ||
let screenItems = getScreenItems(); | ||
expect(screenItems.length).toEqual(size); | ||
}); | ||
|
||
it('should scroll on button presses', () => { | ||
const backButton = screen.getByText('<'); | ||
const forwardButton = screen.getByText('>'); | ||
|
||
fireEvent.click(forwardButton); | ||
checkScreenItems(range(size, 2 * size - 1)); | ||
|
||
fireEvent.click(backButton); | ||
checkScreenItems(range(0, size - 1)); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.
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.
Some good general style conventions, put a comment on public exports. Carousel is pretty self explanatory, but could be nice for more complicated ones, and in general for the props that are being passed
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.
Good call. Added some documentation. The Carousel is shared across the app, but would you suggest documentation on components that only I will be using too?