-
Notifications
You must be signed in to change notification settings - Fork 87
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
Changes from all commits
4fb9a1a
41809d5
a1c0065
48dd782
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
return this.props.name | ||
? React.cloneElement(radio, { | ||
name: this.props.name, | ||
|
@@ -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( | ||
|
@@ -60,7 +72,6 @@ export default class RadioGroup extends React.Component { | |
: undefined | ||
: undefined; | ||
|
||
delete props.onChange; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'> | ||
|
@@ -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; |
There was a problem hiding this comment.
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