Skip to content

Commit 7613d4c

Browse files
committed
feat(Checkbox/Radio): replace isLabelBeforeButton flag with labelPosition "start" / "end"
1 parent 5feb7b3 commit 7613d4c

File tree

6 files changed

+40
-40
lines changed

6 files changed

+40
-40
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ export interface CheckboxProps
1616
inputClassName?: string;
1717
/** Flag to indicate whether the checkbox wrapper element is a <label> element for the checkbox input. Will not apply if a component prop (with a value other than a "label") is specified. */
1818
isLabelWrapped?: boolean;
19-
/** Flag to show if the checkbox label is shown before the checkbox input. */
20-
isLabelBeforeButton?: boolean;
2119
/** Flag to show if the checkbox selection is valid or invalid. */
2220
isValid?: boolean;
2321
/** Flag to show if the checkbox is disabled. */
@@ -31,6 +29,8 @@ export interface CheckboxProps
3129
onChange?: (event: React.FormEvent<HTMLInputElement>, checked: boolean) => void;
3230
/** Label text of the checkbox. */
3331
label?: React.ReactNode;
32+
/** Sets the position of the label. Defaults to 'end' (after the checkbox input). */
33+
labelPosition?: 'start' | 'end';
3434
/** Id of the checkbox. */
3535
id: string;
3636
/** Aria-label of the checkbox. */
@@ -85,7 +85,7 @@ class Checkbox extends React.Component<CheckboxProps, CheckboxState> {
8585
inputClassName,
8686
onChange,
8787
isLabelWrapped,
88-
isLabelBeforeButton,
88+
labelPosition = 'end',
8989
isValid,
9090
isDisabled,
9191
isRequired,
@@ -156,7 +156,7 @@ class Checkbox extends React.Component<CheckboxProps, CheckboxState> {
156156
className={css(styles.check, !label && styles.modifiers.standalone, className)}
157157
htmlFor={wrapWithLabel && props.id}
158158
>
159-
{isLabelBeforeButton ? (
159+
{labelPosition === 'start' ? (
160160
<>
161161
{labelRendered}
162162
{inputRendered}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ test('Renders with span element around the inner label text if component is set
263263
expect(screen.getByText(labelText).tagName).toBe('SPAN');
264264
});
265265

266-
test('Renders label before checkbox input if isLabelBeforeButton is provided', () => {
267-
render(<Checkbox id="test-id" isLabelBeforeButton label={'test checkbox label'} />);
266+
test('Renders label before checkbox input if labelPosition is "start"', () => {
267+
render(<Checkbox id="test-id" labelPosition="start" label={'test checkbox label'} />);
268268

269269
const wrapper = screen.getByRole('checkbox').parentElement!;
270270

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ export interface RadioProps
1717
id: string;
1818
/** Flag to indicate whether the radio wrapper element is a native label element for the radio input. Will not apply if a component prop (with a value other than a "label") is specified. */
1919
isLabelWrapped?: boolean;
20-
/** Flag to show if the radio label is shown before the radio input. */
21-
isLabelBeforeButton?: boolean;
2220
/** Flag to show if the radio is checked. */
2321
checked?: boolean;
2422
/** Flag to show if the radio is checked. */
@@ -29,6 +27,8 @@ export interface RadioProps
2927
isValid?: boolean;
3028
/** Label text of the radio. */
3129
label?: React.ReactNode;
30+
/** Sets the position of the label. Defaults to 'end' (after the radio input). */
31+
labelPosition?: 'start' | 'end';
3232
/** Name for group of radios */
3333
name: string;
3434
/** A callback for when the radio selection changes. */
@@ -79,7 +79,7 @@ class Radio extends React.Component<RadioProps, { ouiaStateId: string }> {
7979
inputClassName,
8080
defaultChecked,
8181
isLabelWrapped,
82-
isLabelBeforeButton,
82+
labelPosition = 'end',
8383
isChecked,
8484
isDisabled,
8585
isValid,
@@ -132,7 +132,7 @@ class Radio extends React.Component<RadioProps, { ouiaStateId: string }> {
132132
className={css(styles.radio, !label && styles.modifiers.standalone, className)}
133133
htmlFor={wrapWithLabel && props.id}
134134
>
135-
{isLabelBeforeButton ? (
135+
{labelPosition === 'start' ? (
136136
<>
137137
{labelRendered}
138138
{inputRendered}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ describe('Radio', () => {
2525
expect(asFragment()).toMatchSnapshot();
2626
});
2727

28-
test('isLabelBeforeButton', () => {
28+
test('labelPosition is "start"', () => {
2929
const { asFragment } = render(
30-
<Radio id="check" isLabelBeforeButton label="Radio label" aria-label="check" name="check" />
30+
<Radio id="check" labelPosition="start" label="Radio label" aria-label="check" name="check" />
3131
);
3232
expect(asFragment()).toMatchSnapshot();
3333
});
@@ -133,8 +133,8 @@ describe('Radio', () => {
133133
expect(screen.getByText(labelText).tagName).toBe('SPAN');
134134
});
135135

136-
test('Renders label before radio input if isLabelBeforeButton is provided', () => {
137-
render(<Radio id="test-id" name="check" isLabelBeforeButton label={'test radio label'} />);
136+
test('Renders label before radio input if labelPosition is "start"', () => {
137+
render(<Radio id="test-id" name="check" labelPosition="start" label={'test radio label'} />);
138138

139139
const wrapper = screen.getByRole('radio').parentElement!;
140140

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

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,6 @@ exports[`Radio isDisabled 1`] = `
4242
</DocumentFragment>
4343
`;
4444

45-
exports[`Radio isLabelBeforeButton 1`] = `
46-
<DocumentFragment>
47-
<div
48-
class="pf-v5-c-radio"
49-
>
50-
<label
51-
class="pf-v5-c-radio__label"
52-
for="check"
53-
>
54-
Radio label
55-
</label>
56-
<input
57-
aria-invalid="false"
58-
class="pf-v5-c-radio__input"
59-
data-ouia-component-id="OUIA-Generated-Radio-4"
60-
data-ouia-component-type="PF5/Radio"
61-
data-ouia-safe="true"
62-
id="check"
63-
name="check"
64-
type="radio"
65-
/>
66-
</div>
67-
</DocumentFragment>
68-
`;
69-
7045
exports[`Radio isLabelWrapped 1`] = `
7146
<DocumentFragment>
7247
<label
@@ -174,6 +149,31 @@ exports[`Radio label is string 1`] = `
174149
</DocumentFragment>
175150
`;
176151

152+
exports[`Radio labelPosition is "start" 1`] = `
153+
<DocumentFragment>
154+
<div
155+
class="pf-v5-c-radio"
156+
>
157+
<label
158+
class="pf-v5-c-radio__label"
159+
for="check"
160+
>
161+
Radio label
162+
</label>
163+
<input
164+
aria-invalid="false"
165+
class="pf-v5-c-radio__input"
166+
data-ouia-component-id="OUIA-Generated-Radio-4"
167+
data-ouia-component-type="PF5/Radio"
168+
data-ouia-safe="true"
169+
id="check"
170+
name="check"
171+
type="radio"
172+
/>
173+
</div>
174+
</DocumentFragment>
175+
`;
176+
177177
exports[`Radio passing HTML attribute 1`] = `
178178
<DocumentFragment>
179179
<div

packages/react-core/src/components/Radio/examples/RadioReversed.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import React from 'react';
22
import { Radio } from '@patternfly/react-core';
33

44
export const RadioReversed: React.FunctionComponent = () => (
5-
<Radio isLabelBeforeButton label="Reversed radio example" id="radio-reversed" name="radio-3" />
5+
<Radio labelPosition="start" label="Reversed radio example" id="radio-reversed" name="radio-3" />
66
);

0 commit comments

Comments
 (0)