@elastic/eui
Version:
Elastic UI Component Library
243 lines (240 loc) • 11.6 kB
JavaScript
var _excluded = ["schema", "aria-describedby"];
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
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; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], t.indexOf(o) >= 0 || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
/*
* 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;