added more structs for helping attribrutes/colors, etc..
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
7551519d2d
commit
61493d382d
@ -49,6 +49,20 @@ open class EntryField<ModelType: EntryFieldModel>: Control<ModelType> {
|
|||||||
// Sizes are from InVision design specs.
|
// Sizes are from InVision design specs.
|
||||||
public let containerSize = CGSize(width: 45, height: 45)
|
public let containerSize = CGSize(width: 45, height: 45)
|
||||||
|
|
||||||
|
internal let primaryColorConfig = DisabledSurfaceColorConfiguration<DefaultLabelModel>().with {
|
||||||
|
$0.disabled.lightColor = VDSColor.interactiveDisabledOnlight
|
||||||
|
$0.disabled.darkColor = VDSColor.interactiveDisabledOndark
|
||||||
|
$0.enabled.lightColor = VDSColor.elementsPrimaryOnlight
|
||||||
|
$0.enabled.darkColor = VDSColor.elementsPrimaryOndark
|
||||||
|
}
|
||||||
|
|
||||||
|
internal let secondaryColorConfig = DisabledSurfaceColorConfiguration<DefaultLabelModel>().with {
|
||||||
|
$0.disabled.lightColor = VDSColor.interactiveDisabledOnlight
|
||||||
|
$0.disabled.darkColor = VDSColor.interactiveDisabledOndark
|
||||||
|
$0.enabled.lightColor = VDSColor.elementsSecondaryOnlight
|
||||||
|
$0.enabled.darkColor = VDSColor.elementsSecondaryOndark
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Public Properties
|
// MARK: - Public Properties
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -73,6 +87,12 @@ open class EntryField<ModelType: EntryFieldModel>: Control<ModelType> {
|
|||||||
@Proxy(\.model.helperTextPlacement)
|
@Proxy(\.model.helperTextPlacement)
|
||||||
open var helperTextPlacement: HelperTextPlacement
|
open var helperTextPlacement: HelperTextPlacement
|
||||||
|
|
||||||
|
@Proxy(\.model.tooltipTitle)
|
||||||
|
open var tooltipTitle: String?
|
||||||
|
|
||||||
|
@Proxy(\.model.tooltipContent)
|
||||||
|
open var tooltipContent: String?
|
||||||
|
|
||||||
@Proxy(\.model.transparentBackground)
|
@Proxy(\.model.transparentBackground)
|
||||||
open var transparentBackground: Bool
|
open var transparentBackground: Bool
|
||||||
|
|
||||||
@ -121,15 +141,12 @@ open class EntryField<ModelType: EntryFieldModel>: Control<ModelType> {
|
|||||||
addSubview(stackView)
|
addSubview(stackView)
|
||||||
|
|
||||||
//create the wrapping view
|
//create the wrapping view
|
||||||
containerView.widthAnchor.constraint(equalToConstant: containerSize.width).isActive = true
|
heightConstraint = containerView.heightAnchor.constraint(greaterThanOrEqualToConstant: containerSize.height)
|
||||||
containerView.heightAnchor.constraint(equalToConstant: containerSize.height).isActive = true
|
|
||||||
|
|
||||||
heightConstraint = containerView.heightAnchor.constraint(equalToConstant: containerSize.height)
|
|
||||||
heightConstraint?.isActive = true
|
heightConstraint?.isActive = true
|
||||||
|
|
||||||
widthConstraint = containerView.widthAnchor.constraint(equalToConstant: containerSize.width)
|
widthConstraint = containerView.widthAnchor.constraint(equalToConstant: 0)
|
||||||
|
|
||||||
minWidthConstraint = containerView.widthAnchor.constraint(greaterThanOrEqualToConstant: containerSize.width)
|
minWidthConstraint = containerView.widthAnchor.constraint(greaterThanOrEqualToConstant: 0)
|
||||||
minWidthConstraint?.isActive = true
|
minWidthConstraint?.isActive = true
|
||||||
|
|
||||||
containerStackView.addArrangedSubview(containerView)
|
containerStackView.addArrangedSubview(containerView)
|
||||||
@ -138,10 +155,20 @@ open class EntryField<ModelType: EntryFieldModel>: Control<ModelType> {
|
|||||||
stackView.addArrangedSubview(errorLabel)
|
stackView.addArrangedSubview(errorLabel)
|
||||||
stackView.addArrangedSubview(successLabel)
|
stackView.addArrangedSubview(successLabel)
|
||||||
|
|
||||||
|
stackView.setCustomSpacing(4, after: titleLabel)
|
||||||
|
stackView.setCustomSpacing(8, after: containerStackView)
|
||||||
|
stackView.setCustomSpacing(8, after: errorLabel)
|
||||||
|
stackView.setCustomSpacing(8, after: successLabel)
|
||||||
|
|
||||||
stackView.topAnchor.constraint(equalTo: topAnchor).isActive = true
|
stackView.topAnchor.constraint(equalTo: topAnchor).isActive = true
|
||||||
stackView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
|
stackView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
|
||||||
stackView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
|
stackView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
|
||||||
stackView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
|
stackView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
|
||||||
|
|
||||||
|
titleLabel.textColorConfiguration = primaryColorConfig
|
||||||
|
successLabel.textColorConfiguration = primaryColorConfig
|
||||||
|
errorLabel.textColorConfiguration = primaryColorConfig
|
||||||
|
helperLabel.textColorConfiguration = secondaryColorConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
public override func reset() {
|
public override func reset() {
|
||||||
@ -154,7 +181,20 @@ open class EntryField<ModelType: EntryFieldModel>: Control<ModelType> {
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
open override func updateView(viewModel: ModelType) {
|
open override func updateView(viewModel: ModelType) {
|
||||||
|
|
||||||
titleLabel.set(with: viewModel.labelModel)
|
//update the title model if the required flag is false
|
||||||
|
var titleLabelModel = viewModel.labelModel
|
||||||
|
.addOptional(required: viewModel.required, colorConfiguration: secondaryColorConfig)
|
||||||
|
|
||||||
|
//tooltip action
|
||||||
|
if let toolTipTitle = viewModel.tooltipTitle, let toolTipContent = viewModel.tooltipContent {
|
||||||
|
let toolTipAction = PassthroughSubject<Void, Never>()
|
||||||
|
titleLabelModel = titleLabelModel.addToolTip(action: toolTipAction, colorConfiguration: primaryColorConfig)
|
||||||
|
toolTipAction.sink {
|
||||||
|
print("clicked Tool Tip: \rtitle:\(toolTipTitle)\rcontent:\(toolTipContent)")
|
||||||
|
}.store(in: &subscribers)
|
||||||
|
}
|
||||||
|
|
||||||
|
titleLabel.set(with: titleLabelModel)
|
||||||
|
|
||||||
//show error or success
|
//show error or success
|
||||||
if viewModel.showError, let errorLabelModel = viewModel.errorLabelModel {
|
if viewModel.showError, let errorLabelModel = viewModel.errorLabelModel {
|
||||||
@ -192,3 +232,83 @@ open class EntryField<ModelType: EntryFieldModel>: Control<ModelType> {
|
|||||||
layoutIfNeeded()
|
layoutIfNeeded()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension DefaultLabelModel {
|
||||||
|
public func addOptional(required: Bool, colorConfiguration: DisabledSurfaceColorConfiguration<Self>) -> DefaultLabelModel {
|
||||||
|
guard let text = text, !required else { return self}
|
||||||
|
let optionColorAttr = LabelAttributeColor(location: text.count + 2,
|
||||||
|
length: 8,
|
||||||
|
color: colorConfiguration.getColor(self))
|
||||||
|
|
||||||
|
let newText = "\(text) Optional"
|
||||||
|
return copyWith {
|
||||||
|
$0.text = newText
|
||||||
|
$0.attributes = [optionColorAttr]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public func addToolTip(action: PassthroughSubject<Void, Never>, colorConfiguration: DisabledSurfaceColorConfiguration<Self>) -> DefaultLabelModel {
|
||||||
|
guard let text = text else { return self}
|
||||||
|
var newAttributes = attributes ?? []
|
||||||
|
let newText = "\(text) "
|
||||||
|
let toolTip = ToolTipLabelAttribute(action: action,
|
||||||
|
location: newText.count,
|
||||||
|
length: 1,
|
||||||
|
tintColor: colorConfiguration.getColor(self))
|
||||||
|
newAttributes.append(toolTip)
|
||||||
|
return copyWith {
|
||||||
|
$0.text = newText
|
||||||
|
$0.attributes = newAttributes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct LabelSystemImageAttachement: LabelAttributeAttachment {
|
||||||
|
public var id = UUID()
|
||||||
|
public var location: Int
|
||||||
|
public var length: Int
|
||||||
|
public var imageName: String
|
||||||
|
public var frame: CGRect
|
||||||
|
public var tintColor: UIColor
|
||||||
|
public static func == (lhs: LabelSystemImageAttachement, rhs: LabelSystemImageAttachement) -> Bool {
|
||||||
|
lhs.isEqual(rhs)
|
||||||
|
}
|
||||||
|
|
||||||
|
public func isEqual(_ equatable: LabelSystemImageAttachement) -> Bool {
|
||||||
|
return id == equatable.id && range == equatable.range && imageName == equatable.imageName
|
||||||
|
}
|
||||||
|
|
||||||
|
public func getAttachment() throws -> NSTextAttachment {
|
||||||
|
let attachment = NSTextAttachment()
|
||||||
|
attachment.image = UIImage(systemName: imageName)?.withTintColor(tintColor)
|
||||||
|
attachment.bounds = frame
|
||||||
|
return attachment
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct ToolTipLabelAttribute: LabelAttributeActionable {
|
||||||
|
public var id = UUID()
|
||||||
|
public var accessibleText: String? = "Tool Tip"
|
||||||
|
public var action = PassthroughSubject<Void, Never>()
|
||||||
|
public var location: Int
|
||||||
|
public var length: Int
|
||||||
|
public var tintColor: UIColor
|
||||||
|
public func setAttribute(on attributedString: NSMutableAttributedString) {
|
||||||
|
let image = LabelSystemImageAttachement(location: location,
|
||||||
|
length: length,
|
||||||
|
imageName: "info.circle",
|
||||||
|
frame: .init(x: 0, y: -2, width: 13.3, height: 13.3),
|
||||||
|
tintColor: tintColor)
|
||||||
|
|
||||||
|
image.setAttribute(on: attributedString)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func == (lhs: ToolTipLabelAttribute, rhs: ToolTipLabelAttribute) -> Bool {
|
||||||
|
lhs.isEqual(rhs)
|
||||||
|
}
|
||||||
|
|
||||||
|
public func isEqual(_ equatable: ToolTipLabelAttribute) -> Bool {
|
||||||
|
return id == equatable.id && range == equatable.range
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -23,6 +23,8 @@ public protocol EntryFieldModel: Modelable, FormFieldable, Errorable {
|
|||||||
var transparentBackground: Bool { get set }
|
var transparentBackground: Bool { get set }
|
||||||
var width: CGFloat? { get set }
|
var width: CGFloat? { get set }
|
||||||
var maxLength: Int? { get set }
|
var maxLength: Int? { get set }
|
||||||
|
var tooltipTitle: String? { get set }
|
||||||
|
var tooltipContent: String? { get set }
|
||||||
}
|
}
|
||||||
|
|
||||||
extension EntryFieldModel {
|
extension EntryFieldModel {
|
||||||
@ -31,7 +33,7 @@ extension EntryFieldModel {
|
|||||||
var model = DefaultLabelModel()
|
var model = DefaultLabelModel()
|
||||||
model.textPosition = .left
|
model.textPosition = .left
|
||||||
model.typograpicalStyle = .BodySmall
|
model.typograpicalStyle = .BodySmall
|
||||||
model.text = labelText ?? ""
|
model.text = labelText
|
||||||
model.surface = surface
|
model.surface = surface
|
||||||
model.disabled = disabled
|
model.disabled = disabled
|
||||||
return model
|
return model
|
||||||
|
|||||||
@ -41,6 +41,8 @@ public struct DefaultTextEntryField: TextEntryFieldModel {
|
|||||||
public var width: CGFloat?
|
public var width: CGFloat?
|
||||||
public var maxLength: Int?
|
public var maxLength: Int?
|
||||||
|
|
||||||
|
public var tooltipTitle: String?
|
||||||
|
public var tooltipContent: String?
|
||||||
|
|
||||||
public var surface: Surface = .light
|
public var surface: Surface = .light
|
||||||
public var disabled: Bool = false
|
public var disabled: Bool = false
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user