@entifix/react-controls 0.23.6 → 0.23.7
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/README.md +16 -0
- package/dist/i18n/catalog/en.d.ts.map +1 -1
- package/dist/i18n/catalog/en.js +2 -1
- package/dist/i18n/catalog/en.js.map +1 -1
- package/dist/i18n/catalog/es.d.ts +1 -0
- package/dist/i18n/catalog/es.d.ts.map +1 -1
- package/dist/i18n/catalog/es.js +2 -1
- package/dist/i18n/catalog/es.js.map +1 -1
- package/dist/i18n/catalog/index.d.ts +2 -0
- package/dist/i18n/catalog/index.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/primitives.d.ts +1 -0
- package/dist/primitives.d.ts.map +1 -1
- package/dist/primitives.js +1 -0
- package/dist/primitives.js.map +1 -1
- package/dist/ui/atoms/cell-value/cell-value.d.ts +2 -1
- package/dist/ui/atoms/cell-value/cell-value.d.ts.map +1 -1
- package/dist/ui/atoms/cell-value/cell-value.js +31 -5
- package/dist/ui/atoms/cell-value/cell-value.js.map +1 -1
- package/dist/ui/atoms/field-control/field-control.d.ts +2 -1
- package/dist/ui/atoms/field-control/field-control.d.ts.map +1 -1
- package/dist/ui/atoms/field-control/field-control.js +19 -0
- package/dist/ui/atoms/field-control/field-control.js.map +1 -1
- package/dist/ui/atoms/money/money-input.d.ts +4 -0
- package/dist/ui/atoms/money/money-input.d.ts.map +1 -1
- package/dist/ui/atoms/money/money-input.js +10 -13
- package/dist/ui/atoms/money/money-input.js.map +1 -1
- package/dist/ui/atoms/number-input/quantity-input.d.ts +19 -4
- package/dist/ui/atoms/number-input/quantity-input.d.ts.map +1 -1
- package/dist/ui/atoms/number-input/quantity-input.js +13 -5
- package/dist/ui/atoms/number-input/quantity-input.js.map +1 -1
- package/dist/ui/atoms/number-input/scaled-quantity-input.d.ts +16 -0
- package/dist/ui/atoms/number-input/scaled-quantity-input.d.ts.map +1 -0
- package/dist/ui/atoms/number-input/scaled-quantity-input.js +135 -0
- package/dist/ui/atoms/number-input/scaled-quantity-input.js.map +1 -0
- package/dist/ui/atoms/number-input/step-button.d.ts +3 -0
- package/dist/ui/atoms/number-input/step-button.d.ts.map +1 -0
- package/dist/ui/atoms/number-input/step-button.js +3 -0
- package/dist/ui/atoms/number-input/step-button.js.map +1 -0
- package/dist/ui/atoms/number-input/unparsed.d.ts +10 -0
- package/dist/ui/atoms/number-input/unparsed.d.ts.map +1 -0
- package/dist/ui/atoms/number-input/unparsed.js +10 -0
- package/dist/ui/atoms/number-input/unparsed.js.map +1 -0
- package/dist/ui/atoms/scaled/index.d.ts +5 -0
- package/dist/ui/atoms/scaled/index.d.ts.map +1 -0
- package/dist/ui/atoms/scaled/index.js +6 -0
- package/dist/ui/atoms/scaled/index.js.map +1 -0
- package/dist/ui/atoms/scaled/scaled-input.d.ts +24 -0
- package/dist/ui/atoms/scaled/scaled-input.d.ts.map +1 -0
- package/dist/ui/atoms/scaled/scaled-input.js +98 -0
- package/dist/ui/atoms/scaled/scaled-input.js.map +1 -0
- package/dist/ui/atoms/scaled/scaled-math.d.ts +13 -0
- package/dist/ui/atoms/scaled/scaled-math.d.ts.map +1 -0
- package/dist/ui/atoms/scaled/scaled-math.js +17 -0
- package/dist/ui/atoms/scaled/scaled-math.js.map +1 -0
- package/dist/ui/atoms/scaled/scaled-value.d.ts +14 -0
- package/dist/ui/atoms/scaled/scaled-value.d.ts.map +1 -0
- package/dist/ui/atoms/scaled/scaled-value.js +14 -0
- package/dist/ui/atoms/scaled/scaled-value.js.map +1 -0
- package/dist/ui/atoms/scaled/use-unit-label.d.ts +4 -0
- package/dist/ui/atoms/scaled/use-unit-label.d.ts.map +1 -0
- package/dist/ui/atoms/scaled/use-unit-label.js +11 -0
- package/dist/ui/atoms/scaled/use-unit-label.js.map +1 -0
- package/dist/ui/organisms/entity-detail-grid/entity-detail-grid.d.ts.map +1 -1
- package/dist/ui/organisms/entity-detail-grid/entity-detail-grid.js +1 -1
- package/dist/ui/organisms/entity-detail-grid/entity-detail-grid.js.map +1 -1
- package/dist/ui/organisms/entity-table/entity-table-slots.d.ts +6 -1
- package/dist/ui/organisms/entity-table/entity-table-slots.d.ts.map +1 -1
- package/dist/ui/organisms/entity-table/entity-table-slots.js.map +1 -1
- package/dist/ui/organisms/entity-table/use-entity-table-columns.d.ts.map +1 -1
- package/dist/ui/organisms/entity-table/use-entity-table-columns.js +2 -0
- package/dist/ui/organisms/entity-table/use-entity-table-columns.js.map +1 -1
- package/package.json +5 -5
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/ui/atoms/cell-value/cell-value.tsx"],"sourcesContent":["'use client';\n\nimport type { Formatters, Locale } from '@entifix/core';\nimport {\n defaultLocale,\n type Entity,\n EntityCollectionLink,\n type EntityFieldDescriptor,\n EntityLink,\n isLocalizedText,\n localize,\n type MetaAccessorType,\n} from '@entifix/core';\nimport { useCallback } from 'react';\n\nimport {\n useEnumLabel,\n useFormatters,\n useLocale,\n useT,\n} from '../../../i18n/index.js';\nimport {\n useReferenceLabel,\n useUnresolvedReference,\n} from '../../utils/reference-labels.js';\nimport { Money } from '../money/money.js';\nimport { currencyOf } from '../money/money-math.js';\nimport { Skeleton } from '../skeleton/index.js';\nimport { StatusTag } from '../status-tag/index.js';\n\n/** Stand-in for an absent value, so an empty cell still reads as intentional. */\nconst EMPTY = '—';\n\n/**\n * A link reads as its target's label once the target is loaded (embedded or\n * resolved). Never as its id: a key is not a name, and showing one fails the\n * reader who came to see which record it is (#200).\n */\nfunction entityLabel(\n target: Entity | undefined,\n linkLabelProperty: string,\n locale: Locale,\n): string | undefined {\n const label = (target as Record<string, unknown> | undefined)?.[\n linkLabelProperty\n ];\n if (label == null) return undefined;\n // A target labelled by a localized member reads in the reader's locale.\n return isLocalizedText(label) ? localize(label, locale) : String(label);\n}\n\nfunction formatLink(\n link: EntityLink<Entity>,\n linkLabelProperty: string,\n locale: Locale,\n): string {\n return link.id == null\n ? EMPTY\n : (entityLabel(link.value, linkLabelProperty, locale) ?? EMPTY);\n}\n\nfunction formatCollectionLink(\n link: EntityCollectionLink<Entity>,\n linkLabelProperty: string,\n locale: Locale,\n): string {\n return (link.values ?? [])\n .map(target => entityLabel(target, linkLabelProperty, locale) ?? EMPTY)\n .join(', ');\n}\n\n/**\n * Everything locale-dependent, resolved once by the component and handed down.\n * The formatters carry an explicit locale, which is what keeps a date rendered\n * on the server byte-identical to the same date rendered in the browser.\n */\ninterface CellFormatting {\n readonly formatters: Formatters;\n readonly yes: string;\n readonly no: string;\n readonly enumLabel: (value: string) => string;\n readonly rowCount: (count: number) => string;\n readonly locale: Locale;\n}\n\nfunction formatByType(\n value: unknown,\n type: MetaAccessorType,\n linkLabelProperty: string,\n formatting: CellFormatting,\n): string {\n switch (type) {\n case 'date':\n return formatting.formatters.date(\n value instanceof Date ? value : String(value),\n );\n case 'number':\n return typeof value === 'number'\n ? formatting.formatters.number(value)\n : String(value);\n case 'boolean':\n return value ? formatting.yes : formatting.no;\n case 'enum':\n return formatting.enumLabel(String(value));\n case 'link':\n return value instanceof EntityLink\n ? formatLink(value, linkLabelProperty, formatting.locale)\n : String(value);\n case 'linkCollection':\n return value instanceof EntityCollectionLink\n ? formatCollectionLink(value, linkLabelProperty, formatting.locale)\n : String(value);\n case 'scalarCollection':\n // The same comma list the draft holds, spaced for reading. Not `String`:\n // an array's own `toString` joins on a bare comma, which reads as one\n // run-on token in a narrow column.\n return Array.isArray(value) ? value.join(', ') : String(value);\n case 'composition':\n // Owned rows have no useful one-line form — a receipt's lines are not a\n // sentence — and the master's own screen is where they are read and\n // edited. So the cell reports the count and stops.\n return Array.isArray(value)\n ? formatting.rowCount(value.length)\n : String(value);\n case 'localized':\n // The reader's locale, falling back to the host's default; a value\n // that is not a text per locale has no reading worth guessing at.\n return isLocalizedText(value)\n ? (localize(value, formatting.locale) ?? '')\n : '';\n default:\n return String(value);\n }\n}\n\n/** The formatting {@link formatByType} reads, resolved for the reader. */\nfunction useCellFormatting(): (\n descriptor: EntityFieldDescriptor,\n) => CellFormatting {\n const t = useT();\n const formatters = useFormatters();\n const enumLabel = useEnumLabel();\n const locale = useLocale();\n return useCallback(\n descriptor => ({\n formatters,\n yes: t('value.yes'),\n no: t('value.no'),\n enumLabel: raw => enumLabel(descriptor, raw),\n rowCount: count => t('value.rowCount', { count }),\n locale,\n }),\n [enumLabel, formatters, locale, t],\n );\n}\n\n/**\n * A filled-in member's value as one line of text, exactly as its cell reads\n * it — for the places that need words rather than an element: a checkbox's\n * name, a confirm dialog's object, a toast.\n */\nexport function useValueText(): (\n value: unknown,\n descriptor: EntityFieldDescriptor,\n) => string {\n const formatting = useCellFormatting();\n return useCallback(\n (value, descriptor) =>\n formatByType(\n value,\n descriptor.type,\n descriptor.linkLabelProperty,\n formatting(descriptor),\n ),\n [formatting],\n );\n}\n\n/**\n * A reference reads as its target's name, in every state the read can be in\n * ([ADR 0030](../../../../../../../docs/adr/0030-a-reference-names-its-target-through-the-owners-read-port.md)):\n * a skeleton while it resolves (the noun and short id for a screen reader); the name; the entity's noun and the short id,\n * muted, when no record answered or the read failed — never an alert, since a\n * list that loaded has not failed because one name could not be read; and the\n * raw id, muted, when the host registered nothing to read it with.\n */\nfunction ReferenceValue({ entity, id }: { entity: string; id: string }) {\n const state = useReferenceLabel(entity, id);\n const unresolved = useUnresolvedReference();\n const locale = useLocale();\n switch (state.status) {\n case 'resolving':\n // The skeleton is hidden from assistive tech, so the noun and short id\n // stand in for it there: a record's title link is never nameless.\n return (\n <>\n <Skeleton shape=\"line\" className=\"w-16\" />\n <span className=\"sr-only\">{unresolved(entity, id)}</span>\n </>\n );\n case 'named': {\n const { label } = state;\n if (typeof label === 'string') return <span>{label}</span>;\n const text = localize(label, locale);\n return (label[locale] ?? '') === '' ? (\n <span lang={defaultLocale()}>{text}</span>\n ) : (\n <span>{text}</span>\n );\n }\n case 'unregistered':\n return <span className=\"text-content-muted\">{id}</span>;\n default:\n return (\n <span className=\"text-content-muted\">{unresolved(entity, id)}</span>\n );\n }\n}\n\n/**\n * Tabular figures, applied once, here — not on `--font-sans` and not per call\n * site. A column of numbers or dates must align on the digit; a paragraph must\n * not, because proportional figures are what prose is set in. The descriptor\n * already knows which is which, so the cell is the only place that has to.\n */\nconst NUMERIC_CLASS: Partial<Record<MetaAccessorType, string>> = {\n number: 'tabular-nums',\n date: 'tabular-nums',\n};\n\nexport interface CellValueProps {\n value: unknown;\n descriptor: EntityFieldDescriptor;\n /**\n * The record the value belongs to. A money member whose currency is another\n * member of the record (`money.currencyMember`) reads it from here.\n */\n record?: Readonly<Record<string, unknown>>;\n}\n\n/**\n * Renders one entity member according to its metadata type. Centralizing this\n * is what lets a table stay generic: a link, a date and a number all arrive as\n * \"the value of an accessor\", and only the descriptor decides how they read.\n */\nexport function CellValue({ value, descriptor, record }: CellValueProps) {\n const formatting = useCellFormatting();\n const locale = useLocale();\n\n if (value === undefined || value === null || value === '') {\n return <span className=\"text-content-muted\">{EMPTY}</span>;\n }\n\n // A key into another entity reads as that record's name, never as itself.\n if (descriptor.references !== undefined) {\n return (\n <ReferenceValue\n entity={descriptor.references.entity}\n id={String(value)}\n />\n );\n }\n\n // An amount reads as money in the reader's locale, never as the stored\n // number. A draft holds it as a string, so a numeric string counts too. A\n // currency the record does not carry leaves it a plain number rather than\n // guessing one.\n if (descriptor.money !== undefined) {\n const amount = typeof value === 'number' ? value : Number(value);\n const currency = currencyOf(descriptor.money, record);\n if (currency !== undefined && Number.isFinite(amount)) {\n return (\n <Money\n amount={amount}\n currency={currency}\n unit={descriptor.money.unit}\n locale={locale}\n />\n );\n }\n }\n\n // A relation whose target has not arrived yet holds its place with a\n // skeleton — the id is all it has, and the id is not what it means.\n if (\n (value instanceof EntityLink &&\n value.id != null &&\n value.value === undefined) ||\n (value instanceof EntityCollectionLink &&\n value.ids.length > 0 &&\n value.values === undefined)\n ) {\n return <Skeleton shape=\"line\" className=\"w-16\" />;\n }\n\n const text = formatByType(\n value,\n descriptor.type,\n descriptor.linkLabelProperty,\n formatting(descriptor),\n );\n\n if (text === '') {\n return <span className=\"text-content-muted\">{EMPTY}</span>;\n }\n\n // A status reads as a tag in its declared tone, its words beside the\n // colour; a value given no tone reads neutral. An enum that declared no\n // tones is not a status, and stays text.\n if (descriptor.type === 'enum' && descriptor.enumTones !== undefined) {\n return (\n <StatusTag tone={descriptor.enumTones[String(value)] ?? 'neutral'}>\n {text}\n </StatusTag>\n );\n }\n\n // Text the reader's locale lacks falls back to the default locale's, and is\n // marked as that language, so it is read in its own voice (WCAG 3.1.2).\n if (\n descriptor.type === 'localized' &&\n isLocalizedText(value) &&\n (value[locale] ?? '') === ''\n ) {\n return <span lang={defaultLocale()}>{text}</span>;\n }\n\n return (\n <span className={NUMERIC_CLASS[descriptor.type] ?? undefined}>{text}</span>\n );\n}\n"],"names":["defaultLocale","EntityCollectionLink","EntityLink","isLocalizedText","localize","useCallback","useEnumLabel","useFormatters","useLocale","useT","useReferenceLabel","useUnresolvedReference","Money","currencyOf","Skeleton","StatusTag","EMPTY","entityLabel","target","linkLabelProperty","locale","label","undefined","String","formatLink","link","id","value","formatCollectionLink","values","map","join","formatByType","type","formatting","formatters","date","Date","number","yes","no","enumLabel","Array","isArray","rowCount","length","useCellFormatting","t","descriptor","raw","count","useValueText","ReferenceValue","entity","state","unresolved","status","shape","className","span","text","lang","NUMERIC_CLASS","CellValue","record","references","money","amount","Number","currency","isFinite","unit","ids","enumTones","tone"],"mappings":"AAAA;;AAGA,SACEA,aAAa,EAEbC,oBAAoB,EAEpBC,UAAU,EACVC,eAAe,EACfC,QAAQ,QAEH,gBAAgB;AACvB,SAASC,WAAW,QAAQ,QAAQ;AAEpC,SACEC,YAAY,EACZC,aAAa,EACbC,SAAS,EACTC,IAAI,QACC,yBAAyB;AAChC,SACEC,iBAAiB,EACjBC,sBAAsB,QACjB,kCAAkC;AACzC,SAASC,KAAK,QAAQ,oBA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KD,SAAS,GAAEI,iCAAAA,aAAa,CAACd,WAAWf,IAAI,CAAC,YAA9B6B,iCAAkCxC;kBAAYsC;;AAEnE"}
|
|
1
|
+
{"version":3,"sources":["../../../../src/ui/atoms/cell-value/cell-value.tsx"],"sourcesContent":["'use client';\n\nimport type { Formatters, Locale } from '@entifix/core';\nimport {\n defaultLocale,\n type Entity,\n EntityCollectionLink,\n type EntityFieldDescriptor,\n EntityLink,\n formatScaled,\n isLocalizedText,\n localize,\n type MetaAccessorType,\n} from '@entifix/core';\nimport { useCallback } from 'react';\n\nimport {\n useEnumLabel,\n useFormatters,\n useLocale,\n useT,\n} from '../../../i18n/index.js';\nimport {\n useReferenceLabel,\n useUnresolvedReference,\n} from '../../utils/reference-labels.js';\nimport { Money } from '../money/money.js';\nimport { currencyOf } from '../money/money-math.js';\nimport { scaleOf } from '../scaled/scaled-math.js';\nimport { ScaledValue } from '../scaled/scaled-value.js';\nimport { useUnitLabel } from '../scaled/use-unit-label.js';\nimport { Skeleton } from '../skeleton/index.js';\nimport { StatusTag } from '../status-tag/index.js';\n\n/** Stand-in for an absent value, so an empty cell still reads as intentional. */\nconst EMPTY = '—';\n\n/**\n * A link reads as its target's label once the target is loaded (embedded or\n * resolved). Never as its id: a key is not a name, and showing one fails the\n * reader who came to see which record it is (#200).\n */\nfunction entityLabel(\n target: Entity | undefined,\n linkLabelProperty: string,\n locale: Locale,\n): string | undefined {\n const label = (target as Record<string, unknown> | undefined)?.[\n linkLabelProperty\n ];\n if (label == null) return undefined;\n // A target labelled by a localized member reads in the reader's locale.\n return isLocalizedText(label) ? localize(label, locale) : String(label);\n}\n\nfunction formatLink(\n link: EntityLink<Entity>,\n linkLabelProperty: string,\n locale: Locale,\n): string {\n return link.id == null\n ? EMPTY\n : (entityLabel(link.value, linkLabelProperty, locale) ?? EMPTY);\n}\n\nfunction formatCollectionLink(\n link: EntityCollectionLink<Entity>,\n linkLabelProperty: string,\n locale: Locale,\n): string {\n return (link.values ?? [])\n .map(target => entityLabel(target, linkLabelProperty, locale) ?? EMPTY)\n .join(', ');\n}\n\n/**\n * Everything locale-dependent, resolved once by the component and handed down.\n * The formatters carry an explicit locale, which is what keeps a date rendered\n * on the server byte-identical to the same date rendered in the browser.\n */\ninterface CellFormatting {\n readonly formatters: Formatters;\n readonly yes: string;\n readonly no: string;\n readonly enumLabel: (value: string) => string;\n readonly rowCount: (count: number) => string;\n readonly locale: Locale;\n readonly fixedScale: number | undefined;\n}\n\nfunction formatByType(\n value: unknown,\n type: MetaAccessorType,\n linkLabelProperty: string,\n formatting: CellFormatting,\n): string {\n switch (type) {\n case 'date':\n return formatting.formatters.date(\n value instanceof Date ? value : String(value),\n );\n case 'number':\n if (typeof value !== 'number') return String(value);\n // Words have no record to read a unit from, so only a fixed scale can\n // be honoured here.\n return formatting.fixedScale !== undefined && Number.isSafeInteger(value)\n ? formatScaled(value, formatting.fixedScale, formatting.locale)\n : formatting.formatters.number(value);\n case 'boolean':\n return value ? formatting.yes : formatting.no;\n case 'enum':\n return formatting.enumLabel(String(value));\n case 'link':\n return value instanceof EntityLink\n ? formatLink(value, linkLabelProperty, formatting.locale)\n : String(value);\n case 'linkCollection':\n return value instanceof EntityCollectionLink\n ? formatCollectionLink(value, linkLabelProperty, formatting.locale)\n : String(value);\n case 'scalarCollection':\n // The same comma list the draft holds, spaced for reading. Not `String`:\n // an array's own `toString` joins on a bare comma, which reads as one\n // run-on token in a narrow column.\n return Array.isArray(value) ? value.join(', ') : String(value);\n case 'composition':\n // Owned rows have no useful one-line form — a receipt's lines are not a\n // sentence — and the master's own screen is where they are read and\n // edited. So the cell reports the count and stops.\n return Array.isArray(value)\n ? formatting.rowCount(value.length)\n : String(value);\n case 'localized':\n // The reader's locale, falling back to the host's default; a value\n // that is not a text per locale has no reading worth guessing at.\n return isLocalizedText(value)\n ? (localize(value, formatting.locale) ?? '')\n : '';\n default:\n return String(value);\n }\n}\n\n/** The formatting {@link formatByType} reads, resolved for the reader. */\nfunction useCellFormatting(): (\n descriptor: EntityFieldDescriptor,\n) => CellFormatting {\n const t = useT();\n const formatters = useFormatters();\n const enumLabel = useEnumLabel();\n const locale = useLocale();\n return useCallback(\n descriptor => ({\n formatters,\n yes: t('value.yes'),\n no: t('value.no'),\n enumLabel: raw => enumLabel(descriptor, raw),\n rowCount: count => t('value.rowCount', { count }),\n locale,\n fixedScale: descriptor.scaled?.scale,\n }),\n [enumLabel, formatters, locale, t],\n );\n}\n\n/**\n * A filled-in member's value as one line of text, exactly as its cell reads\n * it — for the places that need words rather than an element: a checkbox's\n * name, a confirm dialog's object, a toast.\n */\nexport function useValueText(): (\n value: unknown,\n descriptor: EntityFieldDescriptor,\n) => string {\n const formatting = useCellFormatting();\n return useCallback(\n (value, descriptor) =>\n formatByType(\n value,\n descriptor.type,\n descriptor.linkLabelProperty,\n formatting(descriptor),\n ),\n [formatting],\n );\n}\n\n/**\n * A reference reads as its target's name, in every state the read can be in\n * ([ADR 0030](../../../../../../../docs/adr/0030-a-reference-names-its-target-through-the-owners-read-port.md)):\n * a skeleton while it resolves (the noun and short id for a screen reader); the name; the entity's noun and the short id,\n * muted, when no record answered or the read failed — never an alert, since a\n * list that loaded has not failed because one name could not be read; and the\n * raw id, muted, when the host registered nothing to read it with.\n */\nfunction ReferenceValue({ entity, id }: { entity: string; id: string }) {\n const state = useReferenceLabel(entity, id);\n const unresolved = useUnresolvedReference();\n const locale = useLocale();\n switch (state.status) {\n case 'resolving':\n // The skeleton is hidden from assistive tech, so the noun and short id\n // stand in for it there: a record's title link is never nameless.\n return (\n <>\n <Skeleton shape=\"line\" className=\"w-16\" />\n <span className=\"sr-only\">{unresolved(entity, id)}</span>\n </>\n );\n case 'named': {\n const { label } = state;\n if (typeof label === 'string') return <span>{label}</span>;\n const text = localize(label, locale);\n return (label[locale] ?? '') === '' ? (\n <span lang={defaultLocale()}>{text}</span>\n ) : (\n <span>{text}</span>\n );\n }\n case 'unregistered':\n return <span className=\"text-content-muted\">{id}</span>;\n default:\n return (\n <span className=\"text-content-muted\">{unresolved(entity, id)}</span>\n );\n }\n}\n\n/**\n * Tabular figures, applied once, here — not on `--font-sans` and not per call\n * site. A column of numbers or dates must align on the digit; a paragraph must\n * not, because proportional figures are what prose is set in. The descriptor\n * already knows which is which, so the cell is the only place that has to.\n */\nconst NUMERIC_CLASS: Partial<Record<MetaAccessorType, string>> = {\n number: 'tabular-nums',\n date: 'tabular-nums',\n};\n\nexport interface CellValueProps {\n value: unknown;\n descriptor: EntityFieldDescriptor;\n /**\n * The record the value belongs to. A money member whose currency is another\n * member of the record (`money.currencyMember`), and a scaled member whose\n * unit is (`scaled.unitMember`), read it from here.\n */\n record?: Readonly<Record<string, unknown>>;\n}\n\n/**\n * Renders one entity member according to its metadata type. Centralizing this\n * is what lets a table stay generic: a link, a date and a number all arrive as\n * \"the value of an accessor\", and only the descriptor decides how they read.\n */\nexport function CellValue({ value, descriptor, record }: CellValueProps) {\n const formatting = useCellFormatting();\n const locale = useLocale();\n const unitLabel = useUnitLabel();\n\n if (value === undefined || value === null || value === '') {\n return <span className=\"text-content-muted\">{EMPTY}</span>;\n }\n\n // A key into another entity reads as that record's name, never as itself.\n if (descriptor.references !== undefined) {\n return (\n <ReferenceValue\n entity={descriptor.references.entity}\n id={String(value)}\n />\n );\n }\n\n // An amount reads as money in the reader's locale, never as the stored\n // number. A draft holds it as a string, so a numeric string counts too. A\n // currency the record does not carry leaves it a plain number rather than\n // guessing one.\n if (descriptor.money !== undefined) {\n const amount = typeof value === 'number' ? value : Number(value);\n const currency = currencyOf(descriptor.money, record);\n if (currency !== undefined && Number.isFinite(amount)) {\n return (\n <Money\n amount={amount}\n currency={currency}\n unit={descriptor.money.unit}\n locale={locale}\n />\n );\n }\n }\n\n // A scaled integer reads as the decimal it stands for, beside its unit. One\n // whose record names no declared unit stays the number it is, as money\n // without a currency does.\n if (descriptor.scaled !== undefined) {\n const stored = Number(value);\n const resolved = scaleOf(descriptor.scaled, record);\n if (resolved !== undefined && Number.isSafeInteger(stored)) {\n return (\n <ScaledValue\n value={stored}\n scale={resolved.scale}\n unit={unitLabel(descriptor.scaled, resolved.unit)}\n locale={locale}\n />\n );\n }\n }\n\n // A relation whose target has not arrived yet holds its place with a\n // skeleton — the id is all it has, and the id is not what it means.\n if (\n (value instanceof EntityLink &&\n value.id != null &&\n value.value === undefined) ||\n (value instanceof EntityCollectionLink &&\n value.ids.length > 0 &&\n value.values === undefined)\n ) {\n return <Skeleton shape=\"line\" className=\"w-16\" />;\n }\n\n const text = formatByType(\n value,\n descriptor.type,\n descriptor.linkLabelProperty,\n formatting(descriptor),\n );\n\n if (text === '') {\n return <span className=\"text-content-muted\">{EMPTY}</span>;\n }\n\n // A status reads as a tag in its declared tone, its words beside the\n // colour; a value given no tone reads neutral. An enum that declared no\n // tones is not a status, and stays text.\n if (descriptor.type === 'enum' && descriptor.enumTones !== undefined) {\n return (\n <StatusTag tone={descriptor.enumTones[String(value)] ?? 'neutral'}>\n {text}\n </StatusTag>\n );\n }\n\n // Text the reader's locale lacks falls back to the default locale's, and is\n // marked as that language, so it is read in its own voice (WCAG 3.1.2).\n if (\n descriptor.type === 'localized' &&\n isLocalizedText(value) &&\n (value[locale] ?? '') === ''\n ) {\n return <span lang={defaultLocale()}>{text}</span>;\n }\n\n return (\n <span className={NUMERIC_CLASS[descriptor.type] ?? undefined}>{text}</span>\n );\n}\n"],"names":["defaultLocale","EntityCollectionLink","EntityLink","formatScaled","isLocalizedText","localize","useCallback","useEnumLabel","useFormatters","useLocale","useT","useReferenceLabel","useUnresolvedReference","Money","currencyOf","scaleOf","ScaledValue","useUnitLabel","Skeleton","StatusTag","EMPTY","entityLabel","target","linkLabelProperty","locale","label","undefined","String","formatLink","link","id","value","formatCollectionLink","values","map","join","formatByType","type","formatting","formatters","date","Date","fixedScale","Number","isSafeInteger","number","yes","no","enumLabel","Array","isArray","rowCount","length","useCellFormatting","t","descriptor","raw","count","scaled","scale","useValueText","ReferenceValue","entity","state","unresolved","status","shape","className","span","text","lang","NUMERIC_CLASS","CellValue","record","unitLabel","references","money","amount","currency","isFinite","unit","stored","resolved","ids","enumTones","tone"],"mappings":"AAAA;;AAGA,SACEA,aAAa,EAEbC,oBAAoB,EAEpBC,UAAU,EACVC,YAAY,EACZC,eAAe,EACfC,QAAQ,QAEH,gBAAgB;AACvB,SAASC,WAAW,QAAQ,QAAQ;AAEpC,SACEC,YAAY,EACZC,aAAa,EACbC,SAAS,EACTC,IAAI,QACC,yBAAyB;AAChC,SACEC,iBAAiB,EACjBC,sBAAsB,QACjB,kCAAkC;AACzC,SAASC,KAAK,QAAQ,oBAAoB;AAC1C,SAASC,UAAU,QAAQ,yBAAyB;AACpD,SAASC,OAAO,QAAQ,2BAA2B;AACnD,SAASC,WAAW,QAAQ,4BAA4B;AACxD,SAASC,YAAY,QAAQ,8BAA8B;AAC3D,SAASC,QAAQ,QAAQ,uBAAuB;AAChD,SAASC,SAAS,QAAQ,yBAAyB;AAEnD,+EAA+E,GAC/E,MAAMC,QAAQ;AAEd;;;;CAIC,GACD,SAASC,YACPC,MAA0B,EAC1BC,iBAAyB,EACzBC,MAAc;IAEd,MAAMC,QAASH,0BAAD,AAACA,MAAgD,CAC7DC,kBACD;IACD,IAAIE,SAAS,MAAM,OAAOC;IAC1B,wEAAwE;IACxE,OAAOtB,gBAAgBqB,SAASpB,SAASoB,OAAOD,UAAUG,OAAOF;AACnE;AAEA,SAASG,WACPC,IAAwB,EACxBN,iBAAyB,EACzBC,MAAc;QAITH;IAFL,OAAOQ,KAAKC,EAAE,IAAI,OACdV,SACCC,eAAAA,YAAYQ,KAAKE,KAAK,EAAER,mBAAmBC,mBAA3CH,eAAsDD;AAC7D;AAEA,SAASY,qBACPH,IAAkC,EAClCN,iBAAyB,EACzBC,MAAc;QAENK;IAAR,OAAO,EAACA,eAAAA,KAAKI,MAAM,YAAXJ,eAAe,EAAE,EACtBK,GAAG,CAACZ,CAAAA;YAAUD;gBAAAA,eAAAA,YAAYC,QAAQC,mBAAmBC,mBAAvCH,eA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|
|
|
26
26
|
id?: string;
|
|
27
27
|
/**
|
|
28
28
|
* The record being edited, as its draft. A money member whose currency is
|
|
29
|
-
* another member (`money.currencyMember`)
|
|
29
|
+
* another member (`money.currencyMember`), and a scaled one whose unit is
|
|
30
|
+
* (`scaled.unitMember`), read it from here.
|
|
30
31
|
*/
|
|
31
32
|
record?: Readonly<Record<string, unknown>>;
|
|
32
33
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"field-control.d.ts","sourceRoot":"","sources":["../../../../src/ui/atoms/field-control/field-control.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,qBAAqB,EAAoB,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"field-control.d.ts","sourceRoot":"","sources":["../../../../src/ui/atoms/field-control/field-control.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,qBAAqB,EAAoB,MAAM,eAAe,CAAC;AAU7E,MAAM,WAAW,iBAAiB;IAChC,UAAU,EAAE,qBAAqB,CAAC;IAClC;;;;;;;;;;OAUG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B;;;;OAIG;IACH,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAClC,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ;;;;OAIG;IACH,MAAM,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;CAC5C;AAQD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,YAAY,CAAC,EAC3B,UAAU,EACV,KAAK,EACL,QAAQ,EACR,EAAE,EACF,MAAM,EACN,GAAG,IAAI,EACR,EAAE,iBAAiB,+BA8FnB"}
|
|
@@ -6,6 +6,9 @@ import { useEnumLabel, useLocale } from '../../../i18n/index.js';
|
|
|
6
6
|
import { Checkbox, Select, TextInput } from '../field/index.js';
|
|
7
7
|
import { MoneyInput } from '../money/money-input.js';
|
|
8
8
|
import { currencyOf } from '../money/money-math.js';
|
|
9
|
+
import { ScaledInput } from '../scaled/scaled-input.js';
|
|
10
|
+
import { scaleOf } from '../scaled/scaled-math.js';
|
|
11
|
+
import { useUnitLabel } from '../scaled/use-unit-label.js';
|
|
9
12
|
/** Native input type for the text-like controls; text is the default. */ const INPUT_TYPE = {
|
|
10
13
|
number: 'number',
|
|
11
14
|
date: 'date'
|
|
@@ -42,6 +45,7 @@ import { currencyOf } from '../money/money-math.js';
|
|
|
42
45
|
var _INPUT_TYPE_descriptor_type;
|
|
43
46
|
const enumLabel = useEnumLabel();
|
|
44
47
|
const locale = useLocale();
|
|
48
|
+
const unitLabel = useUnitLabel();
|
|
45
49
|
const disabled = descriptor.readonly || descriptor.type === 'link' || descriptor.type === 'linkCollection' || descriptor.type === 'composition';
|
|
46
50
|
if (descriptor.type === 'boolean') {
|
|
47
51
|
return /*#__PURE__*/ _jsx(Checkbox, _extends({
|
|
@@ -89,6 +93,21 @@ import { currencyOf } from '../money/money-math.js';
|
|
|
89
93
|
onChange: onChange
|
|
90
94
|
}));
|
|
91
95
|
}
|
|
96
|
+
// A scaled integer is edited as the decimal it stands for. Unlike money it
|
|
97
|
+
// has no plain fallback: without a scale, `3` typed would be stored as 0.003.
|
|
98
|
+
if (descriptor.scaled !== undefined) {
|
|
99
|
+
const resolved = scaleOf(descriptor.scaled, record);
|
|
100
|
+
return /*#__PURE__*/ _jsx(ScaledInput, _extends({
|
|
101
|
+
id: id
|
|
102
|
+
}, aria, {
|
|
103
|
+
value: value,
|
|
104
|
+
scale: resolved == null ? void 0 : resolved.scale,
|
|
105
|
+
unit: unitLabel(descriptor.scaled, resolved == null ? void 0 : resolved.unit),
|
|
106
|
+
locale: locale,
|
|
107
|
+
disabled: disabled,
|
|
108
|
+
onChange: onChange
|
|
109
|
+
}));
|
|
110
|
+
}
|
|
92
111
|
return /*#__PURE__*/ _jsx(TextInput, _extends({
|
|
93
112
|
id: id
|
|
94
113
|
}, aria, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/ui/atoms/field-control/field-control.tsx"],"sourcesContent":["'use client';\n\nimport type { EntityFieldDescriptor, MetaAccessorType } from '@entifix/core';\n\nimport { useEnumLabel, useLocale } from '../../../i18n/index.js';\nimport { Checkbox, Select, TextInput } from '../field/index.js';\nimport { MoneyInput } from '../money/money-input.js';\nimport { currencyOf } from '../money/money-math.js';\n\nexport interface FieldControlProps {\n descriptor: EntityFieldDescriptor;\n /**\n * ARIA wiring the caller owns, because only the caller knows the shape it is\n * rendered into.\n *\n * A form labels its control with a `<label for>` and needs none of this; a\n * detail grid's cell has no label of its own — the column header is the\n * label, and the cell's message is the description — so it points at both by\n * id. Passing them through here rather than duplicating the type→input map\n * in the grid is what keeps one control deciding what a `date` member looks\n * like.\n */\n 'aria-labelledby'?: string;\n 'aria-describedby'?: string;\n 'aria-invalid'?: boolean;\n 'aria-required'?: boolean;\n /**\n * The draft value as a string — the shape a native input round-trips. A form\n * holds its whole draft as `Record<string, string>` and reconstructs typed\n * values only at submit, so the control never sees a domain value.\n */\n value: string;\n onChange: (value: string) => void;\n id?: string;\n /**\n * The record being edited, as its draft. A money member whose currency is\n * another member (`money.currencyMember`)
|
|
1
|
+
{"version":3,"sources":["../../../../src/ui/atoms/field-control/field-control.tsx"],"sourcesContent":["'use client';\n\nimport type { EntityFieldDescriptor, MetaAccessorType } from '@entifix/core';\n\nimport { useEnumLabel, useLocale } from '../../../i18n/index.js';\nimport { Checkbox, Select, TextInput } from '../field/index.js';\nimport { MoneyInput } from '../money/money-input.js';\nimport { currencyOf } from '../money/money-math.js';\nimport { ScaledInput } from '../scaled/scaled-input.js';\nimport { scaleOf } from '../scaled/scaled-math.js';\nimport { useUnitLabel } from '../scaled/use-unit-label.js';\n\nexport interface FieldControlProps {\n descriptor: EntityFieldDescriptor;\n /**\n * ARIA wiring the caller owns, because only the caller knows the shape it is\n * rendered into.\n *\n * A form labels its control with a `<label for>` and needs none of this; a\n * detail grid's cell has no label of its own — the column header is the\n * label, and the cell's message is the description — so it points at both by\n * id. Passing them through here rather than duplicating the type→input map\n * in the grid is what keeps one control deciding what a `date` member looks\n * like.\n */\n 'aria-labelledby'?: string;\n 'aria-describedby'?: string;\n 'aria-invalid'?: boolean;\n 'aria-required'?: boolean;\n /**\n * The draft value as a string — the shape a native input round-trips. A form\n * holds its whole draft as `Record<string, string>` and reconstructs typed\n * values only at submit, so the control never sees a domain value.\n */\n value: string;\n onChange: (value: string) => void;\n id?: string;\n /**\n * The record being edited, as its draft. A money member whose currency is\n * another member (`money.currencyMember`), and a scaled one whose unit is\n * (`scaled.unitMember`), read it from here.\n */\n record?: Readonly<Record<string, unknown>>;\n}\n\n/** Native input type for the text-like controls; text is the default. */\nconst INPUT_TYPE: Partial<Record<MetaAccessorType, string>> = {\n number: 'number',\n date: 'date',\n};\n\n/**\n * The edit-mode inverse of {@link CellValue}: it turns one metadata descriptor\n * into the input that edits it. Centralizing the type→control choice is what\n * lets a form stay generic, the same way `CellValue` keeps a table generic.\n *\n * A relation (`link`/`linkCollection`) is rendered read-only here, deliberately:\n * editing one needs a picker that can search and page the target, which is a\n * different control with different dependencies. So this atom stays a pure\n * type→input map and a relation never falls back to a broken text box.\n *\n * For a to-one `link` that is only half the story — `EntityForm` wires the\n * picker in one level up from its `linkSources`, so the member does get an\n * editor, just not from here. A `linkCollection` has no editor anywhere yet\n * (#26), and read-only is all it gets. A `composition` is the same: its rows\n * need a detail grid, and a text box holding `[object Object]` would be\n * strictly worse than an honest disabled field.\n *\n * A `scalarCollection` is the exception among the collections and falls through\n * to the plain text input on purpose: a `string[]` has a lossless comma form,\n * so the draft it round-trips through *is* editable text. Splitting on the\n * comma is `reconstructEntity`'s half of that same contract.\n */\nexport function FieldControl({\n descriptor,\n value,\n onChange,\n id,\n record,\n ...aria\n}: FieldControlProps) {\n const enumLabel = useEnumLabel();\n const locale = useLocale();\n const unitLabel = useUnitLabel();\n const disabled =\n descriptor.readonly ||\n descriptor.type === 'link' ||\n descriptor.type === 'linkCollection' ||\n descriptor.type === 'composition';\n\n if (descriptor.type === 'boolean') {\n return (\n <Checkbox\n id={id}\n {...aria}\n label={descriptor.label}\n checked={value === 'true'}\n disabled={disabled}\n onChange={event => onChange(String(event.currentTarget.checked))}\n />\n );\n }\n\n if (descriptor.type === 'enum') {\n return (\n <Select\n id={id}\n {...aria}\n value={value}\n disabled={disabled}\n onChange={event => onChange(event.currentTarget.value)}\n >\n <option value=\"\">—</option>\n {(descriptor.enumValues ?? []).map(option => (\n <option key={option} value={option}>\n {enumLabel(descriptor, option)}\n </option>\n ))}\n </Select>\n );\n }\n\n // An amount is edited in major units, in the reader's decimal separator,\n // and written back in the unit it is stored in. Without a currency to know\n // the minor units by, it stays a plain number field.\n const currency =\n descriptor.money === undefined\n ? undefined\n : currencyOf(descriptor.money, record);\n if (descriptor.money !== undefined && currency !== undefined) {\n return (\n <MoneyInput\n id={id}\n {...aria}\n value={value}\n currency={currency}\n unit={descriptor.money.unit}\n locale={locale}\n disabled={disabled}\n onChange={onChange}\n />\n );\n }\n\n // A scaled integer is edited as the decimal it stands for. Unlike money it\n // has no plain fallback: without a scale, `3` typed would be stored as 0.003.\n if (descriptor.scaled !== undefined) {\n const resolved = scaleOf(descriptor.scaled, record);\n return (\n <ScaledInput\n id={id}\n {...aria}\n value={value}\n scale={resolved?.scale}\n unit={unitLabel(descriptor.scaled, resolved?.unit)}\n locale={locale}\n disabled={disabled}\n onChange={onChange}\n />\n );\n }\n\n return (\n <TextInput\n id={id}\n {...aria}\n // What the field is for, so a browser can fill it (WCAG 1.3.5).\n autoComplete={descriptor.autocomplete}\n type={INPUT_TYPE[descriptor.type] ?? 'text'}\n value={value}\n disabled={disabled}\n onChange={event => onChange(event.currentTarget.value)}\n />\n );\n}\n"],"names":["useEnumLabel","useLocale","Checkbox","Select","TextInput","MoneyInput","currencyOf","ScaledInput","scaleOf","useUnitLabel","INPUT_TYPE","number","date","FieldControl","descriptor","value","onChange","id","record","aria","enumLabel","locale","unitLabel","disabled","readonly","type","label","checked","event","String","currentTarget","option","enumValues","map","currency","money","undefined","unit","scaled","resolved","scale","autoComplete","autocomplete"],"mappings":"AAAA;;;;AAIA,SAASA,YAAY,EAAEC,SAAS,QAAQ,yBAAyB;AACjE,SAASC,QAAQ,EAAEC,MAAM,EAAEC,SAAS,QAAQ,oBAAoB;AAChE,SAASC,UAAU,QAAQ,0BAA0B;AACrD,SAASC,UAAU,QAAQ,yBAAyB;AACpD,SAASC,WAAW,QAAQ,4BAA4B;AACxD,SAASC,OAAO,QAAQ,2BAA2B;AACnD,SAASC,YAAY,QAAQ,8BAA8B;AAmC3D,uEAAuE,GACvE,MAAMC,aAAwD;IAC5DC,QAAQ;IACRC,MAAM;AACR;AAEA;;;;;;;;;;;;;;;;;;;;;CAqBC,GACD,OAAO,SAASC,aAAa,EAOT;QAPS,EAC3BC,UAAU,EACVC,KAAK,EACLC,QAAQ,EACRC,EAAE,EACFC,MAAM,EAEY,OADfC;;;;;;;QAyFOT;IAvFV,MAAMU,YAAYpB;IAClB,MAAMqB,SAASpB;IACf,MAAMqB,YAAYb;IAClB,MAAMc,WACJT,WAAWU,QAAQ,IACnBV,WAAWW,IAAI,KAAK,UACpBX,WAAWW,IAAI,KAAK,oBACpBX,WAAWW,IAAI,KAAK;IAEtB,IAAIX,WAAWW,IAAI,KAAK,WAAW;QACjC,qBACE,KAACvB;YACCe,IAAIA;WACAE;YACJO,OAAOZ,WAAWY,KAAK;YACvBC,SAASZ,UAAU;YACnBQ,UAAUA;YACVP,UAAUY,CAAAA,QAASZ,SAASa,OAAOD,MAAME,aAAa,CAACH,OAAO;;IAGpE;IAEA,IAAIb,WAAWW,IAAI,KAAK,QAAQ;YAUxBX;QATN,qBACE,M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,CAACf,KAAK;;AAG3D"}
|
|
@@ -14,6 +14,10 @@ export interface MoneyInputProps extends Omit<NumberInputProps, 'value' | 'onCha
|
|
|
14
14
|
* back the stored unit. What was typed is kept as typed (a trailing `64,` is
|
|
15
15
|
* not reformatted under the cursor), and the field follows the stored value
|
|
16
16
|
* only when it changes from outside.
|
|
17
|
+
*
|
|
18
|
+
* `1,000` is a thousand in any locale, since no two-decimal currency has
|
|
19
|
+
* three; text with more decimals than the currency holds is refused by
|
|
20
|
+
* validation rather than rounded (`parseScaled`).
|
|
17
21
|
*/
|
|
18
22
|
export declare function MoneyInput({ value, onChange, currency, unit, locale, ...props }: MoneyInputProps): import("react").JSX.Element;
|
|
19
23
|
//# sourceMappingURL=money-input.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"money-input.d.ts","sourceRoot":"","sources":["../../../../src/ui/atoms/money/money-input.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"money-input.d.ts","sourceRoot":"","sources":["../../../../src/ui/atoms/money/money-input.tsx"],"names":[],"mappings":"AAKA,OAAO,EAEL,KAAK,gBAAgB,EACtB,MAAM,iCAAiC,CAAC;AAEzC,OAAO,EAAkB,KAAK,SAAS,EAAgB,MAAM,iBAAiB,CAAC;AAE/E,MAAM,WAAW,eAAgB,SAAQ,IAAI,CAC3C,gBAAgB,EAChB,OAAO,GAAG,UAAU,GAAG,WAAW,CACnC;IACC,yEAAyE;IACzE,KAAK,EAAE,MAAM,CAAC;IACd,uDAAuD;IACvD,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAClC,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,SAAS,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;;;GASG;AACH,wBAAgB,UAAU,CAAC,EACzB,KAAK,EACL,QAAQ,EACR,QAAQ,EACR,IAAI,EACJ,MAAM,EACN,GAAG,KAAK,EACT,EAAE,eAAe,+BA+CjB"}
|
|
@@ -2,23 +2,20 @@
|
|
|
2
2
|
import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
3
3
|
import { _ as _object_without_properties_loose } from "@swc/helpers/_/_object_without_properties_loose";
|
|
4
4
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
5
|
+
import { parseScaled, scaledToDecimal } from '@entifix/core';
|
|
5
6
|
import { useEffect, useState } from 'react';
|
|
6
7
|
import { NumberInput } from '../number-input/number-input.js';
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
// Accept either decimal separator: the last `,` or `.` is the decimal one.
|
|
10
|
-
const normalised = text.trim().replace(/[\s']/g, '');
|
|
11
|
-
if (normalised === '') return undefined;
|
|
12
|
-
const last = Math.max(normalised.lastIndexOf(','), normalised.lastIndexOf('.'));
|
|
13
|
-
const plain = last === -1 ? normalised : `${normalised.slice(0, last).replace(/[.,]/g, '')}.${normalised.slice(last + 1)}`;
|
|
14
|
-
const major = Number(plain);
|
|
15
|
-
return Number.isFinite(major) ? major : undefined;
|
|
16
|
-
};
|
|
8
|
+
import { typedText, unparsedDraft } from '../number-input/unparsed.js';
|
|
9
|
+
import { fractionDigits, toMajorUnits } from './money-math.js';
|
|
17
10
|
/**
|
|
18
11
|
* Edits an amount in major units — what a person means by a price — and writes
|
|
19
12
|
* back the stored unit. What was typed is kept as typed (a trailing `64,` is
|
|
20
13
|
* not reformatted under the cursor), and the field follows the stored value
|
|
21
14
|
* only when it changes from outside.
|
|
15
|
+
*
|
|
16
|
+
* `1,000` is a thousand in any locale, since no two-decimal currency has
|
|
17
|
+
* three; text with more decimals than the currency holds is refused by
|
|
18
|
+
* validation rather than rounded (`parseScaled`).
|
|
22
19
|
*/ export function MoneyInput(_0) {
|
|
23
20
|
let { value, onChange, currency, unit, locale } = _0, props = _object_without_properties_loose(_0, [
|
|
24
21
|
"value",
|
|
@@ -28,7 +25,7 @@ const parseMajor = (text)=>{
|
|
|
28
25
|
"locale"
|
|
29
26
|
]);
|
|
30
27
|
const digits = fractionDigits(currency);
|
|
31
|
-
const display = (stored)=>stored === '' || !Number.isFinite(Number(stored)) ? stored : new Intl.NumberFormat(locale, {
|
|
28
|
+
const display = (stored)=>stored === '' || !Number.isFinite(Number(stored)) ? typedText(stored) : new Intl.NumberFormat(locale, {
|
|
32
29
|
minimumFractionDigits: digits,
|
|
33
30
|
maximumFractionDigits: digits,
|
|
34
31
|
useGrouping: false
|
|
@@ -57,8 +54,8 @@ const parseMajor = (text)=>{
|
|
|
57
54
|
onChange: (event)=>{
|
|
58
55
|
const next = event.currentTarget.value;
|
|
59
56
|
setText(next);
|
|
60
|
-
const
|
|
61
|
-
const stored =
|
|
57
|
+
const minor = parseScaled(next, digits, locale);
|
|
58
|
+
const stored = minor === undefined ? unparsedDraft(next) : unit === 'minor' ? String(minor) : scaledToDecimal(minor, digits);
|
|
62
59
|
setEmitted(stored);
|
|
63
60
|
onChange(stored);
|
|
64
61
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/ui/atoms/money/money-input.tsx"],"sourcesContent":["'use client';\n\nimport { useEffect, useState } from 'react';\n\nimport {\n NumberInput,\n type NumberInputProps,\n} from '../number-input/number-input.js';\nimport {
|
|
1
|
+
{"version":3,"sources":["../../../../src/ui/atoms/money/money-input.tsx"],"sourcesContent":["'use client';\n\nimport { parseScaled, scaledToDecimal } from '@entifix/core';\nimport { useEffect, useState } from 'react';\n\nimport {\n NumberInput,\n type NumberInputProps,\n} from '../number-input/number-input.js';\nimport { typedText, unparsedDraft } from '../number-input/unparsed.js';\nimport { fractionDigits, type MoneyUnit, toMajorUnits } from './money-math.js';\n\nexport interface MoneyInputProps extends Omit<\n NumberInputProps,\n 'value' | 'onChange' | 'inputMode'\n> {\n /** The stored amount, as a draft string (`'6499'`), or `''` for none. */\n value: string;\n /** Called with the stored amount as a draft string. */\n onChange: (value: string) => void;\n currency: string;\n unit: MoneyUnit;\n locale: string;\n}\n\n/**\n * Edits an amount in major units — what a person means by a price — and writes\n * back the stored unit. What was typed is kept as typed (a trailing `64,` is\n * not reformatted under the cursor), and the field follows the stored value\n * only when it changes from outside.\n *\n * `1,000` is a thousand in any locale, since no two-decimal currency has\n * three; text with more decimals than the currency holds is refused by\n * validation rather than rounded (`parseScaled`).\n */\nexport function MoneyInput({\n value,\n onChange,\n currency,\n unit,\n locale,\n ...props\n}: MoneyInputProps) {\n const digits = fractionDigits(currency);\n const display = (stored: string) =>\n stored === '' || !Number.isFinite(Number(stored))\n ? typedText(stored)\n : new Intl.NumberFormat(locale, {\n minimumFractionDigits: digits,\n maximumFractionDigits: digits,\n useGrouping: false,\n }).format(toMajorUnits(Number(stored), currency, unit));\n\n const [text, setText] = useState(() => display(value));\n const [emitted, setEmitted] = useState(value);\n\n useEffect(() => {\n if (value === emitted) return;\n // eslint-disable-next-line react-hooks/set-state-in-effect -- an outside write (a reset, a load) replaces what was typed\n setText(display(value));\n setEmitted(value);\n // eslint-disable-next-line react-hooks/exhaustive-deps -- `display` is derived from props already listed\n }, [value, emitted, currency, unit, locale]);\n\n return (\n <span className=\"inline-flex items-center gap-2xs\">\n <NumberInput\n {...props}\n inputMode={digits === 0 ? 'numeric' : 'decimal'}\n value={text}\n onChange={event => {\n const next = event.currentTarget.value;\n setText(next);\n const minor = parseScaled(next, digits, locale);\n const stored =\n minor === undefined\n ? unparsedDraft(next)\n : unit === 'minor'\n ? String(minor)\n : scaledToDecimal(minor, digits);\n setEmitted(stored);\n onChange(stored);\n }}\n />\n <span className=\"text-step-sm text-content-muted\" aria-hidden=\"true\">\n {currency}\n </span>\n </span>\n );\n}\n"],"names":["parseScaled","scaledToDecimal","useEffect","useState","NumberInput","typedText","unparsedDraft","fractionDigits","toMajorUnits","MoneyInput","value","onChange","currency","unit","locale","props","digits","display","stored","Number","isFinite","Intl","NumberFormat","minimumFractionDigits","maximumFractionDigits","useGrouping","format","text","setText","emitted","setEmitted","span","className","inputMode","event","next","currentTarget","min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ACP0B,OAAOF,SACPpC,gBAAgBoC,OAAOrB;oBAC/Bc,WAAWZ;oBACXP,SAASO;gBACX;;0BAEF,KAACa;gBAAKC,WAAU;gBAAkCQ,eAAY;0BAC3D5B;;;;AAIT"}
|
|
@@ -4,6 +4,20 @@ interface QuantityInputBase {
|
|
|
4
4
|
min?: number;
|
|
5
5
|
max?: number;
|
|
6
6
|
step?: number;
|
|
7
|
+
/**
|
|
8
|
+
* How many decimals a stored unit is: `3` reads `3500` as 3.5. Given, every
|
|
9
|
+
* number in these props is that stored integer and a typed entry is never
|
|
10
|
+
* clamped or rounded — one outside `min`, `max` or `step` is marked invalid
|
|
11
|
+
* and still reported, and text that is no number is reported as `NaN`.
|
|
12
|
+
* `0` is whole units under those same rules.
|
|
13
|
+
*/
|
|
14
|
+
scale?: number;
|
|
15
|
+
/** With `scale`: the unit, as the reader names it. */
|
|
16
|
+
unit?: string;
|
|
17
|
+
/** With `scale`: the caller's own refusal, beside the field's. */
|
|
18
|
+
invalid?: boolean;
|
|
19
|
+
/** With `scale`: what describes the field, usually the refusal's message. */
|
|
20
|
+
'aria-describedby'?: string;
|
|
7
21
|
disabled?: boolean;
|
|
8
22
|
id?: string;
|
|
9
23
|
/**
|
|
@@ -34,9 +48,10 @@ export interface UncontrolledQuantityInputProps extends QuantityInputBase {
|
|
|
34
48
|
}
|
|
35
49
|
export type QuantityInputProps = ControlledQuantityInputProps | UncontrolledQuantityInputProps;
|
|
36
50
|
/**
|
|
37
|
-
* A count,
|
|
38
|
-
*
|
|
39
|
-
*
|
|
51
|
+
* A count — or, given a `scale`, a measure: see that prop — on the WAI-ARIA
|
|
52
|
+
* spinbutton pattern: Up and Down step it, Home and End jump to its bounds,
|
|
53
|
+
* and a count typed is committed on Enter or on leaving the field, clamped
|
|
54
|
+
* to the bounds. The − and + beside it are named
|
|
40
55
|
* "Decrease {label}" and "Increase {label}" and sit at `--target-min`; they are
|
|
41
56
|
* out of the Tab order, as the pattern has them, because the arrow keys
|
|
42
57
|
* already do what they do.
|
|
@@ -45,6 +60,6 @@ export type QuantityInputProps = ControlledQuantityInputProps | UncontrolledQuan
|
|
|
45
60
|
* — the second is how a Server Action form uses it, since a server component
|
|
46
61
|
* can pass neither a state nor a handler.
|
|
47
62
|
*/
|
|
48
|
-
export declare function QuantityInput(
|
|
63
|
+
export declare function QuantityInput(props: QuantityInputProps): import("react").JSX.Element;
|
|
49
64
|
export {};
|
|
50
65
|
//# sourceMappingURL=quantity-input.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"quantity-input.d.ts","sourceRoot":"","sources":["../../../../src/ui/atoms/number-input/quantity-input.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"quantity-input.d.ts","sourceRoot":"","sources":["../../../../src/ui/atoms/number-input/quantity-input.tsx"],"names":[],"mappings":"AASA,UAAU,iBAAiB;IACzB,2EAA2E;IAC3E,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;;;;OAMG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,sDAAsD;IACtD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,kEAAkE;IAClE,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,6EAA6E;IAC7E,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ;;;;;OAKG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,uFAAuF;AACvF,MAAM,WAAW,4BAA6B,SAAQ,iBAAiB;IACrE,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAClC,YAAY,CAAC,EAAE,KAAK,CAAC;CACtB;AAED;;;;;GAKG;AACH,MAAM,WAAW,8BAA+B,SAAQ,iBAAiB;IACvE,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACpC;AAED,MAAM,MAAM,kBAAkB,GAC5B,4BAA4B,GAAG,8BAA8B,CAAC;AAEhE;;;;;;;;;;;;GAYG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,kBAAkB,+BAMtD"}
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
'use client';
|
|
2
|
+
import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
2
3
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
4
|
import { useEffect, useState } from 'react';
|
|
4
5
|
import { useT } from '../../../i18n/index.js';
|
|
5
6
|
import { cn } from '../../utils/cn.js';
|
|
6
|
-
|
|
7
|
+
import { ScaledQuantityInput } from './scaled-quantity-input.js';
|
|
8
|
+
import { STEP_BUTTON } from './step-button.js';
|
|
7
9
|
/**
|
|
8
|
-
* A count,
|
|
9
|
-
*
|
|
10
|
-
*
|
|
10
|
+
* A count — or, given a `scale`, a measure: see that prop — on the WAI-ARIA
|
|
11
|
+
* spinbutton pattern: Up and Down step it, Home and End jump to its bounds,
|
|
12
|
+
* and a count typed is committed on Enter or on leaving the field, clamped
|
|
13
|
+
* to the bounds. The − and + beside it are named
|
|
11
14
|
* "Decrease {label}" and "Increase {label}" and sit at `--target-min`; they are
|
|
12
15
|
* out of the Tab order, as the pattern has them, because the arrow keys
|
|
13
16
|
* already do what they do.
|
|
@@ -15,7 +18,12 @@ const STEP_BUTTON = 'flex min-h-(--target-min) min-w-(--target-min) shrink-0 ite
|
|
|
15
18
|
* Controlled with `value` and `onChange`, or uncontrolled with `defaultValue`
|
|
16
19
|
* — the second is how a Server Action form uses it, since a server component
|
|
17
20
|
* can pass neither a state nor a handler.
|
|
18
|
-
*/ export function QuantityInput(
|
|
21
|
+
*/ export function QuantityInput(props) {
|
|
22
|
+
return props.scale === undefined ? /*#__PURE__*/ _jsx(CountInput, _extends({}, props)) : /*#__PURE__*/ _jsx(ScaledQuantityInput, _extends({}, props, {
|
|
23
|
+
scale: props.scale
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
/** The count: what is typed is committed clamped to the bounds. */ function CountInput({ label, value: controlled, defaultValue, onChange, min = 0, max, step = 1, disabled = false, id, name, className }) {
|
|
19
27
|
const t = useT();
|
|
20
28
|
const [own, setOwn] = useState(defaultValue != null ? defaultValue : min);
|
|
21
29
|
const value = controlled != null ? controlled : own;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/ui/atoms/number-input/quantity-input.tsx"],"sourcesContent":["'use client';\n\nimport { type KeyboardEvent, useEffect, useState } from 'react';\n\nimport { useT } from '../../../i18n/index.js';\nimport { cn } from '../../utils/cn.js';\n\ninterface QuantityInputBase {\n /** What is being counted; names the spinbutton and both of its buttons. */\n label: string;\n min?: number;\n max?: number;\n step?: number;\n disabled?: boolean;\n id?: string;\n /**\n * Posts the count with the form it sits in, under this name — a server\n * action's form works without JavaScript, where the field is the only part\n * a reader can change, so the field itself carries it. The server clamps\n * what it receives: a typed value is only clamped here once committed.\n */\n name?: string;\n className?: string;\n}\n\n/** The caller owns the count: every committed change comes back through `onChange`. */\nexport interface ControlledQuantityInputProps extends QuantityInputBase {\n value: number;\n onChange: (value: number) => void;\n defaultValue?: never;\n}\n\n/**\n * The field owns the count, starting at `defaultValue` (or `min`). This is the\n * shape a server-rendered form takes: a server component cannot hand a client\n * one a function, and with `name` the count posts with the form, so the page\n * needs no client parent of its own. `onChange` still reports each commit.\n */\nexport interface UncontrolledQuantityInputProps extends QuantityInputBase {\n value?: never;\n defaultValue?: number;\n onChange?: (value: number) => void;\n}\n\nexport type QuantityInputProps =\n ControlledQuantityInputProps | UncontrolledQuantityInputProps;\n\
|
|
1
|
+
{"version":3,"sources":["../../../../src/ui/atoms/number-input/quantity-input.tsx"],"sourcesContent":["'use client';\n\nimport { type KeyboardEvent, useEffect, useState } from 'react';\n\nimport { useT } from '../../../i18n/index.js';\nimport { cn } from '../../utils/cn.js';\nimport { ScaledQuantityInput } from './scaled-quantity-input.js';\nimport { STEP_BUTTON } from './step-button.js';\n\ninterface QuantityInputBase {\n /** What is being counted; names the spinbutton and both of its buttons. */\n label: string;\n min?: number;\n max?: number;\n step?: number;\n /**\n * How many decimals a stored unit is: `3` reads `3500` as 3.5. Given, every\n * number in these props is that stored integer and a typed entry is never\n * clamped or rounded — one outside `min`, `max` or `step` is marked invalid\n * and still reported, and text that is no number is reported as `NaN`.\n * `0` is whole units under those same rules.\n */\n scale?: number;\n /** With `scale`: the unit, as the reader names it. */\n unit?: string;\n /** With `scale`: the caller's own refusal, beside the field's. */\n invalid?: boolean;\n /** With `scale`: what describes the field, usually the refusal's message. */\n 'aria-describedby'?: string;\n disabled?: boolean;\n id?: string;\n /**\n * Posts the count with the form it sits in, under this name — a server\n * action's form works without JavaScript, where the field is the only part\n * a reader can change, so the field itself carries it. The server clamps\n * what it receives: a typed value is only clamped here once committed.\n */\n name?: string;\n className?: string;\n}\n\n/** The caller owns the count: every committed change comes back through `onChange`. */\nexport interface ControlledQuantityInputProps extends QuantityInputBase {\n value: number;\n onChange: (value: number) => void;\n defaultValue?: never;\n}\n\n/**\n * The field owns the count, starting at `defaultValue` (or `min`). This is the\n * shape a server-rendered form takes: a server component cannot hand a client\n * one a function, and with `name` the count posts with the form, so the page\n * needs no client parent of its own. `onChange` still reports each commit.\n */\nexport interface UncontrolledQuantityInputProps extends QuantityInputBase {\n value?: never;\n defaultValue?: number;\n onChange?: (value: number) => void;\n}\n\nexport type QuantityInputProps =\n ControlledQuantityInputProps | UncontrolledQuantityInputProps;\n\n/**\n * A count — or, given a `scale`, a measure: see that prop — on the WAI-ARIA\n * spinbutton pattern: Up and Down step it, Home and End jump to its bounds,\n * and a count typed is committed on Enter or on leaving the field, clamped\n * to the bounds. The − and + beside it are named\n * \"Decrease {label}\" and \"Increase {label}\" and sit at `--target-min`; they are\n * out of the Tab order, as the pattern has them, because the arrow keys\n * already do what they do.\n *\n * Controlled with `value` and `onChange`, or uncontrolled with `defaultValue`\n * — the second is how a Server Action form uses it, since a server component\n * can pass neither a state nor a handler.\n */\nexport function QuantityInput(props: QuantityInputProps) {\n return props.scale === undefined ? (\n <CountInput {...props} />\n ) : (\n <ScaledQuantityInput {...props} scale={props.scale} />\n );\n}\n\n/** The count: what is typed is committed clamped to the bounds. */\nfunction CountInput({\n label,\n value: controlled,\n defaultValue,\n onChange,\n min = 0,\n max,\n step = 1,\n disabled = false,\n id,\n name,\n className,\n}: QuantityInputProps) {\n const t = useT();\n const [own, setOwn] = useState(defaultValue ?? min);\n const value = controlled ?? own;\n const [text, setText] = useState(String(value));\n\n useEffect(() => {\n // eslint-disable-next-line react-hooks/set-state-in-effect -- the value moved from outside (a step, a reset)\n setText(String(value));\n }, [value]);\n\n const clamp = (next: number) =>\n Math.min(max ?? Number.POSITIVE_INFINITY, Math.max(min, next));\n const set = (next: number) => {\n const clamped = clamp(next);\n setText(String(clamped));\n if (clamped === value) return;\n setOwn(clamped);\n onChange?.(clamped);\n };\n const commit = () => {\n const typed = Number.parseInt(text, 10);\n set(Number.isNaN(typed) ? value : typed);\n };\n\n const onKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {\n const next = {\n ArrowUp: value + step,\n ArrowDown: value - step,\n Home: min,\n End: max,\n }[event.key];\n if (event.key === 'Enter') {\n commit();\n return;\n }\n if (next === undefined) return;\n event.preventDefault();\n set(next);\n };\n\n return (\n <span className={cn('inline-flex items-center gap-3xs', className)}>\n <button\n type=\"button\"\n tabIndex={-1}\n aria-label={t('quantity.decrease', { label })}\n disabled={disabled || value <= min}\n onClick={() => set(value - step)}\n className={STEP_BUTTON}\n >\n <span aria-hidden=\"true\">−</span>\n </button>\n <input\n id={id}\n name={name}\n type=\"text\"\n role=\"spinbutton\"\n inputMode=\"numeric\"\n aria-label={label}\n aria-valuenow={value}\n aria-valuemin={min}\n aria-valuemax={max}\n disabled={disabled}\n value={text}\n onChange={event => setText(event.currentTarget.value)}\n onBlur={commit}\n onKeyDown={onKeyDown}\n className=\"w-16 min-h-(--target-min) rounded-lg border border-border-control bg-surface-elevated px-2xs py-3xs text-center text-step-sm text-content tabular-nums focus-ring disabled:cursor-not-allowed disabled:opacity-50\"\n />\n <button\n type=\"button\"\n tabIndex={-1}\n aria-label={t('quantity.increase', { label })}\n disabled={disabled || (max !== undefined && value >= max)}\n onClick={() => set(value + step)}\n className={STEP_BUTTON}\n >\n <span aria-hidden=\"true\">+</span>\n </button>\n </span>\n );\n}\n"],"names":["useEffect","useState","useT","cn","ScaledQuantityInput","STEP_BUTTON","QuantityInput","props","scale","undefined","CountInput","label","value","controlled","defaultValue","onChange","min","max","step","disabled","id","name","className","t","own","setOwn","text","setText","String","clamp","next","Math","Number","POSITIVE_INFINITY","set","clamped","commit","typed","parseInt","isNaN","onKeyDown","event","ArrowUp","ArrowDown","Home","End","key","preventDefault","span","button","type","tabIndex","aria-label","onClick","aria-hidden","input","role","inputMode","aria-valuenow","aria-valuemin","aria-valuemax","currentTarget","onBlur"],"mappings":"AAAA;;;AAEA,SAA6BA,SAAS,EAAEC,QAAQ,QAAQ,QAAQ;AAEhE,SAASC,IAAI,QAAQ,yBAAyB;AAC9C,SAASC,EAAE,QAAQ,oBAAoB;AACvC,SAASC,mBAAmB,QAAQ,6BAA6B;AACjE,SAASC,WAAW,QAAQ,mBAAmB;AAwD/C;;;;;;;;;;;;CAYC,GACD,OAAO,SAASC,cAAcC,KAAyB;IACrD,OAAOA,MAAMC,KAAK,KAAKC,0BACrB,KAACC,yBAAeH,wBAEhB,KAACH,kCAAwBG;QAAOC,OAAOD,MAAMC,KAAK;;AAEtD;AAEA,iEAAiE,GACjE,SAASE,WAAW,EAClBC,KAAK,E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CQ,UAAUA,YAAYP,SAASI;gBAC/BqC,SAAS,IAAMnB,IAAItB,QAAQM;gBAC3BI,WAAWjB;0BAEX,cAAA,KAAC2C;oBAAKM,eAAY;8BAAO;;;0BAE3B,KAACC;gBACCnC,IAAIA;gBACJC,MAAMA;gBACN6B,MAAK;gBACLM,MAAK;gBACLC,WAAU;gBACVL,cAAYzC;gBACZ+C,iBAAe9C;gBACf+C,iBAAe3C;gBACf4C,iBAAe3C;gBACfE,UAAUA;gBACVP,OAAOc;gBACPX,UAAU0B,CAAAA,QAASd,QAAQc,MAAMoB,aAAa,CAACjD,KAAK;gBACpDkD,QAAQ1B;gBACRI,WAAWA;gBACXlB,WAAU;;0BAEZ,KAAC2B;gBACCC,MAAK;gBACLC,UAAU,CAAC;gBACXC,cAAY7B,EAAE,qBAAqB;oBAAEZ;gBAAM;gBAC3CQ,UAAUA,YAAaF,QAAQR,aAAaG,SAASK;gBACrDoC,SAAS,IAAMnB,IAAItB,QAAQM;gBAC3BI,WAAWjB;0BAEX,cAAA,KAAC2C;oBAAKM,eAAY;8BAAO;;;;;AAIjC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { QuantityInputProps } from './quantity-input.js';
|
|
2
|
+
/**
|
|
3
|
+
* `QuantityInput` given a `scale`: every number across its props is the stored
|
|
4
|
+
* integer, and the field shows and takes the decimal.
|
|
5
|
+
*
|
|
6
|
+
* ⚠️ It never alters what was typed — no clamp, no rounding, no snap to the
|
|
7
|
+
* step: selling 3 to someone who typed 3,5 is the defect this mode exists to
|
|
8
|
+
* prevent. An entry outside the rules is marked invalid and still reported,
|
|
9
|
+
* and text that is no number is reported as `NaN`, so a caller never keeps
|
|
10
|
+
* the previous quantity under a field that shows another. Only − and +, which
|
|
11
|
+
* ask for a change, land on the step and inside the bounds.
|
|
12
|
+
*/
|
|
13
|
+
export declare function ScaledQuantityInput({ label, value: controlled, defaultValue, onChange, min, max, step, scale, unit, invalid, disabled, id, name, className, 'aria-describedby': describedBy, }: QuantityInputProps & {
|
|
14
|
+
scale: number;
|
|
15
|
+
}): import("react").JSX.Element;
|
|
16
|
+
//# sourceMappingURL=scaled-quantity-input.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scaled-quantity-input.d.ts","sourceRoot":"","sources":["../../../../src/ui/atoms/number-input/scaled-quantity-input.tsx"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAG9D;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CAAC,EAClC,KAAK,EACL,KAAK,EAAE,UAAU,EACjB,YAAY,EACZ,QAAQ,EACR,GAAO,EACP,GAAG,EACH,IAAI,EACJ,KAAK,EACL,IAAI,EACJ,OAAe,EACf,QAAgB,EAChB,EAAE,EACF,IAAI,EACJ,SAAS,EACT,kBAAkB,EAAE,WAAW,GAChC,EAAE,kBAAkB,GAAG;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,+BA0HxC"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { formatScaled, parseScaled, scaledToDecimal } from '@entifix/core';
|
|
4
|
+
import { useEffect, useId, useState } from 'react';
|
|
5
|
+
import { useLocale, useT } from '../../../i18n/index.js';
|
|
6
|
+
import { cn } from '../../utils/cn.js';
|
|
7
|
+
import { STEP_BUTTON } from './step-button.js';
|
|
8
|
+
/**
|
|
9
|
+
* `QuantityInput` given a `scale`: every number across its props is the stored
|
|
10
|
+
* integer, and the field shows and takes the decimal.
|
|
11
|
+
*
|
|
12
|
+
* ⚠️ It never alters what was typed — no clamp, no rounding, no snap to the
|
|
13
|
+
* step: selling 3 to someone who typed 3,5 is the defect this mode exists to
|
|
14
|
+
* prevent. An entry outside the rules is marked invalid and still reported,
|
|
15
|
+
* and text that is no number is reported as `NaN`, so a caller never keeps
|
|
16
|
+
* the previous quantity under a field that shows another. Only − and +, which
|
|
17
|
+
* ask for a change, land on the step and inside the bounds.
|
|
18
|
+
*/ export function ScaledQuantityInput({ label, value: controlled, defaultValue, onChange, min = 0, max, step, scale, unit, invalid = false, disabled = false, id, name, className, 'aria-describedby': describedBy }) {
|
|
19
|
+
const t = useT();
|
|
20
|
+
const locale = useLocale();
|
|
21
|
+
const unitId = useId();
|
|
22
|
+
const editable = (stored)=>Number.isSafeInteger(stored) ? formatScaled(stored, scale, locale, {
|
|
23
|
+
grouping: false
|
|
24
|
+
}) : '';
|
|
25
|
+
const [own, setOwn] = useState(defaultValue != null ? defaultValue : min);
|
|
26
|
+
const value = controlled != null ? controlled : own;
|
|
27
|
+
const [text, setText] = useState(()=>editable(value));
|
|
28
|
+
const [emitted, setEmitted] = useState(value);
|
|
29
|
+
useEffect(()=>{
|
|
30
|
+
if (Object.is(value, emitted)) return;
|
|
31
|
+
// eslint-disable-next-line react-hooks/set-state-in-effect -- the value moved from outside (a reset, a load)
|
|
32
|
+
setText(editable(value));
|
|
33
|
+
setEmitted(value);
|
|
34
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- it answers an outside value, never its own typing
|
|
35
|
+
}, [
|
|
36
|
+
value
|
|
37
|
+
]);
|
|
38
|
+
const report = (next)=>{
|
|
39
|
+
setEmitted(next);
|
|
40
|
+
setOwn(next);
|
|
41
|
+
if (!Object.is(next, value)) onChange == null ? void 0 : onChange(next);
|
|
42
|
+
};
|
|
43
|
+
const jump = (next)=>{
|
|
44
|
+
const bounded = Math.min(max != null ? max : Number.MAX_SAFE_INTEGER, Math.max(min, next));
|
|
45
|
+
setText(editable(bounded));
|
|
46
|
+
report(bounded);
|
|
47
|
+
};
|
|
48
|
+
const readable = Number.isSafeInteger(value);
|
|
49
|
+
const by = step != null ? step : 10 ** scale;
|
|
50
|
+
const more = ()=>jump(readable ? (Math.floor(value / by) + 1) * by : min);
|
|
51
|
+
const less = ()=>jump(readable ? (Math.ceil(value / by) - 1) * by : min);
|
|
52
|
+
const refused = !readable || value < min || max !== undefined && value > max || step !== undefined && value % step !== 0;
|
|
53
|
+
const onKeyDown = (event)=>{
|
|
54
|
+
const move = {
|
|
55
|
+
ArrowUp: more,
|
|
56
|
+
ArrowDown: less,
|
|
57
|
+
Home: ()=>jump(min),
|
|
58
|
+
End: max === undefined ? undefined : ()=>jump(max)
|
|
59
|
+
}[event.key];
|
|
60
|
+
if (move === undefined) return;
|
|
61
|
+
event.preventDefault();
|
|
62
|
+
move();
|
|
63
|
+
};
|
|
64
|
+
const decimal = (stored)=>Number(scaledToDecimal(stored, scale));
|
|
65
|
+
return /*#__PURE__*/ _jsxs("span", {
|
|
66
|
+
className: cn('inline-flex items-center gap-3xs', className),
|
|
67
|
+
children: [
|
|
68
|
+
/*#__PURE__*/ _jsx("button", {
|
|
69
|
+
type: "button",
|
|
70
|
+
tabIndex: -1,
|
|
71
|
+
"aria-label": t('quantity.decrease', {
|
|
72
|
+
label
|
|
73
|
+
}),
|
|
74
|
+
disabled: disabled || readable && value <= min,
|
|
75
|
+
onClick: less,
|
|
76
|
+
className: STEP_BUTTON,
|
|
77
|
+
children: /*#__PURE__*/ _jsx("span", {
|
|
78
|
+
"aria-hidden": "true",
|
|
79
|
+
children: "−"
|
|
80
|
+
})
|
|
81
|
+
}),
|
|
82
|
+
/*#__PURE__*/ _jsx("input", {
|
|
83
|
+
id: id,
|
|
84
|
+
name: name,
|
|
85
|
+
type: "text",
|
|
86
|
+
role: "spinbutton",
|
|
87
|
+
inputMode: scale === 0 ? 'numeric' : 'decimal',
|
|
88
|
+
"aria-label": label,
|
|
89
|
+
"aria-valuenow": readable ? decimal(value) : undefined,
|
|
90
|
+
"aria-valuetext": readable ? [
|
|
91
|
+
formatScaled(value, scale, locale),
|
|
92
|
+
unit
|
|
93
|
+
].filter(Boolean).join(' ') : text || undefined,
|
|
94
|
+
"aria-valuemin": decimal(min),
|
|
95
|
+
"aria-valuemax": max === undefined ? undefined : decimal(max),
|
|
96
|
+
"aria-invalid": invalid || refused || undefined,
|
|
97
|
+
"aria-describedby": [
|
|
98
|
+
describedBy,
|
|
99
|
+
unit === undefined ? undefined : unitId
|
|
100
|
+
].filter(Boolean).join(' ') || undefined,
|
|
101
|
+
disabled: disabled,
|
|
102
|
+
value: text,
|
|
103
|
+
onChange: (event)=>{
|
|
104
|
+
var _parseScaled;
|
|
105
|
+
const next = event.currentTarget.value;
|
|
106
|
+
setText(next);
|
|
107
|
+
report((_parseScaled = parseScaled(next, scale, locale)) != null ? _parseScaled : Number.NaN);
|
|
108
|
+
},
|
|
109
|
+
onKeyDown: onKeyDown,
|
|
110
|
+
className: "w-24 min-h-(--target-min) rounded-lg border border-border-control bg-surface-elevated px-2xs py-3xs text-center text-step-sm text-content tabular-nums focus-ring disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-danger"
|
|
111
|
+
}),
|
|
112
|
+
unit !== undefined && /*#__PURE__*/ _jsx("span", {
|
|
113
|
+
id: unitId,
|
|
114
|
+
className: "text-step-sm text-content-muted",
|
|
115
|
+
children: unit
|
|
116
|
+
}),
|
|
117
|
+
/*#__PURE__*/ _jsx("button", {
|
|
118
|
+
type: "button",
|
|
119
|
+
tabIndex: -1,
|
|
120
|
+
"aria-label": t('quantity.increase', {
|
|
121
|
+
label
|
|
122
|
+
}),
|
|
123
|
+
disabled: disabled || max !== undefined && value >= max,
|
|
124
|
+
onClick: more,
|
|
125
|
+
className: STEP_BUTTON,
|
|
126
|
+
children: /*#__PURE__*/ _jsx("span", {
|
|
127
|
+
"aria-hidden": "true",
|
|
128
|
+
children: "+"
|
|
129
|
+
})
|
|
130
|
+
})
|
|
131
|
+
]
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
//# sourceMappingURL=scaled-quantity-input.js.map
|