Skip to content

Commit

Permalink
Convert find/replace TextEditor attributes to modifiers (#945)
Browse files Browse the repository at this point in the history
* Convert findNavigator to modifier

Closes #329

* Convert findDisabled to modifier

Closes #330

* Convert replaceDisabled to modifier

Closes #331
  • Loading branch information
shadowfacts committed May 23, 2023
1 parent 4e81900 commit f3154c8
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// FindDisabledModifier.swift
// LiveViewNative
//
// Created by Shadowfacts on 5/19/2023.
//

import SwiftUI

/// Controls whether the find navigator is disabled.
///
/// Use this modifier with a ``TextEditor``:
///
/// ```html
/// <TextEditor value-binding="my_text" modifiers={find_disabled(@native, disabled: true)} />
/// ```
///
/// ## Arguments
/// * ``disabled``
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@available(iOS 16.0, *)
struct FindDisabledModifier: ViewModifier, Decodable {
/// Whether the find navigator is disabled or not.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let disabled: Bool

func body(content: Content) -> some View {
content
#if os(iOS)
.findDisabled(disabled)
#endif
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// FindNavigatorModifier.swift
// LiveViewNative
//
// Created by Shadowfacts on 5/19/2023.
//

import SwiftUI

/// Controls whether the find navigator is shown.
///
/// Use this modifier with a ``TextEditor``:
///
/// ```html
/// <TextEditor value-binding="my_text" modifiers={find_navigator(@native, is_presented: :show)} />
/// ```
///
/// ## Arguments
/// * ``isPresented``
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@available(iOS 16.0, *)
struct FindNavigatorModifier: ViewModifier, Decodable {
/// A binding that controls whether the find navigator is shown.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@LiveBinding private var isPresented: Bool

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self._isPresented = try LiveBinding(decoding: .isPresented, in: container)
}

func body(content: Content) -> some View {
content
#if os(iOS)
.findNavigator(isPresented: $isPresented)
#endif
}

enum CodingKeys: String, CodingKey {
case isPresented
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// ReplaceDisabledModifier.swift
// LiveViewNative
//
// Created by Shadowfacts on 5/19/2023.
//

import SwiftUI

/// Controls whether the replace is disabled (but not find).
///
/// Use this modifier with a ``TextEditor``:
///
/// ```html
/// <TextEditor value-binding="my_text" modifiers={replace_disabled(@native, disabled: true)} />
/// ```
///
/// ## Arguments
/// * ``disabled``
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@available(iOS 16.0, *)
struct ReplaceDisabledModifier: ViewModifier, Decodable {
/// Whether the find navigator is disabled or not.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let disabled: Bool

func body(content: Content) -> some View {
content
#if os(iOS)
.replaceDisabled(disabled)
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,9 @@ import SwiftUI
/// <TextEditor value-binding="my_text" phx-focus="editor_focused" />
/// ```
///
/// ## Attributes
/// - ``findDisabled``
/// - ``replaceDisabled``
/// ## Events
/// - ``focusEvent``
/// - ``blurEvent``
/// ## Bindings
/// - ``isFindPresented``
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
Expand All @@ -29,11 +24,6 @@ struct TextEditor: TextFieldProtocol {
@ObservedElement var element: ElementNode
@FormState var value: String?
@FocusState private var isFocused: Bool
/// The `find-presented` attribute is a live binding that controls whether the system find UI is presented.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@LiveBinding(attribute: "find-presented") private var isFindPresented = false

/// An event that fires when the text editor is focused.
#if swift(>=5.8)
Expand All @@ -45,26 +35,11 @@ struct TextEditor: TextFieldProtocol {
@_documentation(visibility: public)
#endif
@Event("phx-blur", type: "blur") var blurEvent
/// Whether find is disabled (defaults to false).
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@Attribute("find-disabled") private var findDisabled: Bool
/// Whether replace is disabled (defaults to false).
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@Attribute("replace-disabled") private var replaceDisabled: Bool

var body: some View {
#if os(iOS) || os(macOS)
SwiftUI.TextEditor(text: textBinding)
.focused($isFocused)
#if os(iOS)
.findNavigator(isPresented: $isFindPresented)
.findDisabled(findDisabled)
.replaceDisabled(replaceDisabled)
#endif
.onChange(of: isFocused, perform: handleFocus)
.preference(key: ProvidedBindingsKey.self, value: ["phx-focus", "phx-blur"])
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule LiveViewNativeSwiftUi.Modifiers.FindDisabled do
use LiveViewNativePlatform.Modifier

modifier_schema "find_disabled" do
field :disabled, :boolean, default: true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule LiveViewNativeSwiftUi.Modifiers.FindNavigator do
use LiveViewNativePlatform.Modifier

alias LiveViewNativeSwiftUi.Types.NativeBindingName

modifier_schema "find_navigator" do
field :is_presented, NativeBindingName
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule LiveViewNativeSwiftUi.Modifiers.ReplaceDisabled do
use LiveViewNativePlatform.Modifier

modifier_schema "replace_disabled" do
field :disabled, :boolean, default: true
end
end

0 comments on commit f3154c8

Please sign in to comment.