Skip to content
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

add caption component for table #513

Closed
wants to merge 1 commit into from
Closed

add caption component for table #513

wants to merge 1 commit into from

Conversation

jhp0621
Copy link
Contributor

@jhp0621 jhp0621 commented Nov 19, 2021

Allow table to have a caption specified: #463

Author Checklist

  • Add unit test(s)
  • Update version in package.json (see the versioning guidelines)
  • Update documentation (if necessary)
  • Add story to storybook (if necessary)
  • Assign dev reviewer

@chawes13 chawes13 linked an issue Nov 22, 2021 that may be closed by this pull request
src/tables/components/table-caption.js Show resolved Hide resolved
src/tables/components/table-caption.js Show resolved Hide resolved
src/tables/sortable-table.js Show resolved Hide resolved
src/tables/sortable-table.js Show resolved Hide resolved
Comment on lines +107 to +170
const columns = getColumnData(children, disableSort);
const {
initialSortPath,
initialSortFunc,
initialValueGetter,
} = getInitialSortControls(initialColumn, columns);
const [ascending, setAscending] = useState(initialAscending);
const [sortPath, setSortPath] = useState(initialSortPath);

// Setting and storing a function object requires special syntax.
// See: https://medium.com/swlh/how-to-store-a-function-with-the-usestate-hook-in-react-8a88dd4eede1
const [sortFunc, setSortFunc] = useState(() => initialSortFunc)
const [valueGetter, setValueGetter] = useState(() => initialValueGetter)
const [sortFunc, setSortFunc] = useState(() => initialSortFunc);
const [valueGetter, setValueGetter] = useState(() => initialValueGetter);

const data = useMemo(() => {
if (controlled || disableSort) return unsortedData
if (controlled || disableSort) return unsortedData;

if (sortFunc) {
const sorted = [...unsortedData].sort(sortFunc)
if (!ascending && !disableReverse) sorted.reverse()
return sorted
}
else {
const order = ascending ? 'asc' : 'desc'
const sorted = [...unsortedData].sort(sortFunc);
if (!ascending && !disableReverse) sorted.reverse();
return sorted;
} else {
const order = ascending ? "asc" : "desc";
const sorted = orderBy(
unsortedData,
(item) => valueGetter ? valueGetter(item) : get(sortPath, item),
(item) => (valueGetter ? valueGetter(item) : get(sortPath, item)),
order
)
return sorted
);
return sorted;
}
}, [ascending, sortPath, sortFunc, valueGetter, controlled, disableSort, disableReverse, unsortedData])
}, [
ascending,
sortPath,
sortFunc,
valueGetter,
controlled,
disableSort,
disableReverse,
unsortedData,
]);

const handleColumnChange = (column) => {
if (column.disabled) return
if (column.disabled) return;

const newSortPath = column.name
const newSortFunc = column.sortFunc || null
const newValueGetter = column.valueGetter || null
const newSortPath = column.name;
const newSortFunc = column.sortFunc || null;
const newValueGetter = column.valueGetter || null;

// Toggle ascending if the path is already selected. Otherwise, default
// to ascending when switching paths...
const newAscending = newSortPath === sortPath ? !ascending : true
const newAscending = newSortPath === sortPath ? !ascending : true;

setAscending(newAscending)
setSortPath(newSortPath)
setSortFunc(() => newSortFunc)
setValueGetter(() => newValueGetter)
setAscending(newAscending);
setSortPath(newSortPath);
setSortFunc(() => newSortFunc);
setValueGetter(() => newValueGetter);

if (onChange) onChange({
ascending: newAscending,
sortPath: newSortPath,
sortFunc: newSortFunc
})
}
if (onChange)
onChange({
ascending: newAscending,
sortPath: newSortPath,
sortFunc: newSortFunc,
});
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did anything change here? It's hard to tell 🙈

src/tables/sortable-table.js Show resolved Hide resolved
{columns.map((column, key) => {
const Header = column.headerComponent || headerComponent || DefaultHeader
<>
<CaptionComponent caption={caption} />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a node, you could just always render the caption. E.g.,

<caption>{caption}</caption>

However, since we'd be introducing a new change this could impact styling on existing projects. I believe this is tagged to the v6 milestone, so please modify the base branch to v6 and add a note in the migration guide

@jhp0621
Copy link
Contributor Author

jhp0621 commented Mar 19, 2022

closing in favor of #540

@jhp0621 jhp0621 closed this Mar 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow for caption to be specified for Table components
2 participants