UNPKG

@elastic/eui

Version:

Elastic UI Component Library

235 lines (232 loc) 8.78 kB
import _extends from "@babel/runtime/helpers/extends"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _typeof from "@babel/runtime/helpers/typeof"; var _excluded = ["schema", "aria-describedby"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0 and the Server Side Public License, v 1; you may not use this file except * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ import React, { useState } from 'react'; import { useEuiTheme, useGeneratedHtmlId } from '../../services'; import { isString } from '../../services/predicate'; import { EuiFlexGroup, EuiFlexItem } from '../flex'; import { EuiToolTip } from '../tool_tip'; import { EuiSearchBox } from './search_box'; import { EuiSearchBarFilters } from './search_filters'; import { Query } from './query'; import { euiSearchBar__searchHolder, euiSearchBar__filtersHolder } from './search_bar.styles'; import { jsx as ___EmotionJSX } from "@emotion/react"; export { Query, AST as Ast } from './query'; /** * When `searchFormat` is 'text', `query` is null and the search is performed * on the `queryText` directly without EQL parsing */ var parseQuery = function parseQuery(query, props) { var _props$box, _props$box2; var schema = undefined; if ((_props$box = props.box) !== null && _props$box !== void 0 && _props$box.schema && _typeof((_props$box2 = props.box) === null || _props$box2 === void 0 ? void 0 : _props$box2.schema) === 'object') { schema = props.box.schema; } var dateFormat = props.dateFormat; var parseOptions = { schema: schema, dateFormat: dateFormat }; if (!query) { return Query.parse('', parseOptions); } return isString(query) ? Query.parse(query, parseOptions) : query; }; function renderTools(tools) { if (!tools) { return undefined; } if (Array.isArray(tools)) { return tools.map(function (tool) { return ___EmotionJSX(EuiFlexItem, { grow: false, key: tool.key == null ? undefined : tool.key }, tool); }); } return ___EmotionJSX(EuiFlexItem, { grow: false }, tools); } function notifyControllingParent(oldState, newState, onChange) { if (!onChange) { return; } var query = newState.query, queryText = newState.queryText, error = newState.error; var isQueryDifferent = oldState.queryText !== queryText; var oldError = oldState.error ? oldState.error.message : null; var newError = error ? error.message : null; var isErrorDifferent = oldError !== newError; if (isQueryDifferent || isErrorDifferent) { if (error == null) { onChange({ query: query, queryText: queryText, error: error }); } else { onChange({ query: null, queryText: queryText, error: error }); } } } export var EuiSearchBar = function EuiSearchBar(props) { var _hint$popoverProps$is, _hint$popoverProps; var _props$box3 = props.box, _props$box4 = _props$box3 === void 0 ? { schema: '' } : _props$box3, schema = _props$box4.schema, boxAriaDescribedBy = _props$box4['aria-describedby'], box = _objectWithoutProperties(_props$box4, _excluded), filters = props.filters, toolsLeft = props.toolsLeft, toolsRight = props.toolsRight, hint = props.hint, compressed = props.compressed, _props$showErrorToolt = props.showErrorTooltip, showErrorTooltip = _props$showErrorToolt === void 0 ? true : _props$showErrorToolt; var theme = useEuiTheme(); var hintId = useGeneratedHtmlId({ prefix: '__hint' }); var _useState = useState(function () { return parseQuery(props.defaultQuery || props.query, props); }), _useState2 = _slicedToArray(_useState, 2), query = _useState2[0], setQuery = _useState2[1]; var _useState3 = useState(query.text), _useState4 = _slicedToArray(_useState3, 2), queryText = _useState4[0], setQueryText = _useState4[1]; var _useState5 = useState(null), _useState6 = _slicedToArray(_useState5, 2), error = _useState6[0], setError = _useState6[1]; var _useState7 = useState(false), _useState8 = _slicedToArray(_useState7, 2), isHintVisibleState = _useState8[0], setIsHintVisibleState = _useState8[1]; var _useState9 = useState(props.query), _useState10 = _slicedToArray(_useState9, 2), prevPropsQuery = _useState10[0], setPrevPropsQuery = _useState10[1]; if (props.query !== prevPropsQuery) { var nextQuery = props.query; var prevQuery = query; var shouldUpdate = (nextQuery || nextQuery === '') && (typeof nextQuery !== 'string' && nextQuery.text !== prevQuery.text || typeof nextQuery === 'string' && nextQuery !== prevQuery.text); if (shouldUpdate) { setPrevPropsQuery(props.query); var parsedQuery = parseQuery(nextQuery, props); setQuery(parsedQuery); setQueryText(parsedQuery.text); setError(null); } } function onSearch(newQueryText) { var oldState = { query: query, queryText: queryText, error: error }; try { var newQuery = parseQuery(newQueryText, props); notifyControllingParent(oldState, { query: newQuery, queryText: newQueryText, error: null }, props.onChange); setQuery(newQuery); setQueryText(newQuery.text); setError(null); } catch (e) { var _error = e instanceof Error ? { name: e.name, message: e.message } : { name: 'Unexpected error', message: String(e) }; notifyControllingParent(oldState, { query: null, queryText: newQueryText, error: _error }, props.onChange); setQueryText(newQueryText); setError(_error); } } function onFiltersChange(newQuery) { notifyControllingParent({ query: query, queryText: queryText, error: error }, { query: newQuery, queryText: newQuery.text, error: null }, props.onChange); setQuery(newQuery); setQueryText(newQuery.text); setError(null); } var isHintVisible = (_hint$popoverProps$is = hint === null || hint === void 0 || (_hint$popoverProps = hint.popoverProps) === null || _hint$popoverProps === void 0 ? void 0 : _hint$popoverProps.isOpen) !== null && _hint$popoverProps$is !== void 0 ? _hint$popoverProps$is : isHintVisibleState; var toolsLeftEl = renderTools(toolsLeft); var toolsRightEl = renderTools(toolsRight); var describedByIds = [boxAriaDescribedBy, isHintVisible ? hintId : undefined].filter(Boolean).join(' '); var searchBox = ___EmotionJSX(EuiSearchBox, _extends({ compressed: compressed }, box, { query: queryText, onSearch: onSearch, isInvalid: error != null, "aria-describedby": describedByIds || undefined, hint: hint ? _objectSpread({ isVisible: isHintVisible, setIsVisible: function setIsVisible(isVisible) { setIsHintVisibleState(isVisible); }, id: hintId }, hint) : undefined })); return ___EmotionJSX(EuiFlexGroup, { gutterSize: "s", alignItems: "center", wrap: true }, toolsLeftEl, ___EmotionJSX(EuiFlexItem, { className: "euiSearchBar__searchHolder", css: euiSearchBar__searchHolder(theme), grow: true }, showErrorTooltip ? ___EmotionJSX(EuiToolTip, { content: error === null || error === void 0 ? void 0 : error.message, display: "block" }, searchBox) : searchBox), filters && ___EmotionJSX(EuiFlexItem, { className: "euiSearchBar__filtersHolder", css: euiSearchBar__filtersHolder(theme), grow: false }, ___EmotionJSX(EuiSearchBarFilters, { filters: filters, query: query, onChange: onFiltersChange, compressed: compressed })), toolsRightEl); }; EuiSearchBar.Query = Query;