import PropTypes from 'prop-types';
import React from 'react';

export const withFixedChangeHandler = (Component, options = {}) => {
    const { getValueFromArgs = evt => evt.target.value } = options;
    const FixChangeHandler = props => {
        const fixedProps = {
            ...props,
        };
        if (fixedProps.onChange) {
            fixedProps.onChange = (...args) =>
                props.onChange && getValueFromArgs(...args) !== props.value && props.onChange(...args);
        }
        return <Component {...fixedProps} />;
    };
    FixChangeHandler.propTypes = {
        onChange: PropTypes.func,
        value: PropTypes.node,
    };
    return FixChangeHandler;
};
