UNPKG

@massds/mayflower-react

Version:

React versions of Mayflower design system UI components

154 lines (121 loc) 5.75 kB
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 _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; } /** * HeaderSearch module. * @module @massds/mayflower-react/HeaderSearch * @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 is from "is"; import ButtonWithIcon from "../ButtonWithIcon/index.mjs"; import TypeAheadDropdown from "../TypeAheadDropdown/index.mjs"; // eslint-disable-next-line import/no-unresolved import IconSearch from "../Icon/IconSearch.mjs"; let HeaderSearch = /*#__PURE__*/function (_React$Component) { _inheritsLoose(HeaderSearch, _React$Component); function HeaderSearch(props) { var _this; _this = _React$Component.call(this, props) || this; _defineProperty(_assertThisInitialized(_this), "handleChange", event => { const query = event.target.value; _this.setState({ value: query }); // invokes custom function if passed in the component if (typeof _this.props.onChange === 'function') { _this.props.onChange(query); } }); _defineProperty(_assertThisInitialized(_this), "handleSubmit", event => { if (is.fn(_this.props.onSubmit)) { _this.props.onSubmit(event); } }); _this.state = { value: _this.props.defaultText }; return _this; } var _proto = HeaderSearch.prototype; _proto.render = function render() { const headerSearch = _extends({}, this.props); const orgDropdown = headerSearch.orgDropdown; const shouldShowTypeAhead = orgDropdown && orgDropdown.dropdownButton && orgDropdown.inputText; const inputProps = { id: headerSearch.id, className: 'ma__header-search__input', placeholder: headerSearch.placeholder, onChange: this.handleChange, type: 'search', value: this.state.value, autoComplete: headerSearch.autoComplete }; if (this.props.inputRef) { inputProps.ref = this.props.inputRef; } headerSearch.buttonSearch.icon = /*#__PURE__*/React.createElement(IconSearch, null); return /*#__PURE__*/React.createElement("div", { className: "ma__header-search__wrapper ma__header-search__wrapper--responsive" }, shouldShowTypeAhead && /*#__PURE__*/React.createElement("div", { className: "ma__header-search__pre-filter" }, /*#__PURE__*/React.createElement(TypeAheadDropdown, orgDropdown)), /*#__PURE__*/React.createElement("div", { className: "ma__header-search" }, /*#__PURE__*/React.createElement("form", { action: "#", className: "ma__form", onSubmit: this.handleSubmit, role: "search" }, /*#__PURE__*/React.createElement("label", { htmlFor: headerSearch.id, className: "ma__header-search__label" }, headerSearch.label), /*#__PURE__*/React.createElement("input", inputProps), this.props.suggestions && this.props.suggestions, this.props.postInputFilter && /*#__PURE__*/React.createElement("div", { className: "ma__header-search__post-filter" }, this.props.postInputFilter), /*#__PURE__*/React.createElement(ButtonWithIcon, headerSearch.buttonSearch)))); }; return HeaderSearch; }(React.Component); HeaderSearch.propTypes = process.env.NODE_ENV !== "production" ? { /** The ID for the input */ id: PropTypes.string, /** The label text for the input */ label: PropTypes.string, /** The placeholder text for the input */ placeholder: PropTypes.string, /** The Search button */ buttonSearch: PropTypes.shape(ButtonWithIcon.propTypes), /** Custom submit function */ onSubmit: PropTypes.func, /** Custom change function for the text input */ onChange: PropTypes.func, /** Default input text value */ defaultText: PropTypes.string, /** Render suggestions as passable element */ suggestions: PropTypes.element, /** @molecules/TypeAheadDropdown */ orgDropdown: PropTypes.shape(TypeAheadDropdown.propTypes), /** postInputFilter passable component */ postInputFilter: PropTypes.element, /** A ref object as created by React.createRef(). Will be applied to the input element. */ inputRef: PropTypes.oneOfType([// Either a function PropTypes.func, // Or the instance of a DOM native element (see the note about SSR) /* eslint-disable-next-line react/forbid-prop-types */ PropTypes.shape({ current: PropTypes.object })]), /** Autocomplete input attribute value */ autoComplete: PropTypes.string } : {}; HeaderSearch.defaultProps = { id: 'header-search', label: 'Search terms', placeholder: 'Search Mass.gov', buttonSearch: { usage: 'secondary' } }; export default HeaderSearch;