forked from Workday/canvas-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from williamjstanton/william-selectable-rows
docs: Table with selectable rows example
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 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
74 changes: 74 additions & 0 deletions
74
modules/preview-react/table/stories/examples/SelectableRows.tsx
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,74 @@ | ||
import React from 'react'; | ||
|
||
import {Table} from '@workday/canvas-kit-preview-react/table'; | ||
import {Heading} from '@workday/canvas-kit-react/text'; | ||
import {Checkbox} from '@workday/canvas-kit-react/checkbox'; | ||
import {styled, useUniqueId} from '@workday/canvas-kit-react/common'; | ||
import {Tooltip} from '@workday/canvas-kit-react/tooltip'; | ||
import {colors} from '@workday/canvas-kit-react/tokens'; | ||
|
||
const pizzaToppingData = [ | ||
{topping: 'Pepperoni', amount: '2.5 oz.'}, | ||
{topping: 'Mozzarella', amount: '5 oz.'}, | ||
{topping: 'Basil', amount: '10 Leaves'}, | ||
{topping: 'Roasted Red Peppers', amount: '3 oz.'}, | ||
{topping: 'Mushrooms', amount: '2 oz.'}, | ||
]; | ||
|
||
const StyledCell = styled(Table.Cell)({ | ||
backgroundColor: 'transparent', | ||
}); | ||
|
||
const CustomRow = props => { | ||
const [isSelected, setIsSelected] = React.useState(props.selected); | ||
const selectRowHandler = e => { | ||
setIsSelected(e.target.checked); | ||
}; | ||
|
||
return ( | ||
<Table.Row background={isSelected && colors.blueberry200}> | ||
<StyledCell> | ||
<Tooltip title={props.rowData.topping}> | ||
<Checkbox checked={isSelected} onChange={selectRowHandler} /> | ||
</Tooltip> | ||
</StyledCell> | ||
<StyledCell>{props.rowData.topping}</StyledCell> | ||
<StyledCell>{props.rowData.amount}</StyledCell> | ||
</Table.Row> | ||
); | ||
}; | ||
|
||
export const SelectableRows = () => { | ||
const headingID = useUniqueId(); | ||
const [selectAll, setSelectAll] = React.useState(false); | ||
const selectAllHandler = e => { | ||
setSelectAll(e.target.checked); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Heading as="h3" id={headingID} size="small" marginBottom="s"> | ||
Pizza Toppings | ||
</Heading> | ||
<Table aria-labelledby={headingID}> | ||
<Table.Row> | ||
<StyledCell backgroundColor={colors.soap100}> | ||
<Tooltip title="Select All"> | ||
<Checkbox checked={selectAll} onChange={selectAllHandler} /> | ||
</Tooltip> | ||
</StyledCell> | ||
<Table.Header scope="col" backgroundColor={colors.soap100}> | ||
Toppings | ||
</Table.Header> | ||
<Table.Header scope="col" backgroundColor={colors.soap100}> | ||
Amount | ||
</Table.Header> | ||
</Table.Row> | ||
|
||
{pizzaToppingData.map(i => ( | ||
<CustomRow rowData={i} selected={selectAll} /> | ||
))} | ||
</Table> | ||
</> | ||
); | ||
}; |