Skip to content

Commit

Permalink
fix(Card): updated markup for actionable cards
Browse files Browse the repository at this point in the history
  • Loading branch information
thatblindgeye committed Aug 15, 2024
1 parent c42de7e commit 6153951
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 44 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
JSXElement,
JSXText,
JSXExpressionContainer,
JSXSpreadChild,
JSXFragment,
} from "estree-jsx";

function getChildJSXElementCallback(
childNode:
| JSXElement
| JSXText
| JSXExpressionContainer
| JSXSpreadChild
| JSXFragment,
name: string
) {
return (
childNode.type === "JSXElement" &&
childNode.openingElement.name.type === "JSXIdentifier" &&
childNode.openingElement.name.name === name
);
}

/** Can be used to run logic if the specific child element exists, or to run logic on the
* specified element.
*/
export function getChildJSXElementByName(node: JSXElement, name: string) {
return node.children?.find((child) =>
getChildJSXElementCallback(child, name)
) as JSXElement | undefined;
}

/** Can be used to run logic if the specific child elements exist, or to run logic on the
* specified elements.
*/
export function getAllChildJSXElementsByName(node: JSXElement, name: string) {
return node.children?.filter((child) =>
getChildJSXElementCallback(child, name)
) as JSXElement[];
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { ImportSpecifier } from "estree-jsx";
import { ImportSpecifier, ImportDefaultSpecifier } from "estree-jsx";
import { getSpecifierFromImports } from "./getSpecifierFromImports";

/** Resolves the local name of an import */
export function getLocalComponentName(
namedImports: ImportSpecifier[],
namedImports: (ImportSpecifier | ImportDefaultSpecifier)[],
importedName: string
) {
const componentImport = namedImports.find(
(name) => name.imported.name === importedName
);

const componentImport = getSpecifierFromImports(namedImports, importedName);
const isDefaultImport = componentImport?.type === "ImportDefaultSpecifier";
const isAlias =
!isDefaultImport &&
componentImport?.imported.name !== componentImport?.local.name;

if (componentImport && isAlias) {
if ((componentImport && isAlias) || isDefaultImport) {
return componentImport.local.name;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ImportSpecifier, ImportDefaultSpecifier } from "estree-jsx";

/** Can be used to extract a specific specifier from an array of imports, such as to only
* run logic if X and Y imports are present or to use the specifier properties in other logic. */
export function getSpecifierFromImports(
imports: (ImportSpecifier | ImportDefaultSpecifier)[],
importedName: string
) {
const importSpecifier = imports.find((imp) =>
imp.type === "ImportDefaultSpecifier"
? imp.local.name === importedName
: imp.imported.name === importedName
);

return importSpecifier;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from "./contextReports";
export * from "./findAncestor";
export * from "./fixers";
export * from "./getAttributeName";
export * from "./getChildJSXElementByName";
export * from "./getCodeModDataTag";
export * from "./getComponentImportName";
export * from "./getDefaultDeclarationString";
Expand All @@ -10,14 +11,15 @@ export * from "./getFromPackage";
export * from "./getImportedName";
export * from "./getLocalComponentName";
export * from "./getNodeName";
export * from "./getSpecifierFromImports";
export * from "./getText";
export * from "./hasCodemodDataTag";
export * from "./helpers";
export * from "./importAndExport";
export * from "./includesImport";
export * from "./interfaces";
export * from "./JSXAttributes";
export * from "./JSXElements";
export * from "./nodeIsComponentNamed";
export * from "./nodeMatches";
export * from "./pfPackageMatches";
export * from "./removeElement";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { JSXElement } from "estree-jsx";

export function nodeIsComponentNamed(node: JSXElement, componentName: string) {
if (node.openingElement.name.type === "JSXIdentifier") {
return node.openingElement.name.name === componentName;
}

return false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
getAttribute,
getAttributeText,
getAttributeValueText,
getChildElementByName,
getChildJSXElementByName,
getDefaultImportsFromPackage,
getExpression,
getFromPackage,
Expand Down Expand Up @@ -159,7 +159,7 @@ module.exports = {
return;
}

const header = getChildElementByName(node, "EmptyStateHeader");
const header = getChildJSXElementByName(node, "EmptyStateHeader");
const emptyStateTitleTextAttribute = getAttribute(node, "titleText");

if (!header && emptyStateTitleTextAttribute) {
Expand All @@ -168,7 +168,7 @@ module.exports = {
return;
}

const titleChild = getChildElementByName(node, "Title");
const titleChild = getChildJSXElementByName(node, "Title");

if (
(!header || header.type !== "JSXElement") &&
Expand All @@ -194,7 +194,7 @@ module.exports = {
removeElements.push(titleChild);
}

const emptyStateIconChild = getChildElementByName(
const emptyStateIconChild = getChildJSXElementByName(
node,
"EmptyStateIcon"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ImportSpecifier } from "estree-jsx";
import { JSXOpeningElementWithParent } from "../../helpers";
import {
getAllImportsFromPackage,
getChildElementByName,
getChildJSXElementByName,
getImportedName,
getLocalComponentName,
hasCodeModDataTag,
Expand All @@ -21,7 +21,7 @@ function moveNodeIntoMastheadMain(
}

const localMastheadMain = getLocalComponentName(namedImports, "MastheadMain");
const mastheadMain = getChildElementByName(
const mastheadMain = getChildJSXElementByName(
node.parent.parent,
localMastheadMain
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { JSXElement } from "estree-jsx";
import {
getFromPackage,
getAttribute,
getAllChildElementsByName,
getAllChildJSXElementsByName,
} from "../../helpers";

// https://github.com/patternfly/patternfly-react/pull/10378
Expand All @@ -29,10 +29,15 @@ module.exports = {
) {
const wizardFooterProp = getAttribute(node, "footer");
const wizardSteps = wizardStepImport
? getAllChildElementsByName(node, wizardStepImport.local.name)
? getAllChildJSXElementsByName(
node,
wizardStepImport.local.name
)
: undefined;
const allWizardStepsHaveFooter = wizardSteps
? wizardSteps.every((step) => getAttribute(step, "footer"))
? (wizardSteps as JSXElement[]).every((step) =>
getAttribute(step, "footer")
)
: false;

if (wizardFooterProp || allWizardStepsHaveFooter) {
Expand Down

0 comments on commit 6153951

Please sign in to comment.