Skip to content

Commit a569636

Browse files
authored
fix(checkbox,radio): pass required prop to label #115 (#498)
* feat(form-label): update label prop type to ReactNode #486 * fix(checkbox,radio): pass required prop to label #115
1 parent d5235e2 commit a569636

7 files changed

Lines changed: 47 additions & 19 deletions

File tree

src/tedi/components/form/checkbox/checkbox.spec.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,10 @@ describe('Checkbox component', () => {
210210

211211
expect(label.click).toHaveBeenCalled();
212212
});
213+
214+
it('renders required indicator when required prop is true', () => {
215+
render(<Checkbox id="checkbox-id" label="Checkbox Label" value="checkbox-value" name="checkbox-group" required />);
216+
217+
expect(screen.getByText('*')).toBeInTheDocument();
218+
});
213219
});

src/tedi/components/form/checkbox/checkbox.stories.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ export const States = () => {
161161
/>
162162
</Col>
163163
</Row>
164+
<Row>
165+
<Col md={3}>
166+
<Text modifiers="bold">Required</Text>
167+
</Col>
168+
<Col>
169+
<Checkbox id="check-required" label="Text" name="check-required" value="check" required />
170+
</Col>
171+
</Row>
164172
</VerticalSpacing>
165173
</Col>
166174
</Row>

src/tedi/components/form/checkbox/checkbox.tsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const Checkbox = (props: CheckboxProps): JSX.Element => {
3434
tooltip,
3535
invalid,
3636
size = 'default',
37+
required,
3738
...rest
3839
} = props;
3940
const [innerChecked, setInnerChecked] = React.useState<boolean>(defaultChecked || false);
@@ -53,7 +54,7 @@ export const Checkbox = (props: CheckboxProps): JSX.Element => {
5354
const helperId = helper ? helper.id ?? `${id}-helper` : undefined;
5455
const tooltipId = tooltip ? `${id}-tooltip` : undefined;
5556

56-
const LabelBEM = cn(styles['tedi-checkbox'], { [styles['tedi-checkbox--disabled']]: disabled });
57+
const LabelBEM = cn(styles['tedi-checkbox__label'], { [styles['tedi-checkbox--disabled']]: disabled });
5758

5859
return (
5960
<div data-name="check" {...rest}>
@@ -100,8 +101,8 @@ export const Checkbox = (props: CheckboxProps): JSX.Element => {
100101
</div>
101102
</div>
102103
</Col>
103-
<Col>
104-
{label && typeof label === 'string' ? (
104+
{label && (
105+
<Col>
105106
<FormLabel
106107
ref={labelRef}
107108
className={LabelBEM}
@@ -110,13 +111,10 @@ export const Checkbox = (props: CheckboxProps): JSX.Element => {
110111
hideLabel={hideLabel}
111112
label={label}
112113
tooltip={tooltip}
114+
required={required}
113115
/>
114-
) : (
115-
<label ref={labelRef} htmlFor={id} className={LabelBEM} data-testid="checkbox-label">
116-
{label}
117-
</label>
118-
)}
119-
</Col>
116+
</Col>
117+
)}
120118
</Row>
121119
{helper && (
122120
<FeedbackText id={helperId} {...helper} className={cn(styles['tedi-checkbox__helper'], helper.className)} />

src/tedi/components/form/choice-input.types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface ChoiceInputProps {
88
/**
99
* Label text
1010
*/
11-
label: string | React.ReactNode;
11+
label: React.ReactNode;
1212
/**
1313
* Additional classes.
1414
*/
@@ -61,4 +61,8 @@ export interface ChoiceInputProps {
6161
* Whether the input is marked as invalid.
6262
*/
6363
invalid?: boolean;
64+
/**
65+
* Whether the input is marked as required.
66+
*/
67+
required?: boolean;
6468
}

src/tedi/components/form/radio/radio.spec.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,10 @@ describe('Radio component', () => {
222222

223223
expect(label.click).toHaveBeenCalled();
224224
});
225+
226+
it('renders required indicator when required prop is true', () => {
227+
render(<Radio id="radio-id" label="Radio Label" value="radio-value" name="radio-group" required />);
228+
229+
expect(screen.getByText('*')).toBeInTheDocument();
230+
});
225231
});

src/tedi/components/form/radio/radio.stories.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ export const States = () => {
131131
/>
132132
</Col>
133133
</Row>
134+
<Row>
135+
<Col md={3}>
136+
<Text modifiers="bold">Required</Text>
137+
</Col>
138+
<Col>
139+
<Radio id="radio-required" label="Text" name="radio-required" value="radio" required />
140+
</Col>
141+
</Row>
134142
</VerticalSpacing>
135143
</Col>
136144
</Row>

src/tedi/components/form/radio/radio.tsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const Radio = (props: RadioProps): JSX.Element => {
2626
tooltip,
2727
size = 'default',
2828
invalid,
29+
required,
2930
...rest
3031
} = props;
3132

@@ -46,7 +47,7 @@ export const Radio = (props: RadioProps): JSX.Element => {
4647
const helperId = helper ? helper.id ?? `${id}-helper` : undefined;
4748
const tooltipId = tooltip ? `${id}-tooltip` : undefined;
4849

49-
const LabelBEM = cn(styles['tedi-radio'], { [styles['tedi-radio--disabled']]: disabled });
50+
const LabelBEM = cn(styles['tedi-radio__label'], { [styles['tedi-radio--disabled']]: disabled });
5051

5152
return (
5253
<div data-name="radio" {...rest}>
@@ -78,8 +79,8 @@ export const Radio = (props: RadioProps): JSX.Element => {
7879
/>
7980
</div>
8081
</Col>
81-
<Col>
82-
{label && typeof label === 'string' ? (
82+
{label && (
83+
<Col>
8384
<FormLabel
8485
ref={labelRef}
8586
className={LabelBEM}
@@ -88,13 +89,10 @@ export const Radio = (props: RadioProps): JSX.Element => {
8889
hideLabel={hideLabel}
8990
label={label}
9091
tooltip={tooltip}
92+
required={required}
9193
/>
92-
) : (
93-
<label ref={labelRef} htmlFor={id} className={LabelBEM} data-testid="radio-label">
94-
{label}
95-
</label>
96-
)}
97-
</Col>
94+
</Col>
95+
)}
9896
</Row>
9997

10098
{helper && (

0 commit comments

Comments
 (0)