Skip to content

Commit b523188

Browse files
author
Eric Olkowski
committed
Removed restriction of variant and children content
1 parent 7578dd0 commit b523188

3 files changed

Lines changed: 47 additions & 39 deletions

File tree

packages/react-core/src/components/Button/Button.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,8 @@ const ButtonBase: React.FunctionComponent<ButtonProps> = ({
153153
'Button: when the isHamburger property is passed in, you must also pass in a boolean value to the isExpanded property. It is expected that a hamburger button controls the expansion of other content.'
154154
);
155155
}
156-
// TODO: Remove isSettings in breaking change to throw this warning for any non-hamburger button that does not have children or aria-label
157-
if (
158-
(isHamburger && !ariaLabel && !props['aria-labelledby']) ||
159-
(isSettings && !ariaLabel && !children && !props['aria-labelledby'])
160-
) {
156+
// TODO: Remove isSettings || isHamburger conditional in breaking change to throw this warning for any button that does not have children or aria name
157+
if ((isSettings || isHamburger) && !ariaLabel && !children && !props['aria-labelledby']) {
161158
// eslint-disable-next-line no-console
162159
console.error(
163160
'Button: you must provide either visible text content or an accessible name via the aria-label or aria-labelledby properties.'
@@ -169,7 +166,6 @@ const ButtonBase: React.FunctionComponent<ButtonProps> = ({
169166
const isButtonElement = Component === 'button';
170167
const isInlineSpan = isInline && Component === 'span';
171168
const isIconAlignedAtEnd = iconPosition === 'end' || iconPosition === 'right';
172-
const shouldForcePlainVariant = isSettings || isHamburger;
173169
const shouldOverrideIcon = isSettings || isHamburger;
174170

175171
const preventedEvents = inoperableEvents.reduce(
@@ -214,7 +210,7 @@ const ButtonBase: React.FunctionComponent<ButtonProps> = ({
214210
);
215211
};
216212
const _icon = renderIcon();
217-
const _children = children && !isHamburger && <span className={css('pf-v6-c-button__text')}>{children}</span>;
213+
const _children = children && <span className={css('pf-v6-c-button__text')}>{children}</span>;
218214
// We only want to render the aria-disabled attribute when true, similar to the disabled attribute natively.
219215
const shouldRenderAriaDisabled = isAriaDisabled || (!isButtonElement && isDisabled);
220216

@@ -227,7 +223,7 @@ const ButtonBase: React.FunctionComponent<ButtonProps> = ({
227223
aria-label={ariaLabel}
228224
className={css(
229225
styles.button,
230-
shouldForcePlainVariant ? styles.modifiers.plain : styles.modifiers[variant],
226+
styles.modifiers[variant],
231227
isSettings && styles.modifiers.settings,
232228
isHamburger && styles.modifiers.hamburger,
233229
isHamburger && hamburgerVariant && styles.modifiers[hamburgerVariant],

packages/react-core/src/components/Button/__tests__/Button.test.tsx

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,8 @@ describe('Hamburger button', () => {
306306
);
307307
});
308308

309-
test('Throws console error when isHamburger is true and neither aria-label nor aria-lablledby are passed', () => {
309+
// TODO: Remove isHamburger in breaking change to throw error for any button that does not have children or aria name
310+
test('Throws console error when isHamburger is true and neither children, aria-label nor aria-lablledby are passed', () => {
310311
const consoleError = jest.spyOn(console, 'error').mockImplementation();
311312

312313
render(<Button isHamburger isExpanded />);
@@ -315,7 +316,22 @@ describe('Hamburger button', () => {
315316
'Button: you must provide either visible text content or an accessible name via the aria-label or aria-labelledby properties.'
316317
);
317318
});
319+
// TODO: Remove isHamburger in breaking change to throw error for any button that does not have children or aria name
320+
test('Does not throw console error when isHamburger is true and children is passed', () => {
321+
const consoleError = jest.spyOn(console, 'error').mockImplementation();
322+
323+
render(
324+
<Button isHamburger isExpanded>
325+
Test
326+
</Button>
327+
);
318328

329+
expect(consoleError).not.toHaveBeenCalledWith(
330+
'Button: you must provide either visible text content or an accessible name via the aria-label or aria-labelledby properties.'
331+
);
332+
});
333+
334+
// TODO: Remove isHamburger in breaking change to throw error for any button that does not have children or aria name
319335
test('Does not throw console error when isHamburger is true and aria-label is passed', () => {
320336
const consoleError = jest.spyOn(console, 'error').mockImplementation();
321337

@@ -326,6 +342,7 @@ describe('Hamburger button', () => {
326342
);
327343
});
328344

345+
// TODO: Remove isHamburger in breaking change to throw error for any button that does not have children or aria name
329346
test('Does not throw console error when isHamburger is true and aria-labelledby is passed', () => {
330347
const consoleError = jest.spyOn(console, 'error').mockImplementation();
331348

@@ -341,19 +358,32 @@ describe('Hamburger button', () => {
341358
);
342359
});
343360

361+
test(`Does not render with class ${styles.modifiers.hamburger} by default`, () => {
362+
render(<Button aria-label="test" />);
363+
364+
expect(screen.getByRole('button')).not.toHaveClass(styles.modifiers.hamburger);
365+
});
366+
344367
test(`Renders with class ${styles.modifiers.hamburger} when isHamburger is true`, () => {
345368
render(<Button isHamburger isExpanded aria-label="test" />);
346369

347370
expect(screen.getByRole('button')).toHaveClass(styles.modifiers.hamburger);
348371
});
349372

350-
test('Does not render with hamburgerVariant class when isHamburger is false', () => {
373+
test('Does not render with hamburgerVariant class when isHamburger is true and hamburgerVariant is not passed', () => {
351374
render(<Button isHamburger isExpanded aria-label="test" />);
352375

353376
expect(screen.getByRole('button')).not.toHaveClass(styles.modifiers.expand);
354377
expect(screen.getByRole('button')).not.toHaveClass(styles.modifiers.collapse);
355378
});
356379

380+
test('Does not render with hamburgerVariant class when isHamburger is false and hamburgerVariant is passed', () => {
381+
render(<Button hamburgerVariant="expand" isExpanded aria-label="test" />);
382+
383+
expect(screen.getByRole('button')).not.toHaveClass(styles.modifiers.expand);
384+
expect(screen.getByRole('button')).not.toHaveClass(styles.modifiers.collapse);
385+
});
386+
357387
test(`Renders with class ${styles.modifiers.expand} when isHamburger is true and hamburgerVariant = expand`, () => {
358388
render(<Button isHamburger hamburgerVariant="expand" isExpanded aria-label="test" />);
359389

@@ -368,32 +398,15 @@ describe('Hamburger button', () => {
368398
expect(screen.getByRole('button')).toHaveClass(styles.modifiers.collapse);
369399
});
370400

371-
test('Forces plain variant when isHamburger is passed', () => {
372-
render(<Button variant="secondary" isHamburger isExpanded aria-label="test" />);
373-
374-
expect(screen.getByRole('button')).not.toHaveClass(styles.modifiers.secondary);
375-
expect(screen.getByRole('button')).toHaveClass(styles.modifiers.plain);
376-
});
377-
378401
test('Does not render custom icon when icon prop and isHamburger are passed', () => {
379402
render(<Button isHamburger isExpanded aria-label="test" icon={<div>Custom icon</div>} />);
380403

381404
expect(screen.queryByText('Custom icon')).not.toBeInTheDocument();
382405
});
383-
384-
test('Does not render text content when isHamburger is passed', () => {
385-
render(
386-
<Button isHamburger isExpanded aria-label="test">
387-
Hamburger text content
388-
</Button>
389-
);
390-
391-
expect(screen.queryByText('Hamburger text content')).not.toBeInTheDocument();
392-
});
393406
});
394407

395408
describe('Settings button', () => {
396-
// TODO: Remove isSettings in breaking change to throw error for any non-hamburger button that does not have children or aria-label
409+
// TODO: Remove isSettings in breaking change to throw error for any button that does not have children or aria name
397410
test('Throws console error when isSettings is true and neither children, aria-label nor aria-lablledby are passed', () => {
398411
const consoleError = jest.spyOn(console, 'error').mockImplementation();
399412

@@ -404,7 +417,7 @@ describe('Settings button', () => {
404417
);
405418
});
406419

407-
// TODO: Remove isSettings in breaking change to throw error for any non-hamburger button that does not have children or aria-label
420+
// TODO: Remove isSettings in breaking change to throw error for any button that does not have children or aria name
408421
test('Does not throw console error when isSettings is true and children is passed', () => {
409422
const consoleError = jest.spyOn(console, 'error').mockImplementation();
410423

@@ -415,7 +428,7 @@ describe('Settings button', () => {
415428
);
416429
});
417430

418-
// TODO: Remove isSettings in breaking change to throw error for any non-hamburger button that does not have children or aria-label
431+
// TODO: Remove isSettings in breaking change to throw error for any button that does not have children or aria name
419432
test('Does not throw console error when isSettings is true and aria-label is passed', () => {
420433
const consoleError = jest.spyOn(console, 'error').mockImplementation();
421434

@@ -426,7 +439,7 @@ describe('Settings button', () => {
426439
);
427440
});
428441

429-
// TODO: Remove isSettings in breaking change to throw error for any non-hamburger button that does not have children or aria-label
442+
// TODO: Remove isSettings in breaking change to throw error for any button that does not have children or aria name
430443
test('Does not throw console error when isSettings is true and aria-labelledby is passed', () => {
431444
const consoleError = jest.spyOn(console, 'error').mockImplementation();
432445

@@ -442,17 +455,16 @@ describe('Settings button', () => {
442455
);
443456
});
444457

445-
test(`Renders with class ${styles.modifiers.settings} when isSettings is true`, () => {
446-
render(<Button isSettings aria-label="test" />);
458+
test(`Does not render with class ${styles.modifiers.settings} by default`, () => {
459+
render(<Button aria-label="test" />);
447460

448-
expect(screen.getByRole('button')).toHaveClass(styles.modifiers.settings);
461+
expect(screen.getByRole('button')).not.toHaveClass(styles.modifiers.settings);
449462
});
450463

451-
test('Forces plain variant when isSettings is passed', () => {
452-
render(<Button variant="secondary" isSettings aria-label="test" />);
464+
test(`Renders with class ${styles.modifiers.settings} when isSettings is true`, () => {
465+
render(<Button isSettings aria-label="test" />);
453466

454-
expect(screen.getByRole('button')).not.toHaveClass(styles.modifiers.secondary);
455-
expect(screen.getByRole('button')).toHaveClass(styles.modifiers.plain);
467+
expect(screen.getByRole('button')).toHaveClass(styles.modifiers.settings);
456468
});
457469

458470
test('Does not render custom icon when icon prop and isSettings are passed', () => {

packages/react-core/src/components/Button/__tests__/__snapshots__/Button.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exports[`Renders basic button 1`] = `
55
<button
66
aria-label="basic button"
77
class="pf-v6-c-button pf-m-primary"
8-
data-ouia-component-id="OUIA-Generated-Button-primary-54"
8+
data-ouia-component-id="OUIA-Generated-Button-primary-57"
99
data-ouia-component-type="PF6/Button"
1010
data-ouia-safe="true"
1111
type="button"

0 commit comments

Comments
 (0)