<commit_before>import React, {Fragment, PureComponent} from "react";
import PropTypes from "prop-types";
import counterpart from "counterpart";
import {Form, Input, Button} from "bitshares-ui-style-guide";
import ChainTypes from "../Utility/ChainTypes";
import BindToChainState from "../Utility/BindToChainState";
import Translate from "react-translate-component";
import {Map} from "immutable";

const AssetInputView = ({
    label,
    hasAction,
    onChange,
    placeholder,
    style,
    inputStyle,
    value,
    validateStatus,
    onAction,
    actionLabel,
    disableActionButton,
    help
}) => (
    <Fragment>
        <Form.Item
            colon={false}
            label={<Translate content={label} />}
            style={style}
            className={"asset-input" + (hasAction ? " with-action" : "")}
            validateStatus={validateStatus}
            hasFeedback
            help={help}
        >
            <Input
                value={value}
                onChange={onChange}
                style={inputStyle}
                placeholder={placeholder}
            />
        </Form.Item>
        {hasAction && (
            <Form.Item>
                <Button
                    type="primary"
                    disabled={disableActionButton}
                    onClick={onAction}
                >
                    <Translate content={actionLabel} />
                </Button>
            </Form.Item>
        )}
    </Fragment>
);

class ControlledAssetInput extends PureComponent {
    static propTypes = {
        asset: ChainTypes.ChainAsset // the selected asset
    };

    checkFound = prevAsset => {
        const {onFound, asset, resolved} = this.props;
        if (
            resolved &&
            asset !== undefined &&
            typeof onFound === "function" &&
            (Map.isMap(prevAsset) && Map.isMap(asset)
                ? prevAsset.get("id") !== asset.get("id")
                : prevAsset != asset)
        ) {
            onFound(Map.isMap(asset) ? asset : null);
        }
    };

    componentDidMount() {
        this.checkFound();
    }

    componentDidUpdate(prevProps) {
        this.checkFound(prevProps.asset);
    }

    handleChange = event => {
        const {onChange} = this.props;
        if (typeof onChange === "function")
            onChange(event.target.value.toUpperCase());
    };

    getValidateStatus = () => {
        const {validateStatus, asset, resolved} = this.props;
        return typeof validateStatus === "string"
            ? validateStatus
            : resolved
            ? Map.isMap(asset)
                ? "success"
                : "error"
            : "validating";
    };

    handleAction = () => {
        const {asset, onAction} = this.props;
        onAction(asset);
    };

    render() {
        const {
            label,
            placeholder,
            inputStyle,
            style,
            value,
            actionLabel,
            help,
            onAction
        } = this.props;
        const validateStatus = this.getValidateStatus();
        const hasAction = typeof onAction === "function";
        return (
            <AssetInputView
                label={label}<commit_msg>AssetInput: don't show as invalid when there is no input<commit_after>import React, {Fragment, PureComponent} from "react";
import PropTypes from "prop-types";
import counterpart from "counterpart";
import {Form, Input, Button} from "bitshares-ui-style-guide";
import ChainTypes from "../Utility/ChainTypes";
import BindToChainState from "../Utility/BindToChainState";
import Translate from "react-translate-component";
import {Map} from "immutable";

const AssetInputView = ({
    label,
    hasAction,
    onChange,
    placeholder,
    style,
    inputStyle,
    value,
    validateStatus,
    onAction,
    actionLabel,
    disableActionButton,
    help
}) => (
    <Fragment>
        <Form.Item
            colon={false}
            label={<Translate content={label} />}
            style={style}
            className={"asset-input" + (hasAction ? " with-action" : "")}
            validateStatus={validateStatus}
            hasFeedback
            help={help}
        >
            <Input
                value={value}
                onChange={onChange}
                style={inputStyle}
                placeholder={placeholder}
            />
        </Form.Item>
        {hasAction && (
            <Form.Item>
                <Button
                    type="primary"
                    disabled={disableActionButton}
                    onClick={onAction}
                >
                    <Translate content={actionLabel} />
                </Button>
            </Form.Item>
        )}
    </Fragment>
);

class ControlledAssetInput extends PureComponent {
    static propTypes = {
        asset: ChainTypes.ChainAsset // the selected asset
    };

    checkFound = prevAsset => {
        const {onFound, asset, resolved} = this.props;
        if (
            resolved &&
            asset !== undefined &&
            typeof onFound === "function" &&
            (Map.isMap(prevAsset) && Map.isMap(asset)
                ? prevAsset.get("id") !== asset.get("id")
                : prevAsset != asset)
        ) {
            onFound(Map.isMap(asset) ? asset : null);
        }
    };

    componentDidMount() {
        this.checkFound();
    }

    componentDidUpdate(prevProps) {
        this.checkFound(prevProps.asset);
    }

    handleChange = event => {
        const {onChange} = this.props;
        if (typeof onChange === "function")
            onChange(event.target.value.toUpperCase());
    };

    getValidateStatus = () => {
        const {validateStatus, asset, resolved, value} = this.props;
        return typeof validateStatus === "string"
            ? validateStatus
            : resolved
            ? Map.isMap(asset)
                ? "success"
                : value
                ? "error"
                : undefined
            : "validating";
    };

    handleAction = () => {
        const {asset, onAction} = this.props;
        onAction(asset);
    };

    render() {
        const {
            label,
            placeholder,
            inputStyle,
            style,
            value,
            actionLabel,
            help,
            onAction
        } = this.props;
        const validateStatus = this.getValidateStatus();
        const hasAction = typeof onAction === "function";
        return (
            <AssetInputView
                label={label}
{'fraction_non_alphanumeric': 0.11428135489777236, 'fraction_numerical': 0.0, 'mean_word_length': 1.5919335705812574, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 35, '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': '6523753', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1667, 'n_words': 353}