Skip to content

Commit

Permalink
chore: unified textfield/textarea api to match config
Browse files Browse the repository at this point in the history
  • Loading branch information
vkaltyrin committed Jan 13, 2025
1 parent a54f635 commit 859f40a
Show file tree
Hide file tree
Showing 25 changed files with 165 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public extension TextAreaAppearance {
endContentColor: .surfaceInverseSolidPrimary.withOpacity(0.96),
textColorReadOnly: .surfaceInverseSolidPrimary.withOpacity(0.96),
captionColorReadOnly: .surfaceInverseSolidPrimary.withOpacity(0.56),
placeholderColorReadOnly: .textDefaultSecondary
counterColorReadOnly: .surfaceInverseSolidPrimary.withOpacity(0.56),
placeholderColorReadOnly: .textDefaultSecondary,
backgroundColorReadOnly: .surfaceDefaultTransparentPrimary
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import SwiftUI
import SDDSComponents

public enum SDDSTextAreaSize: String, TextAreaSizeConfiguration {

case large
case medium
case small
Expand Down Expand Up @@ -34,6 +35,10 @@ public enum SDDSTextAreaSize: String, TextAreaSizeConfiguration {
}
}

public var chipsPadding: CGFloat {
6
}

public var textBeforeLeadingPadding: CGFloat {
return 0
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,23 @@ public enum TextAreaChipSize: String, ChipSizeConfiguration {
}
}

public var borderStyle: ChipBorderStyle {
switch self {
case .large:
return .default(8)
case .medium:
return .default(6)
case .small:
return .default(4)
case .extraSmall:
return .default(2)
public func cornerRadius(style: ComponentShapeStyle) -> CGFloat {
switch style {
case .cornered:
switch self {
case .large:
return 8
case .medium:
return 6
case .small:
return 4
case .extraSmall:
return 2
}
case .pilled:
return height / 2
}
}

}

#Preview {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,23 @@ public enum TextFieldChipSize: ChipSizeConfiguration {
}
}

public var borderStyle: ChipBorderStyle {
switch self {
case .large:
return .default(8)
case .medium:
return .default(6)
case .small:
return .default(4)
case .extraSmall:
return .default(2)
public func cornerRadius(style: SDDSComponents.ComponentShapeStyle) -> CGFloat {
switch style {
case .cornered:
switch self {
case .large:
return 8
case .medium:
return 6
case .small:
return 4
case .extraSmall:
return 2
}
case .pilled:
return height / 2
}
}

}

#Preview {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public extension ColorToken {
}
}

public extension ShapeToken {
static var `default`: ShapeToken {
.default
}
}

