From 17f9240c7e9c8f200c9b5953b877e84d8a74db8a Mon Sep 17 00:00:00 2001 From: Vasavi Kanamarlapudi Date: Mon, 15 Jul 2024 15:05:57 +0530 Subject: [PATCH] Digital ACT-191 ONEAPP-9311 story: added size configuration property that includes large and small --- .../InputStepper/InputStepper.swift | 146 +++++++++++------- 1 file changed, 91 insertions(+), 55 deletions(-) diff --git a/VDS/Components/InputStepper/InputStepper.swift b/VDS/Components/InputStepper/InputStepper.swift index d9b4b6b8..71a3c766 100644 --- a/VDS/Components/InputStepper/InputStepper.swift +++ b/VDS/Components/InputStepper/InputStepper.swift @@ -32,28 +32,30 @@ open class InputStepper: EntryFieldBase { //-------------------------------------------------- // MARK: - Enums //-------------------------------------------------- - - /// Enum used to describe the control width of Input Stepper. - public enum controlWidth: String, CaseIterable { - case auto, value - } - /// Enum used to describe the size of Input Stepper. public enum Size: String, CaseIterable { case large, small } - + //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- - /// Accepts a string or number value to control the width of input stepper to a fixed pixel or percentage value. - open var controlWidth: CGFloat? { didSet { setNeedsUpdate() } } + open var controlWidth: String? { + get { _controlWidth } + set { + setControlWidth(newValue) + setNeedsUpdate() + } + } + + /// Default value of the input stepper, defaults to '0'. + open var defaultValue:Int = 0 { didSet { setNeedsUpdate() } } /// Maximum value of the input stepper, defaults to '99'. open var maxValue: Int? { get { return _maxValue } set { - if let newValue, newValue < 100 && newValue > 0 { + if let newValue, newValue <= 99 && newValue > 0 { _maxValue = newValue } else { _maxValue = 99 @@ -80,7 +82,7 @@ open class InputStepper: EntryFieldBase { get { return _size } set { _size = newValue - setNeedsUpdate() + updateSize() } } @@ -90,13 +92,38 @@ open class InputStepper: EntryFieldBase { //-------------------------------------------------- // MARK: - Private Properties //-------------------------------------------------- + internal var _controlWidth = "auto" internal var _maxValue: Int = 99 internal var _minValue: Int = 0 internal var _size: Size = .large - - private var stepperWidthConstraint: NSLayoutConstraint? - private var stepperHeightConstraint: NSLayoutConstraint? + private var largeMinWidth = 121 + private var smallMinWidth = 90 + + let decrementButton = ButtonIcon().with { + $0.kind = .ghost + $0.iconName = Icon.Name(name: "minus") + $0.iconOffset = .init(x: -2, y: 0) + $0.customContainerSize = 32 + $0.icon.customSize = 16 + $0.backgroundColor = .clear + } + + let incrementButton = ButtonIcon().with { + $0.kind = .ghost + $0.iconName = Icon.Name(name: "plus") + $0.iconOffset = .init(x: 2, y: 0) + $0.customContainerSize = 32 + $0.icon.customSize = 16 + $0.backgroundColor = .clear + } + + let textLabel = Label().with { + $0.setContentCompressionResistancePriority(.required, for: .vertical) + $0.textStyle = .boldBodyLarge + $0.backgroundColor = .clear + } + //-------------------------------------------------- // MARK: - Lifecycle //-------------------------------------------------- @@ -108,8 +135,7 @@ open class InputStepper: EntryFieldBase { super.setup() isAccessibilityElement = false accessibilityLabel = "Input Stepper" - - containerView.isEnabled = false + containerView.isEnabled = false } open override func getFieldContainer() -> UIView { @@ -120,50 +146,60 @@ open class InputStepper: EntryFieldBase { $0.spacing = VDSLayout.space3X $0.backgroundColor = .clear } - let view = View().with { - $0.clipsToBounds = true - $0.backgroundColor = .clear - } - let controlView = View().with { - $0.clipsToBounds = true - $0.backgroundColor = .clear - } - - let label = Label().with { - $0.text = "Label" - } - - let minusButton = ButtonIcon().with { - $0.kind = .ghost - $0.iconName = .leftCaret - $0.iconOffset = .init(x: -2, y: 0) - $0.customContainerSize = 40 - $0.icon.customSize = 16 - } - - let plusButton = ButtonIcon().with { - $0.kind = .ghost - $0.iconName = .rightCaret - $0.iconOffset = .init(x: 2, y: 0) - $0.customContainerSize = 40 - $0.icon.customSize = 16 - } - - controlStackView.addArrangedSubview(minusButton) - controlStackView.addArrangedSubview(label) - controlStackView.addArrangedSubview(plusButton) - controlView.addSubview(controlStackView) - controlStackView.pinToSuperView() - - view.addSubview(controlView) - controlView.pinToSuperView() - view.pinToSuperView() - return view + controlStackView.addArrangedSubview(decrementButton) + controlStackView.addArrangedSubview(textLabel) + controlStackView.addArrangedSubview(incrementButton) + return controlStackView } open override func updateView() { super.updateView() updateContainerView(flag: false) + textLabel.text = String(defaultValue) + " " + (trailingText ?? "") + decrementButton.surface = surface + incrementButton.surface = surface + textLabel.surface = surface + statusIcon.isHidden = true } + + /// Resets to default settings. + open override func reset() { + super.reset() + textLabel.reset() + textLabel.textStyle = .boldBodyLarge + textLabel.text = "" + controlWidth = nil + minValue = nil + maxValue = nil + trailingText = nil + helperTextPlacement = .bottom + } + + //-------------------------------------------------- + // MARK: - Private Methods + //-------------------------------------------------- + internal func updateSize() { + let value = size == .large ? 6.0 : VDSLayout.space1X + updateConstraintsToFieldStackView(value: value) + +// textLabel.textStyle = size == .large ? .boldBodyLarge : .boldBodySmall +// textLabel.heightAnchor.constraint(equalToConstant: size == .large ? 44 : 32).activate() + +// decrementButton.customContainerSize = size == .large ? 32 : 24 +// incrementButton.customContainerSize = size == .large ? 32 : 24 + + } + + internal func setControlWidth(_ text: String?) { + if let text, text == "auto" { + // Set fixed width relative to default value, trailing text label + } else if let controlWidth = Int(text ?? "") { + // Use provided new width either pixel or percentage + width = CGFloat(controlWidth) + } else { + // Use EntryFieldBase width + } + } + }