Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/tedi/components/form/checkbox/checkbox.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,10 @@ describe('Checkbox component', () => {

expect(label.click).toHaveBeenCalled();
});

it('renders required indicator when required prop is true', () => {
render(<Checkbox id="checkbox-id" label="Checkbox Label" value="checkbox-value" name="checkbox-group" required />);

expect(screen.getByText('*')).toBeInTheDocument();
});
});
8 changes: 8 additions & 0 deletions src/tedi/components/form/checkbox/checkbox.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ export const States = () => {
/>
</Col>
</Row>
<Row>
<Col md={3}>
<Text modifiers="bold">Required</Text>
</Col>
<Col>
<Checkbox id="check-required" label="Text" name="check-required" value="check" required />
</Col>
</Row>
</VerticalSpacing>
</Col>
</Row>
Expand Down
16 changes: 7 additions & 9 deletions src/tedi/components/form/checkbox/checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const Checkbox = (props: CheckboxProps): JSX.Element => {
tooltip,
invalid,
size = 'default',
required,
...rest
} = props;
const [innerChecked, setInnerChecked] = React.useState<boolean>(defaultChecked || false);
Expand All @@ -53,7 +54,7 @@ export const Checkbox = (props: CheckboxProps): JSX.Element => {
const helperId = helper ? helper.id ?? `${id}-helper` : undefined;
const tooltipId = tooltip ? `${id}-tooltip` : undefined;

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

return (
<div data-name="check" {...rest}>
Expand Down Expand Up @@ -100,8 +101,8 @@ export const Checkbox = (props: CheckboxProps): JSX.Element => {
</div>
</div>
</Col>
<Col>
{label && typeof label === 'string' ? (
{label && (
<Col>
<FormLabel
ref={labelRef}
className={LabelBEM}
Expand All @@ -110,13 +111,10 @@ export const Checkbox = (props: CheckboxProps): JSX.Element => {
hideLabel={hideLabel}
label={label}
tooltip={tooltip}
required={required}
/>
) : (
<label ref={labelRef} htmlFor={id} className={LabelBEM} data-testid="checkbox-label">
{label}
</label>
)}
</Col>
</Col>
)}
</Row>
{helper && (
<FeedbackText id={helperId} {...helper} className={cn(styles['tedi-checkbox__helper'], helper.className)} />
Expand Down
6 changes: 5 additions & 1 deletion src/tedi/components/form/choice-input.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface ChoiceInputProps {
/**
* Label text
*/
label: string | React.ReactNode;
label: React.ReactNode;
/**
* Additional classes.
*/
Expand Down Expand Up @@ -61,4 +61,8 @@ export interface ChoiceInputProps {
* Whether the input is marked as invalid.
*/
invalid?: boolean;
/**
* Whether the input is marked as required.
*/
required?: boolean;
}
17 changes: 17 additions & 0 deletions src/tedi/components/form/form-label/form-label.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,21 @@ describe('FormLabel component', () => {

expect(getByText('Form Label Text')).toBeInTheDocument();
});

it('renders a ReactNode label correctly', () => {
const { getByTestId } = render(
<FormLabel
id="test-id"
label={
<span data-testid="custom-label">
Custom <strong>Node</strong>
</span>
}
/>
);

const customLabel = getByTestId('custom-label');
expect(customLabel).toBeInTheDocument();
expect(customLabel).toHaveTextContent('Custom Node');
});
});
2 changes: 1 addition & 1 deletion src/tedi/components/form/form-label/form-label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface FormLabelProps {
/**
* The text content of the label that describes the input field.
*/
label: string;
label: React.ReactNode;
/**
* Controls the visibility of the label.
* Use `true` to hide the label visually while maintaining its space in the layout,
Expand Down
6 changes: 6 additions & 0 deletions src/tedi/components/form/radio/radio.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,10 @@ describe('Radio component', () => {

expect(label.click).toHaveBeenCalled();
});

it('renders required indicator when required prop is true', () => {
render(<Radio id="radio-id" label="Radio Label" value="radio-value" name="radio-group" required />);

expect(screen.getByText('*')).toBeInTheDocument();
});
});
8 changes: 8 additions & 0 deletions src/tedi/components/form/radio/radio.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ export const States = () => {
/>
</Col>
</Row>
<Row>
<Col md={3}>
<Text modifiers="bold">Required</Text>
</Col>
<Col>
<Radio id="radio-required" label="Text" name="radio-required" value="radio" required />
</Col>
</Row>
</VerticalSpacing>
</Col>
</Row>
Expand Down
16 changes: 7 additions & 9 deletions src/tedi/components/form/radio/radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const Radio = (props: RadioProps): JSX.Element => {
tooltip,
size = 'default',
invalid,
required,
...rest
} = props;

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

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

return (
<div data-name="radio" {...rest}>
Expand Down Expand Up @@ -78,8 +79,8 @@ export const Radio = (props: RadioProps): JSX.Element => {
/>
</div>
</Col>
<Col>
{label && typeof label === 'string' ? (
{label && (
<Col>
<FormLabel
ref={labelRef}
className={LabelBEM}
Expand All @@ -88,13 +89,10 @@ export const Radio = (props: RadioProps): JSX.Element => {
hideLabel={hideLabel}
label={label}
tooltip={tooltip}
required={required}
/>
) : (
<label ref={labelRef} htmlFor={id} className={LabelBEM} data-testid="radio-label">
{label}
</label>
)}
</Col>
</Col>
)}
</Row>

{helper && (
Expand Down
Loading