From 8bc5d2ee3b1bcf879a2b4fb0d827c9f38d0e74f1 Mon Sep 17 00:00:00 2001 From: TJ Egan Date: Mon, 23 Mar 2020 12:57:50 -0700 Subject: [PATCH 1/4] fix(data-table): require additional props in certain situations --- .../components/DataTable/TableExpandHeader.js | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/react/src/components/DataTable/TableExpandHeader.js b/packages/react/src/components/DataTable/TableExpandHeader.js index e8228cd6e288..efcb7d95a17e 100644 --- a/packages/react/src/components/DataTable/TableExpandHeader.js +++ b/packages/react/src/components/DataTable/TableExpandHeader.js @@ -49,6 +49,21 @@ const TableExpandHeader = ({ ); }; +function isRequiredIfPropExists(requiredProp) { + function checker(props, propName, componentName) { + if (props[requiredProp] === undefined) { + return; + } + if (props[propName] === undefined) { + throw new Error( + `${componentName} requires ${propName} if ${requiredProp} is defined` + ); + } + } + + return checker; +} + TableExpandHeader.propTypes = { className: PropTypes.string, children: PropTypes.node, @@ -57,18 +72,18 @@ TableExpandHeader.propTypes = { * Specify the string read by a voice reader when the expand trigger is * focused */ - ariaLabel: PropTypes.string.isRequired, + ariaLabel: isRequiredIfPropExists('ariaLabel'), /** * Specify whether this row is expanded or not. This helps coordinate data * attributes so that `TableExpandRow` and `TableExapndedRow` work together */ - isExpanded: PropTypes.bool.isRequired, + isExpanded: isRequiredIfPropExists('isExpanded'), /** * Hook for when a listener initiates a request to expand the given row */ - onExpand: PropTypes.func.isRequired, + onExpand: isRequiredIfPropExists('onExpand'), /** * The description of the chevron right icon, to be put in its SVG `` element. From fa295fc875d3fa08486de5fca4a3360bfde6cd89 Mon Sep 17 00:00:00 2001 From: TJ Egan <tw15egan@gmail.com> Date: Mon, 23 Mar 2020 13:27:46 -0700 Subject: [PATCH 2/4] fix(data-table): update snapshots --- .../__snapshots__/PublicAPI-test.js.snap | 30 ++++--------------- 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap b/packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap index 5f46882d9bcc..69e123ddb095 100644 --- a/packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap +++ b/packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap @@ -868,10 +868,7 @@ Map { }, "TableExpandHeader": Object { "propTypes": Object { - "ariaLabel": Object { - "isRequired": true, - "type": "string", - }, + "ariaLabel": [Function], "children": Object { "type": "node", }, @@ -881,14 +878,8 @@ Map { "expandIconDescription": Object { "type": "string", }, - "isExpanded": Object { - "isRequired": true, - "type": "bool", - }, - "onExpand": Object { - "isRequired": true, - "type": "func", - }, + "isExpanded": [Function], + "onExpand": [Function], }, }, "TableExpandRow": Object { @@ -1453,10 +1444,7 @@ Map { }, "TableExpandHeader" => Object { "propTypes": Object { - "ariaLabel": Object { - "isRequired": true, - "type": "string", - }, + "ariaLabel": [Function], "children": Object { "type": "node", }, @@ -1466,14 +1454,8 @@ Map { "expandIconDescription": Object { "type": "string", }, - "isExpanded": Object { - "isRequired": true, - "type": "bool", - }, - "onExpand": Object { - "isRequired": true, - "type": "func", - }, + "isExpanded": [Function], + "onExpand": [Function], }, }, "TableExpandRow" => Object { From bff035de4a3fc05c4491667d08fcb7cb0c531386 Mon Sep 17 00:00:00 2001 From: TJ Egan <tw15egan@gmail.com> Date: Mon, 23 Mar 2020 14:03:54 -0700 Subject: [PATCH 3/4] fix(data-table): fix proptype --- packages/react/src/components/DataTable/TableExpandHeader.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/react/src/components/DataTable/TableExpandHeader.js b/packages/react/src/components/DataTable/TableExpandHeader.js index efcb7d95a17e..8776f0f85eec 100644 --- a/packages/react/src/components/DataTable/TableExpandHeader.js +++ b/packages/react/src/components/DataTable/TableExpandHeader.js @@ -78,12 +78,12 @@ TableExpandHeader.propTypes = { * Specify whether this row is expanded or not. This helps coordinate data * attributes so that `TableExpandRow` and `TableExapndedRow` work together */ - isExpanded: isRequiredIfPropExists('isExpanded'), + isExpanded: isRequiredIfPropExists('enableExpando'), /** * Hook for when a listener initiates a request to expand the given row */ - onExpand: isRequiredIfPropExists('onExpand'), + onExpand: isRequiredIfPropExists('enableExpando'), /** * The description of the chevron right icon, to be put in its SVG `<title>` element. From 190790c2395b18fec0c8591b466b473a0980f993 Mon Sep 17 00:00:00 2001 From: TJ Egan <tw15egan@gmail.com> Date: Tue, 24 Mar 2020 09:25:20 -0700 Subject: [PATCH 4/4] fix(data-table): refactor to use exisiting func --- .../components/DataTable/TableExpandHeader.js | 22 ++++--------------- 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/packages/react/src/components/DataTable/TableExpandHeader.js b/packages/react/src/components/DataTable/TableExpandHeader.js index 8776f0f85eec..482682e8078a 100644 --- a/packages/react/src/components/DataTable/TableExpandHeader.js +++ b/packages/react/src/components/DataTable/TableExpandHeader.js @@ -7,6 +7,7 @@ import cx from 'classnames'; import PropTypes from 'prop-types'; +import requiredIfGivenPropExists from '../../prop-types/requiredIfGivenPropExists'; import React from 'react'; import { ChevronRight16 } from '@carbon/icons-react'; import { settings } from 'carbon-components'; @@ -49,21 +50,6 @@ const TableExpandHeader = ({ ); }; -function isRequiredIfPropExists(requiredProp) { - function checker(props, propName, componentName) { - if (props[requiredProp] === undefined) { - return; - } - if (props[propName] === undefined) { - throw new Error( - `${componentName} requires ${propName} if ${requiredProp} is defined` - ); - } - } - - return checker; -} - TableExpandHeader.propTypes = { className: PropTypes.string, children: PropTypes.node, @@ -72,18 +58,18 @@ TableExpandHeader.propTypes = { * Specify the string read by a voice reader when the expand trigger is * focused */ - ariaLabel: isRequiredIfPropExists('ariaLabel'), + ariaLabel: requiredIfGivenPropExists('enableExpando', PropTypes.string), /** * Specify whether this row is expanded or not. This helps coordinate data * attributes so that `TableExpandRow` and `TableExapndedRow` work together */ - isExpanded: isRequiredIfPropExists('enableExpando'), + isExpanded: requiredIfGivenPropExists('enableExpando', PropTypes.bool), /** * Hook for when a listener initiates a request to expand the given row */ - onExpand: isRequiredIfPropExists('enableExpando'), + onExpand: requiredIfGivenPropExists('enableExpando', PropTypes.func), /** * The description of the chevron right icon, to be put in its SVG `<title>` element.