public extension ColorToken {
func withOpacity(_ opacity: Double) -> ColorToken {
return ColorToken(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public struct ButtonAppearance {
public let size: ButtonSizeConfiguration

/**
Стиль формы кнопки (например, закругленная или овальная), определяемый `ButtonShapeStyle`.
Стиль формы кнопки (например, закругленная или овальная), определяемый `ComponentShapeStyle`.
*/
public let shapeStyle: ButtonShapeStyle
public let shapeStyle: ComponentShapeStyle

/**
Типографика для текста заголовка кнопки, определяемая `TypographyConfiguration`.
Expand Down Expand Up @@ -95,7 +95,7 @@ public struct ButtonAppearance {
*/
public init(
size: ButtonSizeConfiguration = DefaultButtonSize(),
shapeStyle: ButtonShapeStyle = .default,
shapeStyle: ComponentShapeStyle = .cornered,
titleTypography: TypographyConfiguration = .default,
titleColor: ButtonColor = ButtonColor(),
subtitleTypography: TypographyConfiguration = .default,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public protocol ButtonSizeConfiguration: SizeConfiguration, CustomDebugStringCon
/**
Радиус скругления углов кнопки.
*/
var cornerRadius: CGFloat { get }
func cornerRadius(style: ComponentShapeStyle) -> CGFloat

/**
Внутренние отступы кнопки.
Expand All @@ -48,11 +48,12 @@ public protocol ButtonSizeConfiguration: SizeConfiguration, CustomDebugStringCon
Горизонтальный промежуток между заголовком и подзаголовком кнопки.
*/
var titleHorizontalGap: CGFloat { get }

}

public struct DefaultButtonSize: ButtonSizeConfiguration {
public var height: CGFloat = 0
public var cornerRadius: CGFloat = 0
public func cornerRadius(style: ComponentShapeStyle) -> CGFloat { 0 }
public var paddings: EdgeInsets = .init()
public var iconSize: CGSize = .zero
public var spinnerSize: CGSize = .zero
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public extension ButtonAppearance {
)
}

func shapeStyle(_ shapeStyle: ButtonShapeStyle) -> ButtonAppearance {
func shapeStyle(_ shapeStyle: ComponentShapeStyle) -> ButtonAppearance {
return ButtonAppearance(
size: self.size,
shapeStyle: shapeStyle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,7 @@ public struct SDDSButton: View {

private extension SDDSButton {
var cornerRadius: CGFloat {
switch appearance.shapeStyle {
case .default:
return appearance.size.cornerRadius
case .pilled:
return appearance.size.height / 2
}
return appearance.size.cornerRadius(style: appearance.shapeStyle)
}

func currentColor(for buttonColor: ButtonColor) -> Color {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public struct ChipAppearance: Hashable {
public let imageTintColor: ColorToken
public let buttonTintColor: ColorToken
public let backgroundColor: ColorToken
public let shapeStyle: ComponentShapeStyle
public let disabledAlpha: CGFloat

public init(
Expand All @@ -30,6 +31,7 @@ public struct ChipAppearance: Hashable {
imageTintColor: ColorToken = .clearColor,
buttonTintColor: ColorToken = .clearColor,
backgroundColor: ColorToken = .clearColor,
shapeStyle: ComponentShapeStyle = .cornered,
disabledAlpha: CGFloat = 0
) {
self.size = size
Expand All @@ -38,6 +40,7 @@ public struct ChipAppearance: Hashable {
self.imageTintColor = imageTintColor
self.buttonTintColor = buttonTintColor
self.backgroundColor = backgroundColor
self.shapeStyle = shapeStyle
self.disabledAlpha = disabledAlpha
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import Foundation

/**
`ChipBorderStyle` определяет стиль границы для чипа.
`ComponentShapeStyle` определяет стиль границы для чипа.

- `default`: Стиль по умолчанию с заданным радиусом скругления.
- `cornered`: Стиль по умолчанию с заданным радиусом скругления.
- `pilled`: Закругленный стиль с максимальным радиусом скругления.
*/
public enum ChipBorderStyle: Hashable {
case `default`(_ cornerRadius: CGFloat)
public enum ComponentShapeStyle: Hashable {
case cornered
case pilled

public func hash(into hasher: inout Hasher) {
switch self {
case .default(let cornerRadius):
hasher.combine("default")
hasher.combine(cornerRadius)
case .cornered:
hasher.combine("cornered")
case .pilled:
hasher.combine("pilled")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public protocol ChipSizeConfiguration: SizeConfiguration, CustomDebugStringConve
var leadingInset: CGFloat { get }
var trailingInset: CGFloat { get }
var spacing: CGFloat { get }
var borderStyle: ChipBorderStyle { get }
var height: CGFloat { get }
func cornerRadius(style: ComponentShapeStyle) -> CGFloat
}

public struct ZeroChipSize: ChipSizeConfiguration {
Expand All @@ -28,8 +28,8 @@ public struct ZeroChipSize: ChipSizeConfiguration {
public var leadingInset: CGFloat { 0 }
public var trailingInset: CGFloat { 0 }
public var spacing: CGFloat { 0 }
public var borderStyle: ChipBorderStyle { .default(0) }
public var height: CGFloat { 0 }
public func cornerRadius(style: ComponentShapeStyle) -> CGFloat { 0 }
public var debugDescription: String { "ZeroChipSize" }
public init() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,7 @@ public struct SDDSChip: View {
}

private var borderRadius: CGFloat {
switch appearance.size.borderStyle {
case .default(let cornerRadius):
return cornerRadius
case .pilled:
return appearance.size.height / 2
}
return appearance.size.cornerRadius(style: appearance.shapeStyle)
}

private func handleRemove() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,15 +792,10 @@ public struct SDDSTextArea: View {
case .single:
return 0
case .multiple(_, let chips):
guard let chipSize = chips.first?.appearance.size else {
guard let chipAppearance = chips.first?.appearance else {
return 0
}
switch chipSize.borderStyle {
case .default(let radius):
return radius
case .pilled:
return chipSize.height / 2
}
return chipAppearance.size.cornerRadius(style: chipAppearance.shapeStyle)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -779,15 +779,10 @@ public struct SDDSTextField: View {
case .single:
return 0
case .multiple(_, let chips):
guard let chipSize = chips.first?.appearance.size else {
guard let chipAppearance = chips.first?.appearance else {
return 0
}
switch chipSize.borderStyle {
case .default(let radius):
return radius
case .pilled:
return chipSize.height / 2
}
return chipAppearance.size.cornerRadius(style: chipAppearance.shapeStyle)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ extension BasicButtonSize: ButtonSizeConfiguration {
{%- endfor %}
}
}

public var cornerRadius: CGFloat {
switch self {
{%- for variation, config in variations.size %}
case .{{ variation | sizeKey }}: return {{ config.shape.name | adjustedCornerRadius:config.shape.adjustment }}
{%- endfor %}

public func cornerRadius(style: ComponentShapeStyle) -> CGFloat {
switch style {
case .cornered:
switch self {
{%- for variation, config in variations.size %}
case .{{ variation | sizeKey }}: return {{ config.shape.name | adjustedCornerRadius:config.shape.adjustment }}
{%- endfor %}
}
case .pilled:
return height / 2
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ extension IconButtonSize: ButtonSizeConfiguration {
}
}

public var cornerRadius: CGFloat {
switch self {
{%- for variation, config in variations.size %}
case .{{ variation | sizeKey }}: return {{ config.shape.name | adjustedCornerRadius:config.shape.adjustment }}
{%- endfor %}
public func cornerRadius(style: ComponentShapeStyle) -> CGFloat {
switch style {
case .cornered:
switch self {
{%- for variation, config in variations.size %}
case .{{ variation | sizeKey }}: return {{ config.shape.name | adjustedCornerRadius:config.shape.adjustment }}
{%- endfor %}
}
case .pilled:
return height / 2
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ extension LinkButtonSize: ButtonSizeConfiguration {
}
}

public var cornerRadius: CGFloat {
0
}
public func cornerRadius(style: ComponentShapeStyle) -> CGFloat { 0 }

public var paddings: EdgeInsets {
switch self {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Foundation
import SwiftUI

public extension ColorToken {
/// Применяет альфу к токену путем умножения альфы к исходной альфе токена
func withAlpha(multipliedBy alpha: Double) -> ColorToken {
return ColorToken(
darkColor: self.darkColor.opacity(alpha),
lightColor: self.lightColor.opacity(alpha)
)
}
}
Loading

0 comments on commit 859f40a

Please sign in to comment.