|
| 1 | +// |
| 2 | +// File.swift |
| 3 | +// coenttb-web |
| 4 | +// |
| 5 | +// Created by Coen ten Thije Boonkkamp on 03/09/2024. |
| 6 | +// |
| 7 | + |
| 8 | +import HTML |
| 9 | + |
| 10 | +public struct Input<CodingKey: RawRepresentable>: HTML where CodingKey.RawValue == String { |
| 11 | + |
| 12 | + public let codingKey: CodingKey |
| 13 | + public let disabled: Disabled? |
| 14 | + public let form: HTMLAttributeTypes.Form.ID? |
| 15 | + public let type: HTMLElementTypes.Input.Variant |
| 16 | + public var style: Input.Style = .default |
| 17 | + |
| 18 | + public init( |
| 19 | + codingKey: CodingKey, |
| 20 | + disabled: Disabled? = nil, |
| 21 | + form: HTMLAttributeTypes.Form.ID? = nil, |
| 22 | + type: HTMLElementTypes.Input.Variant |
| 23 | + ) { |
| 24 | + self.codingKey = codingKey |
| 25 | + self.disabled = disabled |
| 26 | + self.form = form |
| 27 | + self.type = type |
| 28 | + } |
| 29 | + |
| 30 | + public var body: some HTML { |
| 31 | + style.transform( |
| 32 | + input.init( |
| 33 | + name: .init(codingKey.rawValue), |
| 34 | + disabled: disabled, |
| 35 | + form: form, |
| 36 | + type: type |
| 37 | + ) |
| 38 | + |
| 39 | + .id(codingKey.rawValue) |
| 40 | + ) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +extension Input { |
| 45 | + public enum Style { |
| 46 | + case `default` |
| 47 | + case outlined |
| 48 | + case filled |
| 49 | + case minimal |
| 50 | + case error |
| 51 | + case success |
| 52 | + |
| 53 | + @HTMLBuilder |
| 54 | + public func transform (_ html: some HTML)-> some HTML { |
| 55 | + switch self { |
| 56 | + case .default: |
| 57 | + html |
| 58 | + .padding(vertical: .px(14), horizontal: .px(10)) |
| 59 | + .border(width: .px(1), color: .gray900.withDarkColor(.gray100)) |
| 60 | + .backgroundColor(.white.withDarkColor(.black)) |
| 61 | + .color(.text.secondary) |
| 62 | + .borderRadius(.px(5)) |
| 63 | + |
| 64 | + case .outlined: |
| 65 | + html |
| 66 | + .padding(vertical: .px(14), horizontal: .px(10)) |
| 67 | + .border(width: .px(2), color: .blue500.withDarkColor(.blue400)) |
| 68 | + .backgroundColor(.transparent) |
| 69 | + .color(.text.primary) |
| 70 | + .borderRadius(.px(5)) |
| 71 | + |
| 72 | + case .filled: |
| 73 | + html |
| 74 | + .padding(vertical: .px(14), horizontal: .px(10)) |
| 75 | + .border(.none) |
| 76 | + .backgroundColor(.gray100.withDarkColor(.gray800)) |
| 77 | + .color(.text.primary) |
| 78 | + .borderRadius(.px(5)) |
| 79 | + |
| 80 | + case .minimal: |
| 81 | + html |
| 82 | + .padding(vertical: .px(8), horizontal: .px(4)) |
| 83 | + .border(.none) |
| 84 | + .backgroundColor(.transparent) |
| 85 | + .color(.text.primary) |
| 86 | + .borderBottom(.init(.px(5), .solid)) |
| 87 | + // .borderBottomColor(.color(.gray400.withDarkColor(.gray600))) |
| 88 | + |
| 89 | + case .error: |
| 90 | + html |
| 91 | + .padding(vertical: .px(14), horizontal: .px(10)) |
| 92 | + .border(width: .px(1), color: .red500.withDarkColor(.red400)) |
| 93 | + .backgroundColor(Color.red100.withDarkColor(.red900)) |
| 94 | + .color(.text.primary) |
| 95 | + .borderRadius(.px(5)) |
| 96 | + |
| 97 | + case .success: |
| 98 | + html |
| 99 | + .padding(vertical: .px(14), horizontal: .px(10)) |
| 100 | + .border(width: .px(1), color: .green500.withDarkColor(.green400)) |
| 101 | + .backgroundColor(.green100.withDarkColor(.green900)) |
| 102 | + .color(.text.primary) |
| 103 | + .borderRadius(.px(5)) |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | +extension Input { |
| 111 | + public func style(_ style: Style) -> Self { |
| 112 | + var copy = self |
| 113 | + copy.style = style |
| 114 | + return copy |
| 115 | + } |
| 116 | +} |
0 commit comments