UNPKG

synapse-react-client

Version:

[![npm version](https://badge.fury.io/js/synapse-react-client.svg)](https://badge.fury.io/js/synapse-react-client) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettie

187 lines (186 loc) 5.12 kB
import { jsx as s, Fragment as I, jsxs as g } from "react/jsx-runtime"; import { isEqual as D } from "lodash-es"; import { useState as h, useEffect as y } from "react"; import w from "../../FullWidthAlert/FullWidthAlert.js"; import { convertToArray as b } from "../AnnotationEditorUtils.js"; import { GridLegacy as v, InputLabel as E } from "@mui/material"; import { AdditionalPropertyContextProvider as B } from "../template/AdditionalPropertyContext.js"; const A = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z/, $ = [ "String", "Integer", "Float", "Boolean", "Datetime" ]; function L(r) { return r.length === 0 ? "String" : r.every( (e) => typeof e == "number" || e === "NaN" // "NaN" is technically a float value ) ? r.every((e) => Number.isInteger(e)) ? "Integer" : "Float" : r.every((e) => typeof e == "boolean") ? "Boolean" : r.every((e) => typeof e == "string") && r.every((e) => !!A.exec(e)) ? "Datetime" : "String"; } function S(r, e) { switch (e) { case "Integer": return r.map( (t) => Number.isNaN(Number(t)) ? 0 : Math.floor(Number(t)) ); case "Float": return r.map((t) => { const a = parseFloat(t); return Number.isNaN(a) ? "NaN" : Number.isInteger(a) ? a.toFixed(1) : a; }); case "Datetime": return r.map((t) => typeof t == "string" && A.exec(t) ? t : (/* @__PURE__ */ new Date()).toISOString()); case "Boolean": return r.map((t) => !!t); default: return r.map((t) => String(t)); } } function O(r) { switch (r) { case "Datetime": return { type: "string", format: "datetime" }; case "Boolean": return { type: "boolean" }; case "Float": return { type: "number" }; case "Integer": return { type: "integer" }; default: return { type: "string" }; } } function V(r) { const { formData: e, onChange: t, registry: a, schema: N, name: c, onDropPropertyClick: m, idSchema: T } = r, p = T.$id, { ArrayField: F } = a.fields, { SelectWidget: x } = a.widgets, [i, l] = h( L(b(e)) ), [o, d] = h(i); y(() => { function n() { if (Array.isArray(e)) { const u = e.length === 0 || e.every((f) => f == null || f == ""), C = S( e, o ); (u || o !== i) && D(e, C) && l(o); } } n(); }, [o]), y(() => { function n() { if (Array.isArray(e)) if (e.every((u) => u == null)) m(c)(new CustomEvent("dropEmptyProperty")); else { const u = S( e, o ); l(o), t(u); } } n(); }, [i]); const P = O(i); return y(() => { Array.isArray(e) || t(b(e)); }, [e, t]), Array.isArray(e) ? /* @__PURE__ */ g( B, { value: { dropProperty: (n) => { m(c)(n); } }, children: [ /* @__PURE__ */ g(v, { item: !0, xs: 2, children: [ /* @__PURE__ */ s(E, { htmlFor: `${p}-type`, children: "Type" }), /* @__PURE__ */ s( x, { name: "Type", id: `${p}-type`, schema: {}, options: { enumOptions: $.map((n) => ({ label: n, value: n })) }, value: i, onChange: (n) => { d(n); }, disabled: r.disabled, readOnly: r.readonly, required: !0, isClearable: !1, onBlur: () => { }, onFocus: () => { }, registry: a, label: "Type" } ) ] }), /* @__PURE__ */ s(v, { item: !0, xs: 7, children: /* @__PURE__ */ s( F, { ...r, uiSchema: { ...r.uiSchema, "ui:title": "Value(s)", items: { ...r.uiSchema?.items, "ui:placeholder": "New Value" } }, schema: { ...N, items: { ...P } } } ) }), i !== o && /* @__PURE__ */ s( w, { variant: "warning", title: "Data may be lost when converting types", description: `Are you sure you want to convert ${c} from ${i} to ${o}? Current values may be lost on conversion.`, primaryButtonConfig: { text: "Convert", onClick: () => { l(o); } }, secondaryButtonConfig: { text: "Cancel", onClick: () => { d(i); } }, isGlobal: !1 } ) ] } ) : /* @__PURE__ */ s(I, {}); } export { V as AdditionalPropertiesSchemaField, O as getSchemaForPropertyType, L as guessPropertyType, S as transformDataFromPropertyType }; //# sourceMappingURL=AdditionalPropertiesSchemaField.js.map