import { __ } from 'ct-i18n' import { useRef, useState, Fragment, createElement, createPortal, useEffect, render, } from '@wordpress/element' import $ from 'jquery' import OptionsPanel from '../options/OptionsPanel' import { getValueFromInput } from '../options/helpers/get-value-from-input' const TaxonomyRoot = ({ options, input_name, value, tbody, purpose }) => { const [internalValue, setInternalValue] = useState(value) const input = useRef() useEffect(() => { const cb = (_, __, { data }) => { if (data) { const urlParams = new URLSearchParams(data) if (urlParams.get('action') === 'add-tag') { setInternalValue(value) } } } $(document).on('ajaxComplete', cb) return () => { $(document).off('ajaxComplete', cb) } }, []) return ( {}} name={input_name} type="hidden" ref={input} /> {createPortal( { setInternalValue((internalValue) => ({ ...internalValue, [key]: newValue, })) $(input.current).change() }} purpose={purpose} />, tbody )} ) } export const initTaxonomies = () => { const maybeTaxonomyField = document.querySelector( '[name*="blocksy_taxonomy_meta_options"]' ) if (!maybeTaxonomyField) { return } if (!maybeTaxonomyField.dataset.options) { return } let options = JSON.parse(maybeTaxonomyField.dataset.options) const maybeTable = document.querySelector('.form-table') let tbody = null let purpose = 'taxonomy' if (!maybeTable) { const rootEl = document.querySelector('#addtag') tbody = document.createElement('div') tbody.classList.add('form-field', 'ct-term-screen-create') tbody.textContent = '' rootEl.insertBefore(tbody, rootEl.querySelector('.submit')) purpose = 'default' } else { tbody = maybeTable.querySelector('tbody') } render( , maybeTaxonomyField.parentNode ) }