Module: Campo::Plugins::Aria::InstanceMethods::Convenience

Defined in:
lib/campo/plugins/aria.rb

Instance Method Summary collapse

Instance Method Details

#text(message, opts) ⇒ String #text(message-tuples, opts) ⇒ String #text(messages, opts) ⇒ String

Adds aria-describedby along with a span.

Overloads:

  • #text(message, opts) ⇒ String

    Returns A span with an id (and any options passed in as attributes) as Haml.

    Examples:

    text("postcode").describe("All in caps", class: "description")

    Parameters:

    • message (String)

      The text for the aria-describedby attribute.

    • options (optional, Hash)

      Any attributes for the span.

    Returns:

    • (String)

      A span with an id (and any options passed in as attributes) as Haml.

  • #text(message-tuples, opts) ⇒ String

    Returns A span with an id (and any options passed in as attributes) as Haml, wrapped around an unordered list with a list-item for each message, each list-item receiving the attributes passed in the tuple for it.

    Examples:

    text("Address").describe([["postcode",{class: "British"}],["zipcode", {class: "American"}]], class: "description")

    Parameters:

    • message-tuples (Array<Array<String,Hash>>)

      An array of tuples, each tuple containing the message string and an options hash for attributes.

    • options (optional, Hash)

      Any attributes for the span.

    Returns:

    • (String)

      A span with an id (and any options passed in as attributes) as Haml, wrapped around an unordered list with a list-item for each message, each list-item receiving the attributes passed in the tuple for it.

  • #text(messages, opts) ⇒ String

    Returns A span with an id (and any options passed in as attributes) as Haml, wrapped around an unordered list with a list-item for each message.

    Examples:

    text("Address").describe([["A valid address"],["Don't forget the postcode!"]])

    Parameters:

    • messages (Array<Array<String>>)

      An array of single valued arrays containing a string, the message.

    Returns:

    • (String)

      A span with an id (and any options passed in as attributes) as Haml, wrapped around an unordered list with a list-item for each message.

See Also:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/campo/plugins/aria.rb', line 39

def describe( message, opts={} )
  label, field = if self.kind_of? Campo::Label
    [self,self.fields.first]       
  elsif (parent = self.parent).kind_of? Campo::Label
    [parent, self]
  end
  
  span_id = "#{label.attributes[:for]}_description"
  
  if message.respond_to? :map
    # array
    span = Campo::Span.new( span_id, "%ul", opts )
    message.each do |(x,o)|
      o ||= {}     
      li = Campo.literal("%li",o) << Campo.literal(x)
      span.fields.first << li
    end
  else              
    span = Campo::Span.new( span_id, message, opts )
  end
    
  label.fields.unshift span 
  
  field.attributes[:"aria-describedby"] = span_id
  self
end