UNPKG

ndla-ui

Version:

UI component library for NDLA.

229 lines (207 loc) 8.62 kB
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; }; }(); 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; }; 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; } import React, { Component, Fragment } from 'react'; import PropTypes from 'prop-types'; import BEMHelper from 'react-bem-helper'; import { ChevronRight, ChevronDown } from 'ndla-icons/common'; import { Trans } from 'ndla-i18n'; import { FilterListPhone } from '../Filter'; import SafeLink from '../common/SafeLink'; var classes = new BEMHelper({ name: 'competence-goals', prefix: 'c-' }); var renderItem = function renderItem(item) { var content = item.url ? React.createElement( SafeLink, { to: item.url }, item.text ) : item.text; return React.createElement( 'li', _extends({}, classes('topic-item'), { key: item.text }), content ); }; var CompetenceGoals = function (_Component) { _inherits(CompetenceGoals, _Component); function CompetenceGoals(props) { _classCallCheck(this, CompetenceGoals); var _this = _possibleConstructorReturn(this, (CompetenceGoals.__proto__ || Object.getPrototypeOf(CompetenceGoals)).call(this, props)); _this.state = { expanded: null }; return _this; } _createClass(CompetenceGoals, [{ key: 'render', value: function render() { var _this2 = this; var _props = this.props, messages = _props.messages, headingId = _props.headingId, topics = _props.topics, id = _props.id, menu = _props.menu, search = _props.search, subjectName = _props.subjectName, filterOptions = _props.filterOptions, filterValues = _props.filterValues, onFilterClick = _props.onFilterClick, description = _props.description; return React.createElement( Trans, null, function (_ref) { var t = _ref.t; return React.createElement( 'div', classes('', { menu: menu, search: search }), !menu && !search ? React.createElement( Fragment, null, React.createElement( 'h1', { id: headingId }, messages.heading ), React.createElement('hr', null), React.createElement( 'p', null, description ), React.createElement( 'p', null, messages.listDescription ), React.createElement( 'div', classes('topic'), React.createElement( 'ul', classes('topic-list'), topics[0].items.map(renderItem) ) ) ) : React.createElement( Fragment, null, React.createElement( 'h1', classes('subject-heading'), subjectName ), React.createElement( 'h2', _extends({ id: headingId }, classes('heading')), messages.heading ), React.createElement( 'p', classes('description'), messages.listDescription ), filterOptions && filterOptions.length > 0 && React.createElement( Fragment, null, React.createElement(FilterListPhone, { label: 'Filtrer kompetansem\xE5l', options: filterOptions, alignedGroup: true, values: filterValues, onChange: onFilterClick, messages: { openFilter: t('competenceGoals.openCompentenceGoalsFilter'), useFilter: t('competenceGoals.useCompentenceGoalsFilter'), closeFilter: t('competenceGoals.closeCompentenceGoalsFilter') } }) ), topics.map(function (topic) { return React.createElement( 'div', _extends({}, classes('topic', { expandable: true, expanded: _this2.state.expanded === topic.heading }), { key: topic.heading }), React.createElement( 'h3', classes('topic-heading'), React.createElement( 'button', _extends({}, classes('topic-heading-button'), { type: 'button', 'aria-expanded': _this2.state.expanded === topic.heading, 'aria-controls': id, onClick: function onClick() { _this2.setState(function (prevState) { var expanded = null; if (prevState.expanded !== topic.heading) { expanded = topic.heading; } return { expanded: expanded }; }); } }), _this2.state.expanded === topic.heading ? React.createElement(ChevronDown, null) : React.createElement(ChevronRight, null), topic.heading ) ), React.createElement( 'ul', _extends({ id: id, 'aria-hidden': _this2.state.expanded !== topic.heading }, classes('topic-list')), topic.items.map(function (item) { return renderItem(item); }) ) ); }) ) ); } ); } }]); return CompetenceGoals; }(Component); CompetenceGoals.propTypes = { id: PropTypes.string, headingId: PropTypes.string, menu: PropTypes.bool, search: PropTypes.bool, description: PropTypes.string.isRequired, messages: PropTypes.shape({ heading: PropTypes.string.isRequired, listDescription: PropTypes.string.isRequired }).isRequired, subjectName: PropTypes.string, filterOptions: PropTypes.arrayOf(PropTypes.shape({ title: PropTypes.string.isRequired, value: PropTypes.string.isRequired })), filterValues: PropTypes.arrayOf(PropTypes.string), onFilterClick: PropTypes.func.isRequired, topics: PropTypes.arrayOf(PropTypes.shape({ heading: PropTypes.string, items: PropTypes.arrayOf(PropTypes.shape({ text: PropTypes.string.isRequired, url: PropTypes.string })) })) }; CompetenceGoals.defaultProps = { headingId: null, menu: false, search: false }; export default CompetenceGoals;