Module: Tuile::Component::HasValue
- Included in:
- AbstractStringField, Checkbox, CheckboxGroup, ComboBox, IntegerField, RadioGroup
- Defined in:
- lib/tuile/component/has_value.rb,
sig/tuile.rbs
Overview
The value seam every input component shares: a settable/gettable #value
of any type, an #on_value_change listener, #empty?, and #clear. A
form (a future binder) drives a mix of field types uniformly through it,
not caring that a TextField's value is a String while another field's
is a domain object.
field.on_value_change = ->(v) { puts "now: #{v.inspect}" }
field.value = "hello" # fires the listener
field.clear # value = empty_value, fires again
The default #value=/#value keep the value in @value and are enough
for a component with nothing more natural — you get a repaint and the
listener for free. An includer whose value lives elsewhere overrides both
(AbstractStringField backs them with its text buffer). Override #empty_value
when the empty sentinel isn't nil (a text field's is "").
Implementation details
Deliberately smaller than Vaadin's HasValue: read-only,
required-indicator, the from-client/old-value event payload, and
converters all belong to the not-yet-built form layer, not here.
Instance Attribute Summary collapse
-
#on_value_change ⇒ Proc, ...
@return — one-arg callable fired with the new value whenever #value actually changes — never on a no-op set.
Instance Method Summary collapse
-
#clear ⇒ void
Resets #value to #empty_value.
-
#empty? ⇒ Boolean
@return — true iff #value equals #empty_value.
- #empty_value ⇒ Object
-
#focusable? ⇒ Boolean
Input fields are focusable by default (overrides #focusable?); a read-only display field could override back to
false. -
#value ⇒ Object
@return — the current value;
niluntil first set. -
#value=(new_value) ⇒ void
No-op (no repaint, no listener) when equal to the current value.
Instance Attribute Details
#on_value_change ⇒ Proc, ...
@return — one-arg callable fired with the new value whenever #value actually changes — never on a no-op set.
28 29 30 |
# File 'lib/tuile/component/has_value.rb', line 28 def on_value_change @on_value_change end |
Instance Method Details
#clear ⇒ void
This method returns an undefined value.
Resets #value to #empty_value.
49 |
# File 'lib/tuile/component/has_value.rb', line 49 def clear = (self.value = empty_value) |
#empty? ⇒ Boolean
@return — true iff #value equals #empty_value.
45 |
# File 'lib/tuile/component/has_value.rb', line 45 def empty? = value == empty_value |
#empty_value ⇒ Object
53 |
# File 'lib/tuile/component/has_value.rb', line 53 def empty_value = nil |
#focusable? ⇒ Boolean
Input fields are focusable by default (overrides Tuile::Component#focusable?);
a read-only display field could override back to false. Only
focusable? lives here — tab_stop? diverges between leaf fields and
composing wrappers, so it stays per-class (DECISIONS.md
D-integer-field).
61 |
# File 'lib/tuile/component/has_value.rb', line 61 def focusable? = true |
#value ⇒ Object
@return — the current value; nil until first set.
31 |
# File 'lib/tuile/component/has_value.rb', line 31 def value = @value |
#value=(new_value) ⇒ void
This method returns an undefined value.
No-op (no repaint, no listener) when equal to the current value.
@param new_value
36 37 38 39 40 41 42 |
# File 'lib/tuile/component/has_value.rb', line 36 def value=(new_value) return if value == new_value @value = new_value invalidate on_value_change&.call(new_value) end |