tramway 5.0.0.1 → 5.0.0.2

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 (28) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +17 -1
  3. data/app/assets/javascripts/tramway/tramway.js +201 -2
  4. data/app/assets/tailwind/tramway/components/form.css +256 -59
  5. data/app/components/tailwind_component.rb +1 -0
  6. data/app/components/tramway/form/builder.rb +8 -10
  7. data/app/components/tramway/form/checkbox_component.html.haml +14 -30
  8. data/app/components/tramway/form/checkbox_component.rb +11 -24
  9. data/app/components/tramway/form/date_field_component.rb +4 -8
  10. data/app/components/tramway/form/datetime_field_component.rb +4 -8
  11. data/app/components/tramway/form/datetime_picker_component.html.haml +49 -0
  12. data/app/components/tramway/form/datetime_picker_component.rb +62 -0
  13. data/app/components/tramway/form/file_field_component.html.haml +8 -7
  14. data/app/components/tramway/form/number_field_component.html.haml +11 -6
  15. data/app/components/tramway/form/rich_text_area_component.html.haml +10 -5
  16. data/app/components/tramway/form/select_component.html.haml +11 -6
  17. data/app/components/tramway/form/text_area_component.html.haml +11 -6
  18. data/app/components/tramway/form/text_field_component.html.haml +11 -6
  19. data/app/components/tramway/form/time_field_component.html.haml +11 -6
  20. data/app/components/tramway/form/tramway_select_component.html.haml +19 -14
  21. data/app/views/tramway/entities/_form.html.haml +1 -1
  22. data/config/locales/en.yml +8 -0
  23. data/lib/generators/tramway/install/install_generator.rb +5 -3
  24. data/lib/tramway/helpers/views_helper.rb +18 -9
  25. data/lib/tramway/version.rb +1 -1
  26. metadata +3 -3
  27. data/app/components/tramway/form/date_field_component.html.haml +0 -8
  28. data/app/components/tramway/form/datetime_field_component.html.haml +0 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b74b8ebed4a12cb0a2c9f9387102ab45c44012f44b41f67e766d63d370945321
4
- data.tar.gz: 3db8340bda31fd6f7b0803df590492929457975937e99d37b31b863b1fcbbda3
3
+ metadata.gz: 9126411c682d9b590533fee298b7be7a2664306f7a59370ebb77104b959cd34f
4
+ data.tar.gz: e0fa5b07f3c86eb5dc73f45f2d65dc3c71f10be5c7d81fb45d0ac57fb8396028
5
5
  SHA512:
6
- metadata.gz: 6e170deb34f995a582f1f063fae835db11e6295351ab0b72a5587adbc9c69b9c21ad9930851f290ef3552bfe8fffaec287b1a85e0a5a87b823a15b4e6723fee3
7
- data.tar.gz: 47b8758325dd62d3b97b570fb5e096abbac6a8a1b07fec6392ca34c53f2a3a37bd0baba6a5d66fa315545aa0a097260b1da4b978cb22213bebf8a5e8083a1759
6
+ metadata.gz: 89edbcf06d94ad5e7a7fe69a10e64c594eaefddf9dfcee0553111d8ffff2e5c7c8a3a2c40fdfc75bab3f14bc65d108928347c9ced3fb5b56981578177249eba4
7
+ data.tar.gz: 468cc6157d74c016bfd7cf6efe4451096bb90b0264367facbd1678e19b676f5978d2e82ebe42b250be4f3224b08dbfc49b8814aa7de1d247da8f44d46f1eed8c
data/README.md CHANGED
@@ -1675,7 +1675,23 @@ will render [this](https://play.tailwindcss.com/xho3LfjKkK)
1675
1675
 
1676
1676
  Use `size:` to control the input sizing (`:small`, `:medium`, or `:large`). The default is `:medium`, and supported inputs
1677
1677
  rendered within the form will use the same size value.
1678
- Date and datetime fields open the browser's native picker when clicking anywhere in the input, not only the picker icon.
1678
+ `tramway_form_for` wraps the form in the TimeMagic form card (pass `card: false` to opt out; forms with their own `class:`
1679
+ are not wrapped) and lays every field out like the TimeMagic form: a label column (with an optional icon) on the left and
1680
+ the control with its hint on the right (stacked on mobile), with a hairline between fields. Pass `icon:` with a Font
1681
+ Awesome class string to show an icon next to the label.
1682
+
1683
+ `date_field` and `datetime_field` render the TimeMagic calendar popover instead of the browser's native picker (the form
1684
+ still submits `YYYY-MM-DD` / `YYYY-MM-DDTHH:MM` in the usual field). `datetime_field` adds an hour/minute row; the minute
1685
+ step defaults to 5 and can be changed with `step:`. The `tramway-datetime-picker` Stimulus controller registers itself
1686
+ when your Stimulus application is exposed as `window.Stimulus` (the Rails default); otherwise register it manually:
1687
+
1688
+ ```js
1689
+ import { DatetimePicker } from "@tramway/tramway"
1690
+ application.register('tramway-datetime-picker', DatetimePicker)
1691
+ ```
1692
+
1693
+ `check_box` renders a native checkbox with its text in one label (the TimeMagic look). Pass `check_label:` to show a
1694
+ separate caption in the label column and keep `label:` as the text next to the box.
1679
1695
  Use `hint:` on any field to render muted helper text below the input. The hint option is handled by Tramway and is not
1680
1696
  forwarded as an HTML attribute.
1681
1697
 
@@ -1,5 +1,5 @@
1
1
  import { Controller } from "@hotwired/stimulus"
2
- /* global console, fetch, setInterval, clearInterval, DOMParser */
2
+ /* global console, fetch, setInterval, clearInterval, DOMParser, Event */
3
3
 
4
4
  class TramwaySelect extends Controller {
5
5
  static targets = ["dropdown", "showSelectedArea", "hiddenInput", "caretDown", "caretUp"]
@@ -635,4 +635,203 @@ class AutoRefresh extends Controller {
635
635
  }
636
636
  }
637
637
 
