-
Notifications
You must be signed in to change notification settings - Fork 66
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
[BD-46] feat: create chip carousel #2378
Merged
adamstankiewicz
merged 6 commits into
openedx:master
from
raccoongang:zadorozhnii/chip-carousel
Aug 4, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e6e8b46
feat: create chip carousel
585430f
feat: update ChipCarousel
b34f52a
feat: add gap and docs
2b33591
feat: update according to PR review
06cc767
feat: PR comment update
acaff78
refactor: clean up after rebase and component-generator
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
File renamed without changes.
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,71 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { IntlProvider } from 'react-intl'; | ||
import ChipCarousel from '.'; | ||
|
||
const items = [ | ||
{ | ||
props: { | ||
onClick: jest.fn(), | ||
children: 'Item 1', | ||
'data-testid': 'chip', | ||
}, | ||
}, | ||
{ | ||
props: { | ||
onClick: jest.fn(), | ||
children: 'Item 2', | ||
'data-testid': 'chip', | ||
}, | ||
}, | ||
{ | ||
props: { | ||
onClick: jest.fn(), | ||
children: 'Item 3', | ||
'data-testid': 'chip', | ||
}, | ||
}, | ||
{ | ||
props: { | ||
onClick: jest.fn(), | ||
'data-testid': 'chip', | ||
}, | ||
}, | ||
'Test string', | ||
]; | ||
|
||
const ariaLabel = 'Test aria label'; | ||
function TestingChipCarousel(props) { | ||
return ( | ||
<IntlProvider locale="en"> | ||
<ChipCarousel data-testid="chip-carousel" ariaLabel={ariaLabel} items={items} {...props} /> | ||
</IntlProvider> | ||
); | ||
} | ||
|
||
describe('<ChipCarousel />', () => { | ||
it('should render the carousel correctly', () => { | ||
render(<TestingChipCarousel />); | ||
|
||
const carousel = screen.getByTestId('chip-carousel'); | ||
expect(carousel).toBeTruthy(); | ||
|
||
const chipItems = screen.queryAllByTestId('chip'); | ||
expect(chipItems).toHaveLength(items.length - 2); | ||
for (let i = 0; i < chipItems.length - 2; i++) { | ||
expect(chipItems[i].textContent).toBe(items[i].props.children); | ||
} | ||
}); | ||
|
||
it('should call onClick when a chip item is clicked', async () => { | ||
render(<TestingChipCarousel />); | ||
|
||
const chipItems = screen.getByTestId('chip-carousel'); | ||
for (let i = 0; i < chipItems.length; i++) { | ||
// eslint-disable-next-line no-await-in-loop | ||
await userEvent.click(chipItems[i]); | ||
expect(items[i].props.onClick).toHaveBeenCalledTimes(1); | ||
} | ||
}); | ||
}); |
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,84 @@ | ||
--- | ||
title: 'ChipCarousel' | ||
type: 'component' | ||
components: | ||
- ChipCarousel | ||
categories: | ||
- Content | ||
status: 'New' | ||
designStatus: 'Done' | ||
devStatus: 'Done' | ||
notes: | | ||
--- | ||
|
||
The ``ChipCarousel`` component creates a scrollable horizontal block of chips with buttons for navigating to the previous and next set of chips. | ||
|
||
## Basic Usage | ||
|
||
```jsx live | ||
() => { | ||
const MAX_PERCENTAGE = 100; | ||
const MAX_FIXED = 1000; | ||
const [offset, setOffset] = useState(50); | ||
const [offsetType, setOffsetType] = useState('fixed'); | ||
const [gap, setGap] = useState(3) | ||
|
||
const handleChangeOffsetType = (value) => { | ||
const currentMax = offsetType === 'percentage' ? MAX_PERCENTAGE : MAX_FIXED | ||
const newMax = value === 'percentage' ? MAX_PERCENTAGE : MAX_FIXED | ||
const ration = offset / currentMax | ||
const newOffset = Math.floor(newMax * ration) | ||
setOffset(newOffset); | ||
setOffsetType(value); | ||
} | ||
|
||
return ( | ||
<> | ||
{/* start example form block */} | ||
<ExamplePropsForm | ||
inputs={[ | ||
{ | ||
value: offset, | ||
setValue: setOffset, | ||
range: { | ||
min: 0, | ||
max: offsetType === 'percentage' ? MAX_PERCENTAGE : MAX_FIXED, | ||
step: offsetType === 'percentage' ? 1 : 50, | ||
}, | ||
name: 'offset' | ||
}, | ||
{ | ||
value: offsetType, | ||
setValue: handleChangeOffsetType, | ||
options: ['percentage', 'fixed'], | ||
name: 'offsetType' | ||
}, | ||
{ | ||
value: gap, | ||
setValue: setGap, | ||
range: { min: 0, max: 6, step: 0.5 }, | ||
name: 'gap' | ||
}, | ||
]} | ||
/> | ||
{/* end example form block */} | ||
<ChipCarousel | ||
offset={offset} | ||
offsetType={offsetType} | ||
ariaLabel="example chip carousel" | ||
gap={gap} | ||
items={Array.from({ length: 40 }, | ||
(_, index) => ( | ||
<Chip | ||
key={`Chip-${index}`} | ||
onClick={() => console.log(`Chip #${index + 1} clicked`)} | ||
> | ||
Chip #{index + 1} | ||
</Chip> | ||
) | ||
)} | ||
/> | ||
</> | ||
) | ||
} | ||
``` |
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 @@ | ||
$chip-carousel-controls-top-offset: -3px !default; | ||
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,32 @@ | ||
@import "variables"; | ||
|
||
.pgn__chip-carousel { | ||
position: relative; | ||
|
||
.pgn__overflow-scroll-overflow-container { | ||
--pgn-overflow-scroll-opacity-mask-transparent: rgba(0, 0, 0, 0); | ||
} | ||
|
||
@each $level, $space in $spacers { | ||
&.pgn__chip-carousel-gap__#{$level} { | ||
.pgn__overflow-scroll-overflow-container { | ||
column-gap: $space; | ||
} | ||
} | ||
} | ||
|
||
.pgn__chip-carousel__right-control, | ||
.pgn__chip-carousel__left-control { | ||
position: absolute; | ||
z-index: 2; | ||
top: $chip-carousel-controls-top-offset; | ||
} | ||
|
||
.pgn__chip-carousel__right-control { | ||
right: 0; | ||
} | ||
|
||
.pgn__chip-carousel__left-control { | ||
left: 0; | ||
} | ||
} |
Oops, something went wrong.
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.
It will be necessary to add a new token :)