mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
[MM-56008] Convert ./components/widgets/settings/bool_setting.tsx
to functional component (#25614)
* Updated bool_setting Converted bool_setting.tsx to a function component. and Updated the test file. * Delete package-lock.json * Update bool_setting.tsx Updated the code to reflect the requested changes * Fix lint and types --------- Co-authored-by: Daniel Espino García <larkox@gmail.com> Co-authored-by: Mattermost Build <build@mattermost.com>
This commit is contained in:
parent
f8fc02791b
commit
733fe8b902
@ -204,7 +204,7 @@ export default class DialogElement extends React.PureComponent<Props, State> {
|
|||||||
label={displayNameContent}
|
label={displayNameContent}
|
||||||
value={boolValue || false}
|
value={boolValue || false}
|
||||||
helpText={helpTextContent}
|
helpText={helpTextContent}
|
||||||
placeholder={placeholder}
|
placeholder={placeholder || ''}
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
@ -8,12 +8,15 @@ import BoolSetting from './bool_setting';
|
|||||||
|
|
||||||
describe('components/widgets/settings/BoolSetting', () => {
|
describe('components/widgets/settings/BoolSetting', () => {
|
||||||
test('render component with required props', () => {
|
test('render component with required props', () => {
|
||||||
|
const onChange = jest.fn();
|
||||||
const wrapper = shallow(
|
const wrapper = shallow(
|
||||||
|
|
||||||
<BoolSetting
|
<BoolSetting
|
||||||
id='string.id'
|
id='string.id'
|
||||||
label='some label'
|
label='some label'
|
||||||
value={true}
|
value={true}
|
||||||
placeholder='Text aligned with checkbox'
|
placeholder='Text aligned with checkbox'
|
||||||
|
onChange={onChange}
|
||||||
/>,
|
/>,
|
||||||
);
|
);
|
||||||
expect(wrapper).toMatchInlineSnapshot(`
|
expect(wrapper).toMatchInlineSnapshot(`
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
// 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 type {ChangeEvent} from 'react';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
import Setting from './setting';
|
import Setting from './setting';
|
||||||
@ -8,49 +9,55 @@ import Setting from './setting';
|
|||||||
type Props = {
|
type Props = {
|
||||||
id: string;
|
id: string;
|
||||||
label: React.ReactNode;
|
label: React.ReactNode;
|
||||||
labelClassName: string;
|
labelClassName?: string;
|
||||||
helpText?: React.ReactNode;
|
helpText?: React.ReactNode;
|
||||||
placeholder: string;
|
placeholder: string;
|
||||||
value: boolean;
|
value: boolean;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
inputClassName: string;
|
inputClassName?: string;
|
||||||
onChange(name: string, value: any): void; // value is any since onChange is a common func for inputs and checkboxes
|
onChange(name: string, value: any): void; // value is any since onChange is a common func for inputs and checkboxes
|
||||||
autoFocus?: boolean;
|
autoFocus?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class BoolSetting extends React.PureComponent<Props> {
|
const BoolSetting = ({
|
||||||
public static defaultProps: Partial<Props> = {
|
id,
|
||||||
labelClassName: '',
|
label,
|
||||||
inputClassName: '',
|
labelClassName = '',
|
||||||
|
helpText,
|
||||||
|
placeholder,
|
||||||
|
value,
|
||||||
|
disabled,
|
||||||
|
inputClassName = '',
|
||||||
|
onChange,
|
||||||
|
autoFocus,
|
||||||
|
}: Props) => {
|
||||||
|
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
|
||||||
|
onChange(id, e.target.checked);
|
||||||
};
|
};
|
||||||
|
|
||||||
private handleChange: React.ChangeEventHandler<HTMLInputElement> = (e) => {
|
|
||||||
this.props.onChange(this.props.id, e.target.checked);
|
|
||||||
};
|
|
||||||
|
|
||||||
public render(): JSX.Element {
|
|
||||||
return (
|
return (
|
||||||
<Setting
|
<Setting
|
||||||
label={this.props.label}
|
label={label}
|
||||||
labelClassName={this.props.labelClassName}
|
labelClassName={labelClassName}
|
||||||
inputClassName={this.props.inputClassName}
|
inputClassName={inputClassName}
|
||||||
helpText={this.props.helpText}
|
helpText={helpText}
|
||||||
inputId={this.props.id}
|
inputId={id}
|
||||||
>
|
>
|
||||||
<div className='checkbox'>
|
<div className='checkbox'>
|
||||||
<label>
|
<label>
|
||||||
<input
|
<input
|
||||||
id={this.props.id}
|
id={id}
|
||||||
disabled={this.props.disabled}
|
disabled={disabled}
|
||||||
autoFocus={this.props.autoFocus}
|
autoFocus={autoFocus}
|
||||||
type='checkbox'
|
type='checkbox'
|
||||||
checked={this.props.value}
|
checked={value}
|
||||||
onChange={this.handleChange}
|
onChange={handleChange}
|
||||||
/>
|
/>
|
||||||
<span>{this.props.placeholder}</span>
|
<span>{placeholder}</span>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
</Setting>
|
</Setting>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
export default React.memo(BoolSetting);
|
||||||
|
Loading…
Reference in New Issue
Block a user