-
Notifications
You must be signed in to change notification settings - Fork 0
feat(tables): allow controlling of all accordions at once #57
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ba2cf5f
feat(tables): allow controlling of all accordions at once
Peter-Brick 225e257
refactor(accordionRow): add controller, split row by management
Peter-Brick ef90f65
refactor(accordionController): rename context
Peter-Brick 1ef546a
feat(accordionRows): address PR comments
Peter-Brick 5ce4260
feat(accordionRow): remove unnecessary prop deconstruction
Peter-Brick 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import React, { useState, useContext, useMemo } from 'react'; | ||
|
|
||
| import { useModes } from '@livepreso/content-react'; | ||
|
|
||
| import { AccordionToggleAll } from './AccordionToggleAll'; | ||
|
|
||
| export const AccordionControlContext = React.createContext({ | ||
| registerRow: () => {}, | ||
| unregisterRow: () => {}, | ||
| isOpen: () => false, | ||
| toggleRow: () => {}, | ||
| hasRows: false, | ||
| expandAll: () => {}, | ||
| collapseAll: () => {}, | ||
| allRowsOpen: false, | ||
| hasController: false, | ||
| }); | ||
|
|
||
| export const useAccordionControls = () => { | ||
| const context = useContext(AccordionControlContext); | ||
|
|
||
| if (!context.hasController) { | ||
| throw new Error(` | ||
| useAccordionControls must be used within an AccordionController. | ||
| Please wrap your table with AccordionController to use this hook. | ||
| `); | ||
| } | ||
|
|
||
| return context; | ||
| }; | ||
|
|
||
| export function AccordionController({ children }) { | ||
| const { isPdfScreenshot } = useModes(); | ||
| const [registry, setRegistry] = useState(new Set()); | ||
| const [expandedRows, setExpandedRows] = useState(new Set()); | ||
| const [expandByDefault, setExpandByDefault] = useState(isPdfScreenshot); | ||
|
|
||
| const isOpen = (uid) => expandedRows.has(uid); | ||
|
|
||
| const hasRows = registry.size > 0; | ||
| const allRowsOpen = hasRows && expandedRows.size === registry.size; | ||
|
|
||
| const registerRow = (uid) => { | ||
| setRegistry((prev) => new Set(prev).add(uid)); | ||
|
|
||
| if (expandByDefault) { | ||
| setExpandedRows((prev) => new Set(prev).add(uid)); | ||
| } | ||
| }; | ||
|
|
||
| const unregisterRow = (uid) => { | ||
| setRegistry((prev) => { | ||
| const newSet = new Set(prev); | ||
| newSet.delete(uid); | ||
| return newSet; | ||
| }); | ||
| setExpandedRows((prev) => { | ||
| const newSet = new Set(prev); | ||
| newSet.delete(uid); | ||
| return newSet; | ||
| }); | ||
| }; | ||
|
|
||
| const toggleRow = (uid) => { | ||
| setExpandedRows((prev) => { | ||
| const newSet = new Set(prev); | ||
| if (newSet.has(uid)) { | ||
| newSet.delete(uid); | ||
| } else { | ||
| newSet.add(uid); | ||
| } | ||
| return newSet; | ||
| }); | ||
| }; | ||
|
|
||
| const expandAll = () => { | ||
| setExpandedRows(new Set(registry)); | ||
| setExpandByDefault(true); | ||
| }; | ||
|
|
||
| const collapseAll = () => { | ||
| setExpandedRows(new Set()); | ||
| setExpandByDefault(false); | ||
| }; | ||
|
|
||
| const value = useMemo( | ||
| () => ({ | ||
| registerRow, | ||
| unregisterRow, | ||
| isOpen, | ||
| toggleRow, | ||
| hasRows, | ||
| expandAll, | ||
| collapseAll, | ||
| allRowsOpen, | ||
| hasController: true, | ||
| }), | ||
| [ | ||
| registerRow, | ||
| unregisterRow, | ||
| isOpen, | ||
| toggleRow, | ||
| hasRows, | ||
| expandAll, | ||
| collapseAll, | ||
| allRowsOpen, | ||
| ], | ||
| ); | ||
|
|
||
| return ( | ||
| <AccordionControlContext.Provider value={value}> | ||
| {hasRows && ( | ||
| <div style={{ position: 'relative' }}> | ||
| <AccordionToggleAll /> | ||
| </div> | ||
| )} | ||
|
|
||
| {children} | ||
| </AccordionControlContext.Provider> | ||
| ); | ||
| } |
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,32 @@ | ||
| import React from 'react'; | ||
|
|
||
| import classNames from 'classnames'; | ||
|
|
||
| import { useAccordionControls } from './AccordionController'; | ||
|
|
||
| import style from './AccordionToggleAll.module.scss'; | ||
|
|
||
| export function AccordionToggleAll() { | ||
| const { expandAll, collapseAll, hasRows, allRowsOpen } = | ||
| useAccordionControls(); | ||
|
|
||
| function handleClick() { | ||
| if (allRowsOpen) { | ||
| collapseAll(); | ||
| } else { | ||
| expandAll(); | ||
| } | ||
| } | ||
|
|
||
| if (!hasRows) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <div | ||
| className={classNames(style.toggle, { [style.open]: allRowsOpen })} | ||
| onClick={handleClick} | ||
| title={allRowsOpen ? 'Collapse all' : 'Expand all'} | ||
| ></div> | ||
| ); | ||
|
Peter-Brick marked this conversation as resolved.
|
||
| } | ||
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,55 @@ | ||
| .toggle { | ||
| height: 45px; | ||
| width: 30px; | ||
| position: absolute; | ||
| left: -30px; | ||
| top: 0; | ||
|
|
||
| display: flex; | ||
| flex-direction: column; | ||
| align-items: center; | ||
| justify-content: center; | ||
|
|
||
| cursor: pointer; | ||
|
|
||
| .screenshot-full & { | ||
| display: none; | ||
| } | ||
|
|
||
| &:hover::before { | ||
| margin-bottom: 8px; | ||
| } | ||
|
|
||
| &::before { | ||
| content: ''; | ||
| width: 0; | ||
| height: 0; | ||
| border: 8px solid transparent; | ||
| border-bottom-color: var(--color-text); | ||
| margin-bottom: 4px; | ||
| transition: margin-bottom 150ms ease-out; | ||
| } | ||
| &::after { | ||
| content: ''; | ||
| width: 0; | ||
| height: 0; | ||
| border: 8px solid transparent; | ||
| border-top-color: var(--color-text); | ||
| } | ||
|
|
||
| &.open { | ||
| &::before { | ||
| border-bottom-color: transparent; | ||
| border-top-color: var(--color-text); | ||
| margin-bottom: -10px; | ||
| } | ||
| &::after { | ||
| border-top-color: transparent; | ||
| border-bottom-color: var(--color-text); | ||
| } | ||
|
|
||
| &:hover::before { | ||
| margin-bottom: -14px; | ||
| } | ||
| } | ||
| } |
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,50 +1,55 @@ | ||
| import React from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import { ROW_TYPES } from './table-constants'; | ||
| import { TableBase } from './TableBase'; | ||
| import { OrderableTable } from './OrderableTable'; | ||
|
|
||
| export function Table(props) { | ||
| const { onReorder } = props; | ||
| /** | ||
| * @typedef {Object} TableRow | ||
| * @property {string} uid - Unique identifier for the row. | ||
| * @property {('header'|'body'|'footer')} [type] - The type of row based on ROW_TYPES. | ||
| * @property {Function} [component] - Optional component override for the row. | ||
| * @property {Object[]} [cells] - Array of cell data objects. | ||
| * @property {Object[]} [rows] - Nested rows for expandable table structures. | ||
| * @property {string} [className] - CSS class for the row. | ||
| */ | ||
|
|
||
| /** | ||
| * @param {Object} props | ||
| * @param {boolean} [props.hasBorder=false] - Whether the table has a border. | ||
| * @param {TableRow[]} [props.rows=[]] - Array of row objects to render. | ||
| * @param {(number|string)[]} [props.columnWidths=[]] - Widths for each column. | ||
| * @param {React.ReactNode} [props.children=[]] - Child elements. | ||
| * @param {'none'|'row'|'column'|'both'} [props.sticky='none'] - Provides scrolling with a 'sticky' header or column. | ||
| * @param {boolean} [props.isPresoManagerInteractive=false] - Allows interaction in PresoManager. Mouse events are otherwise ignored. | ||
| * @param {string} [props.tbodyClassName=''] - CSS class for the tbody element. | ||
| * @param {string} [props.className=''] - CSS class for the table element. | ||
| */ | ||
| export function Table({ | ||
| hasBorder = false, | ||
| rows = [], | ||
| columnWidths = [], | ||
| children = [], | ||
| sticky = 'none', | ||
| isPresoManagerInteractive = false, | ||
| tbodyClassName = '', | ||
| className = '', | ||
| ...otherProps | ||
| }) { | ||
| const { onReorder, ...rest } = otherProps; | ||
|
|
||
| const props = { | ||
| hasBorder, | ||
| rows, | ||
| columnWidths, | ||
| children, | ||
| sticky, | ||
| isPresoManagerInteractive, | ||
| tbodyClassName, | ||
| className, | ||
| }; | ||
|
|
||
| if (typeof onReorder === 'function') { | ||
| return <OrderableTable {...props} />; | ||
| return <OrderableTable {...props} {...otherProps} />; | ||
| } | ||
|
|
||
| return <TableBase {...props} />; | ||
| return <TableBase {...props} {...rest} />; | ||
| } | ||
|
|
||
| Table.propTypes = { | ||
| hasBorder: PropTypes.bool, | ||
| rows: PropTypes.arrayOf( | ||
| PropTypes.shape({ | ||
| uid: PropTypes.string.isRequired, | ||
| type: PropTypes.oneOf(Object.values(ROW_TYPES)), | ||
| component: PropTypes.func, | ||
| // We're letting the components further down check the cell types | ||
| // rather than trying to check at the top level due to complexity of the propTypes | ||
| cells: PropTypes.arrayOf(PropTypes.shape({})), | ||
| rows: PropTypes.arrayOf(PropTypes.shape({})), | ||
| className: PropTypes.string, | ||
| }), | ||
| ), | ||
| columnWidths: PropTypes.arrayOf( | ||
| PropTypes.oneOfType([PropTypes.number, PropTypes.string]), | ||
| ), | ||
| children: PropTypes.node, | ||
| sticky: PropTypes.oneOf(['none', 'row', 'column', 'both']), | ||
| isPresoManagerInteractive: PropTypes.bool, | ||
| tbodyClassName: PropTypes.string, | ||
| className: PropTypes.string, | ||
| }; | ||
|
|
||
| Table.defaultProps = { | ||
| hasBorder: false, | ||
| rows: [], | ||
| columnWidths: [], | ||
| children: [], | ||
| sticky: 'none', | ||
| isPresoManagerInteractive: false, | ||
| tbodyClassName: '', | ||
| className: '', | ||
| }; |
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.