1 | import * as React from 'react';
|
2 |
|
3 | import { Wrapper, SelectStyled, Label } from './Select.styled';
|
4 | import Icons from '../Icons';
|
5 |
|
6 | export interface IProps {
|
7 | options?: any;
|
8 | label: string;
|
9 | onChange: (e?: any) => any;
|
10 | }
|
11 |
|
12 | export default class Select extends React.PureComponent<IProps> {
|
13 | state = {
|
14 | selectedValue: '',
|
15 | degree: 180,
|
16 | }
|
17 |
|
18 | renderHTMLOptions = () => this.props.options
|
19 | .map((item: { value: string; label: string }) => (
|
20 | <option value={item.value}>{item.label}</option>
|
21 | ))
|
22 |
|
23 | handleSelectChange = ({ currentTarget: { value } }: React.ChangeEvent<HTMLSelectElement>) => {
|
24 | this.setState({ selectedValue: value });
|
25 |
|
26 | if (value !== 'hide') {
|
27 | this.props.onChange(value);
|
28 | return;
|
29 | }
|
30 |
|
31 | this.props.onChange(undefined);
|
32 | };
|
33 |
|
34 | isInvalid = () => this.state.selectedValue === 'hide';
|
35 |
|
36 | render () {
|
37 | const { selectedValue, degree } = this.state;
|
38 | const { label } = this.props;
|
39 |
|
40 | return (
|
41 | <Wrapper>
|
42 | <SelectStyled
|
43 | onChange={this.handleSelectChange}
|
44 | onBlur={() => this.setState({ degree: 180 })}
|
45 | >
|
46 | <option value='hide'>{label}</option>
|
47 | {this.renderHTMLOptions()}
|
48 | </SelectStyled>
|
49 | <Label active={(!this.isInvalid() && selectedValue)}>{label}</Label>
|
50 | <Icons.Arrow degree={degree} width={20} height={20} />
|
51 | </Wrapper>
|
52 | );
|
53 | }
|
54 | }
|