638
- export { TramwaySelect, UiCheckbox, Tooltip, Navbar, Collapsible, AutoRefresh }
638
+ // Calendar-and-time popover driving a hidden form field. The trigger button
639
+ // opens `popover` via the native `popovertarget` attribute (Escape,
640
+ // outside-click, top-layer stacking and focus restoration come free from the
641
+ // platform, the same way a popover menu does) — this controller owns only what the platform doesn't: building
642
+ // the calendar grid, keeping the hidden input + trigger label in sync with
643
+ // the selected date/time, and the month/time navigation.
644
+ const PICKER_MONTH_NAMES = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
645
+ const PICKER_MONTH_NAMES_SHORT = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
646
+
647
+ class DatetimePicker extends Controller {
648
+ static targets = ["input", "trigger", "display", "popover", "monthLabel", "grid", "hour", "minute"]
649
+ static values = { mode: String, step: { type: Number, default: 5 } }
650
+
651
+ connect() {
652
+ this.selected = this.parseValue(this.inputTarget.value)
653
+ this.viewDate = this.selected ? new Date(this.selected) : new Date()
654
+ this.viewDate.setDate(1)
655
+
656
+ if (this.isDatetime) this.populateTimeSelects()
657
+ this.renderMonth()
658
+
659
+ this.popoverTarget.addEventListener("toggle", this.handleToggle)
660
+ }
661
+
662
+ disconnect() {
663
+ this.popoverTarget.removeEventListener("toggle", this.handleToggle)
664
+ }
665
+
666
+ get isDatetime() {
667
+ return this.modeValue === "datetime"
668
+ }
669
+
670
+ handleToggle = (event) => {
671
+ this.triggerTarget.setAttribute("aria-expanded", String(event.newState === "open"))
672
+ if (event.newState === "open") this.renderMonth()
673
+ }
674
+
675
+ parseValue(raw) {
676
+ if (!raw) return null
677
+
678
+ const [datePart, timePart] = raw.split("T")
679
+ const [year, month, day] = datePart.split("-").map(Number)
680
+ if (!year || !month || !day) return null
681
+
682
+ const date = new Date(year, month - 1, day)
683
+ if (timePart) {
684
+ const [hour, minute] = timePart.split(":").map(Number)
685
+ date.setHours(hour || 0, minute || 0)
686
+ }
687
+ return date
688
+ }
689
+
690
+ prevMonth() {
691
+ this.viewDate.setMonth(this.viewDate.getMonth() - 1)
692
+ this.renderMonth()
693
+ }
694
+
695
+ nextMonth() {
696
+ this.viewDate.setMonth(this.viewDate.getMonth() + 1)
697
+ this.renderMonth()
698
+ }
699
+
700
+ renderMonth() {
701
+ this.monthLabelTarget.textContent = `${PICKER_MONTH_NAMES[this.viewDate.getMonth()]} ${this.viewDate.getFullYear()}`
702
+
703
+ const firstWeekday = new Date(this.viewDate.getFullYear(), this.viewDate.getMonth(), 1).getDay()
704
+ const daysInMonth = new Date(this.viewDate.getFullYear(), this.viewDate.getMonth() + 1, 0).getDate()
705
+ const today = new Date()
706
+
707
+ const cells = []
708
+ for (let i = 0; i < firstWeekday; i++) cells.push(null)
709
+ for (let day = 1; day <= daysInMonth; day++) cells.push(day)
710
+
711
+ this.gridTarget.innerHTML = ""
712
+ cells.forEach((day) => {
713
+ if (day === null) {
714
+ this.gridTarget.appendChild(document.createElement("span"))
715
+ return
716
+ }
717
+
718
+ const cellDate = new Date(this.viewDate.getFullYear(), this.viewDate.getMonth(), day)
719
+ const button = document.createElement("button")
720
+ button.type = "button"
721
+ button.textContent = String(day)
722
+ button.className = "tramway-datetimepicker__day"
723
+ if (this.isSameDay(cellDate, today)) button.classList.add("tramway-datetimepicker__day--today")
724
+ if (this.selected && this.isSameDay(cellDate, this.selected)) button.classList.add("tramway-datetimepicker__day--selected")
725
+ button.addEventListener("click", () => this.selectDay(cellDate))
726
+ this.gridTarget.appendChild(button)
727
+ })
728
+ }
729
+
730
+ isSameDay(a, b) {
731
+ return a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate()
732
+ }
733
+
734
+ selectDay(date) {
735
+ if (this.selected) {
736
+ date.setHours(this.selected.getHours(), this.selected.getMinutes())
737
+ } else if (this.isDatetime) {
738
+ const now = new Date()
739
+ date.setHours(now.getHours(), this.roundToStep(now.getMinutes()))
740
+ }
741
+
742
+ this.selected = date
743
+ this.renderMonth()
744
+ this.syncTimeSelects()
745
+ this.commit()
746
+
747
+ if (!this.isDatetime) this.popoverTarget.hidePopover()
748
+ }
749
+
750
+ roundToStep(minutes) {
751
+ const step = this.stepValue || 5
752
+ return Math.round(minutes / step) * step % 60
753
+ }
754
+
755
+ populateTimeSelects() {
756
+ this.hourTarget.innerHTML = Array.from({ length: 24 }, (_, hour) =>
757
+ `<option value="${hour}">${String(hour).padStart(2, "0")}</option>`
758
+ ).join("")
759
+
760
+ const step = this.stepValue || 5
761
+ this.minuteTarget.innerHTML = Array.from({ length: Math.ceil(60 / step) }, (_, i) => {
762
+ const minute = i * step
763
+ return `<option value="${minute}">${String(minute).padStart(2, "0")}</option>`
764
+ }).join("")
765
+ }
766
+
767
+ syncTimeSelects() {
768
+ if (!this.isDatetime || !this.selected) return
769
+
770
+ this.hourTarget.value = String(this.selected.getHours())
771
+ this.minuteTarget.value = String(this.roundToStep(this.selected.getMinutes()))
772
+ }
773
+
774
+ timeChanged() {
775
+ if (!this.selected) this.selected = new Date(this.viewDate)
776
+ this.selected.setHours(Number(this.hourTarget.value), Number(this.minuteTarget.value))
777
+ this.commit()
778
+ }
779
+
780
+ today() {
781
+ const now = new Date()
782
+ if (this.isDatetime) now.setMinutes(this.roundToStep(now.getMinutes()))
783
+ this.selected = now
784
+ this.viewDate = new Date(now.getFullYear(), now.getMonth(), 1)
785
+ this.renderMonth()
786
+ this.syncTimeSelects()
787
+ this.commit()
788
+ }
789
+
790
+ clear() {
791
+ this.selected = null
792
+ this.inputTarget.value = ""
793
+ this.displayTarget.textContent = this.displayTarget.dataset.placeholder
794
+ this.renderMonth()
795
+ this.inputTarget.dispatchEvent(new Event("change", { bubbles: true }))
796
+ this.popoverTarget.hidePopover()
797
+ }
798
+
799
+ done() {
800
+ this.popoverTarget.hidePopover()
801
+ }
802
+
803
+ commit() {
804
+ if (!this.selected) return
805
+
806
+ const year = this.selected.getFullYear()
807
+ const month = String(this.selected.getMonth() + 1).padStart(2, "0")
808
+ const day = String(this.selected.getDate()).padStart(2, "0")
809
+ let value = `${year}-${month}-${day}`
810
+ let display = `${PICKER_MONTH_NAMES_SHORT[this.selected.getMonth()]} ${this.selected.getDate()}, ${year}`
811
+
812
+ if (this.isDatetime) {
813
+ const hour = String(this.selected.getHours()).padStart(2, "0")
814
+ const minute = String(this.selected.getMinutes()).padStart(2, "0")
815
+ value += `T${hour}:${minute}`
816
+ display += `, ${hour}:${minute}`
817
+ }
818
+
819
+ this.inputTarget.value = value
820
+ this.displayTarget.textContent = display
821
+ this.inputTarget.dispatchEvent(new Event("change", { bubbles: true }))
822
+ }
823
+ }
824
+
825
+ // The form helpers render the picker markup themselves, so the controller registers itself when the host exposes its
826
+ // Stimulus application as `window.Stimulus` (the Rails default); no manual `application.register` is needed.
827
+ function registerDatetimePicker() {
828
+ const application = window.Stimulus
829
+ if (!application || application.router.modulesByIdentifier.has("tramway-datetime-picker")) return
830
+
831
+ application.register("tramway-datetime-picker", DatetimePicker)
832
+ }
833
+
834
+ registerDatetimePicker()
835
+ document.addEventListener("DOMContentLoaded", registerDatetimePicker)
836
+
837
+ export { TramwaySelect, UiCheckbox, Tooltip, Navbar, Collapsible, AutoRefresh, DatetimePicker }
@@ -51,12 +51,12 @@
51
51
  margin-bottom: 0;
