UNPKG

ndla-ui

Version:

UI component library for NDLA.

250 lines (220 loc) 8.79 kB
var _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; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /* * Copyright (c) 2016-present, NDLA. * * This source code is licensed under the GPLv3 license found in the * LICENSE file in the root directory of this source tree. * */ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import BEMHelper from 'react-bem-helper'; import { Search as SearchIcon } from 'ndla-icons/common'; import { injectT } from 'ndla-i18n'; import SafeLink from '../common/SafeLink'; import ActiveFilters from './ActiveFilters'; import ContentTypeResult from './ContentTypeResult'; import { ContentTypeResultShape } from '../shapes'; var classes = new BEMHelper('c-search-field'); var messagesShape = PropTypes.shape({ // required if search result searchResultHeading: PropTypes.string, contentTypeResultShowMoreLabel: PropTypes.string, contentTypeResultShowLessLabel: PropTypes.string, contentTypeResultNoHit: PropTypes.string }); var SearchResult = function SearchResult(_ref) { var result = _ref.result, allResultUrl = _ref.allResultUrl, resourceToLinkProps = _ref.resourceToLinkProps, onNavigate = _ref.onNavigate, t = _ref.t; return React.createElement( 'section', classes('search-result'), React.createElement( 'h1', classes('search-result-heading'), t('searchPage.searchField.searchResultHeading') ), React.createElement( 'div', classes('search-result-content'), result.map(function (contentTypeResult) { return React.createElement(ContentTypeResult, { onNavigate: onNavigate, contentTypeResult: contentTypeResult, resourceToLinkProps: resourceToLinkProps, defaultCount: window.innerWidth > 980 ? 7 : 3, key: contentTypeResult.title, messages: { allResultLabel: t('searchPage.searchField.contentTypeResultShowMoreLabel'), showLessResultLabel: t('searchPage.searchField.contentTypeResultShowLessLabel'), noHit: t('searchPage.searchField.contentTypeResultNoHit') } }); }) ), React.createElement( 'div', classes('go-to-search'), React.createElement( SafeLink, { to: allResultUrl }, t('searchPage.searchField.allResultButtonText') ) ) ); }; SearchResult.propTypes = { result: PropTypes.arrayOf(ContentTypeResultShape), resourceToLinkProps: PropTypes.func.isRequired, allResultUrl: PropTypes.string.isRequired, onNavigate: PropTypes.func, t: PropTypes.func.isRequired }; var SearchField = function (_Component) { _inherits(SearchField, _Component); function SearchField(props) { _classCallCheck(this, SearchField); var _this = _possibleConstructorReturn(this, (SearchField.__proto__ || Object.getPrototypeOf(SearchField)).call(this, props)); _this.state = { inputHasFocus: false }; _this.inputRef = null; _this.handleOnFilterRemove = _this.handleOnFilterRemove.bind(_this); _this.onInputBlur = _this.onInputBlur.bind(_this); _this.onInputFocus = _this.onInputFocus.bind(_this); return _this; } _createClass(SearchField, [{ key: 'onInputBlur', value: function onInputBlur() { this.setState({ inputHasFocus: false }); } }, { key: 'onInputFocus', value: function onInputFocus() { this.setState({ inputHasFocus: true }); } }, { key: 'handleOnFilterRemove', value: function handleOnFilterRemove(value, filterName) { this.props.onFilterRemove(value, filterName); this.inputRef.focus(); } }, { key: 'render', value: function render() { var _this2 = this; var _props = this.props, placeholder = _props.placeholder, value = _props.value, onChange = _props.onChange, filters = _props.filters, searchResult = _props.searchResult, messages = _props.messages, allResultUrl = _props.allResultUrl, onSearch = _props.onSearch, resourceToLinkProps = _props.resourceToLinkProps, small = _props.small, autofocus = _props.autofocus, onNavigate = _props.onNavigate, t = _props.t; var modifiers = []; var hasSearchResult = searchResult && searchResult.length > 0; var searchResultView = null; if (hasSearchResult) { modifiers.push('has-search-result'); searchResultView = React.createElement(SearchResult, { result: searchResult, searchString: value, allResultUrl: allResultUrl, resourceToLinkProps: resourceToLinkProps, autofocus: autofocus, onNavigate: onNavigate, t: t }); } if (filters && filters.length > 0) { modifiers.push('has-filter'); } if (this.state.inputHasFocus) { modifiers.push('input-has-focus'); } return React.createElement( 'form', _extends({ action: '/search/' }, classes('', modifiers), { onSubmit: onSearch }), React.createElement( 'div', classes('input-wrapper'), React.createElement('input', _extends({ ref: function ref(_ref2) { _this2.inputRef = _ref2; }, title: messages.searchFieldTitle, type: 'search' }, classes('input', { small: small }), { 'aria-autocomplete': 'list', autoComplete: 'off', id: 'search', name: 'search', placeholder: placeholder, 'aria-label': placeholder, value: value, onChange: onChange, onBlur: this.onInputBlur, onFocus: this.onInputFocus })), filters && filters.length > 0 && React.createElement( 'div', classes('filters'), React.createElement(ActiveFilters, { filters: filters, onFilterRemove: this.handleOnFilterRemove }) ), React.createElement( 'button', _extends({ tabIndex: '-1' }, classes('button'), { type: 'submit', value: 'Search' }), React.createElement(SearchIcon, null) ) ), searchResultView ); } }]); return SearchField; }(Component); SearchField.propTypes = { value: PropTypes.string.isRequired, placeholder: PropTypes.string.isRequired, onChange: PropTypes.func.isRequired, onSearch: PropTypes.func.isRequired, filters: PropTypes.arrayOf(PropTypes.shape({ value: PropTypes.string.isRequired, title: PropTypes.string.isRequired })), messages: messagesShape, searchResult: PropTypes.arrayOf(ContentTypeResultShape), allResultUrl: PropTypes.string, resourceToLinkProps: PropTypes.func, onFilterRemove: PropTypes.func, small: PropTypes.bool, autofocus: PropTypes.bool, onNavigate: PropTypes.func, t: PropTypes.func.isRequired }; export default injectT(SearchField);