@panneau/fields 5.0.0-alpha.6 → 5.0.0-alpha.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/boolean/BooleanField.d.ts +1 -1
- package/dist/boolean/BooleanField.js +78 -17
- package/dist/boolean/BooleanField.js.map +1 -1
- package/dist/boolean/index.d.ts +1 -1
- package/dist/color/ColorField.d.ts +2 -2
- package/dist/color/ColorField.js.map +1 -1
- package/dist/color/index.d.ts +1 -1
- package/dist/date/DateField.d.ts +1 -1
- package/dist/date/DateField.js.map +1 -1
- package/dist/date/index.d.ts +1 -1
- package/dist/datetime/DateTimeField.d.ts +1 -1
- package/dist/datetime/DateTimeField.js +107 -20
- package/dist/datetime/DateTimeField.js.map +1 -1
- package/dist/datetime/index.d.ts +1 -1
- package/dist/duration/DurationField.d.ts +1 -1
- package/dist/duration/DurationField.js +126 -41
- package/dist/duration/DurationField.js.map +1 -1
- package/dist/duration/index.d.ts +1 -1
- package/dist/email/EmailField.d.ts +2 -2
- package/dist/email/EmailField.js.map +1 -1
- package/dist/email/index.d.ts +1 -1
- package/dist/embed/EmbedField.d.ts +2 -2
- package/dist/embed/EmbedField.js +185 -69
- package/dist/embed/EmbedField.js.map +1 -1
- package/dist/embed/index.d.ts +2 -2
- package/dist/file/FileField.d.ts +2 -2
- package/dist/file/FileField.d.ts.map +1 -1
- package/dist/file/FileField.js +167 -77
- package/dist/file/FileField.js.map +1 -1
- package/dist/file/index.d.ts +2 -2
- package/dist/group/GroupField.d.ts +1 -1
- package/dist/group/GroupField.js.map +1 -1
- package/dist/group/index.d.ts +1 -1
- package/dist/index.d.ts +18 -18
- package/dist/json/JsonField.d.ts +1 -1
- package/dist/json/JsonField.js +104 -22
- package/dist/json/JsonField.js.map +1 -1
- package/dist/json/index.d.ts +1 -1
- package/dist/locales/index.js.map +1 -1
- package/dist/localized/LocalizedField.d.ts +1 -1
- package/dist/localized/LocalizedField.js.map +1 -1
- package/dist/localized/index.d.ts +1 -1
- package/dist/location/LocationField.d.ts +3 -3
- package/dist/location/LocationField.d.ts.map +1 -1
- package/dist/location/LocationField.js +351 -138
- package/dist/location/LocationField.js.map +1 -1
- package/dist/location/LocationMap.d.ts +1 -1
- package/dist/location/LocationMap.d.ts.map +1 -1
- package/dist/location/LocationMap.js +155 -65
- package/dist/location/LocationMap.js.map +1 -1
- package/dist/location/index.d.ts +4 -4
- package/dist/location/leafletCss.js.map +1 -1
- package/dist/location/nominatim.d.ts +1 -1
- package/dist/location/nominatim.js.map +1 -1
- package/dist/missing/MissingField.d.ts +4 -3
- package/dist/missing/MissingField.d.ts.map +1 -1
- package/dist/missing/MissingField.js +3 -2
- package/dist/missing/MissingField.js.map +1 -1
- package/dist/number/NumberField.d.ts +1 -1
- package/dist/number/NumberField.js.map +1 -1
- package/dist/number/index.d.ts +1 -1
- package/dist/range/RangeField.d.ts +1 -1
- package/dist/range/RangeField.js +82 -11
- package/dist/range/RangeField.js.map +1 -1
- package/dist/range/index.d.ts +1 -1
- package/dist/relation/RelationField.d.ts +2 -2
- package/dist/relation/RelationField.js +485 -145
- package/dist/relation/RelationField.js.map +1 -1
- package/dist/relation/index.d.ts +2 -2
- package/dist/repeater/RepeaterField.d.ts +1 -1
- package/dist/repeater/RepeaterField.js +118 -34
- package/dist/repeater/RepeaterField.js.map +1 -1
- package/dist/repeater/index.d.ts +1 -1
- package/dist/select/SelectField.d.ts +1 -1
- package/dist/select/SelectField.js.map +1 -1
- package/dist/select/index.d.ts +1 -1
- package/dist/shared/DefinitionField.d.ts +1 -1
- package/dist/shared/DefinitionField.js.map +1 -1
- package/dist/shared/builtIn.d.ts +1 -1
- package/dist/shared/builtIn.js.map +1 -1
- package/dist/shared/check.js.map +1 -1
- package/dist/shared/context.d.ts +6 -6
- package/dist/shared/context.js.map +1 -1
- package/dist/shared/cx.js.map +1 -1
- package/dist/shared/dates.js.map +1 -1
- package/dist/shared/embed.js.map +1 -1
- package/dist/shared/entry.d.ts +1 -1
- package/dist/shared/entry.js.map +1 -1
- package/dist/shared/relation.js.map +1 -1
- package/dist/slug/SlugField.d.ts +2 -2
- package/dist/slug/SlugField.d.ts.map +1 -1
- package/dist/slug/SlugField.js +184 -36
- package/dist/slug/SlugField.js.map +1 -1
- package/dist/slug/index.d.ts +1 -1
- package/dist/tags/TagsField.d.ts +1 -1
- package/dist/tags/TagsField.js.map +1 -1
- package/dist/tags/index.d.ts +1 -1
- package/dist/tel/TelField.d.ts +2 -2
- package/dist/tel/TelField.js.map +1 -1
- package/dist/tel/index.d.ts +1 -1
- package/dist/text/TextField.d.ts +1 -1
- package/dist/text/TextField.js.map +1 -1
- package/dist/text/index.d.ts +1 -1
- package/dist/textarea/TextAreaField.d.ts +1 -1
- package/dist/textarea/TextAreaField.js.map +1 -1
- package/dist/textarea/index.d.ts +1 -1
- package/dist/time/TimeField.d.ts +1 -1
- package/dist/time/TimeField.js +64 -10
- package/dist/time/TimeField.js.map +1 -1
- package/dist/time/index.d.ts +1 -1
- package/dist/upload/FilePicker.js +107 -24
- package/dist/upload/FilePicker.js.map +1 -1
- package/dist/upload/UploadList.d.ts +1 -1
- package/dist/upload/UploadList.js.map +1 -1
- package/dist/upload/files.js.map +1 -1
- package/dist/upload/useRejectionMessage.d.ts +1 -1
- package/dist/upload/useRejectionMessage.js.map +1 -1
- package/dist/upload/useUploads.d.ts +1 -1
- package/dist/upload/useUploads.js.map +1 -1
- package/dist/uppy/index.d.ts +2 -2
- package/dist/uppy/uploader.d.ts +1 -1
- package/dist/uppy/uploader.js.map +1 -1
- package/dist/url/UrlField.d.ts +2 -2
- package/dist/url/UrlField.js +69 -14
- package/dist/url/UrlField.js.map +1 -1
- package/dist/url/index.d.ts +1 -1
- package/package.json +6 -6
package/dist/shared/builtIn.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { FieldEntry } from './types';
|
|
1
|
+
import type { FieldEntry } from './types.js';
|
|
2
2
|
/**
|
|
3
3
|
* The entries of the built-in field types. `richtext` and `media` come with their plugins
|
|
4
4
|
* (`@panneau/editor` and `@panneau/media`): without them, `DefinitionField` renders a placeholder.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"builtIn.js","names":[],"sources":["../../src/shared/builtIn.ts"],"sourcesContent":["import { fr } from '@panneau/intl';\n\nimport { BooleanFieldEntry } from '../boolean/BooleanField';\nimport { ColorFieldEntry } from '../color/ColorField';\nimport { DateFieldEntry } from '../date/DateField';\nimport { DateTimeFieldEntry } from '../datetime/DateTimeField';\nimport { DurationFieldEntry } from '../duration/DurationField';\nimport { EmailFieldEntry } from '../email/EmailField';\nimport { EmbedFieldEntry } from '../embed/EmbedField';\nimport { FileFieldEntry } from '../file/FileField';\nimport { GroupFieldEntry } from '../group/GroupField';\nimport { JsonFieldEntry } from '../json/JsonField';\nimport { LocationFieldEntry } from '../location/LocationField';\nimport { NumberFieldEntry } from '../number/NumberField';\nimport { RangeFieldEntry } from '../range/RangeField';\nimport { RelationFieldEntry } from '../relation/RelationField';\nimport { RepeaterFieldEntry } from '../repeater/RepeaterField';\nimport { SelectFieldEntry } from '../select/SelectField';\nimport { SlugFieldEntry } from '../slug/SlugField';\nimport { TagsFieldEntry } from '../tags/TagsField';\nimport { TelFieldEntry } from '../tel/TelField';\nimport { TextFieldEntry } from '../text/TextField';\nimport { TextAreaFieldEntry } from '../textarea/TextAreaField';\nimport { TimeFieldEntry } from '../time/TimeField';\nimport { UrlFieldEntry } from '../url/UrlField';\nimport type { FieldEntry } from './types';\n\n/**\n * The entries of the built-in field types. `richtext` and `media` come with their plugins\n * (`@panneau/editor` and `@panneau/media`): without them, `DefinitionField` renders a placeholder.\n * `file` uploads through the upload source of the fields context, or gives the files themselves.\n * `embed` and `location` use its embed source and its maps; `location` loads Leaflet when shown.\n */\nexport const builtInFields: Readonly<Record<string, FieldEntry>> = {\n text: TextFieldEntry,\n textarea: TextAreaFieldEntry,\n number: NumberFieldEntry,\n boolean: BooleanFieldEntry,\n date: DateFieldEntry,\n datetime: DateTimeFieldEntry,\n time: TimeFieldEntry,\n select: SelectFieldEntry,\n email: EmailFieldEntry,\n url: UrlFieldEntry,\n color: ColorFieldEntry,\n slug: SlugFieldEntry,\n file: FileFieldEntry,\n relation: RelationFieldEntry,\n group: GroupFieldEntry,\n repeater: RepeaterFieldEntry,\n tel: TelFieldEntry,\n range: RangeFieldEntry,\n duration: DurationFieldEntry,\n json: JsonFieldEntry,\n tags: TagsFieldEntry,\n embed: EmbedFieldEntry,\n location: LocationFieldEntry,\n};\n\n/**\n * The built-in fields as a `@panneau/core` plugin: their entries, and the French messages of\n * every Panneau package (`@panneau/ui`, `@panneau/fields`, `@panneau/forms`…, from\n * `@panneau/intl`). Pass it before the plugins that override a type.\n */\nexport const fieldsPlugin: {\n id: string;\n fields: Readonly<Record<string, FieldEntry>>;\n locales: Readonly<Record<string, Record<string, string>>>;\n} = {\n id: 'panneau-fields',\n fields: builtInFields,\n locales: { fr },\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCA,
|
|
1
|
+
{"version":3,"file":"builtIn.js","names":["fr","BooleanFieldEntry","ColorFieldEntry","DateFieldEntry","DateTimeFieldEntry","DurationFieldEntry","EmailFieldEntry","EmbedFieldEntry","FileFieldEntry","GroupFieldEntry","JsonFieldEntry","LocationFieldEntry","NumberFieldEntry","RangeFieldEntry","RelationFieldEntry","RepeaterFieldEntry","SelectFieldEntry","SlugFieldEntry","TagsFieldEntry","TelFieldEntry","TextFieldEntry","TextAreaFieldEntry","TimeFieldEntry","UrlFieldEntry","FieldEntry","builtInFields","Readonly","Record","text","textarea","number","boolean","date","datetime","time","select","email","url","color","slug","file","relation","group","repeater","tel","range","duration","json","tags","embed","location","fieldsPlugin","id","fields","locales"],"sources":["../../src/shared/builtIn.ts"],"sourcesContent":["import { fr } from '@panneau/intl';\n\nimport { BooleanFieldEntry } from '../boolean/BooleanField';\nimport { ColorFieldEntry } from '../color/ColorField';\nimport { DateFieldEntry } from '../date/DateField';\nimport { DateTimeFieldEntry } from '../datetime/DateTimeField';\nimport { DurationFieldEntry } from '../duration/DurationField';\nimport { EmailFieldEntry } from '../email/EmailField';\nimport { EmbedFieldEntry } from '../embed/EmbedField';\nimport { FileFieldEntry } from '../file/FileField';\nimport { GroupFieldEntry } from '../group/GroupField';\nimport { JsonFieldEntry } from '../json/JsonField';\nimport { LocationFieldEntry } from '../location/LocationField';\nimport { NumberFieldEntry } from '../number/NumberField';\nimport { RangeFieldEntry } from '../range/RangeField';\nimport { RelationFieldEntry } from '../relation/RelationField';\nimport { RepeaterFieldEntry } from '../repeater/RepeaterField';\nimport { SelectFieldEntry } from '../select/SelectField';\nimport { SlugFieldEntry } from '../slug/SlugField';\nimport { TagsFieldEntry } from '../tags/TagsField';\nimport { TelFieldEntry } from '../tel/TelField';\nimport { TextFieldEntry } from '../text/TextField';\nimport { TextAreaFieldEntry } from '../textarea/TextAreaField';\nimport { TimeFieldEntry } from '../time/TimeField';\nimport { UrlFieldEntry } from '../url/UrlField';\nimport type { FieldEntry } from './types';\n\n/**\n * The entries of the built-in field types. `richtext` and `media` come with their plugins\n * (`@panneau/editor` and `@panneau/media`): without them, `DefinitionField` renders a placeholder.\n * `file` uploads through the upload source of the fields context, or gives the files themselves.\n * `embed` and `location` use its embed source and its maps; `location` loads Leaflet when shown.\n */\nexport const builtInFields: Readonly<Record<string, FieldEntry>> = {\n text: TextFieldEntry,\n textarea: TextAreaFieldEntry,\n number: NumberFieldEntry,\n boolean: BooleanFieldEntry,\n date: DateFieldEntry,\n datetime: DateTimeFieldEntry,\n time: TimeFieldEntry,\n select: SelectFieldEntry,\n email: EmailFieldEntry,\n url: UrlFieldEntry,\n color: ColorFieldEntry,\n slug: SlugFieldEntry,\n file: FileFieldEntry,\n relation: RelationFieldEntry,\n group: GroupFieldEntry,\n repeater: RepeaterFieldEntry,\n tel: TelFieldEntry,\n range: RangeFieldEntry,\n duration: DurationFieldEntry,\n json: JsonFieldEntry,\n tags: TagsFieldEntry,\n embed: EmbedFieldEntry,\n location: LocationFieldEntry,\n};\n\n/**\n * The built-in fields as a `@panneau/core` plugin: their entries, and the French messages of\n * every Panneau package (`@panneau/ui`, `@panneau/fields`, `@panneau/forms`…, from\n * `@panneau/intl`). Pass it before the plugins that override a type.\n */\nexport const fieldsPlugin: {\n id: string;\n fields: Readonly<Record<string, FieldEntry>>;\n locales: Readonly<Record<string, Record<string, string>>>;\n} = {\n id: 'panneau-fields',\n fields: builtInFields,\n locales: { fr },\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCA,IAAayB,gBAAsD;CAC/DG,MAAMR;CACNS,UAAUR;CACVS,QAAQlB;CACRmB,SAAS9B;CACT+B,MAAM7B;CACN8B,UAAU7B;CACV8B,MAAMZ;CACNa,QAAQnB;CACRoB,OAAO9B;CACP+B,KAAKd;CACLe,OAAOpC;CACPqC,MAAMtB;CACNuB,MAAMhC;CACNiC,UAAU3B;CACV4B,OAAOjC;CACPkC,UAAU5B;CACV6B,KAAKzB;CACL0B,OAAOhC;CACPiC,UAAUzC;CACV0C,MAAMrC;CACNsC,MAAM9B;CACN+B,OAAO1C;CACP2C,UAAUvC;AACd;;;;;;AAOA,IAAawC,eAIT;CACAC,IAAI;CACJC,QAAQ5B;CACR6B,SAAS,EAAEtD,GAAG;AAClB"}
|
package/dist/shared/check.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"check.js","names":[],"sources":["../../src/shared/check.ts"],"sourcesContent":["import type { FieldCheckRequest, FieldCheckResponse } from '@panneau/protocol';\n\n/**\n * Where fields with `check` check their value as it is typed: whether a slug is available, and\n * its URL. The value's locale and the form's values come with each check.\n */\nexport interface FieldCheckSource {\n check(\n field: string,\n request: { value: string; locale?: string; values?: Record<string, unknown> },\n options: { signal: AbortSignal },\n ): Promise<FieldCheckResponse>;\n}\n\n/** What `createFieldCheckSource()` needs from an adapter: the `checkField()` of `@panneau/data`. */\nexport interface FieldCheckAdapter {\n checkField?(\n resource: string,\n field: string,\n request: FieldCheckRequest,\n options?: { signal?: AbortSignal },\n ): Promise<FieldCheckResponse>;\n}\n\n/**\n * The check source of a form of a resource, through an adapter (`POST\n * /resources/{resource}/fields/{field}/check`): `id` is the item being edited, or null when\n * creating one. Null when the adapter can't check.\n */\nexport function createFieldCheckSource(\n adapter: FieldCheckAdapter,\n resource: string,\n id: string | null = null,\n): FieldCheckSource | null {\n const { checkField } = adapter;\n if (checkField === undefined) {\n return null;\n }\n return {\n check: (field, request, { signal }) =>\n checkField.call(adapter, resource, field, { ...request, id }, { signal }),\n };\n}\n"],"mappings":";;;;;;AA6BA,
|
|
1
|
+
{"version":3,"file":"check.js","names":["FieldCheckRequest","FieldCheckResponse","FieldCheckSource","check","field","request","value","locale","values","Record","options","signal","AbortSignal","Promise","FieldCheckAdapter","checkField","resource","createFieldCheckSource","adapter","id","undefined","call"],"sources":["../../src/shared/check.ts"],"sourcesContent":["import type { FieldCheckRequest, FieldCheckResponse } from '@panneau/protocol';\n\n/**\n * Where fields with `check` check their value as it is typed: whether a slug is available, and\n * its URL. The value's locale and the form's values come with each check.\n */\nexport interface FieldCheckSource {\n check(\n field: string,\n request: { value: string; locale?: string; values?: Record<string, unknown> },\n options: { signal: AbortSignal },\n ): Promise<FieldCheckResponse>;\n}\n\n/** What `createFieldCheckSource()` needs from an adapter: the `checkField()` of `@panneau/data`. */\nexport interface FieldCheckAdapter {\n checkField?(\n resource: string,\n field: string,\n request: FieldCheckRequest,\n options?: { signal?: AbortSignal },\n ): Promise<FieldCheckResponse>;\n}\n\n/**\n * The check source of a form of a resource, through an adapter (`POST\n * /resources/{resource}/fields/{field}/check`): `id` is the item being edited, or null when\n * creating one. Null when the adapter can't check.\n */\nexport function createFieldCheckSource(\n adapter: FieldCheckAdapter,\n resource: string,\n id: string | null = null,\n): FieldCheckSource | null {\n const { checkField } = adapter;\n if (checkField === undefined) {\n return null;\n }\n return {\n check: (field, request, { signal }) =>\n checkField.call(adapter, resource, field, { ...request, id }, { signal }),\n };\n}\n"],"mappings":";;;;;;AA6BA,SAAgBiB,uBACZC,SACAF,UACAG,KAAoB,MACG;CACvB,MAAM,EAAEJ,eAAeG;CACvB,IAAIH,eAAeK,KAAAA,GACf,OAAO;CAEX,OAAO,EACHjB,QAAQC,OAAOC,SAAS,EAAEM,aACtBI,WAAWM,KAAKH,SAASF,UAAUZ,OAAO;EAAE,GAAGC;EAASc;CAAG,GAAG,EAAER,OAAO,CAAC,EAChF;AACJ"}
|
package/dist/shared/context.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { type ComponentType, type ReactNode } from 'react';
|
|
2
2
|
import type { Field } from '@panneau/protocol';
|
|
3
|
-
import type { MapsConfig } from '../location/types';
|
|
4
|
-
import type { UploadSource } from '../upload/types';
|
|
5
|
-
import type { FieldCheckSource } from './check';
|
|
6
|
-
import type { EmbedSource } from './embed';
|
|
7
|
-
import type { RelationSource } from './relation';
|
|
8
|
-
import type { FieldEntry } from './types';
|
|
3
|
+
import type { MapsConfig } from '../location/types.js';
|
|
4
|
+
import type { UploadSource } from '../upload/types.js';
|
|
5
|
+
import type { FieldCheckSource } from './check.js';
|
|
6
|
+
import type { EmbedSource } from './embed.js';
|
|
7
|
+
import type { RelationSource } from './relation.js';
|
|
8
|
+
import type { FieldEntry } from './types.js';
|
|
9
9
|
/** What a binding gives a field: its value, handlers and errors at a path. */
|
|
10
10
|
export interface BoundFieldProps {
|
|
11
11
|
value: unknown;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.js","names":["value","children","_c"],"sources":["../../src/shared/context.tsx"],"sourcesContent":["import { type ComponentType, type ReactNode, createContext, use } from 'react';\n\nimport type { Field } from '@panneau/protocol';\n\nimport type { MapsConfig } from '../location/types';\nimport type { UploadSource } from '../upload/types';\nimport type { FieldCheckSource } from './check';\nimport type { EmbedSource } from './embed';\nimport type { RelationSource } from './relation';\nimport type { FieldEntry } from './types';\n\n/** What a binding gives a field: its value, handlers and errors at a path. */\nexport interface BoundFieldProps {\n value: unknown;\n onChange: (value: unknown) => void;\n onBlur?: () => void;\n errors?: readonly string[];\n}\n\n/** Props of a binding: it decides whether a field shows, and where its value comes from. */\nexport interface FieldBindingProps {\n field: Field;\n /** Dot path of the field's value. */\n path: string;\n /** Dot path of the object the field belongs to: `''` at the top, `blocks.2` in a block. */\n parentPath: string;\n /** Renders the field with its bound value. */\n render: (bound: BoundFieldProps) => ReactNode;\n}\n\n/**\n * Binds nested fields (in groups, blocks and localized values) to a store by path, instead of the\n * value and `onChange` of their parent. `@panneau/forms` binds them to its form state, so that\n * every value is set, validated and shown in error by its own path.\n */\nexport type FieldBinding = ComponentType<FieldBindingProps>;\n\nexport interface FieldsContextValue {\n /** Registry entries, by field type, over the built-in ones. */\n components: Readonly<Record<string, FieldEntry>>;\n /** Locales of localized values, in order. */\n locales: readonly string[];\n /** Where relation fields search for items. */\n relationSource: RelationSource | null;\n /**\n * Where fields that take files upload them directly to storage. Without one, they give the\n * files themselves.\n */\n uploadSource: UploadSource | null;\n /** Where fields with `check` check their value, such as a slug's availability. */\n checkSource: FieldCheckSource | null;\n /** Where `embed` fields get what a URL embeds. Without one, they keep the URL alone. */\n embedSource: EmbedSource | null;\n /** The tiles and the address search of `location` fields. */\n maps: MapsConfig | null;\n binding: FieldBinding | null;\n}\n\nconst FieldsContext = createContext<FieldsContextValue>({\n components: {},\n locales: [],\n relationSource: null,\n uploadSource: null,\n checkSource: null,\n embedSource: null,\n maps: null,\n binding: null,\n});\n\n/** The fields context of the closest `FieldsProvider`. */\nexport function useFieldsContext(): FieldsContextValue {\n return use(FieldsContext);\n}\n\nexport interface FieldsProviderProps {\n /** Registry entries by type, added over those of a parent provider. */\n components?: Readonly<Record<string, FieldEntry>>;\n locales?: readonly string[];\n relationSource?: RelationSource | null;\n /** Make it once, outside of render: fields make an uploader for each source they get. */\n uploadSource?: UploadSource | null;\n /** `createFieldCheckSource(adapter, resource, id)`, for the form of an item. */\n checkSource?: FieldCheckSource | null;\n /** `createEmbedSource(adapter)`. */\n embedSource?: EmbedSource | null;\n /** The tiles and the address search of `location` fields. */\n maps?: MapsConfig | null;\n binding?: FieldBinding | null;\n children?: ReactNode;\n}\n\n/**\n * Provides what fields rendered from a definition need: the components of each type, the locales\n * of localized values, the sources of relations, uploads, checks and embeds, the maps of\n * locations, and a binding. Values not given come from the parent provider.\n */\nexport function FieldsProvider({\n components,\n locales,\n relationSource,\n uploadSource,\n checkSource,\n embedSource,\n maps,\n binding,\n children,\n}: FieldsProviderProps) {\n const parent = use(FieldsContext);\n const value: FieldsContextValue = {\n components:\n components !== undefined ? { ...parent.components, ...components } : parent.components,\n locales: locales ?? parent.locales,\n relationSource: relationSource === undefined ? parent.relationSource : relationSource,\n uploadSource: uploadSource === undefined ? parent.uploadSource : uploadSource,\n checkSource: checkSource === undefined ? parent.checkSource : checkSource,\n embedSource: embedSource === undefined ? parent.embedSource : embedSource,\n maps: maps === undefined ? parent.maps : maps,\n binding: binding === undefined ? parent.binding : binding,\n };\n return <FieldsContext value={value}>{children}</FieldsContext>;\n}\n\n/** The parent path of a path: `blocks.2` for `blocks.2.title`, `''` for `title`. */\nexport function getParentPath(path: string): string {\n const index = path.lastIndexOf('.');\n return index === -1 ? '' : path.slice(0, index);\n}\n"],"mappings":";;;;AA0DA,
|
|
1
|
+
{"version":3,"file":"context.js","names":["ComponentType","ReactNode","createContext","use","Field","MapsConfig","UploadSource","FieldCheckSource","EmbedSource","RelationSource","FieldEntry","BoundFieldProps","value","onChange","onBlur","errors","FieldBindingProps","field","path","parentPath","render","bound","FieldBinding","FieldsContextValue","components","Readonly","Record","locales","relationSource","uploadSource","checkSource","embedSource","maps","binding","FieldsContext","useFieldsContext","FieldsProviderProps","children","FieldsProvider","t0","$","_c","parent","t1","undefined","t2","t3","t4","t5","t6","t7","t8","t9","t10","getParentPath","index","lastIndexOf","slice","_c"],"sources":["../../src/shared/context.tsx"],"sourcesContent":["import { type ComponentType, type ReactNode, createContext, use } from 'react';\n\nimport type { Field } from '@panneau/protocol';\n\nimport type { MapsConfig } from '../location/types';\nimport type { UploadSource } from '../upload/types';\nimport type { FieldCheckSource } from './check';\nimport type { EmbedSource } from './embed';\nimport type { RelationSource } from './relation';\nimport type { FieldEntry } from './types';\n\n/** What a binding gives a field: its value, handlers and errors at a path. */\nexport interface BoundFieldProps {\n value: unknown;\n onChange: (value: unknown) => void;\n onBlur?: () => void;\n errors?: readonly string[];\n}\n\n/** Props of a binding: it decides whether a field shows, and where its value comes from. */\nexport interface FieldBindingProps {\n field: Field;\n /** Dot path of the field's value. */\n path: string;\n /** Dot path of the object the field belongs to: `''` at the top, `blocks.2` in a block. */\n parentPath: string;\n /** Renders the field with its bound value. */\n render: (bound: BoundFieldProps) => ReactNode;\n}\n\n/**\n * Binds nested fields (in groups, blocks and localized values) to a store by path, instead of the\n * value and `onChange` of their parent. `@panneau/forms` binds them to its form state, so that\n * every value is set, validated and shown in error by its own path.\n */\nexport type FieldBinding = ComponentType<FieldBindingProps>;\n\nexport interface FieldsContextValue {\n /** Registry entries, by field type, over the built-in ones. */\n components: Readonly<Record<string, FieldEntry>>;\n /** Locales of localized values, in order. */\n locales: readonly string[];\n /** Where relation fields search for items. */\n relationSource: RelationSource | null;\n /**\n * Where fields that take files upload them directly to storage. Without one, they give the\n * files themselves.\n */\n uploadSource: UploadSource | null;\n /** Where fields with `check` check their value, such as a slug's availability. */\n checkSource: FieldCheckSource | null;\n /** Where `embed` fields get what a URL embeds. Without one, they keep the URL alone. */\n embedSource: EmbedSource | null;\n /** The tiles and the address search of `location` fields. */\n maps: MapsConfig | null;\n binding: FieldBinding | null;\n}\n\nconst FieldsContext = createContext<FieldsContextValue>({\n components: {},\n locales: [],\n relationSource: null,\n uploadSource: null,\n checkSource: null,\n embedSource: null,\n maps: null,\n binding: null,\n});\n\n/** The fields context of the closest `FieldsProvider`. */\nexport function useFieldsContext(): FieldsContextValue {\n return use(FieldsContext);\n}\n\nexport interface FieldsProviderProps {\n /** Registry entries by type, added over those of a parent provider. */\n components?: Readonly<Record<string, FieldEntry>>;\n locales?: readonly string[];\n relationSource?: RelationSource | null;\n /** Make it once, outside of render: fields make an uploader for each source they get. */\n uploadSource?: UploadSource | null;\n /** `createFieldCheckSource(adapter, resource, id)`, for the form of an item. */\n checkSource?: FieldCheckSource | null;\n /** `createEmbedSource(adapter)`. */\n embedSource?: EmbedSource | null;\n /** The tiles and the address search of `location` fields. */\n maps?: MapsConfig | null;\n binding?: FieldBinding | null;\n children?: ReactNode;\n}\n\n/**\n * Provides what fields rendered from a definition need: the components of each type, the locales\n * of localized values, the sources of relations, uploads, checks and embeds, the maps of\n * locations, and a binding. Values not given come from the parent provider.\n */\nexport function FieldsProvider({\n components,\n locales,\n relationSource,\n uploadSource,\n checkSource,\n embedSource,\n maps,\n binding,\n children,\n}: FieldsProviderProps) {\n const parent = use(FieldsContext);\n const value: FieldsContextValue = {\n components:\n components !== undefined ? { ...parent.components, ...components } : parent.components,\n locales: locales ?? parent.locales,\n relationSource: relationSource === undefined ? parent.relationSource : relationSource,\n uploadSource: uploadSource === undefined ? parent.uploadSource : uploadSource,\n checkSource: checkSource === undefined ? parent.checkSource : checkSource,\n embedSource: embedSource === undefined ? parent.embedSource : embedSource,\n maps: maps === undefined ? parent.maps : maps,\n binding: binding === undefined ? parent.binding : binding,\n };\n return <FieldsContext value={value}>{children}</FieldsContext>;\n}\n\n/** The parent path of a path: `blocks.2` for `blocks.2.title`, `''` for `title`. */\nexport function getParentPath(path: string): string {\n const index = path.lastIndexOf('.');\n return index === -1 ? '' : path.slice(0, index);\n}\n"],"mappings":";;;;AA0DA,IAAMkC,gBAAgBhC,cAAkC;CACpDsB,YAAY,CAAC;CACbG,SAAS,CAAA;CACTC,gBAAgB;CAChBC,cAAc;CACdC,aAAa;CACbC,aAAa;CACbC,MAAM;CACNC,SAAS;AACb,CAAC;;AAGD,SAAgBE,mBAAuC;CACnD,OAAOhC,IAAI+B,aAAa;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EACb;EAACX,EAAA,KAAAG;EAAAH,EAAA,KAAAK;EAAAL,EAAA,KAAAM;EAAAN,EAAA,KAAAO;EAAAP,EAAA,KAAAQ;EAAAR,EAAA,KAAAS;EAAAT,EAAA,KAAAU;EAAAV,EAAA,MAAAW;EAAAX,EAAA,MAAAY;CAAA,OAAAA,KAAAZ,EAAA;CAVD,MAAA5B,QAAkCwC;CAUhC,IAAAC;CAAA,IAAAb,EAAA,QAAAH,YAAAG,EAAA,QAAA5B,OAAA;EACKyC,MAAA,oBAAC,eAAD;GAAsBzC;GAAQyB;EAAhB,CAAA;EAAyCG,EAAA,MAAAH;EAAAG,EAAA,MAAA5B;EAAA4B,EAAA,MAAAa;CAAA,OAAAA,MAAAb,EAAA;CAAA,OAAvDa;AAAuD;;AAIlE,SAAgBC,cAAcpC,MAAsB;CAChD,MAAMqC,QAAQrC,KAAKsC,YAAY,GAAG;CAClC,OAAOD,UAAU,KAAK,KAAKrC,KAAKuC,MAAM,GAAGF,KAAK;AAClD"}
|
package/dist/shared/cx.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cx.js","names":[],"sources":["../../src/shared/cx.ts"],"sourcesContent":["/** Joins class names, skipping empty ones. */\nexport function cx(...classNames: (string | false | null | undefined)[]): string {\n return classNames.filter(Boolean).join(' ');\n}\n"],"mappings":";;AACA,
|
|
1
|
+
{"version":3,"file":"cx.js","names":["cx","classNames","filter","Boolean","join"],"sources":["../../src/shared/cx.ts"],"sourcesContent":["/** Joins class names, skipping empty ones. */\nexport function cx(...classNames: (string | false | null | undefined)[]): string {\n return classNames.filter(Boolean).join(' ');\n}\n"],"mappings":";;AACA,SAAgBA,GAAG,GAAGC,YAA2D;CAC7E,OAAOA,WAAWC,OAAOC,OAAO,CAAC,CAACC,KAAK,GAAG;AAC9C"}
|
package/dist/shared/dates.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dates.js","names":[],"sources":["../../src/shared/dates.ts"],"sourcesContent":["import {\n type CalendarDate,\n type CalendarDateTime,\n type Time,\n type ZonedDateTime,\n getLocalTimeZone,\n parseAbsolute,\n parseDate,\n parseDateTime,\n parseTime,\n} from '@internationalized/date';\n\n/** `2026-10-15`, or the date of a date-time. */\nexport function toCalendarDate(value: string | null | undefined): CalendarDate | null {\n const match = value?.match(/^(\\d{4}-\\d{2}-\\d{2})/);\n if (match?.[1] === undefined) {\n return null;\n }\n try {\n return parseDate(match[1]);\n } catch {\n return null;\n }\n}\n\nconst OFFSET = /(?:Z|[+-]\\d{2}:?\\d{2})$/i;\n\n/**\n * A date-time: with an offset or `Z` (`2026-10-15T13:30:00Z`), in the given time zone; without one\n * (`2026-10-15 09:30:00`), as is.\n */\nexport function toDateTime(\n value: string | null | undefined,\n timeZone: string = getLocalTimeZone(),\n): ZonedDateTime | CalendarDateTime | null {\n if (value === null || value === undefined || value.trim() === '') {\n return null;\n }\n const iso = value.trim().replace(' ', 'T');\n try {\n return OFFSET.test(iso) ? parseAbsolute(iso, timeZone) : parseDateTime(iso);\n } catch {\n return null;\n }\n}\n\n/** Writes a date-time: in UTC (`…Z`) when it has a time zone, without an offset otherwise. */\nexport function fromDateTime(value: ZonedDateTime | CalendarDateTime): string {\n return 'timeZone' in value\n ? value.toAbsoluteString().replace(/\\.000Z$/, 'Z')\n : value.toString();\n}\n\n/** `09:30` or `09:30:15`. */\nexport function toTime(value: string | null | undefined): Time | null {\n if (value === null || value === undefined || !/^\\d{1,2}:\\d{2}(:\\d{2})?/.test(value)) {\n return null;\n }\n try {\n return parseTime(value.length < 5 ? `0${value}` : value);\n } catch {\n return null;\n }\n}\n\n/** Writes a time as `HH:mm`, or `HH:mm:ss` with seconds. */\nexport function fromTime(\n value: Pick<Time, 'hour' | 'minute' | 'second'>,\n withSeconds = false,\n): string {\n const pad = (part: number) => String(part).padStart(2, '0');\n const time = `${pad(value.hour)}:${pad(value.minute)}`;\n return withSeconds ? `${time}:${pad(value.second)}` : time;\n}\n"],"mappings":";;;AAaA,
|
|
1
|
+
{"version":3,"file":"dates.js","names":["CalendarDate","CalendarDateTime","Time","ZonedDateTime","getLocalTimeZone","parseAbsolute","parseDate","parseDateTime","parseTime","toCalendarDate","value","match","undefined","OFFSET","toDateTime","timeZone","trim","iso","replace","test","fromDateTime","toAbsoluteString","toString","toTime","length","fromTime","Pick","withSeconds","pad","part","String","padStart","time","hour","minute","second"],"sources":["../../src/shared/dates.ts"],"sourcesContent":["import {\n type CalendarDate,\n type CalendarDateTime,\n type Time,\n type ZonedDateTime,\n getLocalTimeZone,\n parseAbsolute,\n parseDate,\n parseDateTime,\n parseTime,\n} from '@internationalized/date';\n\n/** `2026-10-15`, or the date of a date-time. */\nexport function toCalendarDate(value: string | null | undefined): CalendarDate | null {\n const match = value?.match(/^(\\d{4}-\\d{2}-\\d{2})/);\n if (match?.[1] === undefined) {\n return null;\n }\n try {\n return parseDate(match[1]);\n } catch {\n return null;\n }\n}\n\nconst OFFSET = /(?:Z|[+-]\\d{2}:?\\d{2})$/i;\n\n/**\n * A date-time: with an offset or `Z` (`2026-10-15T13:30:00Z`), in the given time zone; without one\n * (`2026-10-15 09:30:00`), as is.\n */\nexport function toDateTime(\n value: string | null | undefined,\n timeZone: string = getLocalTimeZone(),\n): ZonedDateTime | CalendarDateTime | null {\n if (value === null || value === undefined || value.trim() === '') {\n return null;\n }\n const iso = value.trim().replace(' ', 'T');\n try {\n return OFFSET.test(iso) ? parseAbsolute(iso, timeZone) : parseDateTime(iso);\n } catch {\n return null;\n }\n}\n\n/** Writes a date-time: in UTC (`…Z`) when it has a time zone, without an offset otherwise. */\nexport function fromDateTime(value: ZonedDateTime | CalendarDateTime): string {\n return 'timeZone' in value\n ? value.toAbsoluteString().replace(/\\.000Z$/, 'Z')\n : value.toString();\n}\n\n/** `09:30` or `09:30:15`. */\nexport function toTime(value: string | null | undefined): Time | null {\n if (value === null || value === undefined || !/^\\d{1,2}:\\d{2}(:\\d{2})?/.test(value)) {\n return null;\n }\n try {\n return parseTime(value.length < 5 ? `0${value}` : value);\n } catch {\n return null;\n }\n}\n\n/** Writes a time as `HH:mm`, or `HH:mm:ss` with seconds. */\nexport function fromTime(\n value: Pick<Time, 'hour' | 'minute' | 'second'>,\n withSeconds = false,\n): string {\n const pad = (part: number) => String(part).padStart(2, '0');\n const time = `${pad(value.hour)}:${pad(value.minute)}`;\n return withSeconds ? `${time}:${pad(value.second)}` : time;\n}\n"],"mappings":";;;AAaA,SAAgBS,eAAeC,OAAuD;CAClF,MAAMC,QAAQD,OAAOC,MAAM,sBAAsB;CACjD,IAAIA,QAAQ,OAAOC,KAAAA,GACf,OAAO;CAEX,IAAI;EACA,OAAON,UAAUK,MAAM,EAAE;CAC7B,QAAQ;EACJ,OAAO;CACX;AACJ;AAEA,IAAME,SAAS;;;;;AAMf,SAAgBC,WACZJ,OACAK,WAAmBX,iBAAiB,GACG;CACvC,IAAIM,UAAU,QAAQA,UAAUE,KAAAA,KAAaF,MAAMM,KAAK,MAAM,IAC1D,OAAO;CAEX,MAAMC,MAAMP,MAAMM,KAAK,CAAC,CAACE,QAAQ,KAAK,GAAG;CACzC,IAAI;EACA,OAAOL,OAAOM,KAAKF,GAAG,IAAIZ,cAAcY,KAAKF,QAAQ,IAAIR,cAAcU,GAAG;CAC9E,QAAQ;EACJ,OAAO;CACX;AACJ;;AAGA,SAAgBG,aAAaV,OAAiD;CAC1E,OAAO,cAAcA,QACfA,MAAMW,iBAAiB,CAAC,CAACH,QAAQ,WAAW,GAAG,IAC/CR,MAAMY,SAAS;AACzB;;AAGA,SAAgBC,OAAOb,OAA+C;CAClE,IAAIA,UAAU,QAAQA,UAAUE,KAAAA,KAAa,CAAC,0BAA0BO,KAAKT,KAAK,GAC9E,OAAO;CAEX,IAAI;EACA,OAAOF,UAAUE,MAAMc,SAAS,IAAI,IAAId,UAAUA,KAAK;CAC3D,QAAQ;EACJ,OAAO;CACX;AACJ;;AAGA,SAAgBe,SACZf,OACAiB,cAAc,OACR;CACN,MAAMC,OAAOC,SAAiBC,OAAOD,IAAI,CAAC,CAACE,SAAS,GAAG,GAAG;CAC1D,MAAMC,OAAO,GAAGJ,IAAIlB,MAAMuB,IAAI,EAAC,GAAIL,IAAIlB,MAAMwB,MAAM;CACnD,OAAOP,cAAc,GAAGK,KAAI,GAAIJ,IAAIlB,MAAMyB,MAAM,MAAMH;AAC1D"}
|
package/dist/shared/embed.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"embed.js","names":[],"sources":["../../src/shared/embed.ts"],"sourcesContent":["import type { EmbedValue } from '@panneau/protocol';\n\n/** Where `embed` fields get what a URL embeds: the backend, from the provider's oEmbed. */\nexport interface EmbedSource {\n resolve(url: string, options: { signal: AbortSignal }): Promise<EmbedValue>;\n}\n\n/** What `createEmbedSource()` needs from an adapter: the `resolveEmbed()` of `@panneau/data`. */\nexport interface EmbedAdapter {\n resolveEmbed?(url: string, options?: { signal?: AbortSignal }): Promise<EmbedValue>;\n}\n\n/**\n * The embed source of an adapter (`GET /embeds?url=`). Null when the adapter can't resolve\n * embeds: `embed` fields then keep the URL alone.\n */\nexport function createEmbedSource(adapter: EmbedAdapter): EmbedSource | null {\n const { resolveEmbed } = adapter;\n if (resolveEmbed === undefined) {\n return null;\n }\n return { resolve: (url, { signal }) => resolveEmbed.call(adapter, url, { signal }) };\n}\n"],"mappings":";;;;;AAgBA,
|
|
1
|
+
{"version":3,"file":"embed.js","names":["EmbedValue","EmbedSource","resolve","url","options","signal","AbortSignal","Promise","EmbedAdapter","resolveEmbed","createEmbedSource","adapter","undefined","call"],"sources":["../../src/shared/embed.ts"],"sourcesContent":["import type { EmbedValue } from '@panneau/protocol';\n\n/** Where `embed` fields get what a URL embeds: the backend, from the provider's oEmbed. */\nexport interface EmbedSource {\n resolve(url: string, options: { signal: AbortSignal }): Promise<EmbedValue>;\n}\n\n/** What `createEmbedSource()` needs from an adapter: the `resolveEmbed()` of `@panneau/data`. */\nexport interface EmbedAdapter {\n resolveEmbed?(url: string, options?: { signal?: AbortSignal }): Promise<EmbedValue>;\n}\n\n/**\n * The embed source of an adapter (`GET /embeds?url=`). Null when the adapter can't resolve\n * embeds: `embed` fields then keep the URL alone.\n */\nexport function createEmbedSource(adapter: EmbedAdapter): EmbedSource | null {\n const { resolveEmbed } = adapter;\n if (resolveEmbed === undefined) {\n return null;\n }\n return { resolve: (url, { signal }) => resolveEmbed.call(adapter, url, { signal }) };\n}\n"],"mappings":";;;;;AAgBA,SAAgBU,kBAAkBC,SAA2C;CACzE,MAAM,EAAEF,iBAAiBE;CACzB,IAAIF,iBAAiBG,KAAAA,GACjB,OAAO;CAEX,OAAO,EAAEV,UAAUC,KAAK,EAAEE,aAAaI,aAAaI,KAAKF,SAASR,KAAK,EAAEE,OAAO,CAAC,EAAE;AACvF"}
|
package/dist/shared/entry.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Field, Rule } from '@panneau/protocol';
|
|
2
|
-
import type { FieldEntryProps, FieldProps } from './types';
|
|
2
|
+
import type { FieldEntryProps, FieldProps } from './types.js';
|
|
3
3
|
/** The rule of a field with this name, if any. */
|
|
4
4
|
export declare function findRule(field: Field, rule: Rule['rule']): Rule | undefined;
|
|
5
5
|
/** The number of a `min` or `max` rule of a field, if any. */
|
package/dist/shared/entry.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entry.js","names":[],"sources":["../../src/shared/entry.ts"],"sourcesContent":["import type { Field, Rule } from '@panneau/protocol';\n\nimport type { FieldEntryProps, FieldProps } from './types';\n\n/** The rule of a field with this name, if any. */\nexport function findRule(field: Field, rule: Rule['rule']): Rule | undefined {\n return field.rules?.find((candidate) => candidate.rule === rule);\n}\n\n/** The number of a `min` or `max` rule of a field, if any. */\nexport function getRuleNumber(field: Field, rule: 'min' | 'max'): number | undefined {\n const found = findRule(field, rule);\n return found !== undefined && 'value' in found && typeof found.value === 'number'\n ? found.value\n : undefined;\n}\n\n/** Whether a field has the `required` rule. */\nexport function isRequired(field: Field): boolean {\n return findRule(field, 'required') !== undefined;\n}\n\n/** A setting of a field (`settings.<name>`), when it has the expected type. */\nexport function getSetting<T>(field: Field, name: string, guard: (value: unknown) => value is T) {\n const value = field.settings?.[name];\n return guard(value) ? value : undefined;\n}\n\nexport const isString = (value: unknown): value is string => typeof value === 'string';\nexport const isNumber = (value: unknown): value is number =>\n typeof value === 'number' && !Number.isNaN(value);\n\n/**\n * The props that every field takes, from a registry entry's props: label (the field's name\n * without one), help, required mark, read-only, errors and handlers. Plugins use it to write their\n * own entries.\n */\nexport function getFieldProps(\n props: FieldEntryProps,\n): Omit<FieldProps<never>, 'value' | 'onChange'> & { placeholder?: string } {\n const { field } = props;\n return {\n name: props.path,\n // A field always has a label: its name when the definition gives none.\n label: field.label ?? field.name,\n description: field.help,\n ...(field.placeholder !== undefined ? { placeholder: field.placeholder } : {}),\n required: isRequired(field),\n readOnly: props.readOnly === true || field.readOnly === true,\n disabled: props.disabled === true,\n errors: props.errors ?? [],\n ...(props.onBlur !== undefined ? { onBlur: props.onBlur } : {}),\n ...(props.className !== undefined ? { className: props.className } : {}),\n };\n}\n\n/** The value as a string, or `null`. */\nexport function asString(value: unknown): string | null {\n return typeof value === 'string' ? value : null;\n}\n\n/** The accepted MIME types of a field that takes files: its `accept`, or its `accept` rule. */\nexport function getAccept(field: Field): string[] | null {\n if (field.accept !== undefined && field.accept.length > 0) {\n return field.accept;\n }\n const rule = findRule(field, 'accept');\n return rule !== undefined && 'value' in rule && Array.isArray(rule.value)\n ? rule.value.filter(isString)\n : null;\n}\n\n/** The largest file of a field, in bytes: its `maxSize` rule. */\nexport function getMaxSize(field: Field): number | null {\n const rule = findRule(field, 'maxSize');\n return rule !== undefined && 'value' in rule && isNumber(rule.value) ? rule.value : null;\n}\n"],"mappings":";;AAKA,
|
|
1
|
+
{"version":3,"file":"entry.js","names":["Field","Rule","FieldEntryProps","FieldProps","findRule","field","rule","rules","find","candidate","getRuleNumber","found","undefined","value","isRequired","getSetting","name","guard","T","settings","isString","isNumber","Number","isNaN","getFieldProps","props","Omit","placeholder","path","label","description","help","required","readOnly","disabled","errors","onBlur","className","asString","getAccept","accept","length","Array","isArray","filter","getMaxSize"],"sources":["../../src/shared/entry.ts"],"sourcesContent":["import type { Field, Rule } from '@panneau/protocol';\n\nimport type { FieldEntryProps, FieldProps } from './types';\n\n/** The rule of a field with this name, if any. */\nexport function findRule(field: Field, rule: Rule['rule']): Rule | undefined {\n return field.rules?.find((candidate) => candidate.rule === rule);\n}\n\n/** The number of a `min` or `max` rule of a field, if any. */\nexport function getRuleNumber(field: Field, rule: 'min' | 'max'): number | undefined {\n const found = findRule(field, rule);\n return found !== undefined && 'value' in found && typeof found.value === 'number'\n ? found.value\n : undefined;\n}\n\n/** Whether a field has the `required` rule. */\nexport function isRequired(field: Field): boolean {\n return findRule(field, 'required') !== undefined;\n}\n\n/** A setting of a field (`settings.<name>`), when it has the expected type. */\nexport function getSetting<T>(field: Field, name: string, guard: (value: unknown) => value is T) {\n const value = field.settings?.[name];\n return guard(value) ? value : undefined;\n}\n\nexport const isString = (value: unknown): value is string => typeof value === 'string';\nexport const isNumber = (value: unknown): value is number =>\n typeof value === 'number' && !Number.isNaN(value);\n\n/**\n * The props that every field takes, from a registry entry's props: label (the field's name\n * without one), help, required mark, read-only, errors and handlers. Plugins use it to write their\n * own entries.\n */\nexport function getFieldProps(\n props: FieldEntryProps,\n): Omit<FieldProps<never>, 'value' | 'onChange'> & { placeholder?: string } {\n const { field } = props;\n return {\n name: props.path,\n // A field always has a label: its name when the definition gives none.\n label: field.label ?? field.name,\n description: field.help,\n ...(field.placeholder !== undefined ? { placeholder: field.placeholder } : {}),\n required: isRequired(field),\n readOnly: props.readOnly === true || field.readOnly === true,\n disabled: props.disabled === true,\n errors: props.errors ?? [],\n ...(props.onBlur !== undefined ? { onBlur: props.onBlur } : {}),\n ...(props.className !== undefined ? { className: props.className } : {}),\n };\n}\n\n/** The value as a string, or `null`. */\nexport function asString(value: unknown): string | null {\n return typeof value === 'string' ? value : null;\n}\n\n/** The accepted MIME types of a field that takes files: its `accept`, or its `accept` rule. */\nexport function getAccept(field: Field): string[] | null {\n if (field.accept !== undefined && field.accept.length > 0) {\n return field.accept;\n }\n const rule = findRule(field, 'accept');\n return rule !== undefined && 'value' in rule && Array.isArray(rule.value)\n ? rule.value.filter(isString)\n : null;\n}\n\n/** The largest file of a field, in bytes: its `maxSize` rule. */\nexport function getMaxSize(field: Field): number | null {\n const rule = findRule(field, 'maxSize');\n return rule !== undefined && 'value' in rule && isNumber(rule.value) ? rule.value : null;\n}\n"],"mappings":";;AAKA,SAAgBI,SAASC,OAAcC,MAAsC;CACzE,OAAOD,MAAME,OAAOC,MAAMC,cAAcA,UAAUH,SAASA,IAAI;AACnE;;AAGA,SAAgBI,cAAcL,OAAcC,MAAyC;CACjF,MAAMK,QAAQP,SAASC,OAAOC,IAAI;CAClC,OAAOK,UAAUC,KAAAA,KAAa,WAAWD,SAAS,OAAOA,MAAME,UAAU,WACnEF,MAAME,QACND,KAAAA;AACV;;AAGA,SAAgBE,WAAWT,OAAuB;CAC9C,OAAOD,SAASC,OAAO,UAAU,MAAMO,KAAAA;AAC3C;;AAGA,SAAgBG,WAAcV,OAAcW,MAAcC,OAAuC;CAC7F,MAAMJ,QAAQR,MAAMc,WAAWH;CAC/B,OAAOC,MAAMJ,KAAK,IAAIA,QAAQD,KAAAA;AAClC;AAEA,IAAaQ,YAAYP,UAAoC,OAAOA,UAAU;AAC9E,IAAaQ,YAAYR,UACrB,OAAOA,UAAU,YAAY,CAACS,OAAOC,MAAMV,KAAK;;;;;;AAOpD,SAAgBW,cACZC,OACwE;CACxE,MAAM,EAAEpB,UAAUoB;CAClB,OAAO;EACHT,MAAMS,MAAMG;EAEZC,OAAOxB,MAAMwB,SAASxB,MAAMW;EAC5Bc,aAAazB,MAAM0B;EACnB,GAAI1B,MAAMsB,gBAAgBf,KAAAA,IAAY,EAAEe,aAAatB,MAAMsB,YAAY,IAAI,CAAC;EAC5EK,UAAUlB,WAAWT,KAAK;EAC1B4B,UAAUR,MAAMQ,aAAa,QAAQ5B,MAAM4B,aAAa;EACxDC,UAAUT,MAAMS,aAAa;EAC7BC,QAAQV,MAAMU,UAAU,CAAA;EACxB,GAAIV,MAAMW,WAAWxB,KAAAA,IAAY,EAAEwB,QAAQX,MAAMW,OAAO,IAAI,CAAC;EAC7D,GAAIX,MAAMY,cAAczB,KAAAA,IAAY,EAAEyB,WAAWZ,MAAMY,UAAU,IAAI,CAAC;CAC1E;AACJ;;AAQA,SAAgBE,UAAUlC,OAA+B;CACrD,IAAIA,MAAMmC,WAAW5B,KAAAA,KAAaP,MAAMmC,OAAOC,SAAS,GACpD,OAAOpC,MAAMmC;CAEjB,MAAMlC,OAAOF,SAASC,OAAO,QAAQ;CACrC,OAAOC,SAASM,KAAAA,KAAa,WAAWN,QAAQoC,MAAMC,QAAQrC,KAAKO,KAAK,IAClEP,KAAKO,MAAM+B,OAAOxB,QAAQ,IAC1B;AACV;;AAGA,SAAgByB,WAAWxC,OAA6B;CACpD,MAAMC,OAAOF,SAASC,OAAO,SAAS;CACtC,OAAOC,SAASM,KAAAA,KAAa,WAAWN,QAAQe,SAASf,KAAKO,KAAK,IAAIP,KAAKO,QAAQ;AACxF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"relation.js","names":["id","active","reference","_c"],"sources":["../../src/shared/relation.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nimport type { Definition, Item, ListQuery } from '@panneau/protocol';\n\n/** A related item: its id, and a label to show it. The value of a relation field. */\nexport interface RelationReference {\n id: string;\n label: string;\n}\n\n/** Where relation fields search for items. */\nexport interface RelationSource {\n /** Items of a resource that match a text, for the picker. */\n search(\n resource: string,\n query: string,\n options: { signal: AbortSignal },\n ): Promise<RelationReference[]>;\n /**\n * The references of items by id, for values that are only ids (`\"12\"`, `[\"12\", \"13\"]`), as a\n * product's own API gives them. In any order; ids it doesn't find are left out, and keep their\n * id as label. Without it, fields show the ids.\n */\n resolve?(\n resource: string,\n ids: readonly string[],\n options: { signal: AbortSignal },\n ): Promise<RelationReference[]>;\n /**\n * Creates an item from the text typed in the picker, which offers \"Create “…”\" when this\n * method exists. It can open a dialog, and resolves with the reference of the new item, or with\n * `null` when nothing was created.\n */\n create?(resource: string, text: string): Promise<RelationReference | null>;\n /** Whether `create()` can create items of a resource. Defaults to true. */\n canCreate?(resource: string): boolean;\n}\n\n/** What `createRelationSource()` needs from an adapter: the `list()`, `get()` and `create()` of `@panneau/data`. */\nexport interface RelationListAdapter {\n list(\n resource: string,\n query?: ListQuery,\n options?: { signal?: AbortSignal },\n ): Promise<{ data: Item[] }>;\n /** One item, for `resolve()`. Without it, the source doesn't resolve ids. */\n get?(resource: string, id: string, options?: { signal?: AbortSignal }): Promise<Item>;\n /** Creates an item, for `create()`. */\n create?(resource: string, values: Record<string, unknown>): Promise<Item>;\n}\n\nexport interface RelationSourceOptions {\n /** The definition, for the field that titles the items of each resource (`itemTitle`). */\n definition?: (Pick<Definition, 'resources'> & Partial<Pick<Definition, 'locales'>>) | null;\n /** Items per search. Defaults to 20. */\n perPage?: number;\n /** The label of an item, when `itemTitle` isn't enough. */\n getLabel?: (item: Item, resource: string) => string;\n /**\n * Lets the picker create items, with the adapter's `create()`. `true` sets the typed text as the\n * value of the resource's `itemTitle` field (in every locale of the definition when it is\n * localized), on the resources that allow `create`. A function gives the values of the new item\n * instead, or `null` for a resource where items can't be created.\n */\n create?: boolean | ((text: string, resource: string) => Record<string, unknown> | null);\n}\n\nfunction labelOf(value: unknown, locale?: string): string | null {\n if (typeof value === 'string' || typeof value === 'number') {\n return String(value);\n }\n if (value !== null && typeof value === 'object') {\n const localized = value as Record<string, unknown>;\n const first =\n (locale !== undefined ? localized[locale] : undefined) ?? Object.values(localized)[0];\n return typeof first === 'string' ? first : null;\n }\n return null;\n}\n\n/**\n * A relation source over an adapter, such as the `DataAdapter` of `@panneau/data`: a search is a\n * list query with `q`, ids are resolved with `get()`, and, with the `create` option, items are\n * created with `create()`. Labels come from the resource's `itemTitle`.\n */\nexport function createRelationSource(\n adapter: RelationListAdapter,\n { definition = null, perPage = 20, getLabel, create }: RelationSourceOptions = {},\n): RelationSource {\n const resourceOf = (resource: string) =>\n definition?.resources.find((candidate) => candidate.id === resource);\n const toReference = (item: Item, resource: string): RelationReference => {\n const titleField = resourceOf(resource)?.itemTitle;\n return {\n id: item.id,\n label:\n getLabel?.(item, resource) ??\n (titleField !== undefined ? labelOf(item[titleField]) : null) ??\n item.id,\n };\n };\n const valuesOf = (text: string, resource: string): Record<string, unknown> | null => {\n if (typeof create === 'function') {\n return create(text, resource);\n }\n const definitionResource = resourceOf(resource);\n const titleField = definitionResource?.itemTitle;\n if (definitionResource === undefined || titleField === undefined) {\n return null;\n }\n const field = definitionResource.fields.find((candidate) => candidate.name === titleField);\n const locales = definition?.locales ?? [];\n return {\n [titleField]:\n field?.localized === true && locales.length > 0\n ? Object.fromEntries(locales.map((locale) => [locale, text]))\n : text,\n };\n };\n const source: RelationSource = {\n async search(resource, query, { signal }) {\n const response = await adapter.list(\n resource,\n { page: 1, perPage, ...(query !== '' ? { q: query } : {}) },\n { signal },\n );\n return response.data.map((item) => toReference(item, resource));\n },\n };\n const { get, create: createItem } = adapter;\n if (get !== undefined) {\n source.resolve = async (resource, ids, { signal }) => {\n const results = await Promise.allSettled(\n ids.map((id) => get.call(adapter, resource, id, { signal })),\n );\n return results.flatMap((result) =>\n result.status === 'fulfilled' ? [toReference(result.value, resource)] : [],\n );\n };\n }\n if (create !== undefined && create !== false && createItem !== undefined) {\n source.canCreate = (resource) =>\n resourceOf(resource)?.abilities?.create !== false && valuesOf('', resource) !== null;\n source.create = async (resource, text) => {\n const values = valuesOf(text, resource);\n if (values === null) {\n return null;\n }\n return toReference(await createItem.call(adapter, resource, values), resource);\n };\n }\n return source;\n}\n\n/** Reads relation values: references, ids, or lists of them. */\nexport function toReferences(value: unknown): RelationReference[] {\n const list = Array.isArray(value)\n ? value\n : value === null || value === undefined\n ? []\n : [value];\n return list.flatMap((entry: unknown) => {\n if (typeof entry === 'string' || typeof entry === 'number') {\n return [{ id: String(entry), label: String(entry) }];\n }\n if (entry !== null && typeof entry === 'object' && 'id' in entry) {\n const { id, label } = entry as { id: unknown; label?: unknown };\n if (typeof id === 'string' || typeof id === 'number') {\n return [{ id: String(id), label: typeof label === 'string' ? label : String(id) }];\n }\n }\n return [];\n });\n}\n\ninterface ResolvedLabels {\n /** Labels by resource and id: `null` for ids that the source didn't find. */\n labels: Map<string, string | null>;\n /** Requests in flight, so that fields with the same ids share one. */\n pending: Map<string, Promise<void>>;\n}\n\nconst resolved = new WeakMap<RelationSource, ResolvedLabels>();\n\nfunction resolvedOf(source: RelationSource): ResolvedLabels {\n let entry = resolved.get(source);\n if (entry === undefined) {\n entry = { labels: new Map(), pending: new Map() };\n resolved.set(source, entry);\n }\n return entry;\n}\n\nconst keyOf = (resource: string, id: string) => `${resource}\\u0000${id}`;\n\n/** Resolves the ids that no request has resolved or is resolving yet, and waits for all of them. */\nfunction resolveIds(source: RelationSource, resource: string, ids: readonly string[]) {\n const { labels, pending } = resolvedOf(source);\n const fresh = ids.filter((id) => !pending.has(keyOf(resource, id)));\n if (fresh.length > 0 && source.resolve !== undefined) {\n // Shared by every field that asks for these ids: it isn't aborted when one unmounts.\n const request = source\n .resolve(resource, fresh, { signal: new AbortController().signal })\n .then(\n (found) => {\n fresh.forEach((id) =>\n labels.set(\n keyOf(resource, id),\n found.find((candidate) => candidate.id === id)?.label ?? null,\n ),\n );\n },\n () => {\n // Tried again the next time a field shows these ids.\n fresh.forEach((id) => pending.delete(keyOf(resource, id)));\n },\n );\n fresh.forEach((id) => pending.set(keyOf(resource, id), request));\n }\n return Promise.all(ids.map((id) => pending.get(keyOf(resource, id))));\n}\n\n/**\n * Relation references with the labels of plain ids: references whose label is their id (from\n * `\"12\"`) are resolved with the source's `resolve()`, once per source, resource and id. They keep\n * their id as label until then, or when the source doesn't find them.\n */\nexport function useRelationReferences(\n source: RelationSource | null,\n resource: string,\n references: readonly RelationReference[],\n): RelationReference[] {\n const cache = source !== null ? resolvedOf(source).labels : null;\n const [labels, setLabels] = useState<Readonly<Record<string, string>>>({});\n const label = (id: string): string | undefined =>\n labels[keyOf(resource, id)] ?? cache?.get(keyOf(resource, id)) ?? undefined;\n const missing =\n source?.resolve !== undefined && cache !== null\n ? references\n .filter((reference) => reference.label === reference.id)\n .map((reference) => reference.id)\n .filter((id) => !cache.has(keyOf(resource, id)))\n : [];\n const missingKey = missing.join('\\u0000');\n\n useEffect(() => {\n const ids = missingKey === '' ? [] : missingKey.split('\\u0000');\n if (source === null || cache === null || ids.length === 0) {\n return undefined;\n }\n let active = true;\n void resolveIds(source, resource, ids).then(() => {\n if (!active) {\n return;\n }\n const next: Record<string, string> = {};\n ids.forEach((id) => {\n const found = cache.get(keyOf(resource, id));\n if (typeof found === 'string') {\n next[keyOf(resource, id)] = found;\n }\n });\n setLabels((current) => ({ ...current, ...next }));\n });\n return () => {\n active = false;\n };\n }, [source, cache, resource, missingKey]);\n\n return references.map((reference) =>\n reference.label === reference.id\n ? { id: reference.id, label: label(reference.id) ?? reference.id }\n : reference,\n );\n}\n"],"mappings":";;;AAmEA,SAAS,QAAQ,OAAgB,QAAgC;CAC7D,IAAI,OAAO,UAAU,YAAY,OAAO,UAAU,UAC9C,OAAO,OAAO,KAAK;CAEvB,IAAI,UAAU,QAAQ,OAAO,UAAU,UAAU;EAC7C,MAAM,YAAY;EAClB,MAAM,SACD,WAAW,KAAA,IAAY,UAAU,UAAU,KAAA,MAAc,OAAO,OAAO,SAAS,CAAC,CAAC;EACvF,OAAO,OAAO,UAAU,WAAW,QAAQ;CAC/C;CACA,OAAO;AACX;;;;;;AAOA,SAAgB,qBACZ,SACA,EAAE,aAAa,MAAM,UAAU,IAAI,UAAU,WAAkC,CAAC,GAClE;CACd,MAAM,cAAc,aAChB,YAAY,UAAU,MAAM,cAAc,UAAU,OAA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C,CAAA,QACzB,SAAQ,CAAC,MAAK,IAAK,MAAM,UAAUH,IAAE,CAAC,CACjD,IALP,CAAA,EAAA,CAMsB,KAAM,IAAQ;CAExC,gBAAU;EACN,MAAA,MAAY,eAAe,KAAf,CAAA,IAAyB,WAAU,MAAO,IAAQ;EAC9D,IAAI,WAAW,QAAQ,UAAU,QAAQ,IAAG,WAAY,GAAC;EAGzD,IAAA,SAAa;EACR,WAAW,QAAQ,UAAU,GAAG,CAAC,CAAA,WAAM;GACxC,IAAI,CAAC,QAAM;GAGX,MAAA,OAAqC,CAAC;GACtC,IAAG,SAAS,SAAA;IACR,MAAA,QAAc,MAAK,IAAK,MAAM,UAAUA,IAAE,CAAC;IAC3C,IAAI,OAAO,UAAU,UACjB,KAAK,MAAM,UAAUA,IAAE,KAAK;GAC/B,CACJ;GACD,WAAU,aAAc;IAAA,GAAK;IAAO,GAAK;GAAK,EAAE;EAAC,CACpD;EAAC,aACK;GACH,SAASC;EAAH;CACT,GACF;EAAC;EAAQ;EAAO;EAAU;CAAU,CAAC;CAAC,OAElC,WAAU,KAAK,gBAClBC,YAAS,UAAWA,YAAS,KAA7B;EAAA,IACYA,YAAS;EAAG,OAAS,MAAMA,YAAS,EAAmB,KAAXA,YAAS;CACnD,IAFd,WAGJ;AAAC;AA9CE,SAAA,OAAA,aAAA;CAAA,OAa+BA,YAAS;AAAG;AAb3C,SAAA,MAAA,WAAA;CAAA,OAYkC,UAAS,UAAW,UAAS;AAAG"}
|
|
1
|
+
{"version":3,"file":"relation.js","names":["useEffect","useState","Definition","Item","ListQuery","RelationReference","id","label","RelationSource","search","resource","query","options","signal","AbortSignal","Promise","resolve","ids","create","text","canCreate","RelationListAdapter","list","data","get","values","Record","RelationSourceOptions","definition","Pick","Partial","perPage","getLabel","item","labelOf","value","locale","String","localized","first","undefined","Object","createRelationSource","adapter","resourceOf","resources","find","candidate","toReference","titleField","itemTitle","valuesOf","definitionResource","field","fields","name","locales","length","fromEntries","map","source","response","page","q","createItem","results","allSettled","call","flatMap","result","status","abilities","toReferences","Array","isArray","entry","ResolvedLabels","labels","Map","pending","resolved","WeakMap","resolvedOf","set","keyOf","resolveIds","fresh","filter","has","request","AbortController","then","found","forEach","delete","all","useRelationReferences","references","$","_c","cache","t0","Symbol","for","setLabels","missing","_temp","_temp2","id_0","missingKey","join","split","active","next","id_1","current","reference_1","reference","reference_0","_c"],"sources":["../../src/shared/relation.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nimport type { Definition, Item, ListQuery } from '@panneau/protocol';\n\n/** A related item: its id, and a label to show it. The value of a relation field. */\nexport interface RelationReference {\n id: string;\n label: string;\n}\n\n/** Where relation fields search for items. */\nexport interface RelationSource {\n /** Items of a resource that match a text, for the picker. */\n search(\n resource: string,\n query: string,\n options: { signal: AbortSignal },\n ): Promise<RelationReference[]>;\n /**\n * The references of items by id, for values that are only ids (`\"12\"`, `[\"12\", \"13\"]`), as a\n * product's own API gives them. In any order; ids it doesn't find are left out, and keep their\n * id as label. Without it, fields show the ids.\n */\n resolve?(\n resource: string,\n ids: readonly string[],\n options: { signal: AbortSignal },\n ): Promise<RelationReference[]>;\n /**\n * Creates an item from the text typed in the picker, which offers \"Create “…”\" when this\n * method exists. It can open a dialog, and resolves with the reference of the new item, or with\n * `null` when nothing was created.\n */\n create?(resource: string, text: string): Promise<RelationReference | null>;\n /** Whether `create()` can create items of a resource. Defaults to true. */\n canCreate?(resource: string): boolean;\n}\n\n/** What `createRelationSource()` needs from an adapter: the `list()`, `get()` and `create()` of `@panneau/data`. */\nexport interface RelationListAdapter {\n list(\n resource: string,\n query?: ListQuery,\n options?: { signal?: AbortSignal },\n ): Promise<{ data: Item[] }>;\n /** One item, for `resolve()`. Without it, the source doesn't resolve ids. */\n get?(resource: string, id: string, options?: { signal?: AbortSignal }): Promise<Item>;\n /** Creates an item, for `create()`. */\n create?(resource: string, values: Record<string, unknown>): Promise<Item>;\n}\n\nexport interface RelationSourceOptions {\n /** The definition, for the field that titles the items of each resource (`itemTitle`). */\n definition?: (Pick<Definition, 'resources'> & Partial<Pick<Definition, 'locales'>>) | null;\n /** Items per search. Defaults to 20. */\n perPage?: number;\n /** The label of an item, when `itemTitle` isn't enough. */\n getLabel?: (item: Item, resource: string) => string;\n /**\n * Lets the picker create items, with the adapter's `create()`. `true` sets the typed text as the\n * value of the resource's `itemTitle` field (in every locale of the definition when it is\n * localized), on the resources that allow `create`. A function gives the values of the new item\n * instead, or `null` for a resource where items can't be created.\n */\n create?: boolean | ((text: string, resource: string) => Record<string, unknown> | null);\n}\n\nfunction labelOf(value: unknown, locale?: string): string | null {\n if (typeof value === 'string' || typeof value === 'number') {\n return String(value);\n }\n if (value !== null && typeof value === 'object') {\n const localized = value as Record<string, unknown>;\n const first =\n (locale !== undefined ? localized[locale] : undefined) ?? Object.values(localized)[0];\n return typeof first === 'string' ? first : null;\n }\n return null;\n}\n\n/**\n * A relation source over an adapter, such as the `DataAdapter` of `@panneau/data`: a search is a\n * list query with `q`, ids are resolved with `get()`, and, with the `create` option, items are\n * created with `create()`. Labels come from the resource's `itemTitle`.\n */\nexport function createRelationSource(\n adapter: RelationListAdapter,\n { definition = null, perPage = 20, getLabel, create }: RelationSourceOptions = {},\n): RelationSource {\n const resourceOf = (resource: string) =>\n definition?.resources.find((candidate) => candidate.id === resource);\n const toReference = (item: Item, resource: string): RelationReference => {\n const titleField = resourceOf(resource)?.itemTitle;\n return {\n id: item.id,\n label:\n getLabel?.(item, resource) ??\n (titleField !== undefined ? labelOf(item[titleField]) : null) ??\n item.id,\n };\n };\n const valuesOf = (text: string, resource: string): Record<string, unknown> | null => {\n if (typeof create === 'function') {\n return create(text, resource);\n }\n const definitionResource = resourceOf(resource);\n const titleField = definitionResource?.itemTitle;\n if (definitionResource === undefined || titleField === undefined) {\n return null;\n }\n const field = definitionResource.fields.find((candidate) => candidate.name === titleField);\n const locales = definition?.locales ?? [];\n return {\n [titleField]:\n field?.localized === true && locales.length > 0\n ? Object.fromEntries(locales.map((locale) => [locale, text]))\n : text,\n };\n };\n const source: RelationSource = {\n async search(resource, query, { signal }) {\n const response = await adapter.list(\n resource,\n { page: 1, perPage, ...(query !== '' ? { q: query } : {}) },\n { signal },\n );\n return response.data.map((item) => toReference(item, resource));\n },\n };\n const { get, create: createItem } = adapter;\n if (get !== undefined) {\n source.resolve = async (resource, ids, { signal }) => {\n const results = await Promise.allSettled(\n ids.map((id) => get.call(adapter, resource, id, { signal })),\n );\n return results.flatMap((result) =>\n result.status === 'fulfilled' ? [toReference(result.value, resource)] : [],\n );\n };\n }\n if (create !== undefined && create !== false && createItem !== undefined) {\n source.canCreate = (resource) =>\n resourceOf(resource)?.abilities?.create !== false && valuesOf('', resource) !== null;\n source.create = async (resource, text) => {\n const values = valuesOf(text, resource);\n if (values === null) {\n return null;\n }\n return toReference(await createItem.call(adapter, resource, values), resource);\n };\n }\n return source;\n}\n\n/** Reads relation values: references, ids, or lists of them. */\nexport function toReferences(value: unknown): RelationReference[] {\n const list = Array.isArray(value)\n ? value\n : value === null || value === undefined\n ? []\n : [value];\n return list.flatMap((entry: unknown) => {\n if (typeof entry === 'string' || typeof entry === 'number') {\n return [{ id: String(entry), label: String(entry) }];\n }\n if (entry !== null && typeof entry === 'object' && 'id' in entry) {\n const { id, label } = entry as { id: unknown; label?: unknown };\n if (typeof id === 'string' || typeof id === 'number') {\n return [{ id: String(id), label: typeof label === 'string' ? label : String(id) }];\n }\n }\n return [];\n });\n}\n\ninterface ResolvedLabels {\n /** Labels by resource and id: `null` for ids that the source didn't find. */\n labels: Map<string, string | null>;\n /** Requests in flight, so that fields with the same ids share one. */\n pending: Map<string, Promise<void>>;\n}\n\nconst resolved = new WeakMap<RelationSource, ResolvedLabels>();\n\nfunction resolvedOf(source: RelationSource): ResolvedLabels {\n let entry = resolved.get(source);\n if (entry === undefined) {\n entry = { labels: new Map(), pending: new Map() };\n resolved.set(source, entry);\n }\n return entry;\n}\n\nconst keyOf = (resource: string, id: string) => `${resource}\\u0000${id}`;\n\n/** Resolves the ids that no request has resolved or is resolving yet, and waits for all of them. */\nfunction resolveIds(source: RelationSource, resource: string, ids: readonly string[]) {\n const { labels, pending } = resolvedOf(source);\n const fresh = ids.filter((id) => !pending.has(keyOf(resource, id)));\n if (fresh.length > 0 && source.resolve !== undefined) {\n // Shared by every field that asks for these ids: it isn't aborted when one unmounts.\n const request = source\n .resolve(resource, fresh, { signal: new AbortController().signal })\n .then(\n (found) => {\n fresh.forEach((id) =>\n labels.set(\n keyOf(resource, id),\n found.find((candidate) => candidate.id === id)?.label ?? null,\n ),\n );\n },\n () => {\n // Tried again the next time a field shows these ids.\n fresh.forEach((id) => pending.delete(keyOf(resource, id)));\n },\n );\n fresh.forEach((id) => pending.set(keyOf(resource, id), request));\n }\n return Promise.all(ids.map((id) => pending.get(keyOf(resource, id))));\n}\n\n/**\n * Relation references with the labels of plain ids: references whose label is their id (from\n * `\"12\"`) are resolved with the source's `resolve()`, once per source, resource and id. They keep\n * their id as label until then, or when the source doesn't find them.\n */\nexport function useRelationReferences(\n source: RelationSource | null,\n resource: string,\n references: readonly RelationReference[],\n): RelationReference[] {\n const cache = source !== null ? resolvedOf(source).labels : null;\n const [labels, setLabels] = useState<Readonly<Record<string, string>>>({});\n const label = (id: string): string | undefined =>\n labels[keyOf(resource, id)] ?? cache?.get(keyOf(resource, id)) ?? undefined;\n const missing =\n source?.resolve !== undefined && cache !== null\n ? references\n .filter((reference) => reference.label === reference.id)\n .map((reference) => reference.id)\n .filter((id) => !cache.has(keyOf(resource, id)))\n : [];\n const missingKey = missing.join('\\u0000');\n\n useEffect(() => {\n const ids = missingKey === '' ? [] : missingKey.split('\\u0000');\n if (source === null || cache === null || ids.length === 0) {\n return undefined;\n }\n let active = true;\n void resolveIds(source, resource, ids).then(() => {\n if (!active) {\n return;\n }\n const next: Record<string, string> = {};\n ids.forEach((id) => {\n const found = cache.get(keyOf(resource, id));\n if (typeof found === 'string') {\n next[keyOf(resource, id)] = found;\n }\n });\n setLabels((current) => ({ ...current, ...next }));\n });\n return () => {\n active = false;\n };\n }, [source, cache, resource, missingKey]);\n\n return references.map((reference) =>\n reference.label === reference.id\n ? { id: reference.id, label: label(reference.id) ?? reference.id }\n : reference,\n );\n}\n"],"mappings":";;;AAmEA,SAASkC,QAAQC,OAAgBC,QAAgC;CAC7D,IAAI,OAAOD,UAAU,YAAY,OAAOA,UAAU,UAC9C,OAAOE,OAAOF,KAAK;CAEvB,IAAIA,UAAU,QAAQ,OAAOA,UAAU,UAAU;EAC7C,MAAMG,YAAYH;EAClB,MAAMI,SACDH,WAAWI,KAAAA,IAAYF,UAAUF,UAAUI,KAAAA,MAAcC,OAAOhB,OAAOa,SAAS,CAAC,CAAC;EACvF,OAAO,OAAOC,UAAU,WAAWA,QAAQ;CAC/C;CACA,OAAO;AACX;;;;;;AAOA,SAAgBG,qBACZC,SACA,EAAEf,aAAa,MAAMG,UAAU,IAAIC,UAAUd,WAAkC,CAAC,GAClE;CACd,MAAM0B,cAAclC,aAChBkB,YAAYiB,UAAUC,MAAMC,cAAcA,UAAUzC,OAAOI,QAAQ;CACvE,MAAMsC,eAAef,MAAYvB,aAAwC;EACrE,MAAMuC,aAAaL,WAAWlC,QAAQ,CAAC,EAAEwC;EACzC,OAAO;GACH5C,IAAI2B,KAAK3B;GACTC,OACIyB,WAAWC,MAAMvB,QAAQ,MACxBuC,eAAeT,KAAAA,IAAYN,QAAQD,KAAKgB,WAAW,IAAI,SACxDhB,KAAK3B;EACb;CACJ;CACA,MAAM6C,YAAYhC,MAAcT,aAAqD;EACjF,IAAI,OAAOQ,WAAW,YAClB,OAAOA,OAAOC,MAAMT,QAAQ;EAEhC,MAAM0C,qBAAqBR,WAAWlC,QAAQ;EAC9C,MAAMuC,aAAaG,oBAAoBF;EACvC,IAAIE,uBAAuBZ,KAAAA,KAAaS,eAAeT,KAAAA,GACnD,OAAO;EAEX,MAAMa,QAAQD,mBAAmBE,OAAOR,MAAMC,cAAcA,UAAUQ,SAASN,UAAU;EACzF,MAAMO,UA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ApB,QACzBqB,SAAQ,CAACR,MAAKZ,IAAKJ,MAAM1E,UAAUJ,IAAE,CAAC,CACjD,IALP,CAAA,EAAA,CAMsBwG,KAAM,IAAQ;CAExC9G,gBAAU;EACN,MAAAiB,MAAY4F,eAAe,KAAf,CAAA,IAAyBA,WAAUE,MAAO,IAAQ;EAC9D,IAAInD,WAAW,QAAQwC,UAAU,QAAQnF,IAAGwC,WAAY,GAAC;EAGzD,IAAAuD,SAAa;EACR3B,WAAWzB,QAAQlD,UAAUO,GAAG,CAAC,CAAA0E,WAAM;GACxC,IAAI,CAACqB,QAAM;GAGX,MAAAC,OAAqC,CAAC;GACtChG,IAAG4E,SAASqB,SAAA;IACR,MAAAtB,QAAcQ,MAAK5E,IAAK4D,MAAM1E,UAAUJ,IAAE,CAAC;IAC3C,IAAI,OAAOsF,UAAU,UACjBqB,KAAK7B,MAAM1E,UAAUJ,IAAE,KAAKsF;GAC/B,CACJ;GACDY,WAAUW,aAAc;IAAA,GAAKA;IAAO,GAAKF;GAAK,EAAE;EAAC,CACpD;EAAC,aACK;GACHD,SAASA;EAAH;CACT,GACF;EAACpD;EAAQwC;EAAO1F;EAAUmG;CAAU,CAAC;CAAC,OAElCZ,WAAUtC,KAAKyD,gBAClBC,YAAS9G,UAAW8G,YAAS/G,KAA7B;EAAAA,IACY+G,YAAS/G;EAAGC,OAASA,MAAM8G,YAAS/G,EAAmB,KAAX+G,YAAS/G;CACnD,IAFd8G,WAGJ;AAAC;AA9CE,SAAAT,OAAAW,aAAA;CAAA,OAa+BD,YAAS/G;AAAG;AAb3C,SAAAoG,MAAAW,WAAA;CAAA,OAYkCA,UAAS9G,UAAW8G,UAAS/G;AAAG"}
|
package/dist/slug/SlugField.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { FieldCheckResponse } from '@panneau/protocol';
|
|
2
|
-
import type { FieldEntryProps } from '../shared/types';
|
|
3
|
-
import { type TextFieldProps } from '../text/TextField';
|
|
2
|
+
import type { FieldEntryProps } from '../shared/types.js';
|
|
3
|
+
import { type TextFieldProps } from '../text/TextField.js';
|
|
4
4
|
/**
|
|
5
5
|
* Turns a text into a slug: lowercase ASCII letters and digits separated by dashes, without
|
|
6
6
|
* accents (`Été à Montréal` gives `ete-a-montreal`).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SlugField.d.ts","sourceRoot":"","sources":["../../src/slug/SlugField.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAM5D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACvD,OAAO,EAAa,KAAK,cAAc,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"SlugField.d.ts","sourceRoot":"","sources":["../../src/slug/SlugField.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAM5D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACvD,OAAO,EAAa,KAAK,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAMnE;;;GAGG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAO5C;AAED,4EAA4E;AAC5E,MAAM,MAAM,SAAS,GAAG,CACpB,KAAK,EAAE,MAAM,EACb,OAAO,EAAE;IAAE,MAAM,EAAE,WAAW,CAAA;CAAE,KAC/B,OAAO,CAAC,kBAAkB,CAAC,CAAC;AAEjC,MAAM,WAAW,cAAe,SAAQ,IAAI,CAAC,cAAc,EAAE,MAAM,GAAG,QAAQ,CAAC;IAC3E;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,0EAA0E;IAC1E,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;OAGG;IACH,OAAO,CAAC,EAAE,SAAS,GAAG,IAAI,CAAC;IAC3B,mFAAmF;IACnF,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;CACrB;AA4CD;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,EACtB,KAAK,EACL,QAAQ,EACR,MAAM,EACN,MAAM,EACN,WAAW,EACX,OAAO,EACP,UAAgB,EAChB,QAAa,EACb,QAAQ,EACR,QAAQ,EACR,MAAM,EACN,MAAM,EACN,WAAW,EACX,GAAG,KAAK,EACX,EAAE,cAAc,+BA4FhB;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAazE;AAeD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,eAAe,+BAqCpD"}
|
package/dist/slug/SlugField.js
CHANGED
|
@@ -9,6 +9,7 @@ import { useEffect, useRef, useState } from "react";
|
|
|
9
9
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
10
10
|
import { WandSparkles } from "lucide-react";
|
|
11
11
|
//#region src/slug/SlugField.tsx
|
|
12
|
+
var suggestionMessage = messages.useSuggestion;
|
|
12
13
|
/**
|
|
13
14
|
* Turns a text into a slug: lowercase ASCII letters and digits separated by dashes, without
|
|
14
15
|
* accents (`Été à Montréal` gives `ete-a-montreal`).
|
|
@@ -82,41 +83,134 @@ function useSlugCheck(value, onCheck, delay, key) {
|
|
|
82
83
|
* slug is taken, and gives its URL.
|
|
83
84
|
*/
|
|
84
85
|
function _temp() {}
|
|
85
|
-
function SlugField(
|
|
86
|
+
function SlugField(t0) {
|
|
87
|
+
const $ = c(65);
|
|
88
|
+
let description;
|
|
89
|
+
let disabled;
|
|
90
|
+
let errors;
|
|
91
|
+
let onBlur;
|
|
92
|
+
let onChange;
|
|
93
|
+
let onCheck;
|
|
94
|
+
let prefix;
|
|
95
|
+
let props;
|
|
96
|
+
let readOnly;
|
|
97
|
+
let source;
|
|
98
|
+
let sourceLabel;
|
|
99
|
+
let t1;
|
|
100
|
+
let t2;
|
|
101
|
+
let value;
|
|
102
|
+
if ($[0] !== t0) {
|
|
103
|
+
({value, onChange, onBlur, source, sourceLabel, onCheck, checkDelay: t1, checkKey: t2, disabled, readOnly, prefix, errors, description, ...props} = t0);
|
|
104
|
+
$[0] = t0;
|
|
105
|
+
$[1] = description;
|
|
106
|
+
$[2] = disabled;
|
|
107
|
+
$[3] = errors;
|
|
108
|
+
$[4] = onBlur;
|
|
109
|
+
$[5] = onChange;
|
|
110
|
+
$[6] = onCheck;
|
|
111
|
+
$[7] = prefix;
|
|
112
|
+
$[8] = props;
|
|
113
|
+
$[9] = readOnly;
|
|
114
|
+
$[10] = source;
|
|
115
|
+
$[11] = sourceLabel;
|
|
116
|
+
$[12] = t1;
|
|
117
|
+
$[13] = t2;
|
|
118
|
+
$[14] = value;
|
|
119
|
+
} else {
|
|
120
|
+
description = $[1];
|
|
121
|
+
disabled = $[2];
|
|
122
|
+
errors = $[3];
|
|
123
|
+
onBlur = $[4];
|
|
124
|
+
onChange = $[5];
|
|
125
|
+
onCheck = $[6];
|
|
126
|
+
prefix = $[7];
|
|
127
|
+
props = $[8];
|
|
128
|
+
readOnly = $[9];
|
|
129
|
+
source = $[10];
|
|
130
|
+
sourceLabel = $[11];
|
|
131
|
+
t1 = $[12];
|
|
132
|
+
t2 = $[13];
|
|
133
|
+
value = $[14];
|
|
134
|
+
}
|
|
135
|
+
const checkDelay = t1 === void 0 ? 300 : t1;
|
|
136
|
+
const checkKey = t2 === void 0 ? "" : t2;
|
|
86
137
|
const intl = usePanneauIntl();
|
|
87
138
|
const previousSource = useRef(source);
|
|
88
139
|
const check = useSlugCheck(value ?? "", onCheck, checkDelay, checkKey);
|
|
89
140
|
const disabledByBackend = check?.disabled === true;
|
|
90
141
|
const editable = disabled !== true && readOnly !== true && !disabledByBackend;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
142
|
+
let t3;
|
|
143
|
+
let t4;
|
|
144
|
+
if ($[15] !== editable || $[16] !== onChange || $[17] !== source || $[18] !== value) {
|
|
145
|
+
t3 = () => {
|
|
146
|
+
const previous = previousSource.current;
|
|
147
|
+
previousSource.current = source;
|
|
148
|
+
if (!editable || source === previous || source === void 0) return;
|
|
149
|
+
const current = value ?? "";
|
|
150
|
+
if (current === "" || current === slugify(previous ?? "")) {
|
|
151
|
+
const next = slugify(source ?? "");
|
|
152
|
+
if (next !== current) onChange?.(next === "" ? null : next);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
t4 = [
|
|
156
|
+
source,
|
|
157
|
+
value,
|
|
158
|
+
onChange,
|
|
159
|
+
editable
|
|
160
|
+
];
|
|
161
|
+
$[15] = editable;
|
|
162
|
+
$[16] = onChange;
|
|
163
|
+
$[17] = source;
|
|
164
|
+
$[18] = value;
|
|
165
|
+
$[19] = t3;
|
|
166
|
+
$[20] = t4;
|
|
167
|
+
} else {
|
|
168
|
+
t3 = $[19];
|
|
169
|
+
t4 = $[20];
|
|
170
|
+
}
|
|
171
|
+
useEffect(t3, t4);
|
|
172
|
+
const t5 = source ?? "";
|
|
173
|
+
let t6;
|
|
174
|
+
if ($[21] !== t5) {
|
|
175
|
+
t6 = slugify(t5);
|
|
176
|
+
$[21] = t5;
|
|
177
|
+
$[22] = t6;
|
|
178
|
+
} else t6 = $[22];
|
|
179
|
+
const generated = t6;
|
|
107
180
|
const taken = check !== null && !check.available;
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
181
|
+
let t7;
|
|
182
|
+
if ($[23] !== check || $[24] !== taken) {
|
|
183
|
+
t7 = taken && isString(check.suggestion) ? check.suggestion : null;
|
|
184
|
+
$[23] = check;
|
|
185
|
+
$[24] = taken;
|
|
186
|
+
$[25] = t7;
|
|
187
|
+
} else t7 = $[25];
|
|
188
|
+
const suggestion = t7;
|
|
189
|
+
let t8;
|
|
190
|
+
if ($[26] !== check || $[27] !== taken) {
|
|
191
|
+
t8 = check !== null && isString(check.url) && !taken ? check.url : null;
|
|
192
|
+
$[26] = check;
|
|
193
|
+
$[27] = taken;
|
|
194
|
+
$[28] = t8;
|
|
195
|
+
} else t8 = $[28];
|
|
196
|
+
const url = t8;
|
|
197
|
+
const t9 = readOnly === true || disabledByBackend;
|
|
198
|
+
let t10;
|
|
199
|
+
if ($[29] !== check || $[30] !== prefix) {
|
|
200
|
+
t10 = check !== null && isString(check.prefix) ? { prefix: check.prefix } : prefix !== void 0 ? { prefix } : {};
|
|
201
|
+
$[29] = check;
|
|
202
|
+
$[30] = prefix;
|
|
203
|
+
$[31] = t10;
|
|
204
|
+
} else t10 = $[31];
|
|
205
|
+
let t11;
|
|
206
|
+
if ($[32] !== errors) {
|
|
207
|
+
t11 = errors !== void 0 ? { errors } : {};
|
|
208
|
+
$[32] = errors;
|
|
209
|
+
$[33] = t11;
|
|
210
|
+
} else t11 = $[33];
|
|
211
|
+
let t12;
|
|
212
|
+
if ($[34] !== description || $[35] !== editable || $[36] !== intl || $[37] !== onChange || $[38] !== suggestion || $[39] !== taken || $[40] !== url) {
|
|
213
|
+
t12 = taken || url !== null || description !== void 0 ? /* @__PURE__ */ jsxs("span", {
|
|
120
214
|
className: SlugField_module_default.description,
|
|
121
215
|
children: [
|
|
122
216
|
description !== void 0 ? /* @__PURE__ */ jsx("span", { children: description }) : null,
|
|
@@ -134,26 +228,80 @@ function SlugField({ value, onChange, onBlur, source, sourceLabel, onCheck, chec
|
|
|
134
228
|
size: "sm",
|
|
135
229
|
appearance: "ghost",
|
|
136
230
|
onPress: () => onChange?.(suggestion),
|
|
137
|
-
children: intl.formatMessage(
|
|
231
|
+
children: intl.formatMessage(suggestionMessage, { suggestion })
|
|
138
232
|
}) : null]
|
|
139
233
|
}) : null
|
|
140
234
|
]
|
|
141
|
-
}) : void 0
|
|
142
|
-
|
|
235
|
+
}) : void 0;
|
|
236
|
+
$[34] = description;
|
|
237
|
+
$[35] = editable;
|
|
238
|
+
$[36] = intl;
|
|
239
|
+
$[37] = onChange;
|
|
240
|
+
$[38] = suggestion;
|
|
241
|
+
$[39] = taken;
|
|
242
|
+
$[40] = url;
|
|
243
|
+
$[41] = t12;
|
|
244
|
+
} else t12 = $[41];
|
|
245
|
+
let t13;
|
|
246
|
+
if ($[42] !== onBlur || $[43] !== onChange || $[44] !== value) {
|
|
247
|
+
t13 = () => {
|
|
143
248
|
if (value !== null && value !== void 0) {
|
|
144
249
|
const next_0 = slugify(value);
|
|
145
250
|
if (next_0 !== value) onChange?.(next_0 === "" ? null : next_0);
|
|
146
251
|
}
|
|
147
252
|
onBlur?.();
|
|
148
|
-
}
|
|
149
|
-
|
|
253
|
+
};
|
|
254
|
+
$[42] = onBlur;
|
|
255
|
+
$[43] = onChange;
|
|
256
|
+
$[44] = value;
|
|
257
|
+
$[45] = t13;
|
|
258
|
+
} else t13 = $[45];
|
|
259
|
+
let t14;
|
|
260
|
+
if ($[46] !== editable || $[47] !== generated || $[48] !== intl || $[49] !== onChange || $[50] !== source || $[51] !== sourceLabel || $[52] !== value) {
|
|
261
|
+
t14 = source !== void 0 && editable ? /* @__PURE__ */ jsx(IconButton, {
|
|
150
262
|
icon: WandSparkles,
|
|
151
263
|
size: "sm",
|
|
152
264
|
label: intl.formatMessage(messages.generateSlug, { field: sourceLabel ?? "" }),
|
|
153
265
|
isDisabled: generated === "" || generated === value,
|
|
154
266
|
onPress: () => onChange?.(generated === "" ? null : generated)
|
|
155
|
-
}) : void 0
|
|
156
|
-
|
|
267
|
+
}) : void 0;
|
|
268
|
+
$[46] = editable;
|
|
269
|
+
$[47] = generated;
|
|
270
|
+
$[48] = intl;
|
|
271
|
+
$[49] = onChange;
|
|
272
|
+
$[50] = source;
|
|
273
|
+
$[51] = sourceLabel;
|
|
274
|
+
$[52] = value;
|
|
275
|
+
$[53] = t14;
|
|
276
|
+
} else t14 = $[53];
|
|
277
|
+
let t15;
|
|
278
|
+
if ($[54] !== disabled || $[55] !== onChange || $[56] !== props || $[57] !== t10 || $[58] !== t11 || $[59] !== t12 || $[60] !== t13 || $[61] !== t14 || $[62] !== t9 || $[63] !== value) {
|
|
279
|
+
t15 = /* @__PURE__ */ jsx(TextField, {
|
|
280
|
+
autoComplete: "off",
|
|
281
|
+
...props,
|
|
282
|
+
value,
|
|
283
|
+
onChange,
|
|
284
|
+
disabled,
|
|
285
|
+
readOnly: t9,
|
|
286
|
+
...t10,
|
|
287
|
+
...t11,
|
|
288
|
+
description: t12,
|
|
289
|
+
onBlur: t13,
|
|
290
|
+
suffix: t14
|
|
291
|
+
});
|
|
292
|
+
$[54] = disabled;
|
|
293
|
+
$[55] = onChange;
|
|
294
|
+
$[56] = props;
|
|
295
|
+
$[57] = t10;
|
|
296
|
+
$[58] = t11;
|
|
297
|
+
$[59] = t12;
|
|
298
|
+
$[60] = t13;
|
|
299
|
+
$[61] = t14;
|
|
300
|
+
$[62] = t9;
|
|
301
|
+
$[63] = value;
|
|
302
|
+
$[64] = t15;
|
|
303
|
+
} else t15 = $[64];
|
|
304
|
+
return t15;
|
|
157
305
|
}
|
|
158
306
|
/**
|
|
159
307
|
* The text of a source value: the value itself, or for a localized value, its value in `locale`.
|