<commit_before>import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
// import { faTrashAlt } from '@fortawesome/free-regular-svg-icons/faTrashAlt';
import { faMinus } from '@fortawesome/free-solid-svg-icons/faMinus';
import { faPlus } from '@fortawesome/free-solid-svg-icons/faPlus';

import ControlButton from './ControlButton';
import AmountInput from './AmountInput';

export default class AmountControl extends PureComponent {
    static propTypes = {
        buttonText: PropTypes.string.isRequired,
        amount: PropTypes.number,
        onChange: PropTypes.func,
        onInput: PropTypes.func,
        onAdd: PropTypes.func,
        onRemove: PropTypes.func,
        disabled: PropTypes.bool,
        disableInput: PropTypes.bool,
        disableAdd: PropTypes.bool,
        disableRemove: PropTypes.bool,
        className: PropTypes.string,
        autoInput: PropTypes.bool,
        buttonFormatHandler: PropTypes.func,
        showInput: PropTypes.bool,
        icon: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
        plusIcon: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
        minusIcon: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
        removeIcon: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
        removeColor: PropTypes.string,
        addColor: PropTypes.string,
        iconColor: PropTypes.string,
        equalize: PropTypes.string,
        focusOnClick: PropTypes.bool,
        contentWidth: PropTypes.number,
        stopPropagation: PropTypes.bool,
    };

    static defaultProps = {
        amount: 0,
        onChange: null,
        onInput: null,
        onAdd: null,
        onRemove: null,
        disabled: false,
        disableInput: false,
        disableAdd: false,
        disableRemove: false,
        className: '',
        autoInput: false,
        buttonFormatHandler: undefined,
        showInput: false,
        icon: null,
        removeColor: null,
        addColor: null,
        iconColor: null,
        equalize: null,
        focusOnClick: true,
        contentWidth: null,
        stopPropagation: false,
        plusIcon: faPlus,
        minusIcon: faMinus,
        removeIcon: faMinus,
    };

    constructor(props) {
        super(props);

        this.state = {
            tempAmount: props.amount,
            tempValue: props.amount,
            showInput: false,
        };

        this.setInput = this.setInput.bind(this);
    }

    componentWillReceiveProps(nextProps) {
        this.setState({
            tempAmount: nextProps.amount,
            tempValue: nextProps.amount,
        });
    }

    onInput = (value) => {
        const { onInput } = this.props;
        let numberValue = 0;

        if (chayns.utils.isNumber(value)) {
            numberValue = value;
        }

        this.setState({
            tempAmount: numberValue,
            tempValue: value,
        });

        if (onInput && (numberValue || numberValue >= 0)) {
            onInput(numberValue);
        }
    };

    getRemoveIcon() {
        const {
            amount, icon, removeIcon, minusIcon,
        } = this.props;
        const { tempAmount } = this.state;

        if (icon && !tempAmount) {
            return icon;
        }

        if (tempAmount > 1 || amount > 1) {
            return minusIcon;
        }

        return removeIcon;
    }

    addItem = () => {
        const { amount, onAdd } = this.props;

        if (onAdd) onAdd();

        this.changeAmount(amount + 1);
    };

    removeItem = () => {
        const { amount, onRemove } = this.props;

        if (onRemove) onRemove();

        if (amount > 0) {
            this.changeAmount(amount - 1);
        } else {
            this.addItem();
        }
    };

    changeAmount = (amount) => {
        const { onChange, onInput } = this.props;

        if (onChange) {
            onChange(amount);
            this.setInput(false);
        }

        if (onInput) {
            onInput(amount);
        }
    };

    setInput = (value) => {
        this.setState({ showInput: value });
    };

