|
| 1 | +import SwiftSoup |
| 2 | + |
| 3 | +/// A control that allows the user to select a value from a range. |
| 4 | +/// |
| 5 | +/// This represents an HTML range input element that provides a slider |
| 6 | +/// interface for selecting a numeric value within a specified range. |
| 7 | +/// |
| 8 | +/// ```swift |
| 9 | +/// // Basic slider with default range (0-100) |
| 10 | +/// Slider() |
| 11 | +/// |
| 12 | +/// // Slider with custom range |
| 13 | +/// Slider(min: 0, max: 10) |
| 14 | +/// |
| 15 | +/// // Slider with initial value and step |
| 16 | +/// Slider(value: 5, min: 0, max: 100, step: 5) |
| 17 | +/// |
| 18 | +/// // Slider with form association |
| 19 | +/// Slider(name: "volume", value: 50, min: 0, max: 100) |
| 20 | +/// ``` |
| 21 | +/// |
| 22 | +/// - SeeAlso: W3C [input type="range"](https://html.spec.whatwg.org/multipage/input.html#range-state-(type=range)) specification. |
| 23 | +@available(iOS 17.0, macOS 14.0, *) |
| 24 | +public struct Slider: View { |
| 25 | + /// Creates a slider input. |
| 26 | + /// |
| 27 | + /// - Parameters: |
| 28 | + /// - name: The name of the form control, as used in form submission. |
| 29 | + /// - value: The default numeric value of the slider. If not specified, |
| 30 | + /// the default is the midpoint between min and max. |
| 31 | + /// - min: The minimum value of the range. Defaults to 0. |
| 32 | + /// - max: The maximum value of the range. Defaults to 100. |
| 33 | + /// - step: The granularity of the value. If not specified, defaults to 1. |
| 34 | + /// - id: The unique identifier for the slider, used for label association. |
| 35 | + /// - autoFocus: If true, indicates that the slider should be focused as soon as |
| 36 | + /// the page is loaded, allowing the user to interact with it without having to |
| 37 | + /// manually focus it first. |
| 38 | + public init(name: String? = nil, value: Double? = nil, min: Double = 0, max: Double = 100, step: Double? = nil, id: String? = nil, autoFocus: Bool = false) { |
| 39 | + self.name = name |
| 40 | + self.value = value |
| 41 | + self.min = min |
| 42 | + self.max = max |
| 43 | + self.step = step |
| 44 | + self.id = id |
| 45 | + self.autoFocus = autoFocus |
| 46 | + } |
| 47 | + |
| 48 | + @_documentation(visibility: private) |
| 49 | + public func render(_ container: Element, environment: EnvironmentValues) throws { |
| 50 | + let element = try container.appendElement("input") |
| 51 | + try element.attr("type", "range") |
| 52 | + |
| 53 | + if let name { |
| 54 | + try element.attr("name", name) |
| 55 | + } |
| 56 | + if let value { |
| 57 | + try element.attr("value", String(value)) |
| 58 | + } |
| 59 | + try element.attr("min", String(min)) |
| 60 | + try element.attr("max", String(max)) |
| 61 | + if let step { |
| 62 | + try element.attr("step", String(step)) |
| 63 | + } |
| 64 | + if let id { |
| 65 | + try element.attr("id", id) |
| 66 | + } |
| 67 | + if autoFocus { |
| 68 | + try element.attr("autofocus", "") |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private let name: String? |
| 73 | + private let value: Double? |
| 74 | + private let min: Double |
| 75 | + private let max: Double |
| 76 | + private let step: Double? |
| 77 | + private let id: String? |
| 78 | + private let autoFocus: Bool |
| 79 | +} |
0 commit comments