-
Notifications
You must be signed in to change notification settings - Fork 1
CORE-675: rename SectionNav to ButtonNav and refactor props from ToggleButtonGroup #85
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import React from "react"; | ||
import { SectionNav } from "./SectionNav/index"; | ||
import { ButtonNav } from "./ButtonNav/index"; | ||
import { ToggleButtonGroup } from "./ToggleButtonGroup/index"; | ||
import { Key } from "react-aria-components"; | ||
|
||
|
@@ -28,14 +28,14 @@ export const Default = () => { | |
|
||
return ( | ||
<> | ||
<SectionNav | ||
<ButtonNav | ||
handlePrevArrow={handlePrevArrow} | ||
handleNextArrow={handleNextArrow} | ||
isPrevArrowDisabled={selectedIndex === 0} | ||
isNextArrowDisabled={selectedIndex === childrenListWithKeys.length - 1} | ||
> | ||
{[childrenListWithKeys]} | ||
</SectionNav> | ||
</ButtonNav> | ||
<span>Selected section: {selectedIndex + 1}</span> | ||
</> | ||
|
||
|
@@ -68,62 +68,63 @@ export const WithToggleButtonGroups = () => { | |
|
||
const flattenedSections = sections.flat(); | ||
|
||
const [selectedItems, setSelectedItems] = React.useState(new Set<Key>([flattenedSections[0].id])); | ||
const [selectedItem, setSelectedItem] = React.useState<string>(flattenedSections ? flattenedSections[0].id : ''); | ||
|
||
const scrollToIndex = (id: Key) => { | ||
const child = document.querySelector(`[data-button-id="${id}"]`) as HTMLElement; | ||
const scrollNavToSection = (sectionDataId: string) => { | ||
const child = document.querySelector<HTMLElement>(`[data-button-id="${sectionDataId}"]`); | ||
if (child) { | ||
child.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'start' }); | ||
} | ||
}; | ||
|
||
const handlePrevArrow = () => { | ||
setSelectedItems((prev) => { | ||
const newSet = new Set<Key>(); | ||
const firstSectionKeys = flattenedSections.map(section => section.id); | ||
const currentIndex = firstSectionKeys.indexOf([...prev][0] as string); | ||
setSelectedItem((prev) => { | ||
const currentIndex = flattenedSections.findIndex((element) => element.id === prev); | ||
let newSelectedItem = prev; | ||
if (currentIndex > 0) { | ||
newSet.add(firstSectionKeys[currentIndex - 1]); | ||
scrollToIndex(firstSectionKeys[currentIndex - 1]); | ||
newSelectedItem = flattenedSections[currentIndex - 1].id; | ||
scrollNavToSection(flattenedSections[currentIndex - 1].id); | ||
} | ||
return newSet; | ||
return newSelectedItem; | ||
}); | ||
}; | ||
|
||
const handleNextArrow = () => { | ||
setSelectedItems((prev) => { | ||
const newSet = new Set<Key>(); | ||
const firstSectionKeys = flattenedSections.map(section => section.id); | ||
const currentIndex = firstSectionKeys.indexOf([...prev][0] as string); | ||
if (currentIndex < firstSectionKeys.length - 1) { | ||
newSet.add(firstSectionKeys[currentIndex + 1]); | ||
scrollToIndex(firstSectionKeys[currentIndex + 1]); | ||
setSelectedItem((prev) => { | ||
const currentIndex = flattenedSections.findIndex((element) => element.id === prev); | ||
let newSelectedItem = prev; | ||
if (currentIndex < flattenedSections.length - 1) { | ||
newSelectedItem = flattenedSections[currentIndex + 1].id; | ||
scrollNavToSection(flattenedSections[currentIndex + 1].id); | ||
} | ||
return newSet; | ||
return newSelectedItem; | ||
}); | ||
}; | ||
|
||
const ToggleGroup = sections.map( | ||
(sectionSet, index) => | ||
<ToggleButtonGroup | ||
key={`section-${index + 1}`} | ||
selectedItems={selectedItems} | ||
onSelectionChange={setSelectedItems} | ||
selectedItems={new Set<Key>([selectedItem])} | ||
onSelectionChange={(newSet) => setSelectedItem([...newSet][0] as string)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be an empty set right? Unselecting an item will throw an error |
||
items={sectionSet} | ||
/> | ||
); | ||
|
||
return ( | ||
<> | ||
<SectionNav | ||
<ButtonNav | ||
handlePrevArrow={handlePrevArrow} | ||
handleNextArrow={handleNextArrow} | ||
isPrevArrowDisabled={selectedItems.has(flattenedSections[0].id) || selectedItems.size === 0} | ||
isNextArrowDisabled={selectedItems.has(flattenedSections[flattenedSections.length - 1].id) || selectedItems.size === 0} | ||
isPrevArrowDisabled={selectedItem === flattenedSections[0].id || selectedItem.length === 0} | ||
isNextArrowDisabled={ | ||
selectedItem === flattenedSections[flattenedSections.length - 1].id || | ||
selectedItem.length === 0 | ||
} | ||
> | ||
{ToggleGroup} | ||
</SectionNav> | ||
<p>Current selections: {[...selectedItems].join(', ')}</p> | ||
</ButtonNav> | ||
<p>Current selected: {selectedItem}</p> | ||
</> | ||
); | ||
|
||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,11 +1,10 @@ | ||
import React from "react"; | ||
import { StyledToggleButtonGroup, StyledToggleButton } from "./styles"; | ||
import { Key } from "react-aria-components"; | ||
|
||
export interface ToggleButtonGroupProps { | ||
items: { id: string, value: string }[]; | ||
selectedItems: Set<Key>; | ||
onSelectionChange?: React.Dispatch<React.SetStateAction<Set<Key>>>; | ||
selectedItems: Iterable<Key> | undefined; | ||
onSelectionChange?: ((keys: Set<Key>) => void) | undefined; | ||
selectionMode?: 'single' | 'multiple'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this already get typed as undefined because of the |
||
className?: string; | ||
} | ||
|
This file contains hidden or 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 hidden or 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.
You can use
newSelectedItem
here