Skip to content

Commit

Permalink
feat: add types to CheckboxGroup component (#16643)
Browse files Browse the repository at this point in the history
Co-authored-by: Gururaj J <[email protected]>
  • Loading branch information
2nikhiltom and Gururajj77 authored Jun 6, 2024
1 parent d5e99fc commit 93740ff
Showing 1 changed file with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,35 @@
*/

import PropTypes from 'prop-types';
import React from 'react';
import React, { ReactNode } from 'react';
import cx from 'classnames';
import { usePrefix } from '../../internal/usePrefix';
import { WarningFilled, WarningAltFilled } from '@carbon/icons-react';
import setupGetInstanceId from '../../tools/setupGetInstanceId';

const getInstanceId = setupGetInstanceId();

function CheckboxGroup({
export interface CheckboxGroupProps {
children?: ReactNode;
className?: string;
helperText?: ReactNode;
invalid?: boolean;
invalidText?: ReactNode;
legendId?: ReactNode;
orientation?: 'horizontal' | 'vertical';
legendText: ReactNode;
readOnly?: boolean;
slug?: ReactNode;
warn?: boolean;
warnText?: ReactNode;
}

export interface CustomType {
size: string;
kind: string;
}

const CheckboxGroup: React.FC<CheckboxGroupProps> = ({
children,
className,
helperText,
Expand All @@ -28,7 +48,7 @@ function CheckboxGroup({
slug,
orientation = 'vertical',
...rest
}) {
}) => {
const prefix = usePrefix();

const showWarning = !readOnly && !invalid && warn;
Expand Down Expand Up @@ -56,20 +76,21 @@ function CheckboxGroup({

// Slug is always size `mini`
let normalizedSlug;
if (slug && slug['type']?.displayName === 'Slug') {
if (
React.isValidElement(slug) &&
(slug['type'] as any)?.displayName === 'Slug'
) {
normalizedSlug = React.cloneElement(slug, {
size: 'mini',
kind: 'default',
});
} as CustomType);
}

return (
<fieldset
className={fieldsetClasses}
data-invalid={invalid ? true : undefined}
aria-labelledby={rest['aria-labelledby'] || legendId}
aria-readonly={readOnly}
orientation="vertical"
aria-describedby={!invalid && !warn && helper ? helperId : undefined}
{...rest}>
<legend
Expand Down Expand Up @@ -98,7 +119,7 @@ function CheckboxGroup({
{showHelper && helper}
</fieldset>
);
}
};

CheckboxGroup.propTypes = {
/**
Expand Down Expand Up @@ -141,7 +162,6 @@ CheckboxGroup.propTypes = {
* Provide the orientation for how the checkbox should be displayed
*/
orientation: PropTypes.oneOf(['horizontal', 'vertical']),

/**
* Whether the CheckboxGroup should be read-only
*/
Expand Down

0 comments on commit 93740ff

Please sign in to comment.