import React from 'react';
import { Select } from 'antd';
import { any, array, func, object, string } from 'prop-types';


export default class TuonSelect extends React.PureComponent {

    static propTypes = {
        data: array,
        onChange: func,
        style: object,
        value: any,
        placeholder: string,
    };
    static defaultProps = {
        data: [],
        style: {},
    };

    static getDerivedStateFromProps(nextProps) {
        if ('value' in nextProps) {
            return {
                value:nextProps.value
            };
        }
        return null;
    }

    constructor(props) {
        super(props);
        this.state = {
            value: props.value,
        };
    }


    triggerChange = value => {
        if (!('value' in this.props)) {
            this.setState({ value });
        }
        this.props.onChange && this.props.onChange(value);
    };

    render() {
        let { data, style, placeholder } = this.props;
        return (
            <Select onChange={this.triggerChange}
                    placeholder={placeholder}
                    value={this.state.value}
                    style={{ minWidth: 150, ...style }}>
                {data.map(item => <Select.Option key={item.id} value={item.value}>{item.name}</Select.Option>)}
            </Select>);
    }


}
