import React from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import {SelectFieldPopup} from './SelectFieldPopup'; import {cloneDeep, differenceBy} from 'lodash'; import {LineInput, Label} from '../'; import {TermsList} from '../../'; import './style.scss'; /** * @ngdoc react * @name SelectMetaTermsInput * @description Component to select metadata terms like Subjects/Category */ export class SelectMetaTermsInput extends React.Component { constructor(props) { super(props); this.state = { multiLevel: false, openSelectPopup: false, }; this.removeValue = this.removeValue.bind(this); this.toggleOpenSelectPopup = this.toggleOpenSelectPopup.bind(this); this.onChange = this.onChange.bind(this); } componentWillMount() { // There is at least one parent or multi-level option this.setState({multiLevel: this.props.options.filter((o) => (o.parent)).length > 0}); } toggleOpenSelectPopup() { this.setState({openSelectPopup: !this.state.openSelectPopup}); this.addBtn.focus(); } removeValue(index, term) { const {value, field, onChange} = this.props; if (term.scheme || term.qcode) { onChange( field, value.filter(({scheme, qcode}) => !(term.scheme === scheme && term.qcode === qcode)) ); } else { // Delete by index onChange(field, cloneDeep(value).filter((item, i) => (i !== index))); } } removeValuesFromOptions() { if (!this.state.multiLevel) { return differenceBy(this.props.options, this.props.value, this.props.valueKey); } else { return this.props.options; } } onChange(opt) { const {value, valueKey, onChange, field} = this.props; // Check if it's duplicate if (value && value.length > 0) { if (value.find((v) => (v[valueKey] === opt[valueKey]))) { return; } onChange(field, [...value, opt]); } else { onChange(field, [opt]); } } render() { const { value, label, labelKey, searchKey, valueKey, popupContainer, readOnly, onFocus, scheme, groupField, onPopupOpen, onPopupClose, maxLength, language, ...props } = this.props; const options = this.removeValuesFromOptions(); const disabled = options.length === 0 || (maxLength && Array.isArray(value) && value.length >= maxLength); const selectedIds: Array = (value || []).map( (option) => option[valueKey] ); let selected = this.props.options.filter( (option) => selectedIds.includes(option[valueKey]) ); if (scheme) { selected = selected.filter((val) => val.scheme === scheme); } return ( {!readOnly && (