[MM-56297] Convert ./components/widgets/settings/radio_setting.tsx from Class Component to Function Component (#25770)
* [MM-56297] Convert `./components/widgets/settings/radio_setting.tsx` from Class Component to Function Component * fix: check-types issue
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
7dcf5f85a5
Коммит
dadc752212
@@ -1,13 +1,14 @@
|
|||||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||||
// See LICENSE.txt for license information.
|
// See LICENSE.txt for license information.
|
||||||
|
|
||||||
import React from 'react';
|
import React, {memo, useCallback} from 'react';
|
||||||
|
import type {ChangeEventHandler} from 'react';
|
||||||
|
|
||||||
import Setting from './setting';
|
import Setting from './setting';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
id: string;
|
id: string;
|
||||||
options: Array<{text: string; value: string}>;
|
options?: Array<{text: string; value: string}>;
|
||||||
label: React.ReactNode;
|
label: React.ReactNode;
|
||||||
onChange(name: string, value: any): void;
|
onChange(name: string, value: any): void;
|
||||||
value?: string;
|
value?: string;
|
||||||
@@ -17,48 +18,51 @@ type Props = {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class RadioSetting extends React.PureComponent<Props> {
|
const RadioSetting = ({
|
||||||
public static defaultProps: Partial<Props> = {
|
labelClassName = '',
|
||||||
labelClassName: '',
|
inputClassName = '',
|
||||||
inputClassName: '',
|
options = [],
|
||||||
options: [],
|
onChange,
|
||||||
};
|
id,
|
||||||
|
label,
|
||||||
|
helpText,
|
||||||
|
value,
|
||||||
|
}: Props) => {
|
||||||
|
const handleChange: ChangeEventHandler<HTMLInputElement> = useCallback((e) => {
|
||||||
|
onChange(id, e.target.value);
|
||||||
|
}, [onChange, id]);
|
||||||
|
|
||||||
private handleChange: React.ChangeEventHandler<HTMLInputElement> = (e) => {
|
return (
|
||||||
this.props.onChange(this.props.id, e.target.value);
|
<Setting
|
||||||
};
|
label={label}
|
||||||
|
labelClassName={labelClassName}
|
||||||
|
inputClassName={inputClassName}
|
||||||
|
helpText={helpText}
|
||||||
|
inputId={id}
|
||||||
|
>
|
||||||
|
{
|
||||||
|
options.map(({value: option, text}) => {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className='radio'
|
||||||
|
key={option}
|
||||||
|
>
|
||||||
|
<label>
|
||||||
|
<input
|
||||||
|
type='radio'
|
||||||
|
value={option}
|
||||||
|
name={id}
|
||||||
|
checked={option === value}
|
||||||
|
onChange={handleChange}
|
||||||
|
/>
|
||||||
|
{text}
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
</Setting>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
public render(): JSX.Element {
|
export default memo(RadioSetting);
|
||||||
return (
|
|
||||||
<Setting
|
|
||||||
label={this.props.label}
|
|
||||||
labelClassName={this.props.labelClassName}
|
|
||||||
inputClassName={this.props.inputClassName}
|
|
||||||
helpText={this.props.helpText}
|
|
||||||
inputId={this.props.id}
|
|
||||||
>
|
|
||||||
{
|
|
||||||
this.props.options.map(({value, text}) => {
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className='radio'
|
|
||||||
key={value}
|
|
||||||
>
|
|
||||||
<label>
|
|
||||||
<input
|
|
||||||
type='radio'
|
|
||||||
value={value}
|
|
||||||
name={this.props.id}
|
|
||||||
checked={value === this.props.value}
|
|
||||||
onChange={this.handleChange}
|
|
||||||
/>
|
|
||||||
{text}
|
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
</Setting>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user