import SwiftUI // MARK: - 简化的选择器组件 struct SimplePickerView: View { let title: String let isRequired: Bool let selection: Binding let options: [T] let optionTitle: (T) -> String let icon: String? init( title: String, isRequired: Bool = false, selection: Binding, options: [T], optionTitle: @escaping (T) -> String, icon: String? = nil ) { self.title = title self.isRequired = isRequired self.selection = selection self.options = options self.optionTitle = optionTitle self.icon = icon } var body: some View { VStack(alignment: .leading, spacing: 8) { InputTitleView.required(title, icon: icon) Picker(title, selection: selection) { ForEach(options, id: \.self) { option in Text(optionTitle(option)).tag(option) } } .pickerStyle(.menu) .padding(.horizontal, 12) .padding(.vertical, 8) .background( RoundedRectangle(cornerRadius: 8) .fill(Color(.systemGray6)) ) } } } // MARK: - 常用的选择器类型 extension SimplePickerView { static func wifiEncryption( selection: Binding, icon: String? = "lock" ) -> SimplePickerView { SimplePickerView( title: "encryption_type".localized, selection: selection, options: WiFiInputView.WiFiEncryptionType.allCases, optionTitle: { $0.displayName }, icon: icon ) } static func socialPlatform( selection: Binding, icon: String? = "globe" ) -> SimplePickerView { SimplePickerView( title: "social_platform".localized, selection: selection, options: SocialInputView.SocialPlatform.allCases, optionTitle: { $0.displayName }, icon: icon ) } static func phoneType( selection: Binding, icon: String? = "phone" ) -> SimplePickerView { SimplePickerView( title: "phone_type".localized, selection: selection, options: PhoneInputView.PhoneInputType.allCases, optionTitle: { $0.displayName }, icon: icon ) } } #Preview { VStack(spacing: 16) { // WiFi加密类型选择器 SimplePickerView.wifiEncryption( selection: .constant(.wpa2) ) // 社交平台选择器 SimplePickerView.socialPlatform( selection: .constant(.instagram) ) // 电话类型选择器 SimplePickerView.phoneType( selection: .constant(.phone) ) } .padding() }