-
-
Notifications
You must be signed in to change notification settings - Fork 810
Switch AccessibleButton and derivatives to using forwardRef
#12054
Changes from all commits
63fa415
1b09a8c
65b0dc5
1c59d58
1c1469e
0515c40
aca53a7
6dcccf9
c2c32eb
3699d29
612208c
eb9cd12
b4444e0
8d3a6ec
757f75f
e2ec5ff
596bec8
e972ed4
807a7ab
cd94693
afcef5a
b4ce004
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,7 +14,7 @@ | |||||||||||||
| limitations under the License. | ||||||||||||||
| */ | ||||||||||||||
|
|
||||||||||||||
| import React, { HTMLAttributes, InputHTMLAttributes } from "react"; | ||||||||||||||
| import React, { forwardRef, FunctionComponent, HTMLAttributes, InputHTMLAttributes, Ref } from "react"; | ||||||||||||||
| import classnames from "classnames"; | ||||||||||||||
|
|
||||||||||||||
| import { getKeyBindingsManager } from "../../../KeyBindingsManager"; | ||||||||||||||
|
|
@@ -66,7 +66,6 @@ type DynamicElementProps<T extends keyof JSX.IntrinsicElements> = Partial< | |||||||||||||
| * Extends props accepted by the underlying element specified using the `element` prop. | ||||||||||||||
| */ | ||||||||||||||
| type Props<T extends keyof JSX.IntrinsicElements> = DynamicHtmlElementProps<T> & { | ||||||||||||||
| inputRef?: React.Ref<Element>; | ||||||||||||||
| /** | ||||||||||||||
| * The base element type. "div" by default. | ||||||||||||||
| */ | ||||||||||||||
|
|
@@ -101,22 +100,26 @@ interface RenderedElementProps extends React.InputHTMLAttributes<Element> { | |||||||||||||
| * as a button. Identifies the element as a button, setting proper tab | ||||||||||||||
| * indexing and keyboard activation behavior. | ||||||||||||||
| * | ||||||||||||||
| * If a ref is passed, it will be forwarded to the rendered element as specified using the `element` prop. | ||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My understanding is that
Now: if you're saying that
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup I misread the typescript. (#include "I shouldn't have to reverse-engineer typescript types.rant") |
||||||||||||||
| * | ||||||||||||||
| * @param {Object} props react element properties | ||||||||||||||
| * @returns {Object} rendered react | ||||||||||||||
| */ | ||||||||||||||
| export default function AccessibleButton<T extends keyof JSX.IntrinsicElements>({ | ||||||||||||||
| element = "div" as T, | ||||||||||||||
| onClick, | ||||||||||||||
| children, | ||||||||||||||
| kind, | ||||||||||||||
| disabled, | ||||||||||||||
| inputRef, | ||||||||||||||
| className, | ||||||||||||||
| onKeyDown, | ||||||||||||||
| onKeyUp, | ||||||||||||||
| triggerOnMouseDown, | ||||||||||||||
| ...restProps | ||||||||||||||
| }: Props<T>): JSX.Element { | ||||||||||||||
| const AccessibleButton = forwardRef(function <T extends keyof JSX.IntrinsicElements>( | ||||||||||||||
t3chguy marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
| { | ||||||||||||||
| element = "div" as T, | ||||||||||||||
| onClick, | ||||||||||||||
| children, | ||||||||||||||
| kind, | ||||||||||||||
| disabled, | ||||||||||||||
| className, | ||||||||||||||
| onKeyDown, | ||||||||||||||
| onKeyUp, | ||||||||||||||
| triggerOnMouseDown, | ||||||||||||||
| ...restProps | ||||||||||||||
| }: Props<T>, | ||||||||||||||
| ref: Ref<HTMLElement>, | ||||||||||||||
| ): JSX.Element { | ||||||||||||||
| const newProps: RenderedElementProps = restProps; | ||||||||||||||
| if (disabled) { | ||||||||||||||
| newProps["aria-disabled"] = true; | ||||||||||||||
|
|
@@ -170,7 +173,7 @@ export default function AccessibleButton<T extends keyof JSX.IntrinsicElements>( | |||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Pass through the ref - used for keyboard shortcut access to some buttons | ||||||||||||||
| newProps.ref = inputRef; | ||||||||||||||
| newProps.ref = ref; | ||||||||||||||
|
|
||||||||||||||
| newProps.className = classnames("mx_AccessibleButton", className, { | ||||||||||||||
| mx_AccessibleButton_hasKind: kind, | ||||||||||||||
|
|
@@ -180,11 +183,13 @@ export default function AccessibleButton<T extends keyof JSX.IntrinsicElements>( | |||||||||||||
|
|
||||||||||||||
| // React.createElement expects InputHTMLAttributes | ||||||||||||||
| return React.createElement(element, newProps, children); | ||||||||||||||
| } | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| AccessibleButton.defaultProps = { | ||||||||||||||
| // Type assertion required due to forwardRef type workaround in react.d.ts | ||||||||||||||
| (AccessibleButton as FunctionComponent).defaultProps = { | ||||||||||||||
| role: "button", | ||||||||||||||
| tabIndex: 0, | ||||||||||||||
| }; | ||||||||||||||
| (AccessibleButton as FunctionComponent).displayName = "AccessibleButton"; | ||||||||||||||
|
|
||||||||||||||
| AccessibleButton.displayName = "AccessibleButton"; | ||||||||||||||
| export default AccessibleButton; | ||||||||||||||


Uh oh!
There was an error while loading. Please reload this page.