<commit_before>import { useRef, useState, useCallback, useEffect } from 'react';

const noop = value => value;

export function useField({
    value,
    onChange,
    onType = noop,
    getValue = noop,
    setValue = noop,
    active
}) {
    const inputRef = useRef(null);
    const [wasActive, setWasActive] = useState(active);
    useEffect(() => {
        if (active && !wasActive && inputRef.current && inputRef.current.focus) {
            inputRef.current.focus();
        } else if (!active && wasActive && inputRef.current && inputRef.current.blur) {
            inputRef.current.blur();
        }

        setWasActive(active);
    }, [active, wasActive]);

    const [prevValue, setPrevValue] = useState(value);
    const [currentValue, setCurrentValue] = useState(getValue(value));

    useEffect(() => {
        if (value !== prevValue) {
            setPrevValue(value);
            setCurrentValue(getValue(value));
        }
    }, [value, prevValue, getValue]);

    const onChangeRaw = useCallback(evt => {
        try {
            const newValue = setValue(evt.target.value);
            setCurrentValue(newValue);
            onType(newValue);
        } catch {
            // do nothing
        }
    }, [setCurrentValue, setValue, onType]);

    const onBlur = useCallback(() => {
        onChange(currentValue);
    }, [onChange, currentValue]);

    return [currentValue, onChangeRaw, onBlur, inputRef];
}
<commit_msg>Select field in field hook when setting to active, if possible<commit_after>import { useRef, useState, useCallback, useEffect } from 'react';

const noop = value => value;

export function useField({
    value,
    onChange,
    onType = noop,
    getValue = noop,
    setValue = noop,
    active
}) {
    const inputRef = useRef(null);
    const [wasActive, setWasActive] = useState(active);
    useEffect(() => {
        if (active && !wasActive && inputRef.current) {
            if (inputRef.current.focus) {
                inputRef.current.focus();
            }
            if (inputRef.current.select) {
                inputRef.current.select();
            }
        } else if (!active && wasActive && inputRef.current && inputRef.current.blur) {
            inputRef.current.blur();
        }

        setWasActive(active);
    }, [active, wasActive]);

    const [prevValue, setPrevValue] = useState(value);
    const [currentValue, setCurrentValue] = useState(getValue(value));

    useEffect(() => {
        if (value !== prevValue) {
            setPrevValue(value);
            setCurrentValue(getValue(value));
        }
    }, [value, prevValue, getValue]);

    const onChangeRaw = useCallback(evt => {
        try {
            const newValue = setValue(evt.target.value);
            setCurrentValue(newValue);
            onType(newValue);
        } catch {
            // do nothing
        }
    }, [setCurrentValue, setValue, onType]);

    const onBlur = useCallback(() => {
        onChange(currentValue);
    }, [onChange, currentValue]);

    return [currentValue, onChangeRaw, onBlur, inputRef];
}

{'fraction_non_alphanumeric': 0.13016596160104132, 'fraction_numerical': 0.0, 'mean_word_length': 2.370614035087719, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '6502904', 'n_tokens_mistral': 876, 'n_tokens_neox': 875, 'n_words': 194}