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}
|
||||
value={boolValue || false}
|
||||
helpText={helpTextContent}
|
||||
placeholder={placeholder}
|
||||
placeholder={placeholder || ''}
|
||||
onChange={onChange}
|
||||
/>
|
||||
);
|
||||
|
@ -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(`
|
||||
|
@ -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);
|
||||
};
|
||||
|
||||
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}
|
||||
label={label}
|
||||
labelClassName={labelClassName}
|
||||
inputClassName={inputClassName}
|
||||
helpText={helpText}
|
||||
inputId={id}
|
||||
>
|
||||
<div className='checkbox'>
|
||||
<label>
|
||||
<input
|
||||
id={this.props.id}
|
||||
disabled={this.props.disabled}
|
||||
autoFocus={this.props.autoFocus}
|
||||
id={id}
|
||||
disabled={disabled}
|
||||
autoFocus={autoFocus}
|
||||
type='checkbox'
|
||||
checked={this.props.value}
|
||||
onChange={this.handleChange}
|
||||
checked={value}
|
||||
onChange={handleChange}
|
||||
/>
|
||||
<span>{this.props.placeholder}</span>
|
||||
<span>{placeholder}</span>
|
||||
</label>
|
||||
</div>
|
||||
</Setting>
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default React.memo(BoolSetting);
|
||||
|
Loading…
Reference in New Issue
Block a user