Skip to content

Commit ea17798

Browse files
authored
feat: Allow using WinUI 3 Controls (#342)
1 parent e366125 commit ea17798

File tree

7 files changed

+31
-21
lines changed

7 files changed

+31
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
| @react-native-picker/picker | react-native | react-native-windows |
1616
| --- | --- | --- |
17-
| master | 0.61+ | 0.64+ |
17+
| >= 2.0.0 | 0.61+ | 0.64+ |
1818
| >= 1.16.0 | 0.61+ | 0.61+ |
1919
| >= 1.2.0 | 0.60+ or 0.59+ with [Jetifier](https://www.npmjs.com/package/jetifier) | N/A |
2020
| >= 1.0.0 | 0.57 | N/A |

windows/ReactNativePicker/ReactNativePicker.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="16.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(SolutionDir)\ExperimentalFeatures.props" Condition="Exists('$(SolutionDir)\ExperimentalFeatures.props')" />
34
<Import Project="$(SolutionDir)\packages\Microsoft.Windows.CppWinRT.2.0.210312.4\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('$(SolutionDir)\packages\Microsoft.Windows.CppWinRT.2.0.210312.4\build\native\Microsoft.Windows.CppWinRT.props')" />
45
<PropertyGroup Label="Globals">
56
<CppWinRTOptimized>true</CppWinRTOptimized>

windows/ReactNativePicker/ReactPickerView.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,26 @@
77
#include "ReactPickerView.g.cpp"
88

99
#include <winrt/Windows.Foundation.Metadata.h>
10-
#include <winrt/Windows.UI.Xaml.Input.h>
10+
#include <UI.Xaml.Input.h>
1111

1212
namespace winrt {
1313
using namespace Microsoft::ReactNative;
1414
using namespace Windows::Foundation;
1515
using namespace Windows::Foundation::Metadata;
16-
using namespace Windows::UI;
17-
using namespace Windows::UI::Xaml;
18-
using namespace Windows::UI::Xaml::Controls;
19-
using namespace Windows::UI::Xaml::Input;
20-
using namespace Windows::UI::Xaml::Media;
16+
using namespace xaml;
17+
using namespace xaml::Controls;
18+
using namespace xaml::Input;
19+
using namespace xaml::Media;
2120
} // namespace winrt
2221

2322
namespace winrt::ReactNativePicker::implementation {
2423

24+
#ifdef USE_WINUI3
25+
const bool ReactPickerView::s_isEditableComboboxSupported = true;
26+
#else
2527
const bool ReactPickerView::s_isEditableComboboxSupported = winrt::ApiInformation::IsPropertyPresent(
26-
L"Windows.UI.Xaml.Controls.ComboBox", L"IsEditableProperty");
28+
L"Windows.UI.Xaml.Controls.ComboBox", L"IsEditableProperty");
29+
#endif
2730

2831
ReactPickerView::ReactPickerView(winrt::IReactContext const& reactContext) : m_reactContext(reactContext) {
2932
this->AllowFocusOnInteraction(true);
@@ -64,9 +67,13 @@ namespace winrt::ReactNativePicker::implementation {
6467
this->ClearValue(winrt::ComboBox::IsEditableProperty());
6568
}
6669
else {
67-
if (auto iComboBox6 = this->try_as<winrt::Controls::IComboBox6>()) {
68-
iComboBox6.IsEditable(propertyValue.AsBoolean());
69-
}
70+
#ifdef USE_WINUI3
71+
this->IsEditable(propertyValue.AsBoolean());
72+
#else
73+
if (auto iComboBox6 = this->try_as<winrt::Controls::IComboBox6>()) {
74+
iComboBox6.IsEditable(propertyValue.AsBoolean());
75+
}
76+
#endif
7077
}
7178
}
7279
}
@@ -191,7 +198,7 @@ namespace winrt::ReactNativePicker::implementation {
191198
}
192199
}
193200

