Skip to content

Commit

Permalink
fix: radio group prop types
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaofan2406 committed May 21, 2024
1 parent 3f36779 commit 2560d5d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
1 change: 0 additions & 1 deletion src/components/Radio/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as React from 'react';
export type RadioValue = string | number;

export interface RadioProps {
id?: string;
className?: string;
name?: string;
label?: React.ReactNode;
Expand Down
35 changes: 15 additions & 20 deletions src/components/Radio/index.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import classnames from 'classnames';
import React from 'react';
import _ from 'lodash';
import PropTypes from 'prop-types';
import { expandDts } from '../../utils';
import invariant from '../../invariant';
Expand All @@ -9,13 +10,12 @@ import './styles.css';
const SELECTION_KEYS = ['Enter', ' '];

const Radio = ({
id,
value,
name,
className,
label,
disabled,
checked: checkedProp,
disabled = false,
checked,
onChange,
text,
icon,
Expand All @@ -25,36 +25,39 @@ const Radio = ({
}) => {
const { onChange: onGroupChange, value: groupValue, variant = 'default', name: radioName = name } = useRadioGroup();

const hasGroupValue = !_.isNil(groupValue);
const hasItemValue = !_.isNil(checked);

invariant(!inline, 'Radio inline prop has been deprecated.');
invariant(!(onChange && onGroupChange), 'Radio should not have onChange when used in a RadioGroup');
invariant(!((icon || text) && variant !== 'box'), 'Radio with icon or text must use box variant.');
invariant(!(groupValue && checkedProp), 'Radio checked state is handled by RadioGroup.');
invariant(!(hasGroupValue && hasItemValue), 'Radio checked state is handled by RadioGroup.');

const checked = groupValue ? value === groupValue : checkedProp;
const isChecked = hasGroupValue ? value === groupValue : !!checked;

const iconClassName = classnames(['aui--radio-input-icon', { checked, disabled }]);
const iconClassName = classnames(['aui--radio-input-icon', { checked: isChecked, disabled }]);

const handleChange = () => {
onGroupChange?.(value);
onChange?.(value);
};

const tabIndex = checked || groupValue?.length === 0 ? 0 : -1;
const tabIndex = isChecked || groupValue?.length === 0 ? 0 : -1;

return (
<div
{...rest}
data-testid="radio-wrapper"
role="radio"
aria-disabled={disabled ? 'true' : undefined}
aria-checked={checked ? 'true' : 'false'}
aria-checked={isChecked ? 'true' : 'false'}
className={classnames(
'aui--radio',
{
'aui--radio-box': variant === 'box',
'aui--radio-default': variant === 'default',
'is-reverse': icon,
'is-selected': checked,
'is-selected': isChecked,
'is-disabled': disabled,
'has-text': text != null,
},
Expand All @@ -63,26 +66,25 @@ const Radio = ({
data-aui-value={value}
tabIndex={!groupValue ? 0 : tabIndex}
onKeyDown={(event) => {
if (SELECTION_KEYS.includes(event.key) && !checked) {
if (SELECTION_KEYS.includes(event.key) && !isChecked) {
event.preventDefault();
handleChange();
}
}}
{...expandDts(dts)}
>
<label className="aui--radio-label-container" htmlFor={id}>
<label className="aui--radio-label-container">
<div className="aui--radio-input-container">
<span className={iconClassName} />
<input
className="aui--radio-input"
data-testid="radio-input"
type="radio"
name={radioName}
checked={checked}
checked={isChecked}
disabled={disabled}
onChange={handleChange}
value={value}
id={id}
/>
</div>

Expand All @@ -101,7 +103,6 @@ const Radio = ({
};

Radio.propTypes = {
id: PropTypes.string,
className: PropTypes.string,
name: PropTypes.string,
label: PropTypes.node,
Expand Down Expand Up @@ -131,10 +132,4 @@ Radio.propTypes = {
inline: PropTypes.bool,
};

Radio.defaultProps = {
dts: '',
disabled: false,
checked: false,
};

export default Radio;
4 changes: 3 additions & 1 deletion src/components/RadioGroup/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as React from 'react';

export type RadioGroupValue = string | number;

export type RadioGroupOrientation = 'vertical' | 'horizontal';

export type RadioGroupVariant = 'default' | 'box';

export interface RadioGroupProps {
value: string;
value: RadioGroupValue;
name: string;
onChange: (...args: any[]) => any;
orientation?: RadioGroupOrientation;
Expand Down
2 changes: 1 addition & 1 deletion src/components/RadioGroup/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const RadioGroup = ({
};

RadioGroup.propTypes = {
value: PropTypes.string.isRequired,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
name: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
orientation: PropTypes.oneOf(['vertical', 'horizontal']),
Expand Down

0 comments on commit 2560d5d

Please sign in to comment.