52
52
  }
53
53
 
54
- /* Inside a form card every field is a row: label on the left, control + hint/error on the right
55
- * (stacked on mobile), rows separated by a hairline. Same layout as the TimeMagic form. */
54
+ /* Inside a form card every field is a row, exactly like the TimeMagic form: a label column (icon + label) and a
55
+ * control column (input + hint/error), stacked on mobile, rows separated by a hairline. */
56
56
  .tramway-form .tramway-form__field {
57
57
  display: grid;
58
- grid-template-columns: minmax(0, 1fr);
59
- row-gap: 0.375rem;
58
+ grid-template-columns: 1fr;
59
+ gap: 0.5rem;
60
60
  margin-bottom: 0;
61
61
  padding-block: 1rem;
62
62
  border-bottom: 1px solid var(--tramway-color-border);
@@ -77,20 +77,26 @@
77
77
 
78
78
  @media (min-width: 768px) {
79
79
  .tramway-form .tramway-form__field {
80
- grid-template-columns: minmax(6rem, 9.5rem) minmax(0, 1fr);
81
- column-gap: 1.5rem;
82
- align-items: start;
80
+ grid-template-columns: 11rem 1fr;
81
+ gap: 0.5rem 2rem;
83
82
  }
83
+ }
84
84
 
85
- .tramway-form .tramway-form__field > * {
86
- grid-column: 2;
87
- }
85
+ .tramway-form__field-label {
86
+ display: flex;
87
+ align-items: flex-start;
88
+ gap: 0.5rem;
89
+ min-width: 0;
90
+ }
88
91
 
89
- .tramway-form .tramway-form__field > .tramway-form__label {
90
- grid-column: 1;
91
- grid-row: 1;
92
- padding-top: 0.625rem;
93
- }
92
+ .tramway-form__label-icon {
93
+ flex: none;
94
+ display: inline-flex;
95
+ justify-content: center;
96
+ width: 1rem;
97
+ margin-top: 0.125rem;
98
+ color: var(--tramway-color-text-muted);
99
+ font-size: 0.8125rem;
94
100
  }
95
101
 
96
102
  .tramway-form__label {
@@ -239,37 +245,29 @@
239
245
  cursor: not-allowed;
240
246
  }
241
247
 
242
- /* --- Checkbox: native-looking box, accent fill and white check when checked --- */
248
+ /* --- Checkbox: every checkbox inside a `.tramway-form` card gets this look, even a plain `check_box_tag`;
249
+ * a native input (appearance: none) with the accent fill and a white check; the box and its text
250
+ * share one <label>, in the control column --- */
243
251
  .tramway-form__check {
244
- display: flex;
245
- flex-direction: column;
246
- gap: 0.375rem;
247
- margin-bottom: 1rem;
248
- }
249
-
250
- .tramway-form__check-row {
251
- display: flex;
252
+ display: inline-flex;
252
253
  align-items: center;
253
254
  gap: 0.5rem;
255
+ width: fit-content;
256
+ cursor: pointer;
254
257
  }
255
258
 
256
- .tramway-form__check-input {
257
- display: inline-flex;
258
- align-items: center;
259
- justify-content: center;
259
+ .tramway-form__check-input,
260
+ .tramway-form input[type='checkbox'] {
261
+ appearance: none;
260
262
  flex: none;
261
263
  box-sizing: border-box;
262
264
  width: 1.125rem;
263
265
  height: 1.125rem;
264
- min-width: 1.125rem;
265
- min-height: 1.125rem;
266
266
  margin: 0;
267
- padding: 0;
268
267
  border: 1px solid var(--tramway-color-border-strong);
269
268
  border-radius: var(--tramway-radius-sm);
270
269
  background-color: var(--tramway-color-bg);
271
- color: var(--tramway-color-on-accent);
272
- line-height: 1;
270
+ cursor: pointer;
273
271
  transition:
274
272
  border-color var(--tramway-duration-fast) var(--tramway-ease-standard),
275
273
  background-color var(--tramway-duration-fast) var(--tramway-ease-standard);
@@ -278,54 +276,48 @@
278
276
  .tramway-form__check-input--sm {
279
277
  width: 1rem;
280
278
  height: 1rem;
281
- min-width: 1rem;
282
- min-height: 1rem;
283
279
  }
284
280
 
285
281
  .tramway-form__check-input--lg {
286
282
  width: 1.5rem;
287
283
  height: 1.5rem;
288
- min-width: 1.5rem;
289
- min-height: 1.5rem;
290
284
  }
291
285
 
292
- .tramway-form__check-input[data-state='checked'] {
286
+ .tramway-form__check-input:checked,
287
+ .tramway-form input[type='checkbox']:checked {
293
288
  border-color: var(--tramway-color-accent);
294
289
  background-color: var(--tramway-color-accent);
290
+ background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='%23ffffff' stroke-width='2'%3E%3Cpath d='m3.5 8.5 3 3 6-6.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
291
+ background-repeat: no-repeat;
292
+ background-position: center;
293
+ background-size: 0.75rem;
295
294
  }
296
295
 
297
- .tramway-form__check-input:disabled,
298
- .tramway-form__check-input[disabled] {
299
- opacity: 0.6;
300
- }
301
-
302
- .tramway-form__check-indicator {
303
- display: flex;
304
- align-items: center;
305
- justify-content: center;
306
- width: 0.75rem;
307
- height: 0.75rem;
308
- pointer-events: none;
296
+ .tramway-form__check-input:focus-visible,
297
+ .tramway-form input[type='checkbox']:focus-visible {
298
+ outline: 2px solid var(--tramway-color-accent);
299
+ outline-offset: 2px;
309
300
  }
310
301
 
311
- .tramway-form__check-indicator[hidden] {
312
- display: none;
302
+ .tramway-form__check-input:disabled,
303
+ .tramway-form input[type='checkbox']:disabled {
304
+ opacity: 0.6;
305
+ cursor: not-allowed;
313
306
  }
314
307
 
315
- .tramway-form__check-icon {
316
- width: 100%;
317
- height: 100%;
308
+ /* The text of a checkbox is clickable too: a label right after a checkbox, or wrapping one, shows a pointer. */
309
+ .tramway-form input[type='checkbox'] + label,
310
+ .tramway-form label:has(input[type='checkbox']) {
311
+ cursor: pointer;
318
312
  }
319
313
 
320
- .tramway-form__check.tramway-form__field--horizontal {
321
- margin-bottom: 0;
314
+ .tramway-form input[type='checkbox']:disabled + label {
315
+ cursor: not-allowed;
322
316
  }
323
317
 
324
318
  .tramway-form__check-label {
325
- margin-bottom: 0;
326
319
  color: var(--tramway-color-text-secondary);
327
320
  font-size: var(--tramway-text-body-sm-size);
328
- cursor: pointer;
329
321
  }
330
322
 
331
323
  /* --- File input: the visible label acts as the button --- */
@@ -406,4 +398,209 @@
406
398
  trix-toolbar .trix-button.trix-active {
407
399
  background-color: var(--tramway-color-accent-surface);
408
400
  }
401
+
402
+ /* --- Date/time picker (Tramway::Form::DatetimePickerComponent) ---
403
+ * A `.tramway-form__input`-styled trigger button that opens a calendar (and,
404
+ * in datetime mode, an hour/minute row) via the native Popover API, the
405
+ * same mechanism as .tramway-popover-menu — top-layer stacking, outside-click,
406
+ * Escape and focus restoration come from the platform. Replaces bare
407
+ * date_field_tag/datetime_field_tag so every browser/OS renders the same
408
+ * picker instead of the inconsistent native widget.
409
+ * See app/assets/javascripts/tramway/tramway.js (DatetimePicker). */
410
+ .tramway-datetimepicker {
411
+ position: relative;
412
+ }
413
+
414
+ .tramway-datetimepicker__trigger {
415
+ display: flex;
416
+ align-items: center;
417
+ gap: 0.5rem;
418
+ cursor: pointer;
419
+ text-align: left;
420
+ }
421
+
422
+ .tramway-datetimepicker__trigger-text {
423
+ flex: 1;
424
+ min-width: 0;
425
+ overflow: hidden;
426
+ text-overflow: ellipsis;
427
+ white-space: nowrap;
428
+ color: var(--tramway-color-text-primary);
429
+ }
430
+
431
+ .tramway-datetimepicker__trigger:has(+ [popover]:popover-open) {
432
+ border-color: var(--tramway-color-accent);
433
+ }
434
+
435
+ .tramway-datetimepicker__trigger-caret {
436
+ flex: none;
437
+ font-size: 0.6875rem;
438
+ color: var(--tramway-color-text-muted);
439
+ }
440
+
441
+ /* Entrance: scales out of the trigger it's anchored to (never the
442
+ * screen center), 180ms strong ease-out — a popover-tier duration, not a
443
+ * modal one. @starting-style + transition-behavior:allow-discrete makes
444
+ * the display:none -> block flip animatable on the native Popover API. */
445
+ .tramway-datetimepicker__popover {
446
+ position: fixed;
447
+ inset: 3.5rem 1rem auto auto;
448
+ margin: 0.25rem 0 0;
449
+ z-index: var(--tramway-z-dropdown);
450
+ width: 17.5rem;
451
+ padding: 0.875rem;
452
+ background-color: var(--tramway-color-surface-raised);
453
+ border: 1px solid var(--tramway-color-border-strong);
454
+ border-radius: var(--tramway-radius-md);
455
+ box-shadow: var(--tramway-shadow-raised);
456
+ transform-origin: top;
457
+ transition: opacity var(--tramway-duration-base) var(--tramway-ease-standard),
458
+ transform var(--tramway-duration-base) var(--tramway-ease-standard),
459
+ display var(--tramway-duration-base) allow-discrete;
460
+ transition-behavior: allow-discrete;
461
+ }
462
+
463
+ @supports (position-area: bottom) {
464
+ .tramway-datetimepicker__popover {
465
+ inset: auto;
466
+ position-area: bottom span-right;
467
+ position-try-fallbacks: flip-block;
468
+ }
469
+ }
470
+
471
+ .tramway-datetimepicker__popover:popover-open {
472
+ opacity: 1;
473
+ transform: scale(1) translateY(0);
474
+ }
475
+
476
+ @starting-style {
477
+ .tramway-datetimepicker__popover:popover-open {
478
+ opacity: 0;
479
+ transform: scale(0.96) translateY(-4px);
480
+ }
481
+ }
482
+
483
+ @media (prefers-reduced-motion: reduce) {
484
+ .tramway-datetimepicker__popover {
485
+ transition: opacity var(--tramway-duration-base) var(--tramway-ease-standard),
486
+ display var(--tramway-duration-base) allow-discrete;
487
+ transform: none;
488
+ }
489
+
490
+ @starting-style {
491
+ .tramway-datetimepicker__popover:popover-open {
492
+ opacity: 0;
493
+ transform: none;
494
+ }
495
+ }
496
+ }
497
+
498
+ .tramway-datetimepicker__header {
499
+ display: flex;
500
+ align-items: center;
501
+ justify-content: space-between;
502
+ margin-bottom: 0.625rem;
503
+ }
504
+
505
+ .tramway-datetimepicker__month {
506
+ font-size: var(--tramway-text-label-size);
507
+ font-weight: 600;
508
+ color: var(--tramway-color-text-primary);
509
+ }
510
+
511
+ .tramway-datetimepicker__weekdays,
512
+ .tramway-datetimepicker__grid {
513
+ display: grid;
514
+ grid-template-columns: repeat(7, 1fr);
515
+ gap: 0.125rem;
516
+ }
517
+
518
+ .tramway-datetimepicker__weekdays span {
519
+ text-align: center;
520
+ font-size: var(--tramway-text-caption-size);
521
+ font-weight: 500;
522
+ color: var(--tramway-color-text-muted);
523
+ padding-block: 0.25rem;
524
+ }
525
+
526
+ .tramway-datetimepicker__day {
527
+ aspect-ratio: 1;
528
+ display: flex;
529
+ align-items: center;
530
+ justify-content: center;
531
+ border-radius: var(--tramway-radius-sm);
532
+ font-size: var(--tramway-text-body-sm-size);
533
+ color: var(--tramway-color-text-secondary);
534
+ cursor: pointer;
535
+ transition: background-color var(--tramway-duration-fast) var(--tramway-ease-standard),
536
+ color var(--tramway-duration-fast) var(--tramway-ease-standard);
537
+ }
538
+
539
+ .tramway-datetimepicker__day:hover {
540
+ background-color: var(--tramway-color-surface);
541
+ color: var(--tramway-color-text-primary);
542
+ }
543
+
544
+ .tramway-datetimepicker__day--today {
545
+ color: var(--tramway-color-accent);
546
+ font-weight: 600;
547
+ }
548
+
549
+ .tramway-datetimepicker__day--selected,
550
+ .tramway-datetimepicker__day--selected:hover {
551
+ background-color: var(--tramway-color-accent);
552
+ color: var(--tramway-color-on-accent);
553
+ font-weight: 600;
554
+ }
555
+
556
+ .tramway-datetimepicker__time {
557
+ display: flex;
558
+ align-items: center;
559
+ gap: 0.375rem;
560
+ margin-top: 0.75rem;
561
+ padding-top: 0.75rem;
562
+ border-top: 1px solid var(--tramway-color-border);
563
+ }
564
+
565
+ .tramway-datetimepicker__time-icon {
566
+ color: var(--tramway-color-text-muted);
567
+ font-size: 0.8125rem;
568
+ margin-right: 0.125rem;
569
+ }
570
+
571
+ .tramway-form__select.tramway-datetimepicker__time-select {
572
+ height: var(--tramway-control-height-sm);
573
+ padding-inline: 0.5rem 1.5rem;
574
+ background-position: right 0.375rem center;
575
+ }
576
+
577
+ .tramway-datetimepicker__time-sep {
578
+ color: var(--tramway-color-text-muted);
579
+ font-weight: 600;
580
+ }
581
+
582
+ .tramway-datetimepicker__footer {
583
+ display: flex;
584
+ align-items: center;
585
+ gap: 0.75rem;
586
+ margin-top: 0.75rem;
587
+ padding-top: 0.75rem;
588
+ border-top: 1px solid var(--tramway-color-border);
589
+ }
590
+
591
+ .tramway-datetimepicker__footer-link {
592
+ font-size: var(--tramway-text-caption-size);
593
+ font-weight: 500;
594
+ color: var(--tramway-color-text-secondary);
595
+ cursor: pointer;
596
+ transition: color var(--tramway-duration-fast) var(--tramway-ease-standard);
597
+ }
598
+
599
+ .tramway-datetimepicker__footer-link:hover {
600
+ color: var(--tramway-color-text-primary);
601
+ }
602
+
603
+ .tramway-datetimepicker__footer .tramway-button {
604
+ margin-left: auto;
605
+ }
409
606
  }
@@ -12,6 +12,7 @@ class TailwindComponent < Tramway::BaseComponent
12
12
  option :label
13
13
  option :for
14
14
  option :hint, optional: true, default: -> {}
15
+ option :icon, optional: true
15
16
  option :size, default: -> { :medium }
16
17
 
17
18
  SIZE_MODIFIERS = {
@@ -74,11 +74,11 @@ module Tramway
74
74
  end
75
75
 
76
76
  def date_field(attribute, **, &)
77
- common_field(:date_field, :date_field, attribute, **, &)
77
+ common_field(:date_field, :hidden_field, attribute, **, &)
78
78
  end
79
79
 
80
80
  def datetime_field(attribute, **, &)
81
- common_field(:datetime_field, :datetime_field, attribute, **, &)
81
+ common_field(:datetime_field, :hidden_field, attribute, **, &)
82
82
  end
83
83
 
84
84
  def time_field(attribute, **, &)
@@ -206,20 +206,18 @@ module Tramway
206
206
  options.merge!(horizontal: true) if @horizontal
207
207
  options.merge!(onchange: 'this.form.requestSubmit()') if @remote
208
208
 
209
- {
210
- attribute:,
211
- label: label_build(attribute, options),
212
- for: options[:id].presence || for_id(attribute),
213
- hint: hint_option(options),
214
- options: options,
215
- size: form_size
216
- }
209
+ { attribute:, label: label_build(attribute, options), for: options[:id].presence || for_id(attribute),
210
+ hint: hint_option(options), icon: icon_option(options), options: options, size: form_size }
217
211
  end
218
212
 
219
213
  def hint_option(options)
220
214
  options.delete(:hint) || options.delete('hint')
221
215
  end
222
216
 
217
+ def icon_option(options)
218
+ options.delete(:icon) || options.delete('icon')
219
+ end
220
+
223
221
  def label_build(attribute, options)
224
222
  label_option = options[:label]
225
223
 
@@ -1,31 +1,15 @@
1
- %div{ class: container_classes, data: { controller: 'ui--checkbox' } }
2
- .tramway-form__check-row
3
- - checkbox_checked = checked?
4
- = @input.call @attribute, **hidden_checkbox_options
5
- %button{ type: 'button',
6
- role: 'checkbox',
7
- 'aria-checked' => checkbox_checked.to_s,
8
- 'data-action' => 'click->ui--checkbox#toggle',
9
- 'data-state' => checkbox_checked ? 'checked' : 'unchecked',
10
- 'data-ui--checkbox-target' => 'button',
11
- value: 'on',
12
- class: checkbox_button_classes,
13
- for: @for,
14
- id: @for }
15
- %span.tramway-form__check-indicator{ hidden: !checkbox_checked, data: { 'ui--checkbox-target' => 'indicator' } }
16
- %svg{ xmlns: 'http://www.w3.org/2000/svg',
17
- width: '24',
18
- height: '24',
19
- viewBox: '0 0 24 24',
20
- fill: 'none',
21
- stroke: 'currentColor',
22
- 'stroke-width' => '3',
23
- 'stroke-linecap' => 'round',
24
- 'stroke-linejoin' => 'round',
25
- class: 'tramway-form__check-icon' }
26
- %polyline{ points: '20 6 9 17 4 12' }
27
- - if @label
28
- = component('tramway/form/label', for: @for, options: { class: label_classes, data: { action: 'click->ui--checkbox#toggle' } }) do
1
+ %div{ class: container_classes }
2
+ - if @options[:check_label].present? && @label
3
+ .tramway-form__field-label
4
+ - if @icon.present?
5
+ %span.tramway-form__label-icon{ aria: { hidden: true } }
6
+ %i{ class: @icon }
7
+ = component('tramway/form/label', for: @for) do
29
8
  = @label
30
- - if @hint.present?
31
- %p{ class: form_hint_classes }= @hint
9
+ .tramway-form__control
10
+ %label.tramway-form__check{ for: @for }
11
+ = @input.call @attribute, **checkbox_options
12
+ - if inline_label.present?
13
+ %span.tramway-form__check-label= inline_label
14
+ - if @hint.present?
15
+ %p{ class: form_hint_classes }= @hint
@@ -2,39 +2,26 @@
2
2
 
3
3
  module Tramway
4
4
  module Form
5
- # Tailwind-styled checkbox field. The `ui--checkbox` Stimulus controller keeps `data-state`,
6
- # `aria-checked` and the indicator in sync; all visuals come from `.tramway-form__check-input`.
5
+ # Checkbox field, exactly like the TimeMagic one: a native input (`appearance: none`) with the accent fill and a
6
+ # white check, sharing one <label> with its text. Pass `check_label:` to show a separate caption in the label
7
+ # column; otherwise `label:` is the text next to the box.
7
8
  class CheckboxComponent < TailwindComponent
8
- def checkbox_button_classes
9
- checkbox_classes
10
- end
11
-
12
9
  def container_classes
13
- return 'tramway-form__check tramway-form__field--horizontal' if options[:horizontal]
14
-
15
- 'tramway-form__check'
16
- end
10
+ return 'tramway-form__field tramway-form__field--horizontal' if options[:horizontal]
17
11
 
18
- def checked?
19
- !!ActiveModel::Type::Boolean.new.cast(options.fetch(:checked, value))
12
+ 'tramway-form__field'
20
13
  end
21
14
 
22
- def hidden_checkbox_options
23
- options.merge(
24
- id: hidden_checkbox_id,
25
- class: 'hidden',
26
- data: options.fetch(:data, {}).merge('ui--checkbox-target' => 'input'),
27
- tabindex: -1,
28
- aria: { hidden: true }
29
- )
15
+ def inline_label
16
+ options[:check_label].presence || @label
30
17
  end
31
18
 
32
- def hidden_checkbox_id
33
- "#{@for}_input"
19
+ def checked?
20
+ !!ActiveModel::Type::Boolean.new.cast(options.fetch(:checked, value))
34
21
  end
35
22
 
36
- def label_classes
37
- 'tramway-form__check-label'
23
+ def checkbox_options
24
+ options.except(:check_label, :horizontal, :label).merge(id: @for, class: checkbox_classes, checked: checked?)
38
25
  end
39
26
  end
40
27
  end
@@ -2,14 +2,10 @@
2
2
 
3
3
  module Tramway
4
4
  module Form
5
- # Tailwind-styled date field
6
- class DateFieldComponent < TailwindComponent
7
- PICKER_ONCLICK = 'try { this.showPicker && this.showPicker() } catch (error) {}'
8
-
9
- private
10
-
11
- def picker_options(classes)
12
- @options.merge(class: classes, onclick: [@options[:onclick], PICKER_ONCLICK].compact.join('; '))
5
+ # Date field: the TimeMagic calendar popover (see DatetimePickerComponent)
6
+ class DateFieldComponent < DatetimePickerComponent
7
+ def mode
8
+ :date
13
9
  end
14
10
  end
15
11
  end
@@ -2,14 +2,10 @@
2
2
 
3
3
  module Tramway
4
4
  module Form
5
- # Tailwind-styled datetime field
6
- class DatetimeFieldComponent < TailwindComponent
7
- PICKER_ONCLICK = 'try { this.showPicker && this.showPicker() } catch (error) {}'
8
-
9
- private
10
-
11
- def picker_options(classes)
12
- @options.merge(class: classes, onclick: [@options[:onclick], PICKER_ONCLICK].compact.join('; '))
5
+ # Date and time field: the TimeMagic calendar and hour/minute popover (see DatetimePickerComponent)
6
+ class DatetimeFieldComponent < DatetimePickerComponent
7
+ def mode
8
+ :datetime
13
9
  end
14
10
  end
15
11
  end
@@ -0,0 +1,49 @@
1
+ %div{ class: default_container_classes }
2
+ - if @label
3
+ .tramway-form__field-label
4
+ - if @icon.present?
5
+ %span.tramway-form__label-icon{ aria: { hidden: true } }
6
+ %i{ class: @icon }
7
+ = component('tramway/form/label', for: @for) do
8
+ = @label
9
+ .tramway-form__control
10
+ .tramway-datetimepicker{ data: { controller: 'tramway-datetime-picker', tramway_datetime_picker_mode_value: mode, tramway_datetime_picker_step_value: step } }
11
+ = @input.call @attribute, **hidden_options
12
+ %button.tramway-datetimepicker__trigger.tramway-form__input{ type: 'button', popovertarget: popover_id, aria: { haspopup: 'dialog', expanded: 'false' },
13
+ data: { tramway_datetime_picker_target: 'trigger' } }
14
+ %i.fa.fa-calendar{ aria: { hidden: true } }
15
+ %span.tramway-datetimepicker__trigger-text{ data: { tramway_datetime_picker_target: 'display', placeholder: placeholder } }
16
+ = initial_display.presence || placeholder
17
+ %i.fa.fa-chevron-down.tramway-datetimepicker__trigger-caret{ aria: { hidden: true } }
18
+
19
+ %div.tramway-datetimepicker__popover{ id: popover_id, popover: 'auto', data: { tramway_datetime_picker_target: 'popover' } }
20
+ .tramway-datetimepicker__header
21
+ %button.tramway-button.tramway-button--ghost.tramway-button--sm.tramway-button--icon{ type: 'button', 'aria-label': t('tramway.pickers.previous_month'),
22
+ data: { tramway_datetime_picker_target: 'prev', action: 'click->tramway-datetime-picker#prevMonth' } }
23
+ %i.fa.fa-chevron-left{ aria: { hidden: true } }
24
+ %span.tramway-datetimepicker__month{ data: { tramway_datetime_picker_target: 'monthLabel' } }
25
+ %button.tramway-button.tramway-button--ghost.tramway-button--sm.tramway-button--icon{ type: 'button', 'aria-label': t('tramway.pickers.next_month'),
26
+ data: { tramway_datetime_picker_target: 'next', action: 'click->tramway-datetime-picker#nextMonth' } }
27
+ %i.fa.fa-chevron-right{ aria: { hidden: true } }
28
+
29
+ .tramway-datetimepicker__weekdays
30
+ - %w[Su Mo Tu We Th Fr Sa].each do |day|
31
+ %span= day
32
+
33
+ .tramway-datetimepicker__grid{ data: { tramway_datetime_picker_target: 'grid' } }
34
+
35
+ - if datetime_mode?
36
+ .tramway-datetimepicker__time
37
+ %span.tramway-datetimepicker__time-icon
38
+ %i.fa-solid.fa-clock{ aria: { hidden: true } }
39
+ %select.tramway-form__select.tramway-datetimepicker__time-select{ data: { tramway_datetime_picker_target: 'hour', action: 'change->tramway-datetime-picker#timeChanged' } }
40
+ %span.tramway-datetimepicker__time-sep :
41
+ %select.tramway-form__select.tramway-datetimepicker__time-select{ data: { tramway_datetime_picker_target: 'minute', action: 'change->tramway-datetime-picker#timeChanged' } }
42
+
43
+ .tramway-datetimepicker__footer
44
+ %button.tramway-datetimepicker__footer-link{ type: 'button', data: { action: 'click->tramway-datetime-picker#today' } }= t('tramway.pickers.today')
45
+ %button.tramway-datetimepicker__footer-link{ type: 'button', data: { action: 'click->tramway-datetime-picker#clear' } }= t('tramway.pickers.clear')
46
+ %button.tramway-button.tramway-button--primary.tramway-button--sm{ type: 'button', data: { action: 'click->tramway-datetime-picker#done' } }= t('tramway.pickers.done')
47
+
48
+ - if @hint.present?
49
+ %p{ class: form_hint_classes }= @hint
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tramway
4
+ module Form
5
+ # Date/time picker, exactly like the TimeMagic one: a hidden form field driven by a popover calendar (and, in
6
+ # `:datetime` mode, an inline hour/minute picker), built on the native Popover API. The `tramway-datetime-picker`
7
+ # Stimulus controller (`DatetimePicker` in `@tramway/tramway`) renders the calendar. `date_field` renders it in
8
+ # `:date` mode and `datetime_field` in `:datetime` mode.
9
+ class DatetimePickerComponent < TailwindComponent
10
+ MODES = %i[date datetime].freeze
11
+ DEFAULT_STEP = 5
12
+
13
+ option :mode, default: -> { :date }
14
+
15
+ def datetime_mode?
16
+ mode.to_sym == :datetime
17
+ end
18
+
19
+ def step
20
+ options.fetch(:step, DEFAULT_STEP)
21
+ end
22
+
23
+ def hidden_options
24
+ options.except(:step, :class, :onclick, :placeholder, :horizontal, :label, :include_seconds).merge(
25
+ id: @for,
26
+ value: initial_value,
27
+ data: (options[:data] || {}).merge(tramway_datetime_picker_target: 'input')
28
+ )
29
+ end
30
+
31
+ def initial_value
32
+ parsed_value&.strftime(datetime_mode? ? '%Y-%m-%dT%H:%M' : '%Y-%m-%d').to_s
33
+ end
34
+
35
+ def initial_display
36
+ time = parsed_value
37
+ return '' unless time
38
+
39
+ time.strftime(datetime_mode? ? '%b %-d, %Y, %H:%M' : '%b %-d, %Y')
40
+ end
41
+
42
+ def placeholder
43
+ options[:placeholder] || t(datetime_mode? ? 'tramway.pickers.select_date_time' : 'tramway.pickers.select_date')
44
+ end
45
+
46
+ def popover_id
47
+ "#{@for}_popover"
48
+ end
49
+
50
+ private
51
+
52
+ def parsed_value
53
+ return if @value.blank?
54
+ return @value.to_time if @value.respond_to?(:strftime)
55
+
56
+ Time.zone.parse(@value.to_s)
57
+ rescue ArgumentError
58
+ nil
59
+ end
60
+ end
61
+ end
62
+ end
@@ -1,9 +1,10 @@
1
1
  %div{ class: default_container_classes }
2
- - if @label
3
- - classes = file_button_classes
2
+ .tramway-form__control
3
+ - if @label
4
+ - classes = file_button_classes
4
5
 
5
- %label{ for: @for, class: classes }
6
- = @label
7
- = @input
8
- - if @hint.present?
9
- %p{ class: form_hint_classes }= @hint
6
+ %label{ for: @for, class: classes }
7
+ = @label
8
+ = @input
9
+ - if @hint.present?
10
+ %p{ class: form_hint_classes }= @hint
@@ -1,8 +1,13 @@
1
1
  %div{ class: default_container_classes }
2
2
  - if @label
3
- = component('tramway/form/label', for: @for, class: 'mb-0') do
4
- = @label
5
- - classes = text_input_classes
6
- = @input.call @attribute, **@options.merge(class: classes), value: @value
7
- - if @hint.present?
8
- %p{ class: form_hint_classes }= @hint
3
+ .tramway-form__field-label
4
+ - if @icon.present?
5
+ %span.tramway-form__label-icon{ aria: { hidden: true } }
6
+ %i{ class: @icon }
7
+ = component('tramway/form/label', for: @for) do
8
+ = @label
9
+ .tramway-form__control
10
+ - classes = text_input_classes
11
+ = @input.call @attribute, **@options.merge(class: classes), value: @value
12
+ - if @hint.present?
13
+ %p{ class: form_hint_classes }= @hint
@@ -1,7 +1,12 @@
1
1
  %div{ class: default_container_classes }
2
2
  - if @label
3
- = component('tramway/form/label', for: @for, class: 'mb-0') do
4
- = @label
5
- = @input.call @attribute, **rich_text_area_options
6
- - if @hint.present?
7
- %p{ class: form_hint_classes }= @hint
3
+ .tramway-form__field-label
4
+ - if @icon.present?
5
+ %span.tramway-form__label-icon{ aria: { hidden: true } }
6
+ %i{ class: @icon }
7
+ = component('tramway/form/label', for: @for) do
8
+ = @label
9
+ .tramway-form__control
10
+ = @input.call @attribute, **rich_text_area_options
11
+ - if @hint.present?
12
+ %p{ class: form_hint_classes }= @hint
@@ -1,8 +1,13 @@
1
1
  %div{ class: default_container_classes }
2
2
  - if @label
3
- = component('tramway/form/label', for: @for, class: 'mb-2') do
4
- = @label
5
- - classes = select_classes
6
- = @input.call(@attribute, @collection, { selected: @value }, @options.merge(class: classes))
7
- - if @hint.present?
8
- %p{ class: form_hint_classes }= @hint
3
+ .tramway-form__field-label
4
+ - if @icon.present?
5
+ %span.tramway-form__label-icon{ aria: { hidden: true } }
6
+ %i{ class: @icon }
7
+ = component('tramway/form/label', for: @for) do
8
+ = @label
9
+ .tramway-form__control
10
+ - classes = select_classes
11
+ = @input.call(@attribute, @collection, { selected: @value }, @options.merge(class: classes))
12
+ - if @hint.present?
13
+ %p{ class: form_hint_classes }= @hint
@@ -1,8 +1,13 @@
1
1
  %div{ class: default_container_classes }
2
2
  - if @label
3
- = component('tramway/form/label', for: @for, class: 'mb-0') do
4
- = @label
5
- - classes = text_area_classes
6
- = @input.call @attribute, **@options.merge(class: classes), value: @value
7
- - if @hint.present?
8
- %p{ class: form_hint_classes }= @hint
3
+ .tramway-form__field-label
4
+ - if @icon.present?
5
+ %span.tramway-form__label-icon{ aria: { hidden: true } }
6
+ %i{ class: @icon }
7
+ = component('tramway/form/label', for: @for) do
8
+ = @label
9
+ .tramway-form__control
10
+ - classes = text_area_classes
11
+ = @input.call @attribute, **@options.merge(class: classes), value: @value
12
+ - if @hint.present?
13
+ %p{ class: form_hint_classes }= @hint
@@ -1,8 +1,13 @@
1
1
  %div{ class: default_container_classes }
2
2
  - if @label
3
- = component('tramway/form/label', for: @for, class: 'mb-0') do
4
- = @label
5
- - classes = text_input_classes
6
- = @input.call @attribute, **@options.merge(class: classes), value: @value
7
- - if @hint.present?
8
- %p{ class: form_hint_classes }= @hint
3
+ .tramway-form__field-label
4
+ - if @icon.present?
5
+ %span.tramway-form__label-icon{ aria: { hidden: true } }
6
+ %i{ class: @icon }
7
+ = component('tramway/form/label', for: @for) do
8
+ = @label
9
+ .tramway-form__control
10
+ - classes = text_input_classes
11
+ = @input.call @attribute, **@options.merge(class: classes), value: @value
12
+ - if @hint.present?
13
+ %p{ class: form_hint_classes }= @hint
@@ -1,8 +1,13 @@
1
1
  %div{ class: default_container_classes }
2
2
  - if @label
3
- = component('tramway/form/label', for: @for, class: 'mb-0') do
4
- = @label
5
- - classes = text_input_classes
6
- = @input.call @attribute, **@options.merge(class: classes), value: @value
7
- - if @hint.present?
8
- %p{ class: form_hint_classes }= @hint
3
+ .tramway-form__field-label
4
+ - if @icon.present?
5
+ %span.tramway-form__label-icon{ aria: { hidden: true } }
6
+ %i{ class: @icon }
7
+ = component('tramway/form/label', for: @for) do
8
+ = @label
9
+ .tramway-form__control
10
+ - classes = text_input_classes
11
+ = @input.call @attribute, **@options.merge(class: classes), value: @value
12
+ - if @hint.present?
13
+ %p{ class: form_hint_classes }= @hint
@@ -1,15 +1,20 @@
1
- .relative{ class: default_container_classes }
1
+ %div{ class: default_container_classes }
2
2
  - if @label
3
- = component('tramway/form/label', for: @for, class: 'mb-0') do
4
- = @label
5
- %div{ role: :combobox, data: tramway_select_hash, id: "#{@for}_tramway_select", class: tramway_select_classes }
6
- - selected_items_classes = ['flex flex-row w-full', multiple ? 'flex-wrap gap-1 min-w-0' : 'flex-nowrap overflow-x-auto space-x-1'].join(' ')
7
- .flex.flex-end.justify-between.gap-1{ data: dropdown_data, **dropdown_options }
8
- %div{ class: selected_items_classes, data: { "tramway-select-target" => "showSelectedArea" } }
9
- .flex.flex-col.justify-center.shrink-0
10
- .caret-down.tramway-select__caret{ data: { "tramway-select-target" => "caretDown" } }
11
- = component 'tramway/form/tramway_select/caret', size:, direction: :down
12
- .caret-up.tramway-select__caret.hidden{ data: { "tramway-select-target" => "caretUp" } }
13
- = component 'tramway/form/tramway_select/caret', size:, direction: :up
14
- - if @hint.present?
15
- %p{ class: form_hint_classes }= @hint
3
+ .tramway-form__field-label
4
+ - if @icon.present?
5
+ %span.tramway-form__label-icon{ aria: { hidden: true } }
6
+ %i{ class: @icon }
7
+ = component('tramway/form/label', for: @for) do
8
+ = @label
9
+ .tramway-form__control.relative
10
+ %div{ role: :combobox, data: tramway_select_hash, id: "#{@for}_tramway_select", class: tramway_select_classes }
11
+ - selected_items_classes = ['flex flex-row w-full', multiple ? 'flex-wrap gap-1 min-w-0' : 'flex-nowrap overflow-x-auto space-x-1'].join(' ')
12
+ .flex.flex-end.justify-between.gap-1{ data: dropdown_data, **dropdown_options }
13
+ %div{ class: selected_items_classes, data: { "tramway-select-target" => "showSelectedArea" } }
14
+ .flex.flex-col.justify-center.shrink-0
15
+ .caret-down.tramway-select__caret{ data: { "tramway-select-target" => "caretDown" } }
16
+ = component 'tramway/form/tramway_select/caret', size:, direction: :down
17
+ .caret-up.tramway-select__caret.hidden{ data: { "tramway-select-target" => "caretUp" } }
18
+ = component 'tramway/form/tramway_select/caret', size:, direction: :up
19
+ - if @hint.present?
20
+ %p{ class: form_hint_classes }= @hint
@@ -1,7 +1,7 @@
1
1
  - url = @record.new_record? ? public_send(@entity.create_helper_method) : public_send(@entity.update_helper_method, @record.id)
2
2
  - method = @record.new_record? ? :post : :patch
3
3
 
4
- = tramway_form_for @record, local: true, url:, method: do |f|
4
+ = tramway_form_for @record, local: true, url:, method:, card: false do |f|
5
5
  .tramway-form
6
6
  - if @record.errors.any?
7
7
  .tramway-form__error-summary{ role: 'alert' }
@@ -1,5 +1,13 @@
1
1
  en:
2
2
  tramway:
3
+ pickers:
4
+ select_date: "Select date"
5
+ select_date_time: "Select date and time"
6
+ previous_month: "Previous month"
7
+ next_month: "Next month"
8
+ today: "Today"
9
+ clear: "Clear"
10
+ done: "Done"
3
11
  pages:
4
12
  new:
5
13
  title: "Create %{model_name}"
@@ -99,7 +99,7 @@ module Tramway
99
99
 
100
100
  def stimulus_controller_imports
101
101
  [
102
- 'import { Navbar, TramwaySelect, UiCheckbox, Tooltip, Collapsible } from "@tramway/tramway"'
102
+ 'import { Navbar, TramwaySelect, UiCheckbox, Tooltip, Collapsible, DatetimePicker } from "@tramway/tramway"'
103
103
  ]
104
104
  end
105
105
 
@@ -109,7 +109,8 @@ module Tramway
109
109
  "application.register('tramway-select', TramwaySelect)",
110
110
  "application.register('ui--checkbox', UiCheckbox)",
111
111
  "application.register('tramway-tooltip', Tooltip)",
112
- "application.register('tramway--collapsible', Collapsible)"
112
+ "application.register('tramway--collapsible', Collapsible)",
113
+ "application.register('tramway-datetime-picker', DatetimePicker)"
113
114
  ]
114
115
  end
115
116
 
@@ -303,7 +304,8 @@ module Tramway
303
304
  "application.register('table-row-preview'",
304
305
  "application.register('ui--checkbox'",
305
306
  "application.register('tramway-tooltip'",
306
- "application.register('tramway--collapsible'"
307
+ "application.register('tramway--collapsible'",
308
+ "application.register('tramway-datetime-picker'"
307
309
  ]
308
310
  end
309
311
 
@@ -11,15 +11,15 @@ module Tramway
11
11
 
12
12
  FORM_SIZES = %i[small medium large].freeze
13
13
 
14
- def tramway_form_for(object, *, size: :medium, **options, &)
15
- form_object_class = object.is_a?(Tramway::BaseForm) ? object.class : nil
16
-
17
- form_for(object, *, **options.merge(
18
- builder: Tramway::Form::Builder,
19
- size: normalize_form_size(size),
20
- form_object_class:,
21
- remote_submit: options[:remote] || false
22
- ), &)
14
+ # The form content is wrapped in the `tramway-form` card (TimeMagic form layout) unless `card: false` is passed
15
+ # or the form brings its own styling (`class:`, `html: { class: }`, `horizontal:`), e.g. an inline form.
16
+ def tramway_form_for(object, *, size: :medium, card: nil, **options, &)
17
+ card = [options[:class], options.dig(:html, :class), options[:horizontal]].none?(&:present?) if card.nil?
18
+
19
+ form_for(object, *, **tramway_form_options(object, size, options)) do |form|
20
+ content = capture(form, &)
21
+ card ? content_tag(:div, content, class: 'tramway-form') : content
22
+ end
23
23
  end
24
24
 
25
25
  def tramway_table(size: :medium, **options, &)
@@ -111,6 +111,15 @@ module Tramway
111
111
 
112
112
  private
113
113
 
114
+ def tramway_form_options(object, size, options)
115
+ options.merge(
116
+ builder: Tramway::Form::Builder,
117
+ size: normalize_form_size(size),
118
+ form_object_class: (object.class if object.is_a?(Tramway::BaseForm)),
119
+ remote_submit: options[:remote] || false
120
+ )
121
+ end
122
+
114
123
  def normalize_form_size(size)
115
124
  FORM_SIZES.include?(size) ? size : :medium
116
125
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tramway
4
- VERSION = '5.0.0.1'
4
+ VERSION = '5.0.0.2'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tramway
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0.1
4
+ version: 5.0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - kalashnikovisme
@@ -234,10 +234,10 @@ files:
234
234
  - app/components/tramway/form/builder.rb
235
235
  - app/components/tramway/form/checkbox_component.html.haml
236
236
  - app/components/tramway/form/checkbox_component.rb
237
- - app/components/tramway/form/date_field_component.html.haml
238
237
  - app/components/tramway/form/date_field_component.rb
239
- - app/components/tramway/form/datetime_field_component.html.haml
240
238
  - app/components/tramway/form/datetime_field_component.rb
239
+ - app/components/tramway/form/datetime_picker_component.html.haml
240
+ - app/components/tramway/form/datetime_picker_component.rb
241
241
  - app/components/tramway/form/file_field_component.html.haml
242
242
  - app/components/tramway/form/file_field_component.rb
243
243
  - app/components/tramway/form/label_component.html.haml
@@ -1,8 +0,0 @@
1
- %div{ class: default_container_classes }
2
- - if @label
3
- = component('tramway/form/label', for: @for, class: 'mb-0') do
4
- = @label
5
- - classes = text_input_classes
6
- = @input.call @attribute, **picker_options(classes), value: @value
7
- - if @hint.present?
8
- %p{ class: form_hint_classes }= @hint
@@ -1,8 +0,0 @@
1
- %div{ class: default_container_classes }
2
- - if @label
3
- = component('tramway/form/label', for: @for, class: 'mb-0') do
4
- = @label
5
- - classes = text_input_classes
6
- = @input.call @attribute, **picker_options(classes), value: @value
7
- - if @hint.present?
8
- %p{ class: form_hint_classes }= @hint