-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
hooks and documentation for use-selection
#7872
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
Closed
Closed
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4cdb63c
[@mantine/hooks] use-selection init
Han5991 ff31e94
[@mantine/hooks] add use-selection.test.ts
Han5991 b7e8c91
[@mantine/docs] demo init
Han5991 f73b83d
[@mantine/hooks] "Fix `isSomeSelected` logic to correctly handle empt…
Han5991 8a2baf6
[@mantine/docs] storybook add
Han5991 d347193
[@mantine/docs] documentation add
Han5991 c65763c
[refactor] applied code review
Han5991 e529129
[refactor] use-selection.mdx documentation
Han5991 1c4dd25
Enhance `isSomeSelected` logic and add related test cases
Han5991 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
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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { HooksDemos } from '@docs/demos'; | ||
| import { Layout } from '@/layout'; | ||
| import { MDX_DATA } from '@/mdx'; | ||
|
|
||
| export default Layout(MDX_DATA.useSelection); | ||
|
|
||
| ## Usage | ||
Han5991 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| `use-selection` returns current selection: | ||
|
|
||
| <Demo data={HooksDemos.useSelectionDemo} /> | ||
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
187 changes: 187 additions & 0 deletions
187
packages/@docs/demos/src/demos/hooks/use-selection.demo.usage.tsx
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 |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| import React from 'react'; | ||
| import { Button, Checkbox, Group, Table, Text } from '@mantine/core'; | ||
| import { useSelection } from '@mantine/hooks'; | ||
| import { MantineDemo } from '@mantinex/demo'; | ||
|
|
||
| const code = ` | ||
| import { Button, Checkbox, Group, Table, Text } from '@mantine/core'; | ||
| import { useSelection } from '@mantine/hooks'; | ||
|
|
||
| const elements = [ | ||
| { id: '1', position: 6, mass: 12.011, symbol: 'C', name: 'Carbon' }, | ||
| { id: '2', position: 7, mass: 14.007, symbol: 'N', name: 'Nitrogen' }, | ||
| { id: '3', position: 39, mass: 88.906, symbol: 'Y', name: 'Yttrium' }, | ||
| { id: '4', position: 56, mass: 137.33, symbol: 'Ba', name: 'Barium' }, | ||
| { id: '5', position: 58, mass: 140.12, symbol: 'Ce', name: 'Cerium' }, | ||
| ]; | ||
|
|
||
| function Demo() { | ||
| // Initialize with the first item selected as an example | ||
| const [selected, handlers] = useSelection(elements, [elements[0]]); | ||
|
|
||
| const allSelected = handlers.isAllSelected(); | ||
| const someSelected = handlers.isSomeSelected(); | ||
|
|
||
| const rows = elements.map((item) => { | ||
| // Check if item (by id) is in the selected array | ||
| const isSelected = selected.some((s) => s.id === item.id); | ||
| const bg = isSelected ? 'var(--mantine-color-blue-light)' : undefined; | ||
| return ( | ||
| <Table.Tr key={item.id} bg={bg}> | ||
| <Table.Td> | ||
| <Checkbox | ||
| checked={isSelected} | ||
| onChange={() => handlers.toggle(item)} | ||
| /> | ||
| </Table.Td> | ||
| <Table.Td>{item.position}</Table.Td> | ||
| <Table.Td>{item.name}</Table.Td> | ||
| <Table.Td>{item.symbol}</Table.Td> | ||
| <Table.Td>{item.mass}</Table.Td> | ||
| </Table.Tr> | ||
| ); | ||
| }); | ||
|
|
||
| const handleSelectAllToggle = () => { | ||
| if (allSelected) { | ||
| handlers.resetSelection(); | ||
| } else { | ||
| handlers.setSelection(elements); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <Group mb="md"> | ||
| <Button onClick={handleSelectAllToggle}> | ||
| {allSelected ? 'Deselect all' : 'Select all'} | ||
| </Button> | ||
| <Button onClick={() => handlers.setSelection([elements[1], elements[3]])} variant="light"> | ||
| Select Nitrogen & Barium | ||
| </Button> | ||
| <Button onClick={handlers.resetSelection} variant="outline" color="red"> | ||
| Reset selection | ||
| </Button> | ||
| </Group> | ||
|
|
||
| <Text mb="xs" size="sm"> | ||
| Selected items: {selected.map(s => s.name).join(', ') || 'None'} (Total: {selected.length}) | ||
| </Text> | ||
|
|
||
| <Table miw={700} verticalSpacing="sm"> | ||
| <Table.Thead> | ||
| <Table.Tr> | ||
| <Table.Th style={{ width: '2rem' }}> | ||
| <Checkbox | ||
| checked={allSelected} | ||
| indeterminate={someSelected && !allSelected} | ||
| onChange={handleSelectAllToggle} | ||
| aria-label="Select all rows" | ||
| /> | ||
| </Table.Th> | ||
| <Table.Th>Element position</Table.Th> | ||
| <Table.Th>Element name</Table.Th> | ||
| <Table.Th>Symbol</Table.Th> | ||
| <Table.Th>Atomic mass</Table.Th> | ||
| </Table.Tr> | ||
| </Table.Thead> | ||
| <Table.Tbody>{rows}</Table.Tbody> | ||
| </Table> | ||
| </> | ||
| ); | ||
| } | ||
| `; | ||
|
|
||
| const elements = [ | ||
| { id: '1', position: 6, mass: 12.011, symbol: 'C', name: 'Carbon' }, | ||
| { id: '2', position: 7, mass: 14.007, symbol: 'N', name: 'Nitrogen' }, | ||
| { id: '3', position: 39, mass: 88.906, symbol: 'Y', name: 'Yttrium' }, | ||
| { id: '4', position: 56, mass: 137.33, symbol: 'Ba', name: 'Barium' }, | ||
| { id: '5', position: 58, mass: 140.12, symbol: 'Ce', name: 'Cerium' }, | ||
| ]; | ||
|
|
||
| function Demo() { | ||
| // Initialize with the first item selected as an example | ||
| const [selected, handlers] = useSelection(elements, [elements[0]]); | ||
|
|
||
| const allSelected = handlers.isAllSelected(); | ||
| const someSelected = handlers.isSomeSelected(); | ||
|
|
||
| const rows = elements.map((item) => { | ||
| const bg = selected.some((s) => s.id === item.id) | ||
| ? 'var(--mantine-color-blue-light)' | ||
| : undefined; | ||
| // Check if item (by id) is in the selected array | ||
| const isSelected = selected.some((s) => s.id === item.id); | ||
| return ( | ||
| <Table.Tr key={item.id} bg={bg}> | ||
| <Table.Td> | ||
| <Checkbox | ||
| checked={isSelected} | ||
| onChange={() => handlers.toggle(item)} | ||
| aria-label={`Select row ${item.name}`} | ||
| /> | ||
| </Table.Td> | ||
| <Table.Td>{item.position}</Table.Td> | ||
| <Table.Td>{item.name}</Table.Td> | ||
| <Table.Td>{item.symbol}</Table.Td> | ||
| <Table.Td>{item.mass}</Table.Td> | ||
| </Table.Tr> | ||
| ); | ||
| }); | ||
|
|
||
| const handleSelectAllToggle = () => { | ||
| if (allSelected) { | ||
| handlers.resetSelection(); | ||
| } else { | ||
| handlers.setSelection(elements); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <Group mb="md"> | ||
| <Button onClick={handleSelectAllToggle}> | ||
| {allSelected ? 'Deselect all' : 'Select all'} | ||
| </Button> | ||
| <Button onClick={() => handlers.setSelection([elements[1], elements[3]])} variant="light"> | ||
| Select Nitrogen & Barium | ||
| </Button> | ||
| <Button onClick={handlers.resetSelection} variant="outline" color="red"> | ||
| Reset selection | ||
| </Button> | ||
| </Group> | ||
|
|
||
| <Text mb="xs" size="sm"> | ||
| Selected items: {selected.map((s) => s.name).join(', ') || 'None'} (Total: {selected.length} | ||
| ) | ||
| </Text> | ||
|
|
||
| <Table miw="auto" verticalSpacing="sm"> | ||
| <Table.Thead> | ||
| <Table.Tr> | ||
| <Table.Th> | ||
| <Checkbox | ||
| checked={allSelected} | ||
| indeterminate={someSelected && !allSelected} | ||
| onChange={handleSelectAllToggle} | ||
| aria-label="Select all rows" | ||
| /> | ||
| </Table.Th> | ||
| <Table.Th>Element position</Table.Th> | ||
| <Table.Th>Element name</Table.Th> | ||
| <Table.Th>Symbol</Table.Th> | ||
| <Table.Th>Atomic mass</Table.Th> | ||
| </Table.Tr> | ||
| </Table.Thead> | ||
| <Table.Tbody>{rows}</Table.Tbody> | ||
| </Table> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| export const useSelectionDemo: MantineDemo = { | ||
| type: 'code', | ||
| component: Demo, | ||
| code, | ||
| }; |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.