Skip to content

Commit

Permalink
feat(Table) conditional actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Manu Ramirez committed Mar 2, 2021
1 parent 2d732a9 commit 7427878
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
4 changes: 3 additions & 1 deletion packages/cascara/src/ui/ActionsMenu/ActionsMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ const ActionsMenu = ({ trigger = DEFAULT_TRIGGER, actions }) => {
const handleMenuItemClick = (item) => {
menu.hide();

onAction(item, record);
if (onAction) {
onAction(item, record);
}
};

return (
Expand Down
55 changes: 49 additions & 6 deletions packages/cascara/src/ui/Table/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ const actionModuleOptions = Object.keys(actionModules);
const dataModuleOptions = Object.keys(dataModules);

const propTypes = {
/** Actions will be appended to each row, they'll appear as buttons. */
actions: pt.shape({
actionButtonMenuIndex: pt.number,

modules: pt.arrayOf(
pt.shape({
module: pt.oneOf(actionModuleOptions).isRequired,
})
),

/** Resolve record actions.
* A function that returns the actions available to the current row */
resolveRecordActions: pt.func,
}),

/** An array of objects.
*
* Every object in this array will potencially be rendered as a table row. */
Expand All @@ -23,6 +38,7 @@ const propTypes = {
* as well as the available actions (if any) for each row. */
dataConfig: pt.shape({
actionButtonMenuIndex: pt.number,

/** Actions will be appended to each row, they'll appear as buttons. */
actions: pt.arrayOf(
pt.shape({
Expand Down Expand Up @@ -55,26 +71,53 @@ const propTypes = {

/** This is a Table */
const Table = ({
data = [],
dataConfig = {},
onAction = (type, data) => type,
resolveRecordActions,
actions,
data,
dataConfig,
onAction,
uniqueIdAttribute,
...rest
}) => {
const { actions = [], display = [] } = dataConfig;
const display = dataConfig?.display;

// // FDS-142: new action props
let actionButtonMenuIndex = actions?.actionButtonMenuIndex;
let modules = actions?.modules;
let resolveRecordActions = actions?.resolveRecordActions;

// old action props
const unwantedActions = dataConfig?.actions;
if (unwantedActions) {
modules = unwantedActions;
// eslint-disable-next-line no-console
console.warn(
'Prop "dataConfig.actions" has been deprecated. Actions have been moved to the root of the Table component as their own prop.'
);
}

const unwantedActionButtonIndex = dataConfig?.actionButtonIndex;
if (unwantedActionButtonIndex) {
actionButtonMenuIndex = unwantedActionButtonIndex;
// eslint-disable-next-line no-console
console.warn(
'Prop "dataConfig.actionButtonIndex" has been deprecated. Actions have been moved to the root of the Table component as their own prop.'
);
}

let columnCount = display.length;

if (actions.length) {
if (modules.length) {
columnCount++;
}

return (
<ErrorBoundary>
<TableProvider
value={{
actionButtonMenuIndex,
data,
dataConfig,
modules,
onAction,
resolveRecordActions,
uniqueIdAttribute,
Expand Down
11 changes: 6 additions & 5 deletions packages/cascara/src/ui/Table/TableRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ const TableRow = ({ config = {}, record = {} }) => {
const { id, columns } = config;
const {
resolveRecordActions,
dataConfig: { actionButtonMenuIndex = 0, actions: userDefinedActions = [] },
actionButtonMenuIndex = 0,
modules: userDefinedModules = [],
} = useContext(ModuleContext);

// If a resolver is passed, get actions from it
// FDS-142: If a resolver is passed, get actions from it
const actions = resolveRecordActions
? resolveRecordActions(record, userDefinedActions)
: resolveRecordActions; // otherwise continue as normal
? resolveRecordActions(record, userDefinedModules)
: userDefinedModules; // otherwise continue as normal

const outsideButtonActions = [];
const insideButtonActions = [];
Expand Down Expand Up @@ -97,7 +98,7 @@ const TableRow = ({ config = {}, record = {} }) => {
);
});

if (userDefinedActions.length) {
if (userDefinedModules.length) {
rowCells.push(rowActions);
}

Expand Down

0 comments on commit 7427878

Please sign in to comment.