194-
void ReactPickerView::UpdateComboBoxItemForegroundResource(winrt::FrameworkElement const& item, winrt::Windows::UI::Xaml::Media::Brush const& color) {
201+
void ReactPickerView::UpdateComboBoxItemForegroundResource(winrt::FrameworkElement const& item, xaml::Media::Brush const& color) {
195202
if (auto comboBoxItem = item.try_as<winrt::ComboBoxItem>()) {
196203
comboBoxItem.Foreground(color);
197204
}

windows/ReactNativePicker/ReactPickerView.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ namespace winrt::ReactNativePicker::implementation {
2222
// FUTURE: remove when we can require RS5+
2323
int32_t m_selectedIndex{ 0 };
2424
std::vector<winrt::hstring> m_itemValues;
25-
winrt::Windows::UI::Xaml::Media::Brush m_comboBoxColor{ nullptr };
26-
winrt::Windows::UI::Xaml::Controls::ComboBox::SelectionChanged_revoker m_selectionChangedRevoker{};
27-
winrt::Windows::UI::Xaml::Controls::ComboBox::DropDownClosed_revoker m_dropDownClosedRevoker{};
25+
xaml::Media::Brush m_comboBoxColor{ nullptr };
26+
xaml::Controls::ComboBox::SelectionChanged_revoker m_selectionChangedRevoker{};
27+
xaml::Controls::ComboBox::DropDownClosed_revoker m_dropDownClosedRevoker{};
2828

2929
void RegisterEvents();
3030
void RepopulateItems(winrt::Microsoft::ReactNative::JSValueArray const& items);
3131
void SetSelectedIndex(int index);
32-
void OnSelectionChanged(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::Controls::SelectionChangedEventArgs const& args);
33-
void UpdateComboBoxItemForegroundResource(winrt::Windows::UI::Xaml::FrameworkElement const& item, winrt::Windows::UI::Xaml::Media::Brush const& color);
32+
void OnSelectionChanged(winrt::Windows::Foundation::IInspectable const& sender, xaml::Controls::SelectionChangedEventArgs const& args);
33+
void UpdateComboBoxItemForegroundResource(xaml::FrameworkElement const& item, xaml::Media::Brush const& color);
3434
};
3535
} // namespace winrt::ReactNativePicker::implementation
3636

windows/ReactNativePicker/ReactPickerView.idl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
#include <NamespaceRedirect.h>
2+
13
namespace ReactNativePicker{
24

35
[default_interface]
4-
runtimeclass ReactPickerView : Windows.UI.Xaml.Controls.ComboBox {
6+
runtimeclass ReactPickerView : XAML_NAMESPACE.Controls.ComboBox {
57
ReactPickerView(Microsoft.ReactNative.IReactContext context);
68
void UpdateProperties(Microsoft.ReactNative.IJSValueReader reader);
79
};

windows/ReactNativePicker/ReactPickerViewManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace winrt {
1010
using namespace Microsoft::ReactNative;
1111
using namespace Windows::Foundation::Collections;
12-
using namespace Windows::UI::Xaml;
12+
using namespace xaml;
1313
}
1414

1515
namespace winrt::ReactNativePicker::implementation {

windows/ReactNativePicker/ReactPickerViewManager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace winrt::ReactNativePicker::implementation {
1818

1919
// IViewManager
2020
winrt::hstring Name() noexcept;
21-
winrt::Windows::UI::Xaml::FrameworkElement CreateView() noexcept;
21+
xaml::FrameworkElement CreateView() noexcept;
2222

2323
// IViewManagerWithReactContext
2424
winrt::Microsoft::ReactNative::IReactContext ReactContext() noexcept;
@@ -30,7 +30,7 @@ namespace winrt::ReactNativePicker::implementation {
3030
NativeProps() noexcept;
3131

3232
void UpdateProperties(
33-
winrt::Windows::UI::Xaml::FrameworkElement const& view,
33+
xaml::FrameworkElement const& view,
3434
winrt::Microsoft::ReactNative::IJSValueReader const& propertyMapReader) noexcept;
3535

3636
// IViewManagerWithExportedEventTypeConstants

0 commit comments

Comments
 (0)