-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* nudger swiftui * typo in readme
- Loading branch information
Showing
20 changed files
with
237 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Backpack - Skyscanner's Design System | ||
* | ||
* Copyright 2018 Skyscanner Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import SwiftUI | ||
|
||
public struct BPKNudger: View { | ||
|
||
@State private var canIncrement = true | ||
@State private var canDecrement = true | ||
@Binding private var value: Int | ||
|
||
private var minValue: Int | ||
private var maxValue: Int | ||
private var step: Int | ||
|
||
/// Creates a `BPKNudger`. | ||
/// - Parameters: | ||
/// - value: The value of the `BPKNudger`. | ||
/// Must be between `min` and `max`. | ||
/// If changed, the value will be clamped to be between `min` and `max`. | ||
/// - min: The minimum value of the `BPKNudger`. | ||
/// Must be less than `max`. | ||
/// - max: The maximum value of the `BPKNudger`. | ||
/// Must be greater than `min`. | ||
/// - step: The step value of the `BPKNudger`. | ||
/// Defaults to `1`. | ||
public init(value: Binding<Int>, min: Int, max: Int, step: Int = 1) { | ||
minValue = min | ||
maxValue = max | ||
self.step = step | ||
self._value = value | ||
} | ||
|
||
public var body: some View { | ||
HStack(spacing: BPKSpacing.md.value) { | ||
BPKButton(icon: .minus, accessibilityLabel: "", enabled: $canDecrement, action: decrement) | ||
.buttonStyle(.secondary) | ||
BPKText("\(value)", style: .heading5) | ||
.frame(minWidth: minWidth) | ||
BPKButton(icon: .plus, accessibilityLabel: "", enabled: $canIncrement, action: increment) | ||
.buttonStyle(.secondary) | ||
} | ||
.accessibilityElement() | ||
.accessibilityValue(Text(String(value))) | ||
.accessibilityAdjustableAction { direction in | ||
switch direction { | ||
case .increment: increment() | ||
case .decrement: decrement() | ||
@unknown default: break | ||
} | ||
} | ||
.onAppear { | ||
if value < minValue || value > maxValue { | ||
value = max(min(value, maxValue), minValue) | ||
} | ||
updateButtonStates() | ||
} | ||
} | ||
|
||
private var minWidth: CGFloat { | ||
// Reserve space for 2 digits. 9 is one of the widest digits | ||
// in Relative so 99 is one of the widest two digit numbers possible | ||
let width = "99".size(withAttributes: [NSAttributedString.Key.font: BPKFontStyle.heading5.font]).width | ||
|
||
// Round the value up to the nearest value that aligns to our spacing grid: | ||
return ceil(width / BPKSpacing.md.value) * BPKSpacing.md.value | ||
} | ||
|
||
private func updateButtonStates() { | ||
canIncrement = value < maxValue | ||
canDecrement = value > minValue | ||
} | ||
|
||
private func increment() { | ||
value = min(value + step, maxValue) | ||
updateButtonStates() | ||
} | ||
|
||
private func decrement() { | ||
value = max(value - step, minValue) | ||
updateButtonStates() | ||
} | ||
} | ||
|
||
struct BPKNudger_Previews: PreviewProvider { | ||
static var previews: some View { | ||
VStack { | ||
BPKNudger(value: .constant(0), min: 0, max: 10) | ||
BPKNudger(value: .constant(5), min: 0, max: 10) | ||
BPKNudger(value: .constant(10), min: 0, max: 10) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Backpack-SwiftUI/Nudger | ||
|
||
[![Cocoapods](https://img.shields.io/cocoapods/v/Backpack-SwiftUI.svg?style=flat)](hhttps://cocoapods.org/pods/Backpack-SwiftUI) | ||
[![class reference](https://img.shields.io/badge/Class%20reference-iOS-blue)](https://backpack.github.io/ios/versions/latest/swiftui/Structs/BPKNudger.html) | ||
[![view on Github](https://img.shields.io/badge/Source%20code-GitHub-lightgrey)](https://github.com/Skyscanner/backpack-ios/tree/main/Backpack-SwiftUI/Nudger) | ||
|
||
## Default | ||
|
||
| Day | Night | | ||
| --- | --- | | ||
| <img src="https://raw.githubusercontent.com/Skyscanner/backpack-ios/main/screenshots/iPhone-swiftui_nudger___default_lm.png" alt="" width="375" /> |<img src="https://raw.githubusercontent.com/Skyscanner/backpack-ios/main/screenshots/iPhone-swiftui_nudger___default_dm.png" alt="" width="375" /> | | ||
|
||
# Usage | ||
|
||
an adjustable control that allows a user to select a numerical value between a min/max determined by the consumer. | ||
|
||
```swift | ||
@State var value = 0 | ||
|
||
BPKNudger(value: $value, min: 1, max: 10, step: 1) | ||
.accessibilityLabel("Passenger") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Backpack - Skyscanner's Design System | ||
* | ||
* Copyright 2018 Skyscanner Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import XCTest | ||
import SwiftUI | ||
@testable import Backpack_SwiftUI | ||
|
||
class BPKNudgerTests: XCTestCase { | ||
|
||
func test_validValue() { | ||
assertSnapshot( | ||
BPKNudger(value: .constant(1), min: 0, max: 10, step: 1) | ||
) | ||
} | ||
|
||
func test_minValue() { | ||
assertSnapshot( | ||
BPKNudger(value: .constant(0), min: 0, max: 10, step: 1) | ||
) | ||
} | ||
|
||
func test_maxValue() { | ||
assertSnapshot( | ||
BPKNudger(value: .constant(10), min: 0, max: 10, step: 1) | ||
) | ||
} | ||
} |
Binary file added
BIN
+2.9 KB
...k-SwiftUI/Tests/Nudger/__Snapshots__/BPKNudgerTests/test_maxValue.dark-mode.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.6 KB
...-SwiftUI/Tests/Nudger/__Snapshots__/BPKNudgerTests/test_maxValue.light-mode.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.53 KB
Backpack-SwiftUI/Tests/Nudger/__Snapshots__/BPKNudgerTests/test_maxValue.rtl.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.77 KB
...k-SwiftUI/Tests/Nudger/__Snapshots__/BPKNudgerTests/test_minValue.dark-mode.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.43 KB
...-SwiftUI/Tests/Nudger/__Snapshots__/BPKNudgerTests/test_minValue.light-mode.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.35 KB
Backpack-SwiftUI/Tests/Nudger/__Snapshots__/BPKNudgerTests/test_minValue.rtl.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.95 KB
...SwiftUI/Tests/Nudger/__Snapshots__/BPKNudgerTests/test_validValue.dark-mode.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.82 KB
...wiftUI/Tests/Nudger/__Snapshots__/BPKNudgerTests/test_validValue.light-mode.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.83 KB
Backpack-SwiftUI/Tests/Nudger/__Snapshots__/BPKNudgerTests/test_validValue.rtl.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
Example/Backpack/SwiftUI/Components/Nudger/NudgerExampleView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
/* | ||
* Backpack - Skyscanner's Design System | ||
* | ||
* Copyright © 2022 Skyscanner Ltd. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import SwiftUI | ||
import Backpack_SwiftUI | ||
|
||
struct NudgerExampleView: View { | ||
@State private var value = 0 | ||
|
||
var body: some View { | ||
BPKNudger(value: $value, min: 1, max: 10, step: 1) | ||
.accessibilityLabel("Passengers") | ||
} | ||
} | ||
|
||
struct NudgerExampleView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
NudgerExampleView() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.