Skip to content

Commit

Permalink
Adds autoFocus prop to FlyoutViewManager (microsoft#8417)
Browse files Browse the repository at this point in the history
* Adds autoFocus prop to FlyoutViewManager

Adds option to auto-focus the first focusable element in the Flyout via
the `autoFocus` prop.

Co-authored-by: Mykola Lando <[email protected]>

* Change files

Co-authored-by: Mykola Lando <[email protected]>
Co-authored-by: Andrew Coates <[email protected]>
  • Loading branch information
3 people authored and chrisglein committed Sep 22, 2021
1 parent 032901d commit 1671258
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Adds autoFocus prop to FlyoutViewManager",
"packageName": "react-native-windows",
"email": "[email protected]",
"dependentChangeType": "patch"
}
15 changes: 15 additions & 0 deletions vnext/Microsoft.ReactNative/Views/FlyoutViewManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class FlyoutShadowNode : public ShadowNodeBase {
float m_verticalOffset = 0;
bool m_isFlyoutShowOptionsSupported = false;
winrt::FlyoutShowOptions m_showOptions = nullptr;
bool m_autoFocus = false;

std::unique_ptr<TouchEventHandler> m_touchEventHanadler;
std::unique_ptr<PreviewKeyboardEventHandlerOnRoot> m_previewKeyboardEventHandlerOnRoot;
Expand Down Expand Up @@ -229,6 +230,17 @@ void FlyoutShadowNode::createView(const winrt::Microsoft::ReactNative::JSValueOb
}
}

if (m_autoFocus) {
if (const auto content = m_flyout.Content()) {
if (const auto elementToFocus = xaml::Input::FocusManager::FindFirstFocusableElement(content)) {
if (const auto uiManager = GetNativeUIManager(GetViewManager()->GetReactContext()).lock()) {
// NativeUIManager::focus is a no-op if the tag is not found
uiManager->focus(GetTag(elementToFocus));
}
}
}
}

flyoutPresenter.AllowFocusOnInteraction(false);
}
}
Expand Down Expand Up @@ -346,6 +358,8 @@ void FlyoutShadowNode::updateProperties(winrt::Microsoft::ReactNative::JSValueOb
}

m_flyout.LightDismissOverlayMode(overlayMode);
} else if (propertyName == "autoFocus") {
m_autoFocus = propertyValue.AsBoolean();
} else if (propertyName == "showMode") {
const auto showMode = json_type_traits<winrt::FlyoutShowMode>::parseJson(propertyValue);
m_flyout.ShowMode(showMode);
Expand Down Expand Up @@ -481,6 +495,7 @@ void FlyoutViewManager::GetNativeProps(const winrt::Microsoft::ReactNative::IJSV
React::WriteProperty(writer, L"target", L"number");
React::WriteProperty(writer, L"verticalOffset", L"number");
React::WriteProperty(writer, L"isOverlayEnabled", L"boolean");
React::WriteProperty(writer, L"autoFocus", L"boolean");
React::WriteProperty(writer, L"showMode", L"string");
React::WriteProperty(writer, L"shouldConstrainToRootBounds", L"boolean");
}
Expand Down
1 change: 1 addition & 0 deletions vnext/src/Libraries/Components/Flyout/FlyoutProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type ShowMode =
export interface IFlyoutProps extends ViewProps {
horizontalOffset?: number;
isLightDismissEnabled?: boolean;
autoFocus?: boolean;
shouldConstrainToRootBounds?: boolean;

/**
Expand Down

0 comments on commit 1671258

Please sign in to comment.