Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When a TextField is focused, SwiftUI initializes the View #1

Open
jeongHunE opened this issue Jan 24, 2024 · 0 comments
Open

When a TextField is focused, SwiftUI initializes the View #1

jeongHunE opened this issue Jan 24, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@jeongHunE
Copy link
Member

jeongHunE commented Jan 24, 2024

Description(Problem)

When a TextField is focused, SwiftUI initializes the signUpView every time

Code

An example is below

struct SignUpView<SVM: SignUpVM>: View {
    @EnvironmentObject private var signUpViewModel: SVM
    @State private var idInput: String = ""
    
    var body: some View {
        ScrollView {
            VStack(spacing: 25) {
                //MARK: - Default Input
                IdInputView<SVM>(idInput: $idInput)
                    .environmentObject(signUpViewModel)
            }
        }
    }
}

struct IdInputView<SVM: SignUpVM>: View {
    @EnvironmentObject private var signUpViewModel: SVM
    @Binding private var idInput: String
    
    var body: some View {
        VStack {
            SignUpInputView(text: $idInput,
                            inputState: Binding(
                                get: {
                                    self.signUpViewModel.defaultStates["id"] ?? .isInitial
                                }, set: { newValue in
                                    self.signUpViewModel.defaultStates["id"] = newValue
                                }
                            ),
                            header: "아이디", placeholder: "학번 또는 교직원 번호를 입력해 주세요", keyboardType: .numberPad)
        }
    }
}

struct SignUpInputView: View {
    @Binding private var text: String
    @Binding private var inputState: InputState
    
    var body: some View {
        VStack {
            HStack {
                Text(header).bold().font(.subheadline).foregroundColor(colorScheme == .light ? .black : .white)
                
                Spacer()
            }
            .offset(x: 16)
            
            RoundedRectangle(cornerRadius: 30)
                .fill(CustomColor.getSignUpInputGray(colorScheme))
                .frame(height: UIScreen.main.bounds.width * 0.13)
                .overlay {
                    switch style {
                    case .normal:
                        TextField(placeholder, text: $text)
                            .keyboardType(keyboardType)
                            .autocorrectionDisabled()
                            .padding()
@jeongHunE jeongHunE added the bug Something isn't working label Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant