Class: Keystone::Ui::InputComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/keystone/ui/input_component.rb

Constant Summary collapse

BASE_CLASSES =
"block w-full rounded-md border border-gray-300 bg-white px-3 py-2 text-sm text-gray-900 placeholder:text-gray-400 focus:outline-none focus:ring-1 dark:bg-zinc-900 dark:border-zinc-700 dark:text-white dark:placeholder:text-gray-500"
DISABLED_CLASSES =
"cursor-not-allowed bg-gray-50 text-gray-500 dark:bg-zinc-800 dark:text-gray-400"
TYPE_MAP =
{
  text: "text",
  number: "number",
  email: "email",
  password: "password",
  date: "date"
}.freeze
FOCUS_CLASSES =
"focus:border-accent-500 focus:ring-accent-500 dark:focus:border-accent-400 dark:focus:ring-accent-400"

Instance Method Summary collapse

Constructor Details

#initialize(name:, type: :text, value: nil, placeholder: nil, disabled: false, min: nil, max: nil, step: nil) ⇒ InputComponent

Returns a new instance of InputComponent.



18
19
20
21
22
23
24
25
26
27
# File 'app/components/keystone/ui/input_component.rb', line 18

def initialize(name:, type: :text, value: nil, placeholder: nil, disabled: false, min: nil, max: nil, step: nil)
  @name = name
  @type = type
  @value = value
  @placeholder = placeholder
  @disabled = disabled
  @min = min
  @max = max
  @step = step
end

Instance Method Details

#classesObject



31
32
33
34
35
# File 'app/components/keystone/ui/input_component.rb', line 31

def classes
  tokens = [ BASE_CLASSES, FOCUS_CLASSES ]
  tokens << DISABLED_CLASSES if @disabled
  tokens.join(" ")
end

#input_typeObject



37
38
39
# File 'app/components/keystone/ui/input_component.rb', line 37

def input_type
  TYPE_MAP.fetch(@type)
end

#tag_optionsObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/components/keystone/ui/input_component.rb', line 41

def tag_options
  options = {
    type: input_type,
    name: @name,
    class: classes
  }
  options[:value] = @value unless @value.nil?
  options[:placeholder] = @placeholder unless @placeholder.nil?
  options[:disabled] = true if @disabled
  options[:min] = @min unless @min.nil?
  options[:max] = @max unless @max.nil?
  options[:step] = @step unless @step.nil?
  options
end