frappe-ui 1.0.2 → 1.2.0

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.
Files changed (194) hide show
  1. package/experimental/Calendar/CalendarEventRow.vue +0 -4
  2. package/experimental/Calendar/calendarUtils.ts +33 -21
  3. package/experimental/CommandPalette/CommandPaletteItem.vue +9 -1
  4. package/experimental/MultiEmailInput/MultiEmailInput.vue +64 -12
  5. package/experimental/MultiEmailInput/types.ts +12 -3
  6. package/experimental/SpriteIcons/IconPicker.vue +2 -1
  7. package/experimental/Stepper/StepIndicator.vue +129 -0
  8. package/experimental/Stepper/Stepper.api.md +117 -0
  9. package/experimental/Stepper/Stepper.md +95 -0
  10. package/experimental/Stepper/Stepper.vue +82 -0
  11. package/experimental/Stepper/StepperHorizontal.vue +93 -0
  12. package/experimental/Stepper/StepperVertical.vue +291 -0
  13. package/experimental/Stepper/index.ts +11 -0
  14. package/experimental/Stepper/resolve.ts +87 -0
  15. package/experimental/Stepper/types.ts +89 -0
  16. package/experimental.ts +2 -21
  17. package/package.json +1 -3
  18. package/src/charts/CalendarHeatmapChart.vue +353 -0
  19. package/src/charts/DonutChart.vue +34 -58
  20. package/src/charts/HeatmapChart.vue +26 -64
  21. package/src/charts/NumberCard.vue +62 -7
  22. package/src/charts/PercentageBarChart.vue +243 -0
  23. package/src/charts/SankeyChart.vue +14 -11
  24. package/src/charts/ScatterChart.vue +24 -20
  25. package/src/charts/axisChartCommon.ts +182 -18
  26. package/src/charts/axisFormat.ts +8 -3
  27. package/src/charts/calendarHeatmapOptions.ts +604 -0
  28. package/src/charts/colorMath.ts +26 -0
  29. package/src/charts/components/ChartContainer.vue +63 -53
  30. package/src/charts/components/ChartLegend.vue +216 -24
  31. package/src/charts/components/ChartScale.vue +85 -0
  32. package/src/charts/core/dataLabelFit.ts +45 -6
  33. package/src/charts/core/useAxisChart.ts +36 -19
  34. package/src/charts/core/useChart.ts +6 -1
  35. package/src/charts/core/usePlotKeyboard.ts +13 -2
  36. package/src/charts/docs/BarChart.md +11 -3
  37. package/src/charts/docs/CalendarHeatmapChart.api.md +165 -0
  38. package/src/charts/docs/CalendarHeatmapChart.md +33 -0
  39. package/src/charts/docs/ChartContainer.api.md +6 -0
  40. package/src/charts/docs/ChartLegend.api.md +1 -1
  41. package/src/charts/docs/ChartLegend.md +25 -3
  42. package/src/charts/docs/DonutChart.api.md +12 -0
  43. package/src/charts/docs/DonutChart.md +17 -0
  44. package/src/charts/docs/HeatmapChart.api.md +1 -1
  45. package/src/charts/docs/LineChart.md +5 -3
  46. package/src/charts/docs/NumberCard.api.md +6 -0
  47. package/src/charts/docs/NumberCard.md +6 -0
  48. package/src/charts/docs/Overview.md +13 -6
  49. package/src/charts/docs/PercentageBarChart.api.md +148 -0
  50. package/src/charts/docs/PercentageBarChart.md +42 -0
  51. package/src/charts/docs/ScatterChart.md +2 -3
  52. package/src/charts/donutChartOptions.ts +22 -130
  53. package/src/charts/format.ts +4 -1
  54. package/src/charts/heatmapOptions.ts +17 -12
  55. package/src/charts/hiddenSeries.ts +8 -3
  56. package/src/charts/index.ts +12 -0
  57. package/src/charts/legendOverflow.ts +40 -0
  58. package/src/charts/numberCardFormat.ts +19 -13
  59. package/src/charts/referenceLines.ts +2 -1
  60. package/src/charts/scatterOptions.ts +16 -3
  61. package/src/charts/seriesData.ts +8 -3
  62. package/src/charts/slices.ts +228 -0
  63. package/src/charts/tokens.ts +99 -0
  64. package/src/charts/types.ts +318 -9
  65. package/src/components/Avatar/Avatar.md +27 -2
  66. package/src/components/Avatar/Avatar.vue +29 -12
  67. package/src/components/Breadcrumbs/Breadcrumbs.vue +2 -1
  68. package/src/components/Combobox/Combobox.vue +9 -2
  69. package/src/components/Combobox/ComboboxResults.vue +6 -1
  70. package/src/components/Combobox/utils.ts +2 -1
  71. package/src/components/DatePicker/DatePicker.md +7 -0
  72. package/src/components/DatePicker/DatePicker.vue +6 -0
  73. package/src/components/DatePicker/DateTimePicker.vue +6 -0
  74. package/src/components/DatePicker/composables.ts +18 -1
  75. package/src/components/DatePicker/utils.ts +53 -0
  76. package/src/components/Dialog/Dialog.md +3 -1
  77. package/src/components/Duration/Duration.api.md +16 -3
  78. package/src/components/Duration/Duration.md +21 -0
  79. package/src/components/Duration/Duration.vue +29 -8
  80. package/src/components/Duration/duration.ts +105 -44
  81. package/src/components/Duration/index.ts +1 -0
  82. package/src/components/Duration/types.ts +30 -1
  83. package/src/components/HoverCard/HoverCard.vue +21 -5
  84. package/src/components/Menu/utils.ts +3 -2
  85. package/src/components/MobileNav/MobileNavItem.vue +3 -22
  86. package/src/components/MultiSelect/MultiSelect.api.md +5 -0
  87. package/src/components/MultiSelect/MultiSelect.vue +34 -0
  88. package/src/components/MultiSelect/MultiSelectResults.vue +3 -4
  89. package/src/components/MultiSelect/types.ts +6 -0
  90. package/src/components/Select/Select.vue +4 -4
  91. package/src/components/Select/utils.ts +1 -0
  92. package/src/components/SettingsDialog/SettingsDialog.api.md +7 -2
  93. package/src/components/SettingsDialog/SettingsDialog.md +2 -1
  94. package/src/components/SettingsDialog/SettingsRow.vue +12 -2
  95. package/src/components/Sidebar/SidebarItem.vue +3 -33
  96. package/src/components/SidebarRail/SidebarRailItem.vue +4 -17
  97. package/src/components/Switch/Switch.vue +3 -3
  98. package/src/components/Tooltip/Tooltip.vue +8 -0
  99. package/src/components/shared/deferredTrigger.ts +174 -0
  100. package/src/components/shared/navLink.ts +45 -0
  101. package/src/components/shared/selection/utils.ts +8 -1
  102. package/src/data-fetching/useCall/useCall.ts +25 -26
  103. package/src/data-fetching/useFrappeFetch.ts +48 -2
  104. package/src/data-fetching/useList/useList.ts +11 -3
  105. package/src/data-fetching/utils.ts +15 -1
  106. package/src/molecules/editor/components/MediaNodeView.vue +10 -1
  107. package/src/molecules/editor/components/MediaResizeGuide.vue +71 -0
  108. package/src/molecules/editor/components/media-node-view-utils.ts +7 -0
  109. package/src/molecules/editor/composables/useNodeViewResize.ts +142 -10
  110. package/src/molecules/editor/extensions/attachment/attachment-engine.ts +1 -1
  111. package/src/molecules/editor/extensions/content-paste/paste-html-utils.ts +16 -6
  112. package/src/molecules/editor/extensions/iframe/IframeNodeView.vue +29 -10
  113. package/src/molecules/editor/extensions/iframe/iframe-allowlist.ts +35 -16
  114. package/src/molecules/editor/extensions/iframe/iframe-embed-utils.ts +1 -1
  115. package/src/molecules/editor/extensions/iframe/iframe-extension.ts +30 -30
  116. package/src/molecules/editor/extensions/link/LinkEditorPopup.vue +1 -1
  117. package/src/molecules/editor/extensions/link/link-paste-plugin.ts +1 -1
  118. package/src/molecules/editor/extensions/shared/media-upload-engine.ts +1 -1
  119. package/src/molecules/editor/useEditor.ts +12 -6
  120. package/src/molecules/list/ListRow.vue +8 -1
  121. package/src/utils/dialog.ts +57 -16
  122. package/src/{molecules/editor/extensions/shared → utils}/url-safety.ts +28 -16
  123. package/tailwind/content.js +1 -1
  124. package/tailwind/plugin.js +22 -0
  125. package/vite/README.md +5 -3
  126. package/vite/codeLanguages.js +41 -17
  127. package/vite/doctypeInterfaceGenerator.js +52 -39
  128. package/vitepress/index.node.ts +0 -2
  129. package/experimental/TextEditor/TextEditor.api.md +0 -161
  130. package/experimental/TextEditor/TextEditor.vue +0 -298
  131. package/experimental/TextEditor/classIcon.ts +0 -21
  132. package/experimental/TextEditor/commands.js +0 -344
  133. package/experimental/TextEditor/components/CodeBlockComponent.vue +0 -103
  134. package/experimental/TextEditor/components/FontColor.vue +0 -153
  135. package/experimental/TextEditor/components/ImageViewerModal.vue +0 -463
  136. package/experimental/TextEditor/components/InsertImage.vue +0 -54
  137. package/experimental/TextEditor/components/InsertLink.vue +0 -13
  138. package/experimental/TextEditor/components/InsertVideo.vue +0 -14
  139. package/experimental/TextEditor/components/LinkPopup.vue +0 -99
  140. package/experimental/TextEditor/components/MediaNodeView.vue +0 -398
  141. package/experimental/TextEditor/components/MentionList.vue +0 -95
  142. package/experimental/TextEditor/components/TextEditorBubbleMenu.vue +0 -78
  143. package/experimental/TextEditor/components/TextEditorFixedMenu.vue +0 -78
  144. package/experimental/TextEditor/components/TextEditorFloatingMenu.vue +0 -61
  145. package/experimental/TextEditor/components/TextEditorMenu.vue +0 -218
  146. package/experimental/TextEditor/components/index.js +0 -0
  147. package/experimental/TextEditor/extensions/code-block.ts +0 -119
  148. package/experimental/TextEditor/extensions/color/color-extension.ts +0 -140
  149. package/experimental/TextEditor/extensions/color/color-styles.css +0 -25
  150. package/experimental/TextEditor/extensions/color/index.ts +0 -2
  151. package/experimental/TextEditor/extensions/content-paste-extension.ts +0 -160
  152. package/experimental/TextEditor/extensions/copy-styles.ts +0 -160
  153. package/experimental/TextEditor/extensions/emoji/EmojiList.vue +0 -55
  154. package/experimental/TextEditor/extensions/emoji/emoji-extension.ts +0 -55
  155. package/experimental/TextEditor/extensions/emoji/emojis.json +0 -7422
  156. package/experimental/TextEditor/extensions/heading/heading.ts +0 -20
  157. package/experimental/TextEditor/extensions/highlight/highlight-extension.ts +0 -241
  158. package/experimental/TextEditor/extensions/highlight/highlight-styles.css +0 -31
  159. package/experimental/TextEditor/extensions/highlight/index.ts +0 -2
  160. package/experimental/TextEditor/extensions/iframe/IframeNodeView.vue +0 -308
  161. package/experimental/TextEditor/extensions/iframe/InsertIframe.vue +0 -209
  162. package/experimental/TextEditor/extensions/iframe/iframe-extension.ts +0 -404
  163. package/experimental/TextEditor/extensions/iframe/index.ts +0 -11
  164. package/experimental/TextEditor/extensions/iframe/utils.ts +0 -283
  165. package/experimental/TextEditor/extensions/image/image-extension.ts +0 -661
  166. package/experimental/TextEditor/extensions/image/index.ts +0 -5
  167. package/experimental/TextEditor/extensions/image-group/ImageGroupNodeView.vue +0 -182
  168. package/experimental/TextEditor/extensions/image-group/ImageGroupUploadDialog.vue +0 -740
  169. package/experimental/TextEditor/extensions/image-group/image-group-extension.ts +0 -187
  170. package/experimental/TextEditor/extensions/image-viewer-extension.ts +0 -162
  171. package/experimental/TextEditor/extensions/link/index.ts +0 -1
  172. package/experimental/TextEditor/extensions/link/link-extension.ts +0 -323
  173. package/experimental/TextEditor/extensions/link/linkPasteHandler.ts +0 -51
  174. package/experimental/TextEditor/extensions/mention/index.ts +0 -1
  175. package/experimental/TextEditor/extensions/mention/mention-extension.ts +0 -204
  176. package/experimental/TextEditor/extensions/mention/style.css +0 -11
  177. package/experimental/TextEditor/extensions/shared/color-utils.ts +0 -236
  178. package/experimental/TextEditor/extensions/slash-commands/SlashCommandsList.vue +0 -59
  179. package/experimental/TextEditor/extensions/slash-commands/slash-commands-extension.ts +0 -184
  180. package/experimental/TextEditor/extensions/suggestion/SuggestionList.vue +0 -130
  181. package/experimental/TextEditor/extensions/suggestion/createSuggestionExtension.ts +0 -151
  182. package/experimental/TextEditor/extensions/suggestion/index.ts +0 -5
  183. package/experimental/TextEditor/extensions/tag/index.ts +0 -1
  184. package/experimental/TextEditor/extensions/tag/tag-extension.ts +0 -170
  185. package/experimental/TextEditor/extensions/toc-node/TocNodeView.vue +0 -439
  186. package/experimental/TextEditor/extensions/toc-node/index.ts +0 -3
  187. package/experimental/TextEditor/extensions/toc-node/toc-node-extension.ts +0 -167
  188. package/experimental/TextEditor/extensions/video-extension.ts +0 -653
  189. package/experimental/TextEditor/hljs-github.css +0 -197
  190. package/experimental/TextEditor/index.ts +0 -21
  191. package/experimental/TextEditor/style.css +0 -190
  192. package/experimental/TextEditor/types.ts +0 -72
  193. package/experimental/TextEditor/utils.js +0 -11
  194. package/src/utils/url-validation.ts +0 -39
