moved underneath tabs class
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
38aff5a637
commit
1205fc7fad
@ -9,19 +9,44 @@ import Foundation
|
||||
import VDSColorTokens
|
||||
import Combine
|
||||
|
||||
public class TabItem: View {
|
||||
extension Tabs {
|
||||
|
||||
open class TabItem: View {
|
||||
//--------------------------------------------------
|
||||
// MARK: - Public Properties
|
||||
//--------------------------------------------------
|
||||
public var label: Label = Label()
|
||||
public var orientation: Tabs.Orientation = .horizontal { didSet { setNeedsUpdate() } }
|
||||
public var size: Tabs.Size = .medium { didSet { setNeedsUpdate() } }
|
||||
public var indicatorPosition: Tabs.IndicatorPosition = .bottom { didSet { setNeedsUpdate() } }
|
||||
public var onClick: (() -> Void)? { didSet { setNeedsUpdate() } }
|
||||
public var width: CGFloat? { didSet { setNeedsUpdate() } }
|
||||
public var selected: Bool = false { didSet { setNeedsUpdate() } }
|
||||
public var text: String? { didSet { setNeedsUpdate() } }
|
||||
///position of the tab
|
||||
open var index: Int = 0
|
||||
|
||||
///label to write out the text
|
||||
open var label: Label = Label()
|
||||
|
||||
///orientation of the tabs
|
||||
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
|
||||
@ -58,7 +83,7 @@ public class TabItem: View {
|
||||
//--------------------------------------------------
|
||||
// MARK: - Overrides
|
||||
//--------------------------------------------------
|
||||
override public func setup() {
|
||||
open override func setup() {
|
||||
super.setup()
|
||||
addSubview(label)
|
||||
backgroundColor = .clear
|
||||
@ -67,35 +92,41 @@ public class TabItem: View {
|
||||
label.translatesAutoresizingMaskIntoConstraints = false
|
||||
label.pinTrailing()
|
||||
|
||||
labelTopConstraint = label.topAnchor.constraint(equalTo: topAnchor, constant: 0)
|
||||
labelTopConstraint = label.topAnchor.constraint(equalTo: topAnchor)
|
||||
labelTopConstraint?.isActive = true
|
||||
|
||||
labelBottomConstraint = label.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0)
|
||||
labelBottomConstraint = label.bottomAnchor.constraint(equalTo: bottomAnchor)
|
||||
labelBottomConstraint?.isActive = true
|
||||
|
||||
labelLeadingConstraint = label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0)
|
||||
labelLeadingConstraint = label.leadingAnchor.constraint(equalTo: leadingAnchor)
|
||||
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
|
||||
|
||||
//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())
|
||||
.sink { [weak self] _ in
|
||||
self?.onClick?()
|
||||
guard let self else { return }
|
||||
self.onClick?(self.index)
|
||||
}.store(in: &subscribers)
|
||||
|
||||
}
|
||||
|
||||
public override func updateView() {
|
||||
if orientation == .horizontal {
|
||||
label.textPosition = .center
|
||||
} else {
|
||||
label.textPosition = .left
|
||||
}
|
||||
open override func updateView() {
|
||||
label.text = text
|
||||
label.textPosition = textPosition
|
||||
label.textStyle = size.textStyle
|
||||
label.textColor = textColorConfiguration.getColor(self)
|
||||
|
||||
labelWidthConstraint?.isActive = false
|
||||
if let width {
|
||||
labelWidthConstraint = label.widthAnchor.constraint(equalToConstant: width)
|
||||
@ -124,7 +155,7 @@ public class TabItem: View {
|
||||
setNeedsLayout()
|
||||
}
|
||||
|
||||
public override func layoutSubviews() {
|
||||
open override func layoutSubviews() {
|
||||
super.layoutSubviews()
|
||||
|
||||
removeBorders()
|
||||
@ -134,7 +165,8 @@ public class TabItem: View {
|
||||
if orientation == .horizontal {
|
||||
indicator = indicatorPosition.value
|
||||
}
|
||||
addBorder(side: indicator, width: indicatorWidth, color: indicatorColorConfiguration.getColor(self))
|
||||
addBorder(side: indicator, width: indicatorWidth, color: indicatorColorConfiguration.getColor(self), offset: 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,14 +7,22 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
public struct TabModel {
|
||||
extension Tabs {
|
||||
public struct TabModel {
|
||||
|
||||
///Text that goes in the Tab
|
||||
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 init(text: String, onClick: (() -> Void)? = nil, width: CGFloat? = nil) {
|
||||
public init(text: String, onClick: ((Int) -> Void)? = nil, width: CGFloat? = nil) {
|
||||
self.text = text
|
||||
self.onClick = onClick
|
||||
self.width = width
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user