moved underneath tabs class

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-05-24 17:39:15 -05:00
parent 38aff5a637
commit 1205fc7fad
2 changed files with 162 additions and 122 deletions

View File

@ -9,19 +9,44 @@ import Foundation
import VDSColorTokens import VDSColorTokens
import Combine import Combine
public class TabItem: View { extension Tabs {
open class TabItem: View {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Public Properties // MARK: - Public Properties
//-------------------------------------------------- //--------------------------------------------------
public var label: Label = Label() ///position of the tab
public var orientation: Tabs.Orientation = .horizontal { didSet { setNeedsUpdate() } } open var index: Int = 0
public var size: Tabs.Size = .medium { didSet { setNeedsUpdate() } }
public var indicatorPosition: Tabs.IndicatorPosition = .bottom { didSet { setNeedsUpdate() } } ///label to write out the text
public var onClick: (() -> Void)? { didSet { setNeedsUpdate() } } open var label: Label = Label()
public var width: CGFloat? { didSet { setNeedsUpdate() } }
public var selected: Bool = false { didSet { setNeedsUpdate() } } ///orientation of the tabs
public var text: String? { didSet { setNeedsUpdate() } } open var orientation: Tabs.Orientation = .horizontal { didSet { setNeedsUpdate() } }
///Size for tab
open var size: Tabs.Size = .medium { didSet { setNeedsUpdate() } }
///Text position left or center
open var textPosition: TextPosition = .left { didSet { setNeedsUpdate() } }
///Sets the Position of the Selected/Hover Border Accent for All Tabs.
open var indicatorPosition: Tabs.IndicatorPosition = .bottom { didSet { setNeedsUpdate() } }
///An optional callback that is called when this Tab is clicked. Passes parameters (tabIndex).
open var onClick: ((Int) -> Void)? { didSet { setNeedsUpdate() } }
///If provided, it will set fixed width for this Tab.
open var width: CGFloat? { didSet { setNeedsUpdate() } }
///If provided, it will set this Tab to the Active Tab on render.
open var selected: Bool = false { didSet { setNeedsUpdate() } }
///The text label of the tab.
open var text: String? { didSet { setNeedsUpdate() } }
///Minimum width for the tab
open var minWidth: CGFloat = 44.0 { didSet { setNeedsUpdate() } }
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Private Properties // MARK: - Private Properties
@ -58,7 +83,7 @@ public class TabItem: View {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Overrides // MARK: - Overrides
//-------------------------------------------------- //--------------------------------------------------
override public func setup() { open override func setup() {
super.setup() super.setup()
addSubview(label) addSubview(label)
backgroundColor = .clear backgroundColor = .clear
@ -67,35 +92,41 @@ public class TabItem: View {
label.translatesAutoresizingMaskIntoConstraints = false label.translatesAutoresizingMaskIntoConstraints = false
label.pinTrailing() label.pinTrailing()
labelTopConstraint = label.topAnchor.constraint(equalTo: topAnchor, constant: 0) labelTopConstraint = label.topAnchor.constraint(equalTo: topAnchor)
labelTopConstraint?.isActive = true labelTopConstraint?.isActive = true
labelBottomConstraint = label.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0) labelBottomConstraint = label.bottomAnchor.constraint(equalTo: bottomAnchor)
labelBottomConstraint?.isActive = true labelBottomConstraint?.isActive = true
labelLeadingConstraint = label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0) labelLeadingConstraint = label.leadingAnchor.constraint(equalTo: leadingAnchor)
labelLeadingConstraint?.isActive = true labelLeadingConstraint?.isActive = true
labelWidthConstraint = label.widthAnchor.constraint(greaterThanOrEqualToConstant: 44.0) let layoutGuide = UILayoutGuide()
addLayoutGuide(layoutGuide)
labelWidthConstraint = layoutGuide.widthAnchor.constraint(greaterThanOrEqualToConstant: 44.0)
labelWidthConstraint?.isActive = true labelWidthConstraint?.isActive = true
//activate the constraints
NSLayoutConstraint.activate([layoutGuide.topAnchor.constraint(equalTo: topAnchor),
layoutGuide.bottomAnchor.constraint(equalTo: bottomAnchor),
layoutGuide.leadingAnchor.constraint(equalTo: leadingAnchor),
layoutGuide.trailingAnchor.constraint(equalTo: trailingAnchor)])
publisher(for: UITapGestureRecognizer()) publisher(for: UITapGestureRecognizer())
.sink { [weak self] _ in .sink { [weak self] _ in
self?.onClick?() guard let self else { return }
self.onClick?(self.index)
}.store(in: &subscribers) }.store(in: &subscribers)
} }
public override func updateView() { open override func updateView() {
if orientation == .horizontal {
label.textPosition = .center
} else {
label.textPosition = .left
}
label.text = text label.text = text
label.textPosition = textPosition
label.textStyle = size.textStyle label.textStyle = size.textStyle
label.textColor = textColorConfiguration.getColor(self) label.textColor = textColorConfiguration.getColor(self)
labelWidthConstraint?.isActive = false labelWidthConstraint?.isActive = false
if let width { if let width {
labelWidthConstraint = label.widthAnchor.constraint(equalToConstant: width) labelWidthConstraint = label.widthAnchor.constraint(equalToConstant: width)
@ -124,7 +155,7 @@ public class TabItem: View {
setNeedsLayout() setNeedsLayout()
} }
public override func layoutSubviews() { open override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
removeBorders() removeBorders()
@ -134,7 +165,8 @@ public class TabItem: View {
if orientation == .horizontal { if orientation == .horizontal {
indicator = indicatorPosition.value indicator = indicatorPosition.value
} }
addBorder(side: indicator, width: indicatorWidth, color: indicatorColorConfiguration.getColor(self)) addBorder(side: indicator, width: indicatorWidth, color: indicatorColorConfiguration.getColor(self), offset: 1)
}
} }
} }
} }

View File

@ -7,14 +7,22 @@
import Foundation import Foundation
extension Tabs {
public struct TabModel { public struct TabModel {
///Text that goes in the Tab
public var text: String public var text: String
public var onClick: (() -> Void)?
///Click event when you click on a tab
public var onClick: ((Int) -> Void)?
///Width of the tab
public var width: CGFloat? public var width: CGFloat?
public init(text: String, onClick: (() -> Void)? = nil, width: CGFloat? = nil) { public init(text: String, onClick: ((Int) -> Void)? = nil, width: CGFloat? = nil) {
self.text = text self.text = text
self.onClick = onClick self.onClick = onClick
self.width = width self.width = width
} }
} }
}