| File was renamed from src/editor-fields/fields/selector-field.js |
| | |
| | | const SelectorField = ({ field, value, onChange }) => { |
| | | const subtype = resolveSubtype(field); |
| | | const canCreate = subtype === 'taxonomy'; |
| | | console.log(field); |
| | | |
| | | const buildPath = searchPath(field); |
| | | const ids = Array.isArray(value) ? value : value ? value.split(',') : []; |
| | | |
| | |
| | | |
| | | if (pendingCreate && canCreate && !creating) { |
| | | setCreating(true); |
| | | apiFetch({ path: `/wp/v2/${field.taxonomy}`, method: 'POST', data: createPayload(field, subtype, search) }) |
| | | apiFetch({ path: `/wp/v2/${field.taxonomy}`, method: 'POST', data: createPayload(subtype, search) }) |
| | | .then((item) => { |
| | | const label = labelOf(item, subtype); |
| | | setIdByLabel((prev) => ({ ...prev, [label]: item.id })); |
| | | setTokens((prev) => { |
| | | const finalTokens = prev.filter((t) => t !== createLabel).concat(label); |
| | | const newIds = finalTokens.map((l) => idByLabel[l] ?? options[l] ?? (l === label ? item.id : undefined)) |
| | | const finalTokens = tokens.filter((t) => t !== createLabel).concat(label); |
| | | const newIds = finalTokens |
| | | .map((l) => idByLabel[l] ?? options[l] ?? (l === label ? item.id : undefined)) |
| | | .filter((id) => id !== undefined); |
| | | |
| | | setIdByLabel((prev) => ({ ...prev, [label]: item.id })); |
| | | setTokens(finalTokens); |
| | | commitIds(newIds); |
| | | return finalTokens; |
| | | }); |
| | | }) |
| | | .catch(() => {}) |
| | | .finally(() => setCreating(false)); |