import React from 'react';
import { EditCellProps, EditCellState } from './interface';

export default class EditCell extends React.Component {
    static getDerivedStateFromProps(nextProps: EditCellProps, prevState: EditCellState) {
        const { prevProps } = prevState;
        let nextState: EditCellState = {
            ...prevState,
            prevProps: nextProps,
        };
        if (JSON.stringify(prevProps.columnAttrObj) !== JSON.stringify(nextProps.columnAttrObj)) {
            nextState.columnAttrObj = nextProps.columnAttrObj;
        }
        return nextState;
    }
    constructor(props: EditCellProps) {
        super(props);
        this.state = {
            itemConfig: {},
        };
    }

    handleChange = ({ newValue, originValue, emitFlag }: any) => {
        this.props.onChange(newValue, originValue, emitFlag);
    };

    render() {
        const Component = this.props.component;
        const { itemConfig } = this.state;
        return (
            <Component
                value={this.props.value}
                changeParams={this.handleChange}
                columnConfig={itemConfig}
                autoFocus
                style={{ width: '100%' }}
            />
        );
    }
}
