@massds/mayflower-react
Version:
React versions of Mayflower design system UI components
99 lines (78 loc) • 4.05 kB
JavaScript
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/**
* GenTeaserSearch module.
* @module @massds/mayflower-react/GenTeaserSearch
* @requires module:@massds/mayflower-assets/scss/01-atoms/button-with-icon
* @requires module:@massds/mayflower-assets/scss/01-atoms/button-search
* @requires module:@massds/mayflower-assets/scss/01-atoms/input-typeahead
* @requires module:@massds/mayflower-assets/scss/01-atoms/svg-icons
* @requires module:@massds/mayflower-assets/scss/01-atoms/svg-loc-icons
*/
import React from "react";
import PropTypes from "prop-types";
import HeaderSearch from "../HeaderSearch/index.mjs";
let GenTeaserSearch = /*#__PURE__*/function (_React$Component) {
_inheritsLoose(GenTeaserSearch, _React$Component);
function GenTeaserSearch(props) {
var _this;
_this = _React$Component.call(this, props) || this;
_defineProperty(_assertThisInitialized(_this), "onClick", e => {
e.preventDefault();
const _this$props = _this.props,
target = _this$props.target,
queryInput = _this$props.queryInput;
const query = _this.inputRef.current.value;
if (query.length > 0) {
const searchURL = queryInput ? target.replace("{" + queryInput + "}", query) : target;
_this.redirect(searchURL);
}
});
_defineProperty(_assertThisInitialized(_this), "redirect", searchURL => {
if (window.location !== window.parent.location) {
window.parent.location.assign(searchURL);
} else {
window.location.assign(searchURL);
}
});
_this.inputRef = /*#__PURE__*/React.createRef();
return _this;
}
var _proto = GenTeaserSearch.prototype;
_proto.render = function render() {
const _this$props2 = this.props,
placeholder = _this$props2.placeholder,
id = _this$props2.id,
queryInput = _this$props2.queryInput,
rest = _objectWithoutPropertiesLoose(_this$props2, ["placeholder", "id", "queryInput"]);
return /*#__PURE__*/React.createElement(HeaderSearch, _extends({
buttonSearch: {
'aria-label': '',
onClick: e => this.onClick(e),
text: 'Search',
usage: ''
},
defaultText: "",
id: id,
label: "Search terms",
onSubmit: e => this.onSubmit(e),
inputRef: this.inputRef,
placeholder: placeholder
}, rest));
};
return GenTeaserSearch;
}(React.Component);
GenTeaserSearch.propTypes = process.env.NODE_ENV !== "production" ? {
/** The target url of the search bar */
target: PropTypes.string.isRequired,
/** The id of the search bar */
id: PropTypes.string.isRequired,
/** The query input variable to replace in the target url with the user entered term */
queryInput: PropTypes.string.isRequired,
/** Placeholder text of the search bar. */
placeholder: PropTypes.string.isRequired
} : {};
export default GenTeaserSearch;