import { createElement, Component, createContext, Fragment } from '@wordpress/element' import classnames from 'classnames' import { SortableContainer, SortableElement, SortableHandle } from 'react-sortable-hoc' import arrayMove from 'array-move' import OptionsPanel from '../OptionsPanel' import { getValueFromInput } from '../helpers/get-value-from-input' import Select from './ct-select' import nanoid from 'nanoid' const valueWithUniqueIds = value => value.map(singleItem => ({ ...singleItem, ...(singleItem.__id ? {} : { __id: nanoid() }) })) const itemsThatAreNotAdded = (value, option) => Object.keys(option.settings).filter( optionId => !value.find(({ id }) => id === optionId) ) const getDefaultState = () => ({ currentlyPickedItem: null }) const { Provider, Consumer } = createContext(getDefaultState()) const LayerControls = SortableHandle( ({ items, onChange, toggleOptionsPanel, value }) => ( {({ removeForId, addForId, option }) => (
{ ( option.settings[value.id] || { label: value.id } ).label } {option.settings[value.id] && option.settings[value.id].clone && items.filter(({ id }) => id === value.id).length < 2 && ( )} {(option.manageable || (option.settings[value.id] && option.settings[value.id].clone && items.filter(({ id }) => id === value.id).length > 1) || !option.settings[value.id]) && (
)}
) ) class SingleItem extends Component { state = { isOpen: false } render() { const { value, items, onChange } = this.props return ( {({ option }) => (
  • { onChange(items) this.setState(({ isOpen }) => ({ isOpen: !isOpen })) }} /> {option.settings[value.id] && option.settings[value.id].options && this.state.isOpen && (
    { onChange( items.map(l => l.__id === value.__id ? { ...l, [key]: newValue } : l ) ) }} value={getValueFromInput( option.settings[value.id].options, value )} options={option.settings[value.id].options} />
    )}
  • )}
    ) } } const SortableItem = SortableElement(SingleItem) const SortableList = SortableContainer(({ items, onChange }) => ( {({ option }) => ( )} )) const SelectThatAddsItems = ({ value, option }) => { const notAddedItems = itemsThatAreNotAdded(value, option) return notAddedItems.length > 0 ? ( {({ currentlyPickedItem, setCurrentItem, addCurrentlySelectedItem }) => (