bdaralan / BDUIKnit

A Swift Package Manager packed with SwiftUI custom reusable UI components and extensions.
MIT License
23 stars 1 forks source link

Cancel not working on ModalTextField #1

Closed satan87 closed 4 years ago

satan87 commented 4 years ago

Hello,

Great work on the component, they're very nice and easy to use.

I believe there is a bug on the ModelTextField with the cancel option. Here how to reproduce it:

Thanks, Nicolas

bdaralan commented 4 years ago

Hey Nicolas, thank you for reporting the issue.

I have looked at it, it is not a bug but intended. The use case should be that you store your own text. The model is only used to update the view, developer should handle the data.

I will update the sample code soon, but here is a quick demo.

@State private var yourText = ""

@State private var textFieldModel = BDModalTextFieldModel()

func presentModalTextField() {

    // assign your text to the text field's text before present
    textFieldModel.text = yourText 

    textFieldModel.onCancel = {
        // ignore to update yourText and dismiss
        self.textFieldModel.isFirstResponder = false
        self.presentSheet = false
    }

    textFieldModel.onCommit = {
        // grab the text from the text field and dismiss
        self.yourText = self.textFieldModel.text 
        self.textFieldModel.isFirstResponder = false
        self.presentSheet = false
    }

    textFieldModel.onReturnKey = {
        // grab the text from the text field and dismiss
        self.yourText = self.textFieldModel.text
        self.textFieldModel.isFirstResponder = false
        self.presentSheet = false
    }
}
satan87 commented 4 years ago

Thank you for the quick answer .. it is working well thanks.