    render() {
        const {
            amount,
            buttonText,
            disabled,
            disableInput,
            disableAdd,
            disableRemove,
            className,
            autoInput,
            buttonFormatHandler,<commit_msg>:bug: Prevent insert higher amounts while disableAdd is set<commit_after>import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
// import { faTrashAlt } from '@fortawesome/free-regular-svg-icons/faTrashAlt';
import { faMinus } from '@fortawesome/free-solid-svg-icons/faMinus';
import { faPlus } from '@fortawesome/free-solid-svg-icons/faPlus';

import ControlButton from './ControlButton';
import AmountInput from './AmountInput';

export default class AmountControl extends PureComponent {
    static propTypes = {
        buttonText: PropTypes.string.isRequired,
        amount: PropTypes.number,
        onChange: PropTypes.func,
        onInput: PropTypes.func,
        onAdd: PropTypes.func,
        onRemove: PropTypes.func,
        disabled: PropTypes.bool,
        disableInput: PropTypes.bool,
        disableAdd: PropTypes.bool,
        disableRemove: PropTypes.bool,
        className: PropTypes.string,
        autoInput: PropTypes.bool,
        buttonFormatHandler: PropTypes.func,
        showInput: PropTypes.bool,
        icon: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
        plusIcon: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
        minusIcon: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
        removeIcon: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
        removeColor: PropTypes.string,
        addColor: PropTypes.string,
        iconColor: PropTypes.string,
        equalize: PropTypes.string,
        focusOnClick: PropTypes.bool,
        contentWidth: PropTypes.number,
        stopPropagation: PropTypes.bool,
    };

    static defaultProps = {
        amount: 0,
        onChange: null,
        onInput: null,
        onAdd: null,
        onRemove: null,
        disabled: false,
        disableInput: false,
        disableAdd: false,
        disableRemove: false,
        className: '',
        autoInput: false,
        buttonFormatHandler: undefined,
        showInput: false,
        icon: null,
        removeColor: null,
        addColor: null,
        iconColor: null,
        equalize: null,
        focusOnClick: true,
        contentWidth: null,
        stopPropagation: false,
        plusIcon: faPlus,
        minusIcon: faMinus,
        removeIcon: faMinus,
    };

    constructor(props) {
        super(props);

        this.state = {
            tempAmount: props.amount,
            tempValue: props.amount,
            showInput: false,
        };

        this.setInput = this.setInput.bind(this);
    }

    componentWillReceiveProps(nextProps) {
        this.setState({
            tempAmount: nextProps.amount,
            tempValue: nextProps.amount,
        });
    }

    onInput = (value) => {
        const { onInput } = this.props;
        let numberValue = 0;

        if (chayns.utils.isNumber(value)) {
            numberValue = value;
        }

        this.setState({
            tempAmount: numberValue,
            tempValue: value,
        });

        if (onInput && (numberValue || numberValue >= 0)) {
            onInput(numberValue);
        }
    };

    getRemoveIcon() {
        const {
            amount, icon, removeIcon, minusIcon,
        } = this.props;
        const { tempAmount } = this.state;

        if (icon && !tempAmount) {
            return icon;
        }

        if (tempAmount > 1 || amount > 1) {
            return minusIcon;
        }

        return removeIcon;
    }

    addItem = () => {
        const { amount, onAdd } = this.props;

        if (onAdd) onAdd();

        this.changeAmount(amount + 1);
    };

    removeItem = () => {
        const { amount, onRemove } = this.props;

        if (onRemove) onRemove();

        if (amount > 0) {
            this.changeAmount(amount - 1);
        } else {
            this.addItem();
        }
    };

    changeAmount = (amount) => {
        const {
            onChange,
            onInput,
            amount: oldAmount,
            disableAdd,
            disableRemove,
        } = this.props;

        if (onInput) {
            onInput(amount);
        }

        if (onChange) {
            if ((disableAdd && amount > oldAmount)
                || (disableRemove && amount < oldAmount)) {
                this.setState({
                    tempValue: oldAmount,
                });

                return;
            }

            onChange(amount);
            this.setInput(false);
        }
    };

    setInput = (value) => {
        this.setState({ showInput: value });
    };

    render() {
        const {
            amount,
            buttonText,
            disabled,
            disableInput,
            disableAdd,
            disableRemove,
            className,
            autoInput,
            buttonFormatHandler,
{'fraction_non_alphanumeric': 0.10539956803455723, 'fraction_numerical': 0.0017278617710583153, 'mean_word_length': 1.7953516450347118, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '978439', 'n_tokens_mistral': 2536, 'n_tokens_neox': 2528, 'n_words': 546}