alinea
Version:
[](https://npmjs.org/package/alinea) [](https://packagephobia.com/result?p=alinea)
60 lines (58 loc) • 1.8 kB
JavaScript
import "../../chunks/chunk-U5RRZUYZ.js";
// src/input/link/LinkField.tsx
import { Hint } from "alinea/core";
import { ListField } from "alinea/core/field/ListField";
import { UnionField } from "alinea/core/field/UnionField";
import { entries, fromEntries } from "alinea/core/util/Objects";
var LinkField = class extends UnionField {
};
function createLink(label, options) {
const pickers = entries(options.pickers);
const blocks = fromEntries(
pickers.map(([type, picker]) => [type, picker.shape])
);
const hint = pickers.length === 1 ? pickers[0][1].hint : Hint.Union(pickers.map(([, picker]) => picker.hint));
return new LinkField(blocks, {
hint,
options: { label, ...options },
async postProcess(value, loader) {
const type = value.type;
const picker = options.pickers[type];
if (!picker)
return;
if (picker.postProcess)
await picker.postProcess(value, loader);
}
});
}
var LinksField = class extends ListField {
};
function createLinks(label, options) {
const pickers = entries(options.pickers);
const blocks = fromEntries(
pickers.map(([type, picker]) => [type, picker.shape])
);
const hint = pickers.length === 1 ? pickers[0][1].hint : Hint.Union(pickers.map(([, picker]) => picker.hint));
return new LinksField(blocks, {
hint: Hint.Array(hint),
options: { label, ...options },
async postProcess(rows, loader) {
const tasks = [];
for (const row of rows) {
const type = row.type;
const picker = options.pickers[type];
if (!picker)
return;
if (picker.postProcess)
tasks.push(picker.postProcess(row, loader));
}
await Promise.all(tasks);
}
});
}
export {
LinkField,
LinksField,
createLink,
createLinks
};