Skip to content

Commit

Permalink
fix(TabsView): Allows consumers to select an item and deselect all (#328
Browse files Browse the repository at this point in the history
)

* fix(TabsView): Allows consummers to select an item and deselect all

* Run swiftformat

* Alex's feedback

* Run swiftformat

---------

Co-authored-by: crisrojas <crisrojas@users.noreply.github.com>
  • Loading branch information
crisrojas and crisrojas committed Jan 15, 2024
1 parent f01d0fd commit 1191096
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions Sources/Mistica/Components/Tabs/TabsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,21 @@ public extension TabsView {
self.selectTabItem(at: max(0, index - 1))
}
}

func selectTabItem(at row: Int) {
deselect()
let indexPath = IndexPath(item: row, section: 0)
firstIndexPathForSelectedItem = indexPath

if let tabItemView = collectionView.cellForItem(at: indexPath) as? TabItemViewCell {
tabItemView.showSelected()
}

collectionView.selectItem(at: indexPath, animated: true, scrollPosition: .centeredHorizontally)

let tabItem = tabsItems[row]
delegate?.tabsView(self, didSelectTab: tabItem)
}
}

// MARK: - Private
Expand Down Expand Up @@ -141,18 +156,10 @@ private extension TabsView {
])
}

func selectTabItem(at row: Int) {
let indexPath = IndexPath(item: row, section: 0)
firstIndexPathForSelectedItem = indexPath

if let tabItemView = collectionView.cellForItem(at: indexPath) as? TabItemViewCell {
tabItemView.showSelected()
func deselect() {
for indexPath in collectionView.indexPathsForSelectedItems ?? [] {
deselectTabItem(at: indexPath.row)
}

collectionView.selectItem(at: indexPath, animated: true, scrollPosition: .centeredHorizontally)

let tabItem = tabsItems[row]
delegate?.tabsView(self, didSelectTab: tabItem)
}

func deselectTabItem(at row: Int) {
Expand Down

0 comments on commit 1191096

Please sign in to comment.