Skip to content

Commit

Permalink
feat: add id property to for all components
Browse files Browse the repository at this point in the history
  • Loading branch information
jtiala committed Aug 31, 2023
1 parent fda0781 commit b7f51fd
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 19 deletions.
2 changes: 2 additions & 0 deletions packages/react/src/components/Anchor/Anchor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export function Anchor({
onTouchStart,
onTouchEnd,
children,
id,
testId,
}: AnchorProps) {
const openInNewTab = target === "_blank";
Expand All @@ -107,6 +108,7 @@ export function Anchor({
onTouchStart={onTouchStart}
onTouchEnd={onTouchEnd}
className={className}
id={id}
data-testid={testId}
>
{children}
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/components/Blockquote/Blockquote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ export function Blockquote({
author,
source,
children,
id,
testId,
}: BlockquoteProps) {
return (
<blockquote
cite={cite}
className={blockquoteClassName}
id={id}
data-testid={testId}
>
<div className={quoteClassName}>{children}</div>
Expand Down
32 changes: 25 additions & 7 deletions packages/react/src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import { ButtonHTMLAttributes, ReactNode } from "react";
type HTMLButtonProps = ButtonHTMLAttributes<HTMLButtonElement>;

export type ButtonProps = {
/**
* Button content.
*/
children: ReactNode;

/**
* Type of the button.
*/
Expand All @@ -28,15 +23,38 @@ export type ButtonProps = {
* Callback to call when the button loses focus.
*/
onBlur?: HTMLButtonProps["onBlur"];

/**
* Button content.
*/
children: ReactNode;
} & CommonComponentProps;

const className = cn("button");

/**
* A clickable UI element commonly used to trigger actions, such as submitting a form.
*/
export function Button({ type = "button", testId, ...rest }: ButtonProps) {
export function Button({
type = "button",
onClick,
onFocus,
onBlur,
children,
id,
testId,
}: ButtonProps) {
return (
<button type={type} {...rest} className={className} data-testid={testId} />
<button
type={type}
onClick={onClick}
onFocus={onFocus}
onBlur={onBlur}
className={className}
id={id}
data-testid={testId}
>
{children}
</button>
);
}
8 changes: 6 additions & 2 deletions packages/react/src/components/Heading/Heading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ const className = cn("heading");
/**
* Display a section heading.
*/
export function Heading({ level, children, testId }: HeadingProps) {
export function Heading({ level, children, id, testId }: HeadingProps) {
const element = "h" + level;

return createElement(element, { className, "data-testid": testId }, children);
return createElement(
element,
{ className, id, "data-testid": testId },
children,
);
}
8 changes: 6 additions & 2 deletions packages/react/src/components/List/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ const className = cn("list");
/**
* Display a list of items.
*/
export function List({ type, children, testId }: ListProps) {
export function List({ type, children, id, testId }: ListProps) {
const element = type === "ordered" ? "ol" : "ul";

return createElement(element, { className, "data-testid": testId }, children);
return createElement(
element,
{ className, id, "data-testid": testId },
children,
);
}

List.Item = ListItem;
8 changes: 6 additions & 2 deletions packages/react/src/components/List/ListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export type ListItemProps = {
/**
* Display a list item.
*/
export function ListItem({ children, testId }: ListItemProps) {
return <li data-testid={testId}>{children}</li>;
export function ListItem({ children, id, testId }: ListItemProps) {
return (
<li id={id} data-testid={testId}>
{children}
</li>
);
}
4 changes: 2 additions & 2 deletions packages/react/src/components/Paragraph/Paragraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const className = cn("paragraph");
/**
* Display a text paragraph.
*/
export function Paragraph({ children, testId }: ParagraphProps) {
export function Paragraph({ children, id, testId }: ParagraphProps) {
return (
<p className={className} data-testid={testId}>
<p className={className} id={id} data-testid={testId}>
{children}
</p>
);
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/components/Stack/Stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const horizontalClassName = cn("stack-horizontal");
export function Stack({
direction = "vertical",
children,
id,
testId,
}: StackProps) {
const classNames = [
Expand All @@ -31,7 +32,7 @@ export function Stack({
].join(" ");

return (
<div className={classNames} data-testid={testId}>
<div className={classNames} id={id} data-testid={testId}>
{children}
</div>
);
Expand Down
12 changes: 10 additions & 2 deletions packages/react/src/components/Text/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,18 @@ const className = cn("text");
/**
* Display inline text content with different styles.
*/
export function Text({ type, children, testId, ...rest }: TextProps) {
export function Text({
type,
title,
cite,
dateTime,
children,
id,
testId,
}: TextProps) {
return createElement(
type || "span",
{ className, ...rest, "data-testid": testId },
{ className, title, cite, id, dateTime, "data-testid": testId },
children,
);
}
8 changes: 7 additions & 1 deletion packages/utils/src/components.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
export type CommonComponentProps = {
/**
* Unique identifier.
*/
id?: string;

/**
* Provide `data-testid` attribute for the component to be used with testing tools.
* While the `testId` is a useful way for targeting the component, it should be used as a last resort. Whenever possible, use other attributes or selectors that have semantic meaning, such as `id` or `name`.
* While the `testId` is a useful way for targeting the component, it should be used as a last resort.
* Whenever possible, use other attributes or selectors that have semantic meaning, such as `id` or `name`.
*/
testId?: string;
};

0 comments on commit b7f51fd

Please sign in to comment.