Class: Shadcn::DatePickerComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/shadcn/date_picker_component.rb

Overview

Date Picker component - a button that opens a calendar popover Composition of Popover and Calendar components Matches shadcn/ui Date Picker component

Examples:

Basic date picker

<%= render Shadcn::DatePickerComponent.new %>

With selected date

<%= render Shadcn::DatePickerComponent.new(selected: Date.today) %>

With name for form submission

<%= render Shadcn::DatePickerComponent.new(name: "event[date]") %>

With date constraints

<%= render Shadcn::DatePickerComponent.new(
  min_date: Date.today,
  max_date: Date.today + 30.days
) %>

Constant Summary collapse

TRIGGER_CLASSES =
"inline-flex items-center justify-start whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 border border-input bg-background shadow-sm hover:bg-accent hover:text-accent-foreground h-9 px-4 py-2 w-[240px] pl-3 text-left"
PLACEHOLDER_CLASSES =
"text-muted-foreground"
CONTENT_CLASSES =
"p-0 w-auto"
MONTHS =
%w[January February March April May June July August September October November December].freeze

Instance Attribute Summary

Attributes inherited from BaseComponent

#class_name, #data, #html_options

Instance Method Summary collapse

Methods inherited from BaseComponent

#content

Methods included from Rails::Helpers::ClassNameHelper

#cn

Constructor Details

#initialize(selected: nil, month: nil, min_date: nil, max_date: nil, name: nil, disabled_dates: [], disabled_days_of_week: [], show_outside_days: true, week_starts_on: 0, placeholder: "Pick a date", format: :medium, disabled: false, **options) ⇒ DatePickerComponent

Returns a new instance of DatePickerComponent.

Parameters:

  • selected (Date, nil) (defaults to: nil)

    Currently selected date

  • month (Date, nil) (defaults to: nil)

    Month to display (defaults to current month)

  • min_date (Date, nil) (defaults to: nil)

    Minimum selectable date

  • max_date (Date, nil) (defaults to: nil)

    Maximum selectable date

  • name (String, nil) (defaults to: nil)

    Form field name for hidden input

  • disabled_dates (Array<Date>) (defaults to: [])

    Specific dates that cannot be selected

  • disabled_days_of_week (Array<Integer>) (defaults to: [])

    Days of week to disable (0=Sun, 6=Sat)

  • show_outside_days (Boolean) (defaults to: true)

    Whether to show days outside current month

  • week_starts_on (Integer) (defaults to: 0)

    First day of week (0=Sunday, 1=Monday, etc.)

  • placeholder (String) (defaults to: "Pick a date")

    Placeholder text when no date selected

  • format (Symbol) (defaults to: :medium)

    Date format (:short, :medium, :long)

  • disabled (Boolean) (defaults to: false)

    Whether the date picker is disabled



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/components/shadcn/date_picker_component.rb', line 42

def initialize(
  selected: nil,
  month: nil,
  min_date: nil,
  max_date: nil,
  name: nil,
  disabled_dates: [],
  disabled_days_of_week: [],
  show_outside_days: true,
  week_starts_on: 0,
  placeholder: "Pick a date",
  format: :medium,
  disabled: false,
  **options
)
  super(**options)
  @selected = selected
  @month = month || selected || Date.today
  @min_date = min_date
  @max_date = max_date
  @name = name
  @disabled_dates = disabled_dates
  @disabled_days_of_week = disabled_days_of_week
  @show_outside_days = show_outside_days
  @week_starts_on = week_starts_on
  @placeholder = placeholder
  @format = format
  @disabled = disabled
end

Instance Method Details

#callObject



72
73
74
# File 'app/components/shadcn/date_picker_component.rb', line 72

def call
  (:div, picker_content, picker_attributes)
end