session-desktop/ts/components/session/settings/SessionSettingListItem.tsx

131 lines
3.6 KiB
TypeScript
Raw Normal View History

2020-01-09 01:38:48 +01:00
import React from 'react';
2020-01-14 07:42:04 +01:00
import classNames from 'classnames';
2020-01-16 02:08:31 +01:00
import Slider from 'rc-slider';
2020-01-09 01:38:48 +01:00
import { SessionToggle } from '../SessionToggle';
2020-01-15 02:26:14 +01:00
import { SessionButton } from '../SessionButton';
2020-01-14 03:28:31 +01:00
import { SessionSettingType } from './SessionSettings';
2020-01-15 02:26:14 +01:00
import { SessionRadioGroup } from '../SessionRadioGroup';
import { SessionConfirmDialogProps } from '../SessionConfirm';
2020-01-14 07:42:04 +01:00
2020-01-09 02:15:12 +01:00
interface Props {
title?: string;
2020-01-09 02:15:12 +01:00
description?: string;
type: SessionSettingType | undefined;
2020-01-14 03:28:31 +01:00
value: any;
options?: Array<any>;
2020-01-09 02:15:12 +01:00
onClick?: any;
2020-01-16 02:08:31 +01:00
onSliderChange?: any;
2020-01-15 02:26:14 +01:00
content: any;
confirmationDialogParams?: SessionConfirmDialogProps;
// for updating modal in redux
2021-06-17 08:51:59 +02:00
updateConfirmModal?: any;
2020-01-09 02:15:12 +01:00
}
2020-01-09 01:38:48 +01:00
2020-01-16 02:08:31 +01:00
interface State {
sliderValue: number | null;
}
export class SessionSettingListItem extends React.Component<Props, State> {
2020-01-14 07:42:04 +01:00
public static defaultProps = {
inline: true,
2020-01-15 03:49:07 +01:00
};
2020-01-09 01:38:48 +01:00
public constructor(props: Props) {
super(props);
2020-01-16 02:08:31 +01:00
this.state = {
sliderValue: null,
};
2020-01-14 03:28:31 +01:00
this.handleClick = this.handleClick.bind(this);
2020-01-09 01:38:48 +01:00
}
public render(): JSX.Element {
2020-01-15 03:49:07 +01:00
const { title, description, type, value, content } = this.props;
2020-01-20 03:03:50 +01:00
const inline =
2021-04-22 10:03:58 +02:00
!!type && ![SessionSettingType.Options, SessionSettingType.Slider].includes(type);
2020-01-15 02:26:14 +01:00
2020-01-16 02:08:31 +01:00
const currentSliderValue =
type === SessionSettingType.Slider && (this.state.sliderValue || value);
2020-01-09 01:38:48 +01:00
return (
2020-01-15 02:26:14 +01:00
<div className={classNames('session-settings-item', inline && 'inline')}>
2020-01-15 03:49:07 +01:00
<div className="session-settings-item__info">
<div className="session-settings-item__title">{title}</div>
2020-01-09 01:38:48 +01:00
2021-04-22 10:03:58 +02:00
{description && <div className="session-settings-item__description">{description}</div>}
2020-01-09 01:38:48 +01:00
</div>
2020-01-15 02:26:14 +01:00
<div className="session-settings-item__content">
{type === SessionSettingType.Toggle && (
<div className="session-settings-item__selection">
2020-01-15 03:49:07 +01:00
<SessionToggle
active={Boolean(value)}
onClick={this.handleClick}
2020-02-03 04:19:37 +01:00
confirmationDialogParams={this.props.confirmationDialogParams}
updateConfirmModal={this.props.updateConfirmModal}
2020-01-15 03:49:07 +01:00
/>
2020-01-15 02:26:14 +01:00
</div>
)}
2020-01-09 02:15:12 +01:00
2020-01-15 02:26:14 +01:00
{type === SessionSettingType.Button && (
<SessionButton
text={content.buttonText}
buttonColor={content.buttonColor}
onClick={this.handleClick}
/>
)}
2020-01-14 07:42:04 +01:00
2020-01-15 02:26:14 +01:00
{type === SessionSettingType.Options && (
<SessionRadioGroup
initialItem={content.options.initalItem}
2020-01-15 02:26:14 +01:00
group={content.options.group}
items={content.options.items}
onClick={(selectedRadioValue: string) => {
this.props.onClick(selectedRadioValue);
}}
2020-01-15 02:26:14 +01:00
/>
)}
2020-01-16 02:08:31 +01:00
{type === SessionSettingType.Slider && (
<div className="slider-wrapper">
<Slider
2020-01-20 04:36:46 +01:00
dots={true}
step={content.step}
min={content.min}
max={content.max}
defaultValue={currentSliderValue}
2020-03-09 13:41:02 +01:00
onAfterChange={sliderValue => {
2020-01-20 04:36:46 +01:00
this.handleSlider(sliderValue);
}}
2020-01-16 02:08:31 +01:00
/>
2020-02-26 03:30:56 +01:00
2020-01-16 02:08:31 +01:00
<div className="slider-info">
<p>{content.info(currentSliderValue)}</p>
2020-01-16 02:08:31 +01:00
</div>
</div>
)}
2020-01-15 02:26:14 +01:00
</div>
2020-01-09 01:38:48 +01:00
</div>
);
}
2020-01-14 03:28:31 +01:00
private handleClick() {
if (this.props.onClick) {
this.props.onClick();
}
2020-01-14 03:28:31 +01:00
}
2020-01-16 02:08:31 +01:00
private handleSlider(value: any) {
2020-01-20 04:36:46 +01:00
if (this.props.onSliderChange) {
this.props.onSliderChange(value);
}
2020-01-16 02:08:31 +01:00
this.setState({
sliderValue: value,
});
}
2020-01-09 01:38:48 +01:00
}