Skip to content

Commit

Permalink
Update app/lib/primer/forms/text_field.rb
Browse files Browse the repository at this point in the history
Co-authored-by: Cameron Dutro <[email protected]>
  • Loading branch information
bsatarnejad and camertron authored Jan 6, 2025
1 parent 4fc03e8 commit 1bbaa39
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions app/lib/primer/forms/text_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,40 @@ def auto_check_authenticity_token
end
end

def trailing_visual_render
def trailing_visual_component
return @trailing_visual_component if defined?(@trailing_visual_component)
visual = @input.trailing_visual
return unless visual

content = ActiveSupport::SafeBuffer.new # Use SafeBuffer for safe HTML concatenation

# Render icon if specified
content << render(Primer::Beta::Octicon.new(icon: visual[:icon], classes: "FormControl-input-trailingVisualIcon")) if visual[:icon]

# Render label if specified
content << render(Primer::Beta::Label.new(classes: "FormControl-input-trailingVisualLabel")) { visual[:label] } if visual[:label]
@trailing_visual_component = if (icon_arguments = visual[:icon])
icon_arguments[:classes] = class_names(
icon_arguments.delete(:classes),
"FormControl-input-trailingVisualIcon"
)

# Render counter if specified
content << render(Primer::Beta::Counter.new(count: visual[:counter], classes: "FormControl-input-trailingVisualCounter")) if visual[:counter]
Primer::Beta::Octicon.new(**icon_arguments)
elsif (label_arguments = visual[:label])
# Render label if specified
label_arguments[:classes] = class_names(
label_arguments.delete(:classes),
"FormControl-input-trailingVisualLabel"
)

# Render text if specified
content << content_tag(:span, visual[:text], class: "FormControl-input-trailingVisualText") if visual[:text]
text = label_arguments.delete(:text)
Primer::Beta::Label.new(**label_arguments).with_content(text)
elsif (counter_arguments = visual[:counter])
# Render counter if specified
counter_arguments[:classes] = class_names(
counter_arguments.delete(:classes),
"FormControl-input-trailingVisualCounter"
)

# Wrap in the trailing visual container
content_tag(:span, content, class: "FormControl-input-trailingVisualWrap")
Primer::Beta::Counter.new(**counter_arguments))
elsif (truncate_arguments = visual[:text])
# Render text if specified
text = truncate_arguments.delete(:text)
Primer::Beta::Truncate.new(**truncate_arguments).with_content(text)
end
end


Expand Down

0 comments on commit 1bbaa39

Please sign in to comment.