@@ -57,6 +57,13 @@ People can type a date into the input by default. Set `:typeable="false"` to
57
57
  allow picking from the calendar only. On `DateTimePicker`, `typeable` covers
58
58
  both the date and the time inputs.
59
59
 
60
+ With a `format`, typed numbers read in that format's day-month-year order.
61
+ Under `DD-MM-YYYY`, `3-11-2026`, `3/11/26` and `03.11.2026` all mean
62
+ 3 November 2026. A `DateTimePicker` also takes the date without its seconds or
63
+ without a time, and a 12-hour time in either AM/PM case. ISO text (`2026-11-03`) and text with a month name
64
+ (`Nov 3, 2026`) read as they do with no `format`. A focus and blur with no edit
65
+ leaves the value as it is.
66
+
60
67
  ### Clearing
61
68
 
62
69
  `clearable` is `true` by default, and an empty input clears the value. On
@@ -254,6 +254,12 @@ function commitInput(close = false): void {
254
254
  }
255
255
  return
256
256
  }
257
+ // An unedited blur keeps the value. Reading the shown text back would drop
258
+ // what the format leaves out, such as the seconds.
259
+ if (props.modelValue && raw === displayLabel.value) {
260
+ if (close && !shouldKeepOpen.value) isOpen.value = false
261
+ return
262
+ }
257
263
  const d = coerceToDayjs(raw)
258
264
  if (d && !checkUnavailable(d)) {
259
265
  selectDate(d)
@@ -320,6 +320,12 @@ function commitInput(close = false): void {
320
320
  if (close && !shouldKeepOpen.value) isOpen.value = false
321
321
  return
322
322
  }
323
+ // An unedited blur keeps the value. Reading the shown text back would drop
324
+ // what the format leaves out, such as the seconds.
325
+ if (props.modelValue && raw === displayLabel.value) {
326
+ if (close && !shouldKeepOpen.value) isOpen.value = false
327
+ return
328
+ }
323
329
  const parsed = coerceToDayjs(raw)
324
330
  if (parsed && !checkUnavailable(parsed)) {
325
331
  selectDate(parsed)
@@ -1,7 +1,14 @@
1
1
  import { ref, computed, watch } from 'vue'
2
2
  import type { Dayjs } from 'dayjs/esm'
3
3
  import { dayjs } from '../../utils/dayjs'
4
- import { generateWeeks, monthStart, getDateValue } from './utils'
4
+ import {
5
+ generateWeeks,
6
+ monthStart,
7
+ getDateValue,
8
+ ISO_DATE,
9
+ typedFormats,
10
+ withFormatSeparator,
11
+ } from './utils'
5
12
  import type { CalendarPanelExposed } from './calendarTypes'
6
13
  import type {
7
14
  CommonDatePickerProps,
@@ -94,6 +101,9 @@ export function useKeepOpen(props: CommonDatePickerProps) {
94
101
  }
95
102
 
96
103
  // Coerce arbitrary string input to a Dayjs, respecting an optional explicit format.
104
+ // With a format, text of numbers alone reads in that format's day-month-year
105
+ // order: `3-11-2026` under `DD-MM-YYYY` is 3 November, not 11 March. ISO text
106
+ // (the value itself) and text with a month name keep the loose parse.
97
107
  export function useDateCoercion(getFormat: () => string | undefined) {
98
108
  return function coerceToDayjs(val?: string | null): Dayjs | null {
99
109
  if (!val) return null
@@ -103,6 +113,13 @@ export function useDateCoercion(getFormat: () => string | undefined) {
103
113
  if (format) {
104
114
  const dStrict = dayjs(raw, format, true)
105
115
  if (dStrict.isValid()) return dStrict
116
+ if (!ISO_DATE.test(raw)) {
117
+ const typed = withFormatSeparator(raw, format)
118
+ const dTyped = dayjs(typed, typedFormats(format), true)
119
+ if (dTyped.isValid()) return dTyped
120
+ // AM/PM is not a month name: the loose parse would read it month-first.
121
+ if (!/[a-z]/i.test(raw.replace(/[ap]\.?m\.?/gi, ''))) return null
122
+ }
106
123
  }
107
124
  const dLoose = dayjs(raw)
108
125
  if (dLoose.isValid()) return dLoose
@@ -53,6 +53,59 @@ export function generateWeeks(
53
53
  return chunked
54
54
  }
55
55
 
56
+ /** Text that starts like an ISO date (`2026-11-03`, `2026/11/03`), which a loose parse reads right. */
57
+ export const ISO_DATE = /^\d{4}[-/]\d{1,2}[-/]\d{1,2}/
58
+
59
+ // A format token, or a `[literal]` that must stay as written.
60
+ const TOKEN_OR_LITERAL = /\[[^\]]*\]|[aA]/g
61
+
62
+ // Each turns a format into one more a person may type under it.
63
+ const TYPED_VARIANTS: ((format: string) => string)[] = [
64
+ (f) => f.replace(/(?<!D)DD(?!D)/, 'D'),
65
+ (f) => f.replace(/(?<!M)MM(?!M)/, 'M'),
66
+ (f) => f.replace(/(?<!Y)YYYY(?!Y)/, 'YY'),
67
+ (f) => f.replace(/(?<![Hh])(HH|hh)(?![Hh])/, (t) => t[0]),
68
+ (f) => f.replace(/:ss/, ''),
69
+ (f) => (/s/.test(f) ? f : f.replace(/mm/, 'mm:ss')),
70
+ (f) => f.replace(/\s+[Hh].*$/, ''),
71
+ // `PM` typed under `a`, or `pm` under `A`.
72
+ (f) =>
73
+ f.replace(TOKEN_OR_LITERAL, (t) => (t === 'a' ? 'A' : t === 'A' ? 'a' : t)),
74
+ ]
75
+
76
+ const typedFormatCache = new Map<string, string[]>()
77
+
78
+ /**
79
+ * The formats a person may type a date in under a display `format`, in its
80
+ * day-month-year order: a day, month or hour with no leading zero, a two-digit
81
+ * year, seconds added or left out, no time, or the other AM/PM case.
82
+ * `DD-MM-YYYY HH:mm:ss` takes `3-11-26 9:15` and `03-11-2026`. Pass the text
83
+ * through `withFormatSeparator` first, so `3/11/26` is read too.
84
+ */
85
+ export function typedFormats(format: string): string[] {
86
+ const cached = typedFormatCache.get(format)
87
+ if (cached) return cached
88
+ let formats = [format]
89
+ for (const variant of TYPED_VARIANTS) {
90
+ formats = [...new Set(formats.flatMap((f) => [f, variant(f)]))]
91
+ }
92
+ typedFormatCache.set(format, formats)
93
+ return formats
94
+ }
95
+
96
+ /**
97
+ * `text` with `-`, `/` or `.` between its date numbers changed to the one
98
+ * `format` uses. Only the date, before the first space, changes: a time such
99
+ * as `9.15` keeps its own separator.
100
+ */
101
+ export function withFormatSeparator(text: string, format: string): string {
102
+ const separator = /(?<=[DMY])[-/.](?=[DMY])/.exec(format)?.[0]
103
+ if (!separator) return text
104
+ return text.replace(/^\S+/, (date) =>
105
+ date.replace(/(?<=\d)[-/.](?=\d)/g, separator),
106
+ )
107
+ }
108
+
56
109
  export function getDateValue(date: Date | string) {
57
110
  if (!date || date.toString() === 'Invalid Date') return ''
58
111
 
@@ -88,7 +88,9 @@ right away. They need `<Dialogs />` somewhere in the app, which
88
88
  through `FormControl`, so any `FormControl` `type` works. A field's
89
89
  `validate` function runs after the `required` check and returns an error
90
90
  message, or `null` when the value is fine. Its second argument holds every
91
- field's value, so one field can check another.
91
+ field's value, so one field can check another. The first text or textarea
92
+ field gets focus when the dialog opens, and Enter in a single-line field
93
+ submits.
92
94
 
93
95
  ### Space above the dialog
94
96
 
@@ -15,10 +15,9 @@
15
15
  },
16
16
  {
17
17
  name: 'placeholder',
18
- description: 'Placeholder shown when the input is empty. Defaults to `1h 30m 45s`.',
18
+ description: 'Placeholder shown when the input is empty. Defaults to `1h 30m` when\n`smallestUnit` is `minute`, and `1h 30m 45s` otherwise.',
19
19
  required: false,
20
- type: 'string',
21
- default: '"1h 30m 45s"'
20
+ type: 'string'
22
21
  },
23
22
  {
24
23
  name: 'format',
@@ -27,6 +26,20 @@
27
26
  type: 'DurationFormat',
28
27
  default: '"short"'
29
28
  },
29
+ {
30
+ name: 'largestUnit',
31
+ description: 'The biggest unit of the `short` and `long` presets and of the edit text.\n`day` shows 93784 as `1d 2h 3m 4s`. Defaults to `hour` (`26h 3m 4s`).',
32
+ required: false,
33
+ type: 'DurationLargestUnit',
34
+ default: '"hour"'
35
+ },
36
+ {
37
+ name: 'smallestUnit',
38
+ description: 'The smallest unit of the `short` and `long` presets and of the edit text.\n`minute` rounds to the nearest minute, and a typed value is saved rounded.\nIn a minute field, a bare number is minutes and `a:b` is hours and\nminutes: `15` is 15 minutes and `1:30` is 1h 30m. Defaults to `second`.',
39
+ required: false,
40
+ type: 'DurationSmallestUnit',
41
+ default: '"second"'
42
+ },
30
43
  {
31
44
  name: 'size',
32
45
  description: 'Visual size of the input. Forwarded to the underlying `TextInput`.',
@@ -32,6 +32,7 @@ in any order.
32
32
  | ------------ | -------------------------------------- | ------------------ |
33
33
  | Short units | `1h 30m 45s`, `1h30m45s`, `45s 1h 30m` | `5445` |
34
34
  | Long units | `1 hour 30 minutes`, `2hrs 15min` | `5400`, `8100` |
35
+ | Days | `1d 2h`, `2 days` | `93600`, `172800` |
35
36
  | Colon | `1:30:45`, `1:30`, `:45` | `5445`, `90`, `45` |
36
37
  | Bare integer | `90` | `90` |
37
38
 
@@ -69,6 +70,26 @@ as units.
69
70
  While the input has focus, it always shows the short notation (`2h 2m 3s`), so
70
71
  the text can be edited and read back the same way whatever the format.
71
72
 
73
+ ### Units
74
+
75
+ `largestUnit` and `smallestUnit` set which units the `short` and `long` presets
76
+ show. The edit text uses the same units. `colon` and templates name their own
77
+ units, so these props do not change them.
78
+
79
+ | Props | `93784` |
80
+ | ------------------------ | ------------- |
81
+ | _(default)_ | `26h 3m 4s` |
82
+ | `largest-unit="day"` | `1d 2h 3m 4s` |
83
+ | `smallest-unit="minute"` | `26h 3m` |
84
+
85
+ With `smallest-unit="minute"`, the value shows rounded to the nearest minute, so
86
+ `3599` shows `1h`. A typed value is saved rounded. A saved value that is not
87
+ edited keeps its seconds.
88
+
89
+ A minute field also reads a bare number as minutes and `a:b` as hours and
90
+ minutes, so `15` saves `900` and `1:30` saves `5400`. `1:30:45` and text with
91
+ units, such as `45s`, read the same as in a seconds field.
92
+
72
93
  ### Label, description and error
73
94
 
74
95
  `label` renders above the field and `description` below it. `error` renders
@@ -10,7 +10,7 @@
10
10
  :disabled="disabled"
11
11
  :size="size"
12
12
  :variant="variant"
13
- :placeholder="placeholder"
13
+ :placeholder="placeholderText"
14
14
  @focus="handleFocus"
15
15
  @blur="handleBlur"
16
16
  @input="handleInput"
@@ -30,12 +30,13 @@
30
30
  <script setup lang="ts">
31
31
  import { computed, nextTick, ref } from 'vue'
32
32
  import { TextInput } from '../TextInput'
33
- import { formatDuration, parseDuration } from './duration'
33
+ import { formatDuration, parseDuration, roundDuration } from './duration'
34
34
  import type { DurationProps, DurationExposed } from './types'
35
35
 
36
36
  const props = withDefaults(defineProps<DurationProps>(), {
37
- placeholder: '1h 30m 45s',
38
37
  format: 'short',
38
+ largestUnit: 'hour',
39
+ smallestUnit: 'second',
39
40
  })
40
41
 
41
42
  const model = defineModel<number | null>({ default: null })
@@ -50,13 +51,25 @@ defineSlots<{
50
51
  const inputRef = ref<InstanceType<typeof TextInput> | null>(null)
51
52
  const isFocused = ref(false)
52
53
  const editValue = ref('')
54
+ const focusValue = ref('')
53
55
  const internalError = ref('')
54
56
  const cancelled = ref(false)
55
57
 
58
+ const units = computed(() => ({
59
+ largestUnit: props.largestUnit,
60
+ smallestUnit: props.smallestUnit,
61
+ }))
62
+
63
+ const placeholderText = computed(
64
+ () =>
65
+ props.placeholder ??
66
+ (props.smallestUnit === 'minute' ? '1h 30m' : '1h 30m 45s'),
67
+ )
68
+
56
69
  const displayValue = computed(() =>
57
70
  isFocused.value || internalError.value
58
71
  ? editValue.value
59
- : formatDuration(model.value, props.format),
72
+ : formatDuration(model.value, props.format, units.value),
60
73
  )
61
74
 
62
75
  function handleFocus() {
@@ -67,7 +80,8 @@ function handleFocus() {
67
80
  isFocused.value = true
68
81
  // Always edit in the canonical "1h 30m 45s" notation so the typed value
69
82
  // round-trips through parseDuration regardless of the display `format`.
70
- editValue.value = formatDuration(model.value, 'short')
83
+ editValue.value = formatDuration(model.value, 'short', units.value)
84
+ focusValue.value = editValue.value
71
85
  internalError.value = ''
72
86
  nextTick(() => inputRef.value?.inputElement?.select())
73
87
  }
@@ -102,6 +116,13 @@ function handleKeydown(event: KeyboardEvent) {
102
116
  function commit() {
103
117
  const raw = editValue.value.trim()
104
118
 
119
+ // The edit text may be rounded, so committing it unchanged would rewrite the value.
120
+ if (raw === focusValue.value) {
121
+ internalError.value = ''
122
+ isFocused.value = false
123
+ return
124
+ }
125
+
105
126
  if (raw === '') {
106
127
  internalError.value = ''
107
128
  isFocused.value = false
@@ -109,10 +130,10 @@ function commit() {
109
130
  return
110
131
  }
111
132
 
112
- const seconds = parseDuration(raw)
133
+ const seconds = parseDuration(raw, units.value)
113
134
  if (seconds === null) {
114
135
  internalError.value =
115
- 'Invalid format. Try: 1h 30m 45s, 1 hour 30 minutes, 1:30:45, 90s'
136
+ 'Invalid format. Try: 1h 30m 45s, 1 hour 30 minutes, 1d 2h, 1:30:45, 90s'
116
137
  // Report the error and let focus leave. The rejected text stays visible
117
138
  // (displayValue tracks the error state) as a reminder, but they aren't
118
139
  // trapped in the field. Re-focusing restarts from the canonical saved
@@ -123,7 +144,7 @@ function commit() {
123
144
 
124
145
  internalError.value = ''
125
146
  isFocused.value = false
126
- model.value = seconds
147
+ model.value = roundDuration(seconds, props.smallestUnit)
127
148
  }
128
149
 
129
150
  defineExpose<DurationExposed>({
@@ -2,7 +2,12 @@
2
2
  // Duration helpers: convert between a seconds value and human-readable text.
3
3
  // ---------------------------------------------------------------------------
4
4
 
5
- import type { DurationFormat } from './types'
5
+ import type {
6
+ DurationFormat,
7
+ DurationLargestUnit,
8
+ DurationSmallestUnit,
9
+ DurationUnits,
10
+ } from './types'
6
11
 
7
12
  /**
8
13
  * Format a duration in seconds as text.
@@ -17,24 +22,41 @@ import type { DurationFormat } from './types'
17
22
  * formatDuration(5445, "h'h' m'm' s's'") // "1h 30m 45s" (template, all parts)
18
23
  * formatDuration(90, "h'h' m'm' s's'") // "0h 1m 30s"
19
24
  * formatDuration(7323, 'hh:mm:ss') // "02:02:03"
25
+ *
26
+ * `units` sets the unit range of the `short` and `long` presets:
27
+ *
28
+ * formatDuration(93784, 'short', { largestUnit: 'day' }) // "1d 2h 3m 4s"
29
+ * formatDuration(93784, 'short', { smallestUnit: 'minute' }) // "26h 3m"
20
30
  */
21
31
  export function formatDuration(
22
32
  totalSeconds: number | null | undefined,
23
33
  format: DurationFormat = 'short',
34
+ units: DurationUnits = {},
24
35
  ): string {
25
36
  if (totalSeconds === null || totalSeconds === undefined) return ''
26
37
 
27
38
  const seconds = Number(totalSeconds)
28
39
  if (Number.isNaN(seconds)) return ''
29
40
 
30
- const hours = Math.floor(seconds / 3600)
31
- const minutes = Math.floor((seconds % 3600) / 60)
32
- const secs = seconds % 60
41
+ const { largestUnit = 'hour', smallestUnit = 'second' } = units
42
+ const isPreset = format === 'short' || format === 'long'
43
+ // `colon` and templates have no day token and show the seconds unrounded.
44
+ const parts = isPreset
45
+ ? splitDuration(seconds, largestUnit, smallestUnit)
46
+ : splitDuration(seconds, 'hour', 'second')
47
+
48
+ if (format === 'short') return formatShort(parts, smallestUnit)
49
+ if (format === 'long') return formatLong(parts, smallestUnit)
50
+ if (format === 'colon') return formatColon(parts)
51
+ return renderTemplate(format, parts)
52
+ }
33
53
 
34
- if (format === 'short') return formatShort(hours, minutes, secs)
35
- if (format === 'long') return formatLong(hours, minutes, secs)
36
- if (format === 'colon') return formatColon(hours, minutes, secs)
37
- return renderTemplate(format, hours, minutes, secs)
54
+ /** Round seconds to the smallest unit, as the presets show them. */
55
+ export function roundDuration(
56
+ seconds: number,
57
+ smallestUnit: DurationSmallestUnit,
58
+ ): number {
59
+ return smallestUnit === 'minute' ? Math.round(seconds / 60) * 60 : seconds
38
60
  }
39
61
 
40
62
  // ---------------------------------------------------------------------------
@@ -48,12 +70,7 @@ export function formatDuration(
48
70
  // "h'h' m'm' s's'" → "1h 30m 45s"
49
71
  // "hh:mm:ss" → "01:30:45"
50
72
  // ---------------------------------------------------------------------------
51
- function renderTemplate(
52
- template: string,
53
- hours: number,
54
- minutes: number,
55
- secs: number,
56
- ): string {
73
+ function renderTemplate(template: string, parts: DurationParts): string {
57
74
  const pad = (value: number) => String(value).padStart(2, '0')
58
75
  let out = ''
59
76
  let i = 0
@@ -79,7 +96,8 @@ function renderTemplate(
79
96
  if (ch === 'h' || ch === 'm' || ch === 's') {
80
97
  let len = 1
81
98
  while (template[i + len] === ch) len++
82
- const value = ch === 'h' ? hours : ch === 'm' ? minutes : secs
99
+ const unit = ch === 'h' ? 'hour' : ch === 'm' ? 'minute' : 'second'
100
+ const value = parts[unit]
83
101
  out += len >= 2 ? pad(value) : String(value)
84
102
  i += len
85
103
  continue
@@ -92,53 +110,94 @@ function renderTemplate(
92
110
  return out
93
111
  }
94
112
 
95
- function formatShort(hours: number, minutes: number, secs: number): string {
96
- const parts: string[] = []
97
- if (hours) parts.push(`${hours}h`)
98
- if (minutes) parts.push(`${minutes}m`)
99
- if (secs) parts.push(`${secs}s`)
100
- return parts.length ? parts.join(' ') : '0s'
113
+ type DurationPartUnit = 'day' | 'hour' | 'minute' | 'second'
114
+ type DurationParts = Record<DurationPartUnit, number>
115
+
116
+ const PART_UNITS: DurationPartUnit[] = ['day', 'hour', 'minute', 'second']
117
+ const SHORT_LABELS: Record<DurationPartUnit, string> = {
118
+ day: 'd',
119
+ hour: 'h',
120
+ minute: 'm',
121
+ second: 's',
122
+ }
123
+
124
+ function splitDuration(
125
+ totalSeconds: number,
126
+ largestUnit: DurationLargestUnit,
127
+ smallestUnit: DurationSmallestUnit,
128
+ ): DurationParts {
129
+ const seconds = roundDuration(totalSeconds, smallestUnit)
130
+ const day = largestUnit === 'day' ? Math.floor(seconds / 86400) : 0
131
+ return {
132
+ day,
133
+ hour: Math.floor((seconds - day * 86400) / 3600),
134
+ minute: Math.floor((seconds % 3600) / 60),
135
+ second: seconds % 60,
136
+ }
101
137
  }
102
138
 
103
- function formatLong(hours: number, minutes: number, secs: number): string {
104
- const parts: string[] = []
105
- if (hours) parts.push(hours === 1 ? '1 hour' : `${hours} hours`)
106
- if (minutes) parts.push(minutes === 1 ? '1 minute' : `${minutes} minutes`)
107
- if (secs) parts.push(secs === 1 ? '1 second' : `${secs} seconds`)
108
- return parts.length ? parts.join(' ') : '0 seconds'
139
+ function formatShort(
140
+ parts: DurationParts,
141
+ smallestUnit: DurationPartUnit,
142
+ ): string {
143
+ const text = PART_UNITS.filter((unit) => parts[unit]).map(
144
+ (unit) => `${parts[unit]}${SHORT_LABELS[unit]}`,
145
+ )
146
+ return text.length ? text.join(' ') : `0${SHORT_LABELS[smallestUnit]}`
109
147
  }
110
148
 
111
- function formatColon(hours: number, minutes: number, secs: number): string {
149
+ function formatLong(
150
+ parts: DurationParts,
151
+ smallestUnit: DurationPartUnit,
152
+ ): string {
153
+ const text = PART_UNITS.filter((unit) => parts[unit]).map((unit) =>
154
+ parts[unit] === 1 ? `1 ${unit}` : `${parts[unit]} ${unit}s`,
155
+ )
156
+ return text.length ? text.join(' ') : `0 ${smallestUnit}s`
157
+ }
158
+
159
+ function formatColon({ hour, minute, second }: DurationParts): string {
112
160
  const pad = (value: number) => String(value).padStart(2, '0')
113
- if (hours) return `${hours}:${pad(minutes)}:${pad(secs)}`
114
- return `${minutes}:${pad(secs)}`
161
+ if (hour) return `${hour}:${pad(minute)}:${pad(second)}`
162
+ return `${minute}:${pad(second)}`
115
163
  }
116
164
 
117
165
  // ---------------------------------------------------------------------------
118
166
  // Parse: human-readable string → seconds (integer) or null if invalid.
119
167
  //
120
168
  // Supported formats (case-insensitive, spaces optional, any unit order):
121
- // 1h 30m 45s | 4m 3h 4s | 4sec 3hour 4min | 1h30m45s
169
+ // 1h 30m 45s | 4m 3h 4s | 4sec 3hour 4min | 1h30m45s | 1d 2h | 2 days
122
170
  // 1:30:45 | 1:30 | :45
123
- // 90 | 90s (bare integer treated as seconds)
171
+ // 90 | 90s
172
+ //
173
+ // A bare integer and the last colon group are read in the smallest unit:
174
+ // `90` is 90 seconds and `1:30` is 1m 30s by default, but with
175
+ // `smallestUnit: 'minute'` `90` is 90 minutes and `1:30` is 1h 30m.
176
+ // `1:30:45` is always h:m:s, and named units (`90s`) always keep their unit.
124
177
  // ---------------------------------------------------------------------------
125
- export function parseDuration(str: string): number | null {
178
+ export function parseDuration(
179
+ str: string,
180
+ units: DurationUnits = {},
181
+ ): number | null {
126
182
  const input = str.trim().toLowerCase()
127
183
  if (!input) return null
128
184
 
129
- const colonSeconds = parseColon(input)
185
+ const unitSeconds = units.smallestUnit === 'minute' ? 60 : 1
186
+
187
+ const colonSeconds = parseColon(input, unitSeconds)
130
188
  if (colonSeconds !== null) return colonSeconds
131
189
 
132
- // Bare integer: treated as seconds.
133
- if (/^\d+$/.test(input)) return parseInt(input, 10)
190
+ if (/^\d+$/.test(input)) return parseInt(input, 10) * unitSeconds
134
191
 
135
192
  return parseUnits(input)
136
193
  }
137
194
 
138
- // Colon-separated: h:m:s or m:s or :s. Groups are not bounded to 0-59:
139
- // out-of-range parts overflow by design, so "1:75" parses to 135s (1m + 75s)
140
- // and "5:99" to 399s. This mirrors stopwatch-style entry and is harmless.
141
- function parseColon(input: string): number | null {
195
+ // Colon-separated: h:m:s, or two groups ending in the smallest unit (m:s or
196
+ // h:m), or one such group (:s or :m). Groups are not bounded to 0-59:
197
+ // out-of-range parts overflow by design. In seconds mode "1:75" parses to 135s
198
+ // (1m + 75s) and "5:99" to 399s; in minute mode "1:75" is 8100s (1h + 75m).
199
+ // This mirrors stopwatch-style entry and is harmless.
200
+ function parseColon(input: string, unitSeconds: number): number | null {
142
201
  const match = input.match(/^(\d+):(\d+):(\d+)$|^(\d+):(\d+)$|^:(\d+)$/)
143
202
  if (!match) return null
144
203
 
@@ -150,15 +209,16 @@ function parseColon(input: string): number | null {
150
209
  )
151
210
  }
152
211
  if (match[4] !== undefined) {
153
- return parseInt(match[4], 10) * 60 + parseInt(match[5], 10)
212
+ return (parseInt(match[4], 10) * 60 + parseInt(match[5], 10)) * unitSeconds
154
213
  }
155
- return parseInt(match[6], 10)
214
+ return parseInt(match[6], 10) * unitSeconds
156
215
  }
157
216
 
158
217
  // Named units in any order: scan (number)(unit) tokens left-to-right and
159
218
  // verify no unexpected text exists between tokens. Rejects duplicate units.
160
219
  function parseUnits(input: string): number | null {
161
- const tokenRe = /(\d+)\s*(hours?|hrs?|h|minutes?|mins?|m|seconds?|secs?|s)/g
220
+ const tokenRe =
221
+ /(\d+)\s*(days?|d|hours?|hrs?|h|minutes?|mins?|m|seconds?|secs?|s)/g
162
222
  const tokens: { value: number; unit: string }[] = []
163
223
  let lastEnd = 0
164
224
  let match: RegExpExecArray | null
@@ -177,7 +237,8 @@ function parseUnits(input: string): number | null {
177
237
  for (const { value, unit } of tokens) {
178
238
  if (seen.has(unit)) return null
179
239
  seen.add(unit)
180
- if (unit === 'h') total += value * 3600
240
+ if (unit === 'd') total += value * 86400
241
+ else if (unit === 'h') total += value * 3600
181
242
  else if (unit === 'm') total += value * 60
182
243
  else total += value
183
244
  }
@@ -5,4 +5,5 @@ export type {
5
5
  DurationEmits,
6
6
  DurationExposed,
7
7
  DurationFormat,
8
+ DurationUnits,
8
9
  } from './types'
@@ -21,11 +21,26 @@ export type DurationFormatPreset = 'short' | 'long' | 'colon'
21
21
  // `string & {}` keeps preset autocomplete while still accepting any template.
22
22
  export type DurationFormat = DurationFormatPreset | (string & {})
23
23
 
24
+ /** The biggest unit the `short` and `long` presets use. `hour` folds days into hours. */
25
+ export type DurationLargestUnit = 'day' | 'hour'
26
+
27
+ /** The smallest unit the `short` and `long` presets use. `minute` rounds the seconds. */
28
+ export type DurationSmallestUnit = 'minute' | 'second'
29
+
30
+ /** The unit range of the `short` and `long` presets and of the edit text. */
31
+ export interface DurationUnits {
32
+ largestUnit?: DurationLargestUnit
33
+ smallestUnit?: DurationSmallestUnit
34
+ }
35
+
24
36
  export interface DurationProps extends InputLabelingProps {
25
37
  /** The duration value in seconds (two-way via `v-model`). */
26
38
  modelValue?: number | null
27
39
 
28
- /** Placeholder shown when the input is empty. Defaults to `1h 30m 45s`. */
40
+ /**
41
+ * Placeholder shown when the input is empty. Defaults to `1h 30m` when
42
+ * `smallestUnit` is `minute`, and `1h 30m 45s` otherwise.
43
+ */
29
44
  placeholder?: string
30
45
 
31
46
  /**
@@ -35,6 +50,20 @@ export interface DurationProps extends InputLabelingProps {
35
50
  */
36
51
  format?: DurationFormat
37
52
 
53
+ /**
54
+ * The biggest unit of the `short` and `long` presets and of the edit text.
55
+ * `day` shows 93784 as `1d 2h 3m 4s`. Defaults to `hour` (`26h 3m 4s`).
56
+ */
57
+ largestUnit?: DurationLargestUnit
58
+
59
+ /**
60
+ * The smallest unit of the `short` and `long` presets and of the edit text.
61
+ * `minute` rounds to the nearest minute, and a typed value is saved rounded.
62
+ * In a minute field, a bare number is minutes and `a:b` is hours and
63
+ * minutes: `15` is 15 minutes and `1:30` is 1h 30m. Defaults to `second`.
64
+ */
65
+ smallestUnit?: DurationSmallestUnit
66
+
38
67
  /** Visual size of the input. Forwarded to the underlying `TextInput`. */
39
68
  size?: InputSize
40
69