velvetkit 0.3.0 → 0.4.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +80 -0
- data/README.md +5 -1
- data/lib/generators/velvetkit/add/add_generator.rb +58 -0
- data/lib/velvetkit/version.rb +4 -1
- data/lib/velvetkit.rb +7 -0
- data/registry/app/assets/tailwind/velvetkit.css +90 -155
- data/registry/app/components/ui/chart.html.erb +47 -0
- data/registry/app/components/ui/chart.rb +177 -0
- data/registry/app/components/ui/command/group.rb +27 -0
- data/registry/app/components/ui/command/item.rb +64 -0
- data/registry/app/components/ui/command.html.erb +41 -0
- data/registry/app/components/ui/command.rb +122 -0
- data/registry/app/components/ui/date_picker.html.erb +58 -64
- data/registry/app/components/ui/date_picker.rb +199 -75
- data/registry/app/components/ui/file_field.html.erb +14 -4
- data/registry/app/components/ui/file_field.rb +179 -28
- data/registry/app/components/ui/gallery/image.rb +20 -0
- data/registry/app/components/ui/gallery.html.erb +52 -0
- data/registry/app/components/ui/gallery.rb +164 -0
- data/registry/app/components/ui/kbd.html.erb +27 -0
- data/registry/app/components/ui/kbd.rb +134 -0
- data/registry/app/components/ui/number_input.html.erb +11 -0
- data/registry/app/components/ui/number_input.rb +103 -0
- data/registry/app/components/ui/progress/segment.rb +25 -0
- data/registry/app/components/ui/progress.html.erb +25 -0
- data/registry/app/components/ui/progress.rb +182 -0
- data/registry/app/components/ui/select.html.erb +24 -5
- data/registry/app/components/ui/select.rb +44 -5
- data/registry/app/components/ui/timeline/item.html.erb +21 -0
- data/registry/app/components/ui/timeline/item.rb +145 -0
- data/registry/app/components/ui/timeline.html.erb +1 -0
- data/registry/app/components/ui/timeline.rb +42 -0
- data/registry/app/javascript/chart_js.ts +37 -0
- data/registry/app/javascript/controllers/ui/_internal/date_format.ts +74 -0
- data/registry/app/javascript/controllers/ui/_internal/shortcut.ts +80 -0
- data/registry/app/javascript/controllers/ui/chart_controller.ts +585 -0
- data/registry/app/javascript/controllers/ui/command_controller.ts +454 -0
- data/registry/app/javascript/controllers/ui/date_picker_controller.ts +247 -82
- data/registry/app/javascript/controllers/ui/file_field_controller.ts +288 -16
- data/registry/app/javascript/controllers/ui/gallery_controller.ts +568 -0
- data/registry/app/javascript/controllers/ui/kbd_controller.ts +60 -0
- data/registry/app/javascript/controllers/ui/number_input_controller.ts +111 -0
- data/registry/app/javascript/controllers/ui/progress_controller.ts +87 -0
- data/registry/app/javascript/controllers/ui/select_controller.ts +198 -8
- data/registry/app/javascript/vanilla_calendar.ts +15 -0
- data/registry/components.yml +19 -3
- data/registry/config/locales/velvetkit.en.yml +73 -0
- data/registry/icons/lucide/download.svg +17 -0
- data/registry/icons/lucide/file-plus.svg +18 -0
- data/registry/icons/lucide/file.svg +16 -0
- data/registry/icons/lucide/folder.svg +15 -0
- data/registry/icons/lucide/keyboard.svg +23 -0
- data/registry/icons/lucide/minus.svg +15 -0
- data/registry/icons/lucide/package.svg +18 -0
- data/registry/icons/lucide/pause.svg +16 -0
- data/registry/icons/lucide/percent.svg +17 -0
- data/registry/icons/lucide/printer.svg +17 -0
- data/registry/icons/lucide/receipt.svg +17 -0
- data/registry/icons/lucide/sun-moon.svg +19 -0
- data/registry/icons/lucide/truck.svg +19 -0
- data/registry/icons/lucide/user-plus.svg +18 -0
- data/registry/icons/lucide/zoom-in.svg +18 -0
- data/registry/icons/lucide/zoom-out.svg +17 -0
- data/registry/icons/phosphor/circle-half.svg +2 -0
- data/registry/icons/phosphor/download-simple.svg +2 -0
- data/registry/icons/phosphor/file-plus.svg +2 -0
- data/registry/icons/phosphor/file.svg +2 -0
- data/registry/icons/phosphor/folder.svg +2 -0
- data/registry/icons/phosphor/keyboard.svg +2 -0
- data/registry/icons/phosphor/magnifying-glass-minus.svg +2 -0
- data/registry/icons/phosphor/magnifying-glass-plus.svg +2 -0
- data/registry/icons/phosphor/minus.svg +2 -0
- data/registry/icons/phosphor/package.svg +2 -0
- data/registry/icons/phosphor/pause.svg +2 -0
- data/registry/icons/phosphor/percent.svg +2 -0
- data/registry/icons/phosphor/printer.svg +2 -0
- data/registry/icons/phosphor/receipt.svg +2 -0
- data/registry/icons/phosphor/truck.svg +2 -0
- data/registry/icons/phosphor/user-plus.svg +2 -0
- data/registry/icons/tabler/download.svg +18 -0
- data/registry/icons/tabler/file-plus.svg +19 -0
- data/registry/icons/tabler/file.svg +17 -0
- data/registry/icons/tabler/folder.svg +16 -0
- data/registry/icons/tabler/keyboard.svg +23 -0
- data/registry/icons/tabler/minus.svg +16 -0
- data/registry/icons/tabler/package.svg +20 -0
- data/registry/icons/tabler/percentage.svg +18 -0
- data/registry/icons/tabler/player-pause.svg +17 -0
- data/registry/icons/tabler/printer.svg +18 -0
- data/registry/icons/tabler/receipt.svg +16 -0
- data/registry/icons/tabler/sun-moon.svg +21 -0
- data/registry/icons/tabler/truck.svg +18 -0
- data/registry/icons/tabler/user-plus.svg +19 -0
- data/registry/icons/tabler/zoom-in.svg +19 -0
- data/registry/icons/tabler/zoom-out.svg +18 -0
- data/registry/icons.yml +16 -0
- data/registry/test/components/previews/ui/chart_preview/cash_flow.html.erb +6 -0
- data/registry/test/components/previews/ui/chart_preview/donut.html.erb +6 -0
- data/registry/test/components/previews/ui/chart_preview/margin.html.erb +6 -0
- data/registry/test/components/previews/ui/chart_preview/pie.html.erb +6 -0
- data/registry/test/components/previews/ui/chart_preview/sales.html.erb +6 -0
- data/registry/test/components/previews/ui/chart_preview/stock.html.erb +6 -0
- data/registry/test/components/previews/ui/chart_preview/takings.html.erb +8 -0
- data/registry/test/components/previews/ui/chart_preview.rb +46 -0
- data/registry/test/components/previews/ui/command_preview/_picked.html.erb +14 -0
- data/registry/test/components/previews/ui/command_preview/dialog.html.erb +24 -0
- data/registry/test/components/previews/ui/command_preview/inline.html.erb +16 -0
- data/registry/test/components/previews/ui/command_preview/playground.html.erb +15 -0
- data/registry/test/components/previews/ui/command_preview/server_search.html.erb +24 -0
- data/registry/test/components/previews/ui/command_preview/shortcuts.html.erb +12 -0
- data/registry/test/components/previews/ui/command_preview/till.html.erb +17 -0
- data/registry/test/components/previews/ui/command_preview.rb +25 -0
- data/registry/test/components/previews/ui/date_picker_preview/custom_presets.html.erb +6 -0
- data/registry/test/components/previews/ui/date_picker_preview/in_form.html.erb +11 -0
- data/registry/test/components/previews/ui/date_picker_preview/range.html.erb +5 -0
- data/registry/test/components/previews/ui/date_picker_preview/states.html.erb +4 -3
- data/registry/test/components/previews/ui/date_picker_preview.rb +26 -6
- data/registry/test/components/previews/ui/file_field_preview/multiple_files.html.erb +1 -1
- data/registry/test/components/previews/ui/file_field_preview/product_photos.html.erb +12 -0
- data/registry/test/components/previews/ui/file_field_preview/single_image.html.erb +4 -0
- data/registry/test/components/previews/ui/file_field_preview.rb +32 -5
- data/registry/test/components/previews/ui/gallery_preview/hover_zoom.html.erb +13 -0
- data/registry/test/components/previews/ui/gallery_preview/landscape.html.erb +5 -0
- data/registry/test/components/previews/ui/gallery_preview/product_page.html.erb +13 -0
- data/registry/test/components/previews/ui/gallery_preview/single.html.erb +3 -0
- data/registry/test/components/previews/ui/gallery_preview.rb +45 -0
- data/registry/test/components/previews/ui/kbd_preview/bound.html.erb +20 -0
- data/registry/test/components/previews/ui/kbd_preview/keys.html.erb +13 -0
- data/registry/test/components/previews/ui/kbd_preview/menu.html.erb +13 -0
- data/registry/test/components/previews/ui/kbd_preview/search_field.html.erb +8 -0
- data/registry/test/components/previews/ui/kbd_preview/shortcuts_list.html.erb +17 -0
- data/registry/test/components/previews/ui/kbd_preview/till.html.erb +23 -0
- data/registry/test/components/previews/ui/kbd_preview/tooltip_and_buttons.html.erb +14 -0
- data/registry/test/components/previews/ui/kbd_preview.rb +32 -0
- data/registry/test/components/previews/ui/number_input_preview/cart.html.erb +11 -0
- data/registry/test/components/previews/ui/number_input_preview/decimals.html.erb +4 -0
- data/registry/test/components/previews/ui/number_input_preview/sizes.html.erb +5 -0
- data/registry/test/components/previews/ui/number_input_preview/states.html.erb +7 -0
- data/registry/test/components/previews/ui/number_input_preview/stock_adjustment.html.erb +3 -0
- data/registry/test/components/previews/ui/number_input_preview.rb +36 -0
- data/registry/test/components/previews/ui/progress_preview/file_upload.html.erb +36 -0
- data/registry/test/components/previews/ui/progress_preview/free_shipping.html.erb +4 -0
- data/registry/test/components/previews/ui/progress_preview/indeterminate.html.erb +4 -0
- data/registry/test/components/previews/ui/progress_preview/sizes_and_tones.html.erb +8 -0
- data/registry/test/components/previews/ui/progress_preview/steps.html.erb +9 -0
- data/registry/test/components/previews/ui/progress_preview/storage.html.erb +8 -0
- data/registry/test/components/previews/ui/progress_preview/storage_by_category.html.erb +7 -0
- data/registry/test/components/previews/ui/progress_preview/tasks_by_status.html.erb +14 -0
- data/registry/test/components/previews/ui/progress_preview/traffic_by_source.html.erb +15 -0
- data/registry/test/components/previews/ui/progress_preview.rb +46 -0
- data/registry/test/components/previews/ui/select_preview/server_search.html.erb +22 -0
- data/registry/test/components/previews/ui/select_preview.rb +5 -0
- data/registry/test/components/previews/ui/timeline_preview/activity_log.html.erb +7 -0
- data/registry/test/components/previews/ui/timeline_preview/colored.html.erb +7 -0
- data/registry/test/components/previews/ui/timeline_preview/colored_by_meaning.html.erb +7 -0
- data/registry/test/components/previews/ui/timeline_preview/failed_delivery.html.erb +6 -0
- data/registry/test/components/previews/ui/timeline_preview/order_status.html.erb +9 -0
- data/registry/test/components/previews/ui/timeline_preview/real_dates.html.erb +5 -0
- data/registry/test/components/previews/ui/timeline_preview/stock_history.html.erb +6 -0
- data/registry/test/components/previews/ui/timeline_preview.rb +40 -0
- data/registry/test/components/ui/chart_test.rb +187 -0
- data/registry/test/components/ui/command/group_test.rb +19 -0
- data/registry/test/components/ui/command/item_test.rb +47 -0
- data/registry/test/components/ui/command_test.rb +110 -0
- data/registry/test/components/ui/date_picker_test.rb +129 -111
- data/registry/test/components/ui/file_field_test.rb +105 -9
- data/registry/test/components/ui/gallery_test.rb +192 -0
- data/registry/test/components/ui/kbd_test.rb +150 -0
- data/registry/test/components/ui/number_input_test.rb +155 -0
- data/registry/test/components/ui/progress_test.rb +188 -0
- data/registry/test/components/ui/select_test.rb +62 -0
- data/registry/test/components/ui/timeline_test.rb +175 -0
- metadata +147 -2
- data/registry/app/javascript/cally.ts +0 -8
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
|
|
5
|
+
# A bar showing how far along something is: an upload, an import, storage
|
|
6
|
+
# used, a profile half filled in. Leave out `value:` and it shows that work is
|
|
7
|
+
# going on without saying how much is left.
|
|
8
|
+
#
|
|
9
|
+
# <%= render(UI::Progress.new(value: 64, label: "Uploading report.pdf", show_value: true)) %>
|
|
10
|
+
# <%= render(UI::Progress.new(value: 3, max: 12, label: "Importing", value_text: "%{value} of %{max} files", show_value: true)) %>
|
|
11
|
+
# <%= render(UI::Progress.new(label: "Preparing your export")) %>
|
|
12
|
+
#
|
|
13
|
+
# Split by category, it adds up its segments and shows a legend:
|
|
14
|
+
#
|
|
15
|
+
# <%= render(UI::Progress.new(max: 15, label: "Storage", value_text: "%{value} GB of %{max} GB",
|
|
16
|
+
# segment_text: "%{value} GB", show_value: true)) do |progress| %>
|
|
17
|
+
# <% progress.with_segment(value: 6.4, label: "Photos") %>
|
|
18
|
+
# <% progress.with_segment(value: 3.1, label: "Mail") %>
|
|
19
|
+
# <% end %>
|
|
20
|
+
#
|
|
21
|
+
# With `max:` the segments fill as far as their total and the rest of the track
|
|
22
|
+
# is what's left: free space, tasks to do. Leave `max:` out and they fill the
|
|
23
|
+
# bar as shares of a whole, like traffic by source:
|
|
24
|
+
#
|
|
25
|
+
# <%= render(UI::Progress.new(label: "Visits by source")) do |progress| %>
|
|
26
|
+
# <% progress.with_segment(value: 5_210, label: "Search") %>
|
|
27
|
+
# <% progress.with_segment(value: 2_870, label: "Direct") %>
|
|
28
|
+
# <% end %>
|
|
29
|
+
#
|
|
30
|
+
# Segments take the chart colours in order. The legend names them, since a
|
|
31
|
+
# colour alone doesn't say which is which; `legend: false` leaves it out when
|
|
32
|
+
# the page already says. `segment_text:` is each one's amount in the legend,
|
|
33
|
+
# with %{value} and %{percent} (its share of max). It defaults to the value
|
|
34
|
+
# against a max, and to the percentage for shares of a whole.
|
|
35
|
+
#
|
|
36
|
+
# `value_text:` is what's shown (with `show_value: true`) and what a screen
|
|
37
|
+
# reader hears. It can use %{value}, %{max} and %{percent}, and defaults to
|
|
38
|
+
# "40%". `tone:` colours the bar (:success, :warning, :destructive) for a bar
|
|
39
|
+
# that means something, like storage nearly full.
|
|
40
|
+
#
|
|
41
|
+
# Updating it: re-render it from a Turbo Stream (with `method: :morph` the bar
|
|
42
|
+
# slides to its new width), or from JavaScript set its
|
|
43
|
+
# `data-ui--progress-value-value` attribute, as an upload's progress event
|
|
44
|
+
# would. It fires `ui--progress:complete` when it reaches the end.
|
|
45
|
+
class UI::Progress < UI::Base
|
|
46
|
+
SIZES = { sm: "h-1", default: "h-2", lg: "h-3" }.freeze
|
|
47
|
+
|
|
48
|
+
TONES = {
|
|
49
|
+
default: { track: "bg-primary/15", bar: "bg-primary" },
|
|
50
|
+
success: { track: "bg-emerald-500/15", bar: "bg-emerald-500 dark:bg-emerald-600" },
|
|
51
|
+
warning: { track: "bg-amber-500/15", bar: "bg-amber-500 dark:bg-amber-600" },
|
|
52
|
+
destructive: { track: "bg-destructive/15", bar: "bg-destructive" }
|
|
53
|
+
}.freeze
|
|
54
|
+
|
|
55
|
+
# Slides a short bar across, again and again. With reduced motion it's a
|
|
56
|
+
# full, faint bar instead: still there, not moving.
|
|
57
|
+
INDETERMINATE = "absolute inset-y-0 w-2/5 animate-[velvetkit-progress_1.4s_ease-in-out_infinite] " \
|
|
58
|
+
"motion-reduce:w-full motion-reduce:animate-none motion-reduce:opacity-50"
|
|
59
|
+
DETERMINATE = "h-full transition-[width] duration-300 ease-out motion-reduce:transition-none"
|
|
60
|
+
|
|
61
|
+
SWATCHES = { 1 => "bg-chart-1", 2 => "bg-chart-2", 3 => "bg-chart-3", 4 => "bg-chart-4", 5 => "bg-chart-5" }.freeze
|
|
62
|
+
|
|
63
|
+
renders_many :segments, UI::Progress::Segment
|
|
64
|
+
|
|
65
|
+
def initialize(value: nil, max: nil, label: nil, value_text: nil, show_value: false, size: :default,
|
|
66
|
+
tone: :default, legend: true, segment_text: nil, class: nil, **html_attrs)
|
|
67
|
+
@given_max = max if max.to_f.positive?
|
|
68
|
+
@value = value&.clamp(0, @given_max || 100)
|
|
69
|
+
@legend = legend
|
|
70
|
+
@segment_text = segment_text
|
|
71
|
+
@label = label
|
|
72
|
+
@value_text = value_text
|
|
73
|
+
@show_value = show_value
|
|
74
|
+
@size = SIZES.key?(size.to_sym) ? size.to_sym : :default
|
|
75
|
+
@tone = TONES.key?(tone.to_sym) ? tone.to_sym : :default
|
|
76
|
+
@user_class = binding.local_variable_get(:class)
|
|
77
|
+
@aria = html_attrs.delete(:aria) || {}
|
|
78
|
+
@html_attrs = html_attrs
|
|
79
|
+
@label_id = "ui-progress-#{SecureRandom.hex(4)}"
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
attr_reader :label, :label_id
|
|
83
|
+
|
|
84
|
+
# 100 unless given; with segments and no max:, their total, so they fill the
|
|
85
|
+
# bar as shares of a whole.
|
|
86
|
+
def max
|
|
87
|
+
return @given_max if @given_max
|
|
88
|
+
|
|
89
|
+
total = segments.sum(&:value)
|
|
90
|
+
whole? ? total : 100
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Segments with no max: shares of a whole, not parts of a limit.
|
|
94
|
+
def whole? = @given_max.nil? && segmented? && segments.sum(&:value).positive?
|
|
95
|
+
|
|
96
|
+
# With segments, their total; the bar's own value: is ignored.
|
|
97
|
+
def value
|
|
98
|
+
return @value unless segmented?
|
|
99
|
+
|
|
100
|
+
segments.sum(&:value).clamp(0, max)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def segmented? = segments.any?
|
|
104
|
+
|
|
105
|
+
def indeterminate? = value.nil?
|
|
106
|
+
|
|
107
|
+
# Rounded down, so it never says 100% before it's done.
|
|
108
|
+
def percent
|
|
109
|
+
return nil if indeterminate?
|
|
110
|
+
|
|
111
|
+
(value.to_f / max * 100).floor
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def text_format = @value_text || t("ui.progress.value_text", default: "%{percent}%")
|
|
115
|
+
|
|
116
|
+
def value_text
|
|
117
|
+
return nil if indeterminate?
|
|
118
|
+
|
|
119
|
+
text_format.gsub("%{value}", number(value)).gsub("%{max}", number(max)).gsub("%{percent}", percent.to_s)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def header? = @label.present? || (@show_value && !indeterminate?)
|
|
123
|
+
|
|
124
|
+
def root_attrs
|
|
125
|
+
{ **@html_attrs, class: cn("w-full", (header? || legend?) && "space-y-1.5", @user_class),
|
|
126
|
+
data: { slot: "progress", controller: "ui--progress", "ui--progress-max-value": number(max),
|
|
127
|
+
"ui--progress-format-value": text_format,
|
|
128
|
+
"ui--progress-determinate-class": DETERMINATE, "ui--progress-indeterminate-class": INDETERMINATE,
|
|
129
|
+
**(@html_attrs[:data] || {}),
|
|
130
|
+
**(indeterminate? ? {} : { "ui--progress-value-value": number(value) }) } }
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def bar_attrs
|
|
134
|
+
named = @label.present? ? { labelledby: label_id } : { label: t("ui.progress.label", default: "Progress") }
|
|
135
|
+
{ role: "progressbar", class: cn("relative w-full overflow-hidden rounded-full", SIZES[@size], TONES[@tone][:track]),
|
|
136
|
+
data: { state: state, "ui--progress-target": "bar" },
|
|
137
|
+
aria: { valuemin: 0, valuemax: number(max), valuenow: (number(value) unless indeterminate?),
|
|
138
|
+
valuetext: value_text, busy: (true if state != "complete"), **named, **@aria }.compact }
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def indicator_attrs
|
|
142
|
+
colour = segmented? ? "flex gap-0.5 overflow-hidden" : TONES[@tone][:bar]
|
|
143
|
+
{ class: cn("rounded-full", colour, indeterminate? ? INDETERMINATE : DETERMINATE),
|
|
144
|
+
style: ("width: #{percent}%" unless indeterminate?),
|
|
145
|
+
data: { slot: "progress-indicator", "ui--progress-target": "indicator" } }.compact
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def show_value? = @show_value && !indeterminate?
|
|
149
|
+
|
|
150
|
+
def legend? = segmented? && @legend
|
|
151
|
+
|
|
152
|
+
# Each segment's share of the filled part, so together they fill it; a
|
|
153
|
+
# small one keeps a sliver, so it doesn't vanish.
|
|
154
|
+
def segment_attrs(segment, index)
|
|
155
|
+
{ class: cn("h-full min-w-1", swatch(segment, index), segment.user_class),
|
|
156
|
+
style: "flex: #{number(segment.value)} 1 0%", title: "#{segment.label}: #{segment_text(segment)}",
|
|
157
|
+
data: { slot: "progress-segment" } }
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def swatch(segment, index) = SWATCHES[segment.color] || SWATCHES[(index % SWATCHES.size) + 1]
|
|
161
|
+
|
|
162
|
+
def segment_text(segment)
|
|
163
|
+
segment.value_text ||
|
|
164
|
+
(@segment_text || (whole? ? "%{percent}%" : "%{value}"))
|
|
165
|
+
.gsub("%{value}", number(segment.value))
|
|
166
|
+
.gsub("%{percent}", share(segment).to_s)
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
private
|
|
170
|
+
|
|
171
|
+
def state
|
|
172
|
+
return "indeterminate" if indeterminate?
|
|
173
|
+
|
|
174
|
+
value >= max ? "complete" : "loading"
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# Rounded, not down: shares of a whole should add up to about 100.
|
|
178
|
+
def share(segment) = (segment.value / max.to_f * 100).round
|
|
179
|
+
|
|
180
|
+
# 3, not 3.0; 2.5 stays 2.5.
|
|
181
|
+
def number(n) = (n.to_f % 1).zero? ? n.to_i.to_s : n.to_s
|
|
182
|
+
end
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<%= tag.div(class: "relative w-full", data: root_data) do %>
|
|
1
|
+
<%= tag.div(class: "relative w-full", data: remote? ? root_data.merge(status_texts) : root_data) do %>
|
|
2
2
|
<% if render_label? %>
|
|
3
3
|
<div class="mb-2"><%= render(label_component) { label_text } %></div>
|
|
4
4
|
<% end %>
|
|
@@ -13,7 +13,13 @@
|
|
|
13
13
|
"data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95") do %>
|
|
14
14
|
<% if searchable? %>
|
|
15
15
|
<div class="flex items-center gap-2 border-b px-3 py-2">
|
|
16
|
-
|
|
16
|
+
<% if remote? %>
|
|
17
|
+
<%# The magnifying glass turns into a spinner while a search is slow. %>
|
|
18
|
+
<span class="flex shrink-0 text-muted-foreground" data-ui--select-target="searchIcon"><%= render(UI::Icon.new(name: "search", size: :sm)) %></span>
|
|
19
|
+
<span class="hidden shrink-0 text-muted-foreground" data-ui--select-target="spinner" aria-hidden="true"><%= render(UI::Icon.new(name: "loader", size: :sm, class: "animate-spin motion-reduce:animate-[spin_1.5s_linear_infinite]")) %></span>
|
|
20
|
+
<% else %>
|
|
21
|
+
<%= render(UI::Icon.new(name: "search", size: :sm, class: "shrink-0 text-muted-foreground")) %>
|
|
22
|
+
<% end %>
|
|
17
23
|
<%= tag.input(type: "search", id: search_id, autocomplete: "off",
|
|
18
24
|
placeholder: t("ui.select.search", default: "Search"),
|
|
19
25
|
class: "w-full bg-transparent text-sm outline-none placeholder:text-muted-foreground [&::-webkit-search-cancel-button]:hidden",
|
|
@@ -32,9 +38,7 @@
|
|
|
32
38
|
<%= tag.div(id: option_id(index), role: "option", aria: { selected: selected?(value).to_s },
|
|
33
39
|
data: { value: value, "ui--select-target": "option",
|
|
34
40
|
action: "click->ui--select#choose mousemove->ui--select#hover" },
|
|
35
|
-
class:
|
|
36
|
-
"outline-none data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground aria-selected:font-medium " \
|
|
37
|
-
"#{value.to_s.empty? ? "text-muted-foreground" : ""}") do %>
|
|
41
|
+
class: option_classes(value)) do %>
|
|
38
42
|
<span class="truncate"><%= label %></span>
|
|
39
43
|
<span class="absolute right-2 flex size-4 items-center justify-center text-primary dark:text-foreground" data-ui--select-target="check" <%= "hidden" unless selected?(value) %>>
|
|
40
44
|
<%= render(UI::Icon.new(name: "check", size: :sm)) %>
|
|
@@ -46,6 +50,21 @@
|
|
|
46
50
|
<% if searchable? %>
|
|
47
51
|
<p hidden data-ui--select-target="empty" class="px-3 py-3 text-sm text-muted-foreground"><%= t("ui.select.no_matches", default: "No matches") %></p>
|
|
48
52
|
<% end %>
|
|
53
|
+
<% if remote? %>
|
|
54
|
+
<%# Searching and failures are announced; the options themselves are read as they're highlighted. %>
|
|
55
|
+
<p hidden role="status" data-ui--select-target="status" class="flex items-center gap-2 px-3 py-3 text-sm text-muted-foreground"></p>
|
|
56
|
+
<template data-ui--select-target="template">
|
|
57
|
+
<%= tag.div(role: "option", aria: { selected: "false" },
|
|
58
|
+
data: { value: "", "ui--select-target": "option",
|
|
59
|
+
action: "click->ui--select#choose mousemove->ui--select#hover" },
|
|
60
|
+
class: option_classes("x")) do %>
|
|
61
|
+
<span class="truncate"></span>
|
|
62
|
+
<span class="absolute right-2 flex size-4 items-center justify-center text-primary dark:text-foreground" data-ui--select-target="check" hidden>
|
|
63
|
+
<%= render(UI::Icon.new(name: "check", size: :sm)) %>
|
|
64
|
+
</span>
|
|
65
|
+
<% end %>
|
|
66
|
+
</template>
|
|
67
|
+
<% end %>
|
|
49
68
|
<% end %>
|
|
50
69
|
|
|
51
70
|
<% if wrapper.hint.present? || wrapper.error_message.present? %>
|
|
@@ -17,6 +17,25 @@ require "securerandom"
|
|
|
17
17
|
# `searchable: true` adds a search box that filters the list; `multiple: true`
|
|
18
18
|
# lets people pick several, posting `name[]` like a native multiple select.
|
|
19
19
|
#
|
|
20
|
+
# `url:` searches on the server instead, for lists too long to send to the
|
|
21
|
+
# page, such as products, customers or suppliers. Opening the list asks
|
|
22
|
+
# `GET url?q=` for a starting list, and typing asks again with what was typed,
|
|
23
|
+
# a moment after the typing stops. The server answers with JSON, either
|
|
24
|
+
# `[{ "value": 7, "label": "Acme Traders" }, ...]` or `[["Acme Traders", 7], ...]`;
|
|
25
|
+
# it decides what matches and how many come back. `param:` renames `q`.
|
|
26
|
+
# `options:` are what show before then, so pass the current choice, or an edit
|
|
27
|
+
# form opens with nothing named:
|
|
28
|
+
#
|
|
29
|
+
# <%= render(UI::Select.new(form: f, field: :customer_id, url: customers_search_path,
|
|
30
|
+
# options: [[@order.customer&.name, @order.customer_id]].reject { _1.first.nil? },
|
|
31
|
+
# prompt: "Find a customer")) %>
|
|
32
|
+
#
|
|
33
|
+
# # app/controllers/customers_controller.rb
|
|
34
|
+
# def search
|
|
35
|
+
# customers = Customer.where("name ILIKE ?", "%#{Customer.sanitize_sql_like(params[:q].to_s)}%").order(:name).limit(20)
|
|
36
|
+
# render json: customers.map { { value: _1.id, label: _1.name } }
|
|
37
|
+
# end
|
|
38
|
+
#
|
|
20
39
|
# UI::NativeSelect is the browser's own control, for when a phone's system
|
|
21
40
|
# picker is the better fit.
|
|
22
41
|
#
|
|
@@ -42,7 +61,7 @@ class UI::Select < UI::Base
|
|
|
42
61
|
)
|
|
43
62
|
|
|
44
63
|
def initialize(name: nil, options: [], selected: nil, prompt: nil,
|
|
45
|
-
label: nil, hint: nil, size: :default, searchable: false, multiple: false,
|
|
64
|
+
label: nil, hint: nil, size: :default, searchable: false, multiple: false, url: nil, param: "q",
|
|
46
65
|
required: false, error: nil, disabled: false,
|
|
47
66
|
form: nil, field: nil,
|
|
48
67
|
class: nil, **html_attrs)
|
|
@@ -52,7 +71,9 @@ class UI::Select < UI::Base
|
|
|
52
71
|
@prompt = prompt
|
|
53
72
|
@label = label
|
|
54
73
|
@size = size
|
|
55
|
-
@
|
|
74
|
+
@url = url
|
|
75
|
+
@param = param
|
|
76
|
+
@searchable = searchable || url.present?
|
|
56
77
|
@multiple = multiple
|
|
57
78
|
@required = required
|
|
58
79
|
@error = error
|
|
@@ -73,6 +94,7 @@ class UI::Select < UI::Base
|
|
|
73
94
|
delegate :render_label?, :label_text, :label_component, to: :wrapper
|
|
74
95
|
|
|
75
96
|
def searchable? = @searchable
|
|
97
|
+
def remote? = @url.present?
|
|
76
98
|
def multiple? = @multiple
|
|
77
99
|
|
|
78
100
|
def trigger_id = "ui-select-#{@uid}"
|
|
@@ -138,9 +160,26 @@ class UI::Select < UI::Base
|
|
|
138
160
|
def summary = t("ui.select.selected", default: "%{count} selected", count: "%{count}")
|
|
139
161
|
|
|
140
162
|
def root_data
|
|
141
|
-
{ controller: "ui--select", "ui--select-multiple-value": multiple?.to_s,
|
|
142
|
-
|
|
143
|
-
|
|
163
|
+
data = { controller: "ui--select", "ui--select-multiple-value": multiple?.to_s,
|
|
164
|
+
"ui--select-summary-value": summary,
|
|
165
|
+
"ui--select-placeholder-value": prompt || t("ui.select.placeholder", default: "Choose…") }
|
|
166
|
+
return data unless remote?
|
|
167
|
+
|
|
168
|
+
data.merge("ui--select-url-value": @url, "ui--select-param-value": @param)
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# What a fetched option is built from: one rendered here, so it has the same
|
|
172
|
+
# classes as the options the page arrived with.
|
|
173
|
+
def option_classes(value)
|
|
174
|
+
"relative flex cursor-default select-none items-center rounded-sm py-1.5 pr-8 pl-2 #{size == :sm ? "text-xs" : "text-sm"} " \
|
|
175
|
+
"outline-none data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground aria-selected:font-medium " \
|
|
176
|
+
"#{value.to_s.empty? ? "text-muted-foreground" : ""}"
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
# Said while a search is on its way, and when one fails.
|
|
180
|
+
def status_texts
|
|
181
|
+
{ "ui--select-searching-value": t("ui.select.searching", default: "Searching…"),
|
|
182
|
+
"ui--select-failed-value": t("ui.select.failed", default: "Couldn't load the results. Try again.") }
|
|
144
183
|
end
|
|
145
184
|
|
|
146
185
|
# The hidden native select. Everything the caller passed beyond our own
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
<%= tag.li(**item_attrs) do %>
|
|
2
|
+
<%= tag.span class: marker_classes, aria: { hidden: true } do %>
|
|
3
|
+
<% if !dot? && marker_icon %><%= render(UI::Icon.new(name: marker_icon, size: :sm)) %><% end %>
|
|
4
|
+
<% if !dot? && !marker_icon && status == :current %><%= tag.span class: "size-2 rounded-full bg-current" %><% end %>
|
|
5
|
+
<% end %>
|
|
6
|
+
<% if connector? %><%= tag.span class: connector_classes, aria: { hidden: true } %><% end %>
|
|
7
|
+
<%= tag.div class: text_classes do %>
|
|
8
|
+
<%= tag.div class: "flex flex-wrap items-baseline justify-between gap-x-3 gap-y-0.5" do %>
|
|
9
|
+
<%= tag.span class: title_classes do %><%= title %><%= tag.span ", #{status_text}", class: "sr-only" %><% end %>
|
|
10
|
+
<% if time? %>
|
|
11
|
+
<% if date? %>
|
|
12
|
+
<%= tag.time time_text, datetime: @time.iso8601, class: "text-xs text-muted-foreground" %>
|
|
13
|
+
<% else %>
|
|
14
|
+
<%= tag.span time_text, class: "text-xs text-muted-foreground" %>
|
|
15
|
+
<% end %>
|
|
16
|
+
<% end %>
|
|
17
|
+
<% end %>
|
|
18
|
+
<% if description? %><%= tag.p description, class: "text-sm text-muted-foreground" %><% end %>
|
|
19
|
+
<% if content? %><%= tag.div content, class: "mt-1.5 text-sm text-foreground" %><% end %>
|
|
20
|
+
<% end %>
|
|
21
|
+
<% end %>
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# One entry in a UI::Timeline: its marker, its title, when it happened, and the
|
|
4
|
+
# line on to the next entry. Its block is shown under the title, for anything
|
|
5
|
+
# more than `description:` (a link, a note, a badge).
|
|
6
|
+
#
|
|
7
|
+
# `status:` is :complete (the default, since most entries record something that
|
|
8
|
+
# has happened), :current, :upcoming or :error. `icon:` draws an icon in place
|
|
9
|
+
# of the status's own mark; a :dot timeline draws no icons.
|
|
10
|
+
#
|
|
11
|
+
# In a :colored timeline each circle takes the next colour in COLORS, or the one
|
|
12
|
+
# `color:` names, for colours that mean something (:emerald for stock received,
|
|
13
|
+
# :rose for written off). :upcoming and :error keep their own look, so a failure
|
|
14
|
+
# always reads as one.
|
|
15
|
+
#
|
|
16
|
+
# `time:` is a Time or Date, shown with the locale's :long format inside a
|
|
17
|
+
# <time> element; `time_label:` changes the words but keeps the machine-readable
|
|
18
|
+
# date ("2 hours ago"). A String is shown as it is, for what isn't a date yet
|
|
19
|
+
# ("Expected Friday").
|
|
20
|
+
class UI::Timeline::Item < UI::Base
|
|
21
|
+
STATUSES = %i[complete current upcoming error].freeze
|
|
22
|
+
|
|
23
|
+
ICONS = { complete: "check", error: "x" }.freeze
|
|
24
|
+
|
|
25
|
+
# Written out in full, status by status, so Tailwind finds every class.
|
|
26
|
+
MARKER = {
|
|
27
|
+
default: {
|
|
28
|
+
complete: "size-8 bg-primary text-primary-foreground",
|
|
29
|
+
current: "size-8 border-2 border-primary bg-background text-primary ring-4 ring-primary/15",
|
|
30
|
+
upcoming: "size-8 bg-muted text-muted-foreground",
|
|
31
|
+
error: "size-8 bg-destructive text-white ring-4 ring-destructive/15"
|
|
32
|
+
},
|
|
33
|
+
dot: {
|
|
34
|
+
complete: "mt-1 size-3 bg-primary",
|
|
35
|
+
current: "mt-1 size-3 border-2 border-primary bg-background ring-4 ring-primary/15",
|
|
36
|
+
upcoming: "mt-1 size-3 bg-muted-foreground/30",
|
|
37
|
+
error: "mt-1 size-3 bg-destructive ring-4 ring-destructive/15"
|
|
38
|
+
}
|
|
39
|
+
}.freeze
|
|
40
|
+
|
|
41
|
+
# Each colour's circle, then the ring it wears as the current item.
|
|
42
|
+
COLORS = {
|
|
43
|
+
blue: [ "bg-blue-600 text-white", "ring-4 ring-blue-600/20" ],
|
|
44
|
+
emerald: [ "bg-emerald-600 text-white", "ring-4 ring-emerald-600/20" ],
|
|
45
|
+
amber: [ "bg-amber-600 text-white", "ring-4 ring-amber-600/20" ],
|
|
46
|
+
violet: [ "bg-violet-600 text-white", "ring-4 ring-violet-600/20" ],
|
|
47
|
+
rose: [ "bg-rose-600 text-white", "ring-4 ring-rose-600/20" ],
|
|
48
|
+
teal: [ "bg-teal-600 text-white", "ring-4 ring-teal-600/20" ]
|
|
49
|
+
}.freeze
|
|
50
|
+
|
|
51
|
+
TITLE = {
|
|
52
|
+
complete: "text-foreground",
|
|
53
|
+
current: "text-foreground",
|
|
54
|
+
upcoming: "text-muted-foreground",
|
|
55
|
+
error: "text-destructive"
|
|
56
|
+
}.freeze
|
|
57
|
+
|
|
58
|
+
STATUS_TEXT = {
|
|
59
|
+
complete: "Completed",
|
|
60
|
+
current: "In progress",
|
|
61
|
+
upcoming: "Not started",
|
|
62
|
+
error: "Failed"
|
|
63
|
+
}.freeze
|
|
64
|
+
|
|
65
|
+
attr_reader :status, :title, :description, :icon
|
|
66
|
+
|
|
67
|
+
def initialize(title:, description: nil, time: nil, time_label: nil, status: :complete, icon: nil,
|
|
68
|
+
color: nil, class: nil, **html_attrs)
|
|
69
|
+
@title = title
|
|
70
|
+
@description = description
|
|
71
|
+
@time = time
|
|
72
|
+
@time_label = time_label
|
|
73
|
+
@status = STATUSES.include?(status&.to_sym) ? status.to_sym : :complete
|
|
74
|
+
@icon = icon
|
|
75
|
+
@color = color&.to_sym
|
|
76
|
+
@user_class = binding.local_variable_get(:class)
|
|
77
|
+
@html_attrs = html_attrs
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def place(variant:, index:, last:)
|
|
81
|
+
@variant = variant
|
|
82
|
+
@index = index
|
|
83
|
+
@last = last
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def dot? = @variant == :dot
|
|
87
|
+
def colored? = @variant == :colored && %i[complete current].include?(@status)
|
|
88
|
+
def color = COLORS.key?(@color) ? @color : COLORS.keys[@index % COLORS.size]
|
|
89
|
+
def connector? = !@last
|
|
90
|
+
def description? = @description.present?
|
|
91
|
+
def time? = @time.present?
|
|
92
|
+
def date? = @time.respond_to?(:iso8601)
|
|
93
|
+
def marker_icon = @icon || ICONS[@status]
|
|
94
|
+
|
|
95
|
+
def time_text
|
|
96
|
+
return @time_label if @time_label.present?
|
|
97
|
+
|
|
98
|
+
date? ? I18n.l(@time, format: :long) : @time.to_s
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def item_attrs
|
|
102
|
+
attrs = @html_attrs.except(:data, :aria)
|
|
103
|
+
attrs[:class] = cn("relative flex min-w-0 gap-3", { "pb-6" => connector? }, @user_class)
|
|
104
|
+
attrs[:aria] = { current: ("step" if @status == :current) }.compact.merge(@html_attrs[:aria] || {})
|
|
105
|
+
attrs[:data] = { slot: "timeline-item", status: @status }.merge(@html_attrs[:data] || {})
|
|
106
|
+
attrs
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def marker_classes
|
|
110
|
+
# A circle is taller than a line of text. Its overhang doesn't count towards
|
|
111
|
+
# the item's height, so the text alone sets the gap to the next item, however
|
|
112
|
+
# much text there is. The last item has nothing below to overhang into.
|
|
113
|
+
cn("relative z-10 flex shrink-0 items-center justify-center rounded-full", marker_look,
|
|
114
|
+
{ "-mb-2" => !dot? && connector? })
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def marker_look
|
|
118
|
+
return MARKER[dot? ? :dot : :default][@status] unless colored?
|
|
119
|
+
|
|
120
|
+
fill, ring = COLORS[color]
|
|
121
|
+
cn("size-8", fill, (ring if @status == :current))
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Unbroken from this marker to the next one, drawn in while the entry is done
|
|
125
|
+
# (a plain line between colours, which would clash with any one of them).
|
|
126
|
+
# A dot sits 0.25rem down its item, so the line runs that far into the next.
|
|
127
|
+
# Placed from the start edge so it follows the marker in a right-to-left page.
|
|
128
|
+
def connector_classes
|
|
129
|
+
cn("absolute w-0.5",
|
|
130
|
+
dot? ? "start-[calc(0.375rem-1px)] top-4 -bottom-1" : "start-[calc(1rem-1px)] top-8 bottom-0",
|
|
131
|
+
@status == :complete && @variant != :colored ? "bg-primary" : "bg-border")
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def text_classes
|
|
135
|
+
cn("flex min-w-0 flex-1 flex-col gap-0.5", { "pt-1" => !dot? })
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def title_classes
|
|
139
|
+
cn("text-sm font-medium", TITLE[@status])
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def status_text
|
|
143
|
+
t("ui.timeline.#{@status}", default: STATUS_TEXT[@status])
|
|
144
|
+
end
|
|
145
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<%= tag.ol(**root_attrs) do %><% items.each do |item| %><%= item %><% end %><% end %>
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# What happened, and what's still to come, as a list down the page: an order's
|
|
4
|
+
# progress, a product's stock history, an account's activity. UI::Stepper is
|
|
5
|
+
# for moving someone through a form; this only records.
|
|
6
|
+
#
|
|
7
|
+
# <%= render(UI::Timeline.new(label: "Order history")) do |timeline| %>
|
|
8
|
+
# <% timeline.with_item(title: "Paid", time: order.paid_at, icon: "credit-card") %>
|
|
9
|
+
# <% timeline.with_item(title: "Shipped", status: :current) do %>
|
|
10
|
+
# <%= link_to "Track parcel", order.tracking_url %>
|
|
11
|
+
# <% end %>
|
|
12
|
+
# <% timeline.with_item(title: "Delivered", status: :upcoming, time: "Expected Friday") %>
|
|
13
|
+
# <% end %>
|
|
14
|
+
#
|
|
15
|
+
# Items show in the order they're given, so newest-first is up to the caller.
|
|
16
|
+
# `variant: :default` draws a circle holding the item's icon or a mark for its
|
|
17
|
+
# status, `:dot` a small dot, for a long log, and `:colored` the same circles in
|
|
18
|
+
# a different colour each. Each item's options are on UI::Timeline::Item.
|
|
19
|
+
class UI::Timeline < UI::Base
|
|
20
|
+
VARIANTS = %i[default dot colored].freeze
|
|
21
|
+
|
|
22
|
+
renders_many :items, UI::Timeline::Item
|
|
23
|
+
|
|
24
|
+
def initialize(variant: :default, label: nil, class: nil, **html_attrs)
|
|
25
|
+
@variant = VARIANTS.include?(variant.to_sym) ? variant.to_sym : :default
|
|
26
|
+
@label = label
|
|
27
|
+
@user_class = binding.local_variable_get(:class)
|
|
28
|
+
@html_attrs = html_attrs
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def before_render
|
|
32
|
+
items.each_with_index { |item, index| item.place(variant: @variant, index: index, last: index == items.size - 1) }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def root_attrs
|
|
36
|
+
attrs = @html_attrs.except(:data, :aria)
|
|
37
|
+
attrs[:class] = cn("flex w-full flex-col", @user_class)
|
|
38
|
+
attrs[:aria] = { label: @label || t("ui.timeline.label", default: "Timeline") }.merge(@html_attrs[:aria] || {})
|
|
39
|
+
attrs[:data] = { slot: "timeline", variant: @variant }.merge(@html_attrs[:data] || {})
|
|
40
|
+
attrs
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Chart.js, built as its own entry point rather than bundled into
|
|
2
|
+
// application.js, so a page without a chart never downloads it. Only the line,
|
|
3
|
+
// bar, pie and doughnut parts are registered, a fraction of the full build.
|
|
4
|
+
//
|
|
5
|
+
// Same shape as ag_grid.ts and cally.ts, and for the same reasons: a bun
|
|
6
|
+
// code-split chunk can't be used because Propshaft serves digested filenames,
|
|
7
|
+
// and the URL is handed to the Stimulus controller from Ruby.
|
|
8
|
+
import {
|
|
9
|
+
ArcElement,
|
|
10
|
+
BarController,
|
|
11
|
+
BarElement,
|
|
12
|
+
CategoryScale,
|
|
13
|
+
Chart,
|
|
14
|
+
DoughnutController,
|
|
15
|
+
LinearScale,
|
|
16
|
+
LineController,
|
|
17
|
+
LineElement,
|
|
18
|
+
PieController,
|
|
19
|
+
PointElement,
|
|
20
|
+
Tooltip,
|
|
21
|
+
} from 'chart.js';
|
|
22
|
+
|
|
23
|
+
Chart.register(
|
|
24
|
+
ArcElement,
|
|
25
|
+
BarController,
|
|
26
|
+
BarElement,
|
|
27
|
+
CategoryScale,
|
|
28
|
+
DoughnutController,
|
|
29
|
+
LinearScale,
|
|
30
|
+
LineController,
|
|
31
|
+
LineElement,
|
|
32
|
+
PieController,
|
|
33
|
+
PointElement,
|
|
34
|
+
Tooltip,
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
export { Chart };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// Typed dates in the locale's order, and ISO dates for the form.
|
|
2
|
+
//
|
|
3
|
+
// The order ("DMY") and separator come from Ruby, which derives them from
|
|
4
|
+
// date.formats.typed in the locale, so one string governs what a date field
|
|
5
|
+
// accepts, shows and hints at.
|
|
6
|
+
const ISO = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
7
|
+
// Any of / - . or a space between the parts: people type what is in front of
|
|
8
|
+
// them, and being strict about the separator only creates work.
|
|
9
|
+
const PARTS = /^(\d{1,4})[/\-. ](\d{1,4})[/\-. ](\d{1,4})$/;
|
|
10
|
+
const SEPARATORS = /[/\-. ]/;
|
|
11
|
+
const LENGTH: Record<string, number> = { D: 2, M: 2, Y: 4 };
|
|
12
|
+
|
|
13
|
+
// The locale's order in, ISO out. Rejects 31/02 rather than rolling it into
|
|
14
|
+
// the 3rd of March, which is what Date does and how a wrong date gets stored
|
|
15
|
+
// without anyone seeing an error.
|
|
16
|
+
export function toIso(typed: string, order: string): string | null {
|
|
17
|
+
const match = PARTS.exec(typed.trim());
|
|
18
|
+
if (!match) return null;
|
|
19
|
+
|
|
20
|
+
const parts = order.split('');
|
|
21
|
+
const day = match[parts.indexOf('D') + 1];
|
|
22
|
+
const month = match[parts.indexOf('M') + 1];
|
|
23
|
+
const year = match[parts.indexOf('Y') + 1];
|
|
24
|
+
if (!day || !month || !year || year.length !== 4) return null;
|
|
25
|
+
|
|
26
|
+
const iso = `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')}`;
|
|
27
|
+
const date = new Date(`${iso}T00:00:00`);
|
|
28
|
+
if (Number.isNaN(date.getTime())) return null;
|
|
29
|
+
|
|
30
|
+
// Round-tripped, because Date accepts the 31st of February and quietly
|
|
31
|
+
// returns the 3rd of March.
|
|
32
|
+
return date.getDate() === Number(day) && date.getMonth() + 1 === Number(month) ? iso : null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function toTyped(iso: string, order: string, separator: string): string {
|
|
36
|
+
const match = ISO.exec((iso ?? '').trim());
|
|
37
|
+
if (!match) return '';
|
|
38
|
+
|
|
39
|
+
const [, year, month, day] = match;
|
|
40
|
+
const parts: Record<string, string> = { D: day, M: month, Y: year };
|
|
41
|
+
return order.split('').map((key) => parts[key]).join(separator);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// What a date field holds as it's typed: digits only, in the locale's order,
|
|
45
|
+
// the separator put in as each part fills. A separator typed early ends the
|
|
46
|
+
// part, so "1/" becomes "01/". A pasted ISO date is turned round. `growing`
|
|
47
|
+
// is false while deleting, so backspacing over a separator isn't undone.
|
|
48
|
+
export function maskDate(raw: string, order: string, separator: string, growing = true): string {
|
|
49
|
+
if (ISO.test(raw.trim())) return toTyped(raw.trim(), order, separator);
|
|
50
|
+
|
|
51
|
+
const keys = order.split('');
|
|
52
|
+
const parts: string[] = [];
|
|
53
|
+
let current = '';
|
|
54
|
+
for (const char of raw) {
|
|
55
|
+
if (parts.length === keys.length) break;
|
|
56
|
+
|
|
57
|
+
const key = keys[parts.length];
|
|
58
|
+
if (/\d/.test(char)) {
|
|
59
|
+
current += char;
|
|
60
|
+
if (current.length === LENGTH[key]) {
|
|
61
|
+
parts.push(current);
|
|
62
|
+
current = '';
|
|
63
|
+
}
|
|
64
|
+
} else if (SEPARATORS.test(char) && current !== '' && key !== 'Y') {
|
|
65
|
+
parts.push(current.padStart(LENGTH[key], '0'));
|
|
66
|
+
current = '';
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
let out = parts.join(separator);
|
|
71
|
+
if (current) out += (parts.length ? separator : '') + current;
|
|
72
|
+
else if (parts.length > 0 && parts.length < keys.length && (growing || SEPARATORS.test(raw.slice(-1)))) out += separator;
|
|
73
|
+
return out;
|
|
74
|
+
}
|