<commit_before>// Dependencies

let builder = require('focus').component.builder;
let type = require('focus').component.types;
let React = require('react');
let words = require('lodash/string/words');
let actionWrapper = require('../../page/search/search-header/action-wrapper');
let assign = require('lodash/object/extend');

// Components

let Scope = require('./scope').component;

// Mixins

let stylable = require('../../mixin/stylable');
let i18n = require('../../common/i18n/mixin');
let storeBehaviour = require('../../common/mixin/store-behaviour');
let queryStoreBehaviour = require('../mixin/query-store-behaviour');

/**
 * SearchBar component
 * @type {Object}
 */
let SearchBar = {
    mixins: [i18n, stylable, storeBehaviour, queryStoreBehaviour],
    stores: [{
        store: Focus.search.builtInStore.queryStore,
        properties: ['query', 'scope']
    }],
    displayName: 'SearchBar',
    getDefaultProps() {
        return {
            placeholder: 'Enter your search here...',
            scopes: [],
            minChar: 0,
            loading: false,
            helpTranslationPath: 'search.bar.help',
            hasScopes: true
        };
    },
    propTypes: {
        placeholder: type('string'),
        value: type('string'),
        scopes: type('array'),
        minChar: type('number'),
        loading: type('bool'),
        helpTranslationPath: type('string'),
        hasScopes: type('bool')
    },
    getInitialState() {
        return assign({loading: this.props.loading, scope: this.props.scope, query: this.props.query}, this._getStateFromStores());
    },
    componentDidMount() {
        React.findDOMNode(this.refs.query).focus();
    },
    onChange() {
        this.setState(this._getStateFromStores())
    },
    _getClassName() {
        return `form-control`;
    },
    _handleQueryChange() {
        actionWrapper(() => {
            this._updateQuery(React.findDOMNode(this.refs.query).value);
        })();
    },
    _handleInputChange(event) {
        this.setState({query: event.target.value}, ()=> {
            if (this.state.query.length >= this.props.minChar) {
                if (this.props.handleKeyUp) {
                    this.props.handleKeyUp(event);
                }
                this._handleQueryChange();
            }
        });

    },
    _handleInputKeyPress(event) {
        if (event.key === 'Enter' && this.props.onEnterKeyPressed) {
            actionWrapper(() => {
                this._updateQuery(React.findDOMNode(this.refs.query).value);
            }, null, 0)();
        }
    },
    _handleOnClickScope() {
        this._focusQuery();
        this._updateScope(this.refs.scope.getValue());
    },
    _renderHelp() {
        return (
            <div className='sb-help' ref='help'>{this.i18n(this.props.helpTranslationPath)}</div>
        );
    },
    _focusQuery() {
        React.findDOMNode(this.refs.query).focus();
    },<commit_msg>[search-bar] Add search trigger when enter key is pressed<commit_after>// Dependencies

let builder = require('focus').component.builder;
let type = require('focus').component.types;
let React = require('react');
let words = require('lodash/string/words');
let actionWrapper = require('../../page/search/search-header/action-wrapper');
let assign = require('lodash/object/extend');

// Components

let Scope = require('./scope').component;

// Mixins

let stylable = require('../../mixin/stylable');
let i18n = require('../../common/i18n/mixin');
let storeBehaviour = require('../../common/mixin/store-behaviour');
let queryStoreBehaviour = require('../mixin/query-store-behaviour');

/**
 * SearchBar component
 * @type {Object}
 */
let SearchBar = {
    mixins: [i18n, stylable, storeBehaviour, queryStoreBehaviour],
    stores: [{
        store: Focus.search.builtInStore.queryStore,
        properties: ['query', 'scope']
    }],
    displayName: 'SearchBar',
    getDefaultProps() {
        return {
            placeholder: 'Enter your search here...',
            scopes: [],
            minChar: 0,
            loading: false,
            helpTranslationPath: 'search.bar.help',
            hasScopes: true
        };
    },
    propTypes: {
        placeholder: type('string'),
        value: type('string'),
        scopes: type('array'),
        minChar: type('number'),
        loading: type('bool'),
        helpTranslationPath: type('string'),
        hasScopes: type('bool')
    },
    getInitialState() {
        return assign({loading: this.props.loading, scope: this.props.scope, query: this.props.query}, this._getStateFromStores());
    },
    componentDidMount() {
        React.findDOMNode(this.refs.query).focus();
    },
    onChange() {
        this.setState(this._getStateFromStores())
    },
    _getClassName() {
        return `form-control`;
    },
    _handleQueryChange() {
        actionWrapper(() => {
            this._updateQuery(React.findDOMNode(this.refs.query).value);
        })();
    },
    _handleInputChange(event) {
        this.setState({query: event.target.value}, ()=> {
            if (this.state.query.length >= this.props.minChar) {
                if (this.props.handleKeyUp) {
                    this.props.handleKeyUp(event);
                }
                this._handleQueryChange();
            }
        });

    },
    _handleInputKeyPress(event) {
        if (event.key === 'Enter') {
            actionWrapper(() => {
                this._updateQuery(React.findDOMNode(this.refs.query).value);
            }, null, 0)();
        }
    },
    _handleOnClickScope() {
        this._focusQuery();
        this._updateScope(this.refs.scope.getValue());
    },
    _renderHelp() {
        return (
            <div className='sb-help' ref='help'>{this.i18n(this.props.helpTranslationPath)}</div>
        );
    },
    _focusQuery() {
        React.findDOMNode(this.refs.query).focus();
    },
{'fraction_non_alphanumeric': 0.16174473862864902, 'fraction_numerical': 0.0033944331296673455, 'mean_word_length': 2.3293785310734463, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '3713643', 'n_tokens_mistral': 1752, 'n_tokens_neox': 1708, 'n_words': 277}