2019-01-03 16:00:55 +03:00
|
|
|
import React, { Component } from 'react';
|
2020-09-10 17:10:53 +03:00
|
|
|
import { Base16Theme } from 'base16';
|
2019-01-03 16:00:55 +03:00
|
|
|
import createStyledComponent from '../utils/createStyledComponent';
|
|
|
|
import styles from './styles';
|
|
|
|
|
|
|
|
const SegmentedWrapper = createStyledComponent(styles);
|
|
|
|
|
2020-09-09 17:35:22 +03:00
|
|
|
export interface SegmentedControlProps {
|
|
|
|
values: string[];
|
|
|
|
selected?: string;
|
|
|
|
onClick: (value: string) => void;
|
|
|
|
disabled?: boolean;
|
2020-09-10 17:10:53 +03:00
|
|
|
theme?: Base16Theme;
|
2020-09-09 17:35:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class SegmentedControl extends Component<SegmentedControlProps> {
|
|
|
|
shouldComponentUpdate(nextProps: SegmentedControlProps) {
|
2019-01-10 21:51:14 +03:00
|
|
|
return (
|
|
|
|
nextProps.disabled !== this.props.disabled ||
|
|
|
|
nextProps.selected !== this.props.selected
|
|
|
|
);
|
2019-01-03 16:00:55 +03:00
|
|
|
}
|
|
|
|
|
2020-09-09 17:35:22 +03:00
|
|
|
onClick: React.MouseEventHandler<HTMLButtonElement> = (e) => {
|
|
|
|
this.props.onClick(e.currentTarget.value);
|
2019-01-03 16:00:55 +03:00
|
|
|
};
|
|
|
|
|
2020-09-09 17:35:22 +03:00
|
|
|
onMouseUp: React.MouseEventHandler<HTMLButtonElement> = (e) => {
|
|
|
|
e.currentTarget.blur();
|
2019-01-03 16:00:55 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { values, selected } = this.props;
|
|
|
|
return (
|
|
|
|
<SegmentedWrapper disabled={this.props.disabled} theme={this.props.theme}>
|
2020-08-08 23:26:39 +03:00
|
|
|
{values.map((button) => (
|
2019-01-03 16:00:55 +03:00
|
|
|
<button
|
|
|
|
key={button}
|
|
|
|
value={button}
|
|
|
|
data-selected={button === selected ? true : undefined}
|
|
|
|
onMouseUp={this.onMouseUp}
|
|
|
|
onClick={this.onClick}
|
|
|
|
>
|
|
|
|
{button}
|
|
|
|
</button>
|
|
|
|
))}
|
|
|
|
</SegmentedWrapper>
|
|
|
|
);
|
|
|
|
}
|
2020-09-09 17:35:22 +03:00
|
|
|
}
|