Skip to content

Rewrite in Typescript: RadioGroup #308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
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
43 changes: 19 additions & 24 deletions src/scripts/RadioGroup.js → src/scripts/RadioGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import FormElement from './FormElement';

export default class RadioGroup extends React.Component {
constructor() {
super();
export type RadioGroupProps = {
className?: string;
label?: string;
required?: boolean;
error?: any; // FIXME: should be FormElementProps.error
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix this after FormElement is converted to TypeScript

name?: string;
onChange?: (e: any, value: any) => void;
totalCols?: number;
cols?: number;
style?: object;
};

export class RadioGroup extends React.Component<RadioGroupProps, {}> {
static isFormElement = true;

constructor(props: Readonly<RadioGroupProps>) {
super(props);
this.renderControl = this.renderControl.bind(this);
}

onControlChange(value, e) {
onControlChange(value: any, e: any) {
if (this.props.onChange) {
this.props.onChange(e, value);
}
}

renderControl(radio) {
renderControl(radio: any) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't figure out how I can give more strict type annotation here and onChange prop.
It seems children are expected to only <Radio> component, but I didn't know how I express this.

return this.props.name
? React.cloneElement(radio, {
name: this.props.name,
Expand All @@ -35,6 +46,7 @@ export default class RadioGroup extends React.Component {
cols,
style,
children,
onChange, // eslint-disable-line @typescript-eslint/no-unused-vars
...props
} = this.props;
const grpClassNames = classnames(
Expand All @@ -60,7 +72,6 @@ export default class RadioGroup extends React.Component {
: undefined
: undefined;

delete props.onChange;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since TypeScript doesn't recognize that a certain property is deleted, a type error occured here

<fieldset className={grpClassNames} style={grpStyles} {...props}>

(related to this? microsoft/TypeScript#13783)

So I used destructuring assignment above.

return (
<fieldset className={grpClassNames} style={grpStyles} {...props}>
<legend className='slds-form-element__label slds-form-element__label--top'>
Expand All @@ -79,19 +90,3 @@ export default class RadioGroup extends React.Component {
);
}
}

RadioGroup.propTypes = {
className: PropTypes.string,
label: PropTypes.string,
required: PropTypes.bool,
error: FormElement.propTypes.error,
name: PropTypes.string,
onChange: PropTypes.func,
totalCols: PropTypes.number,
cols: PropTypes.number,
children: PropTypes.node,
/* eslint-disable react/forbid-prop-types */
style: PropTypes.object,
};

RadioGroup.isFormElement = true;
3 changes: 1 addition & 2 deletions src/scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import Form from './Form';
import FormElement from './FormElement';
import Input from './Input';
import Textarea from './Textarea';
import RadioGroup from './RadioGroup';
import Checkbox from './Checkbox';
import CheckboxGroup from './CheckboxGroup';
import Select, { Option } from './Select';
Expand Down Expand Up @@ -75,7 +74,6 @@ export {
FormElement,
Input,
Textarea,
RadioGroup,
Checkbox,
CheckboxGroup,
Select,
Expand Down Expand Up @@ -122,5 +120,6 @@ export * from './Button';
export * from './ButtonGroup';
export * from './MediaObject';
export * from './Radio';
export * from './RadioGroup';

export * from './ComponentSettings';