-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: implemented table row selection
- Loading branch information
1 parent
b5ccb4f
commit 274dab7
Showing
6 changed files
with
112 additions
and
61 deletions.
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
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 |
---|---|---|
@@ -1,30 +1,49 @@ | ||
import React, { useMemo, useState } from 'react'; | ||
import React, { useState } from "react"; | ||
import { TableState } from "./types"; | ||
|
||
export interface TableState { | ||
selectable?: boolean; | ||
onSelect?: () => void; | ||
} | ||
|
||
export const TableContext = React.createContext<TableState | undefined>(undefined); | ||
export const TableContext = React.createContext<TableState | undefined>( | ||
undefined | ||
); | ||
|
||
export function useTableState() { | ||
const result = React.useContext(TableContext); | ||
if (!result) { | ||
throw new Error('TableContext not initialized'); | ||
throw new Error("TableContext not initialized"); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
export function useInitTableState({ selectable }: TableState): TableState { | ||
// TODO: should we do something with onSelect here? | ||
const [innerSelectable, setSelectable] = useState(selectable); | ||
|
||
return useMemo( | ||
() => ({ | ||
selectable: innerSelectable, | ||
setSelectable, | ||
}), | ||
[innerSelectable], | ||
export function useInitTableState({ | ||
selectable, | ||
onSelect, | ||
selected, | ||
}: TableState): TableState { | ||
const [isSelectedAll, setIsSelectedAll] = React.useState<boolean>(false); | ||
const [uniqueIds, setUniqueIds] = useState<Set<string>>( | ||
new Set(selected || []) | ||
); | ||
|
||
const onSelectedAll = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setIsSelectedAll(event.target.checked || false); | ||
onSelected(event); | ||
}; | ||
const onSelected = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const newSet = new Set(uniqueIds); // Create a copy to avoid mutation | ||
if (event.target.checked) { | ||
newSet.add(event.target.id); | ||
} else { | ||
newSet.delete(event.target.id); | ||
} | ||
setUniqueIds(newSet); | ||
onSelect?.([...newSet]); | ||
}; | ||
return { | ||
selectable, | ||
onSelect, | ||
selected: [...uniqueIds], | ||
onSelected, | ||
onSelectedAll, | ||
isSelectedAll, | ||
}; | ||
} |
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