From c499fa6b4b29b6b45098df9c9a94380cd2527352 Mon Sep 17 00:00:00 2001 From: Chris Lowis Date: Thu, 1 Aug 2024 14:53:09 +0100 Subject: [PATCH] Extract a tab_bar component --- app/helpers/tab_bar_helper.rb | 37 ++++++++++++++++++ app/views/shared/_tab_bar.html.erb | 9 +++++ app/views/users/show.html.erb | 23 +++-------- test/components/previews/tab_bar_preview.rb | 11 ++++++ test/helpers/tab_bar_helper_test.rb | 42 +++++++++++++++++++++ 5 files changed, 104 insertions(+), 18 deletions(-) create mode 100644 app/helpers/tab_bar_helper.rb create mode 100644 app/views/shared/_tab_bar.html.erb create mode 100644 test/components/previews/tab_bar_preview.rb create mode 100644 test/helpers/tab_bar_helper_test.rb diff --git a/app/helpers/tab_bar_helper.rb b/app/helpers/tab_bar_helper.rb new file mode 100644 index 00000000..febae158 --- /dev/null +++ b/app/helpers/tab_bar_helper.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +module TabBarHelper + def tab_bar_item(item) + content_tag( + :li, + safe_join([li_icon(item), li_content(item)]), + class: li_class(item) + ) + end + + private + + def li_content(item) + if item[:link] + link_to item[:title], item[:link] + else + item[:title] + end + end + + def li_class(item) + 'jam-tab-bar__selected' if item[:selected] + end + + def li_icon(item) + content_tag(:span, icon_svg.html_safe, class: 'icon') if item[:icon] # rubocop:disable Rails/OutputSafety + end + + def icon_svg + %( + + + + ) + end +end diff --git a/app/views/shared/_tab_bar.html.erb b/app/views/shared/_tab_bar.html.erb new file mode 100644 index 00000000..4e7ba8a4 --- /dev/null +++ b/app/views/shared/_tab_bar.html.erb @@ -0,0 +1,9 @@ +
+ +
diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 58d4aef9..69713b1c 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -1,22 +1,9 @@ <%= render('shared/box', title: 'My account') do %> -
- -
+ <%= render('shared/tab_bar', items: [ + { title: 'Personal', selected: true }, + Current.user.artists.map { |artist| { title: artist.name, link: edit_artist_path(artist) } }, + ( { title: 'Add artist', link: new_artist_path, icon: true } if policy(Artist).new? ) + ].flatten.compact) %>