import React from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import {SelectFieldPopup} from './SelectFieldPopup'; import {differenceBy, get, cloneDeep} from 'lodash'; import {gettext} from 'core/utils'; 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 { static propTypes: any; static defaultProps: any; addBtn: any; 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) { const {value, field, onChange} = this.props; const newValue = cloneDeep(value); newValue.splice(index, 1); onChange(field, newValue); } 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, ...props} = this.props; const options = this.removeValuesFromOptions(); return ( {!readOnly && (