[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:
Amir Helali 2024-04-08 07:54:32 -04:00 committed by GitHub
parent f8fc02791b
commit 733fe8b902
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 46 additions and 36 deletions

View File

@ -204,7 +204,7 @@ export default class DialogElement extends React.PureComponent<Props, State> {
label={displayNameContent}
value={boolValue || false}
helpText={helpTextContent}
placeholder={placeholder}
placeholder={placeholder || ''}
onChange={onChange}
/>
);

View File

@ -8,12 +8,15 @@ import BoolSetting from './bool_setting';
describe('components/widgets/settings/BoolSetting', () => {
test('render component with required props', () => {
const onChange = jest.fn();
const wrapper = shallow(
<BoolSetting
id='string.id'
label='some label'
value={true}
placeholder='Text aligned with checkbox'
onChange={onChange}
/>,
);
expect(wrapper).toMatchInlineSnapshot(`

View File

@ -1,6 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import type {ChangeEvent} from 'react';
import React from 'react';
import Setting from './setting';
@ -8,49 +9,55 @@ import Setting from './setting';
type Props = {
id: string;
label: React.ReactNode;
labelClassName: string;
labelClassName?: string;
helpText?: React.ReactNode;
placeholder: string;
value: 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
autoFocus?: boolean;
}
export default class BoolSetting extends React.PureComponent<Props> {
public static defaultProps: Partial<Props> = {
labelClassName: '',
inputClassName: '',
const BoolSetting = ({
id,
label,
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);
};
return (
<Setting
label={label}
labelClassName={labelClassName}
inputClassName={inputClassName}
helpText={helpText}
inputId={id}
>
<div className='checkbox'>
<label>
<input
id={id}
disabled={disabled}
autoFocus={autoFocus}
type='checkbox'
checked={value}
onChange={handleChange}
/>
<span>{placeholder}</span>
</label>
</div>
</Setting>
);
};
public render(): JSX.Element {
return (
<Setting
label={this.props.label}
labelClassName={this.props.labelClassName}
inputClassName={this.props.inputClassName}
helpText={this.props.helpText}
inputId={this.props.id}
>
<div className='checkbox'>
<label>
<input
id={this.props.id}
disabled={this.props.disabled}
autoFocus={this.props.autoFocus}
type='checkbox'
checked={this.props.value}
onChange={this.handleChange}
/>
<span>{this.props.placeholder}</span>
</label>
</div>
</Setting>
);
}
}
export default React.memo(BoolSetting);