UNPKG

ndla-ui

Version:

UI component library for NDLA.

225 lines (196 loc) 8.41 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 _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } 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. * FRI OG BEGRENSET */ import React, { Component, createElement, Fragment } from 'react'; import PropTypes from 'prop-types'; import BEMHelper from 'react-bem-helper'; import { ChevronDown, ChevronUp } from 'ndla-icons/common'; var filterClasses = new BEMHelper({ name: 'filter', prefix: 'c-' }); var FilterList = function (_Component) { _inherits(FilterList, _Component); function FilterList(props) { _classCallCheck(this, FilterList); var _this = _possibleConstructorReturn(this, (FilterList.__proto__ || Object.getPrototypeOf(FilterList)).call(this, props)); _this.state = { visibleCount: props.defaultVisibleCount }; return _this; } _createClass(FilterList, [{ key: 'render', value: function render() { var _this2 = this; var _props = this.props, modifiers = _props.modifiers, label = _props.label, labelNotVisible = _props.labelNotVisible, options = _props.options, values = _props.values, _onChange = _props.onChange, defaultVisibleCount = _props.defaultVisibleCount, showLabel = _props.showLabel, hideLabel = _props.hideLabel, alignedGroup = _props.alignedGroup, collapseMobile = _props.collapseMobile; var showAll = defaultVisibleCount === null || options.length <= defaultVisibleCount; var labelModifiers = []; if (labelNotVisible) { labelModifiers.push('hidden'); } return React.createElement( 'section', filterClasses('list', modifiers), React.createElement( 'h1', filterClasses('label', labelModifiers), label ), this.props.noFilterSelectedLabel && options.length === 0 && React.createElement( 'span', filterClasses('no-filter-selected'), this.props.noFilterSelectedLabel ), React.createElement( 'ul', filterClasses('item-wrapper', { 'aligned-grouping': alignedGroup, 'collapse-mobile': collapseMobile }), options.map(function (option, index) { var itemModifiers = []; var checked = values.some(function (value) { return value === option.value; }); if (!showAll && !checked && index + 1 > _this2.state.visibleCount) { itemModifiers.push('hidden'); } var disabled = option.noResults || option.hits === 0; if (disabled) { itemModifiers.push('no-results'); } return React.createElement( 'li', _extends({}, filterClasses('item', itemModifiers), { key: option.value }), React.createElement('input', _extends({}, filterClasses('input'), { type: 'checkbox', id: option.value, value: option.value, disabled: disabled, tabIndex: disabled ? -1 : 0, checked: checked, onChange: function onChange(event) { var newValues = null; if (event.currentTarget.checked) { newValues = [].concat(_toConsumableArray(values), [option.value]); } else { newValues = values.filter(function (value) { return value !== option.value; }); } if (_onChange) { _onChange(newValues, option.value); } } })), React.createElement( 'label', { htmlFor: option.value }, React.createElement('span', filterClasses('item-checkbox')), React.createElement( 'span', filterClasses('text'), option.title, option.hits !== undefined && ' (' + option.hits + ')' ), option.icon ? createElement(option.icon, { className: 'c-icon--22 ' + filterClasses('icon').className }) : null ) ); }) ), !showAll && React.createElement( 'button', _extends({}, filterClasses('expand'), { type: 'button', onClick: function onClick() { _this2.setState(function (prevState) { if (prevState.visibleCount === defaultVisibleCount) { return { visibleCount: options.length }; } return { visibleCount: defaultVisibleCount }; }); } }), this.state.visibleCount === defaultVisibleCount ? React.createElement( Fragment, null, React.createElement( 'span', null, showLabel ), ' ', React.createElement(ChevronDown, null) ) : React.createElement( Fragment, null, React.createElement( 'span', null, hideLabel ), ' ', React.createElement(ChevronUp, null) ) ) ); } }]); return FilterList; }(Component); var valueShape = PropTypes.oneOfType([PropTypes.string, PropTypes.number]); FilterList.propTypes = { children: PropTypes.node, label: PropTypes.string, labelNotVisible: PropTypes.bool, modifiers: PropTypes.string, onChange: PropTypes.func, // isRequired options: PropTypes.arrayOf(PropTypes.shape({ title: PropTypes.string.isRequired, value: valueShape.isRequired, hits: PropTypes.number, icon: PropTypes.func, noResults: PropTypes.bool })).isRequired, values: PropTypes.arrayOf(valueShape), defaultVisibleCount: PropTypes.number, showLabel: PropTypes.string, noFilterSelectedLabel: PropTypes.string, hideLabel: PropTypes.string, alignedGroup: PropTypes.bool, collapseMobile: PropTypes.bool }; FilterList.defaultProps = { label: 'FILTER:', modifiers: '', values: [], defaultVisibleCount: null }; export default FilterList;