<commit_before>﻿import * as React from 'react'
import { classes, Dic } from '../Globals'



export interface TypeaheadProps {
    value?: string;
    onChange?: (newValue: string) => void;
    onBlur?: () => void;
    getItems: (query: string) => Promise<any[]>;
    getItemsDelay?: number;
    minLength?: number;
    renderList?: (typeAhead: Typeahead) => React.ReactNode;
    renderItem?: (item: any, query: string) => React.ReactNode;
    onSelect?: (item: any, e: React.SyntheticEvent<any>) => string | null;
    scrollHeight?: number;
    spanAttrs?: React.HTMLAttributes<HTMLSpanElement>;
    inputAttrs?: React.HTMLAttributes<HTMLInputElement>;
    liAttrs?: (item: any) => React.HTMLAttributes<HTMLLIElement>;
    loadingMessage?: string;
    noResultsMessage?: string;
}

export interface TypeaheadState {
    shown?: boolean;
    items?: any[];
    query?: string;
    selectedIndex?: number;
}


export default class Typeahead extends React.Component<TypeaheadProps, TypeaheadState>
{
    constructor(props: TypeaheadProps) {
        super(props);
        this.state = {
            shown: false,
            items: undefined,
            selectedIndex: undefined,
        };
    }

    static highlightedText = (val: string, query?: string): React.ReactNode => {

        if (query == undefined)
            return val;

        const index = val.toLowerCase().indexOf(query.toLowerCase());
        if (index == -1)
            return val;

        return [
            val.substr(0, index),
            <strong key={0}>{val.substr(index, query.length)}</strong>,
            val.substr(index + query.length)
        ];
    }

    static defaultProps: TypeaheadProps = {
        getItems: undefined as any,
        getItemsDelay: 200,
        minLength: 1,
        renderItem: Typeahead.highlightedText,
        onSelect: (elem, event) => elem,
        scrollHeight: 0,
        loadingMessage: "Loading...",
        noResultsMessage: " - No results -",
    };

    handle: number;

    lookup() {
        if (!this.props.getItemsDelay) {
            this.popupate();
        }
        else {
            if (this.handle)
                clearTimeout(this.handle);

            setTimeout(() => this.popupate(), this.props.getItemsDelay);
        }
    }

    componentWillUnmount() {
        if (this.handle)
            clearTimeout(this.handle);
    }

    popupate() {
<commit_msg>fix delay on Typeahead (autocomplete)<commit_after>﻿import * as React from 'react'
import { classes, Dic } from '../Globals'



export interface TypeaheadProps {
    value?: string;
    onChange?: (newValue: string) => void;
    onBlur?: () => void;
    getItems: (query: string) => Promise<any[]>;
    getItemsDelay?: number;
    minLength?: number;
    renderList?: (typeAhead: Typeahead) => React.ReactNode;
    renderItem?: (item: any, query: string) => React.ReactNode;
    onSelect?: (item: any, e: React.SyntheticEvent<any>) => string | null;
    scrollHeight?: number;
    spanAttrs?: React.HTMLAttributes<HTMLSpanElement>;
    inputAttrs?: React.HTMLAttributes<HTMLInputElement>;
    liAttrs?: (item: any) => React.HTMLAttributes<HTMLLIElement>;
    loadingMessage?: string;
    noResultsMessage?: string;
}

export interface TypeaheadState {
    shown?: boolean;
    items?: any[];
    query?: string;
    selectedIndex?: number;
}


export default class Typeahead extends React.Component<TypeaheadProps, TypeaheadState>
{
    constructor(props: TypeaheadProps) {
        super(props);
        this.state = {
            shown: false,
            items: undefined,
            selectedIndex: undefined,
        };
    }

    static highlightedText = (val: string, query?: string): React.ReactNode => {

        if (query == undefined)
            return val;

        const index = val.toLowerCase().indexOf(query.toLowerCase());
        if (index == -1)
            return val;

        return [
            val.substr(0, index),
            <strong key={0}>{val.substr(index, query.length)}</strong>,
            val.substr(index + query.length)
        ];
    }

    static defaultProps: TypeaheadProps = {
        getItems: undefined as any,
        getItemsDelay: 200,
        minLength: 1,
        renderItem: Typeahead.highlightedText,
        onSelect: (elem, event) => elem,
        scrollHeight: 0,
        loadingMessage: "Loading...",
        noResultsMessage: " - No results -",
    };

    handle: number;

    lookup() {
        if (!this.props.getItemsDelay) {
            this.populate();
        }
        else {
            if (this.handle)
                clearTimeout(this.handle);

            this.handle = setTimeout(() => this.populate(), this.props.getItemsDelay);
        }
    }

    componentWillUnmount() {
        if (this.handle)
            clearTimeout(this.handle);
    }

    populate() {

{'fraction_non_alphanumeric': 0.12955298013245034, 'fraction_numerical': 0.0033112582781456954, 'mean_word_length': 2.3702928870292888, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 39, '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': '6946733', 'n_tokens_mistral': 1408, 'n_tokens_neox': 1367, 'n_words': 321}