|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +import type {ViewProps} from '../../../../Libraries/Components/View/ViewPropTypes'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Converts the web-style accessibility props (`aria-*`, `id`, `tabIndex`) to |
| 15 | + * the props the native side reads, the same way `View` does. For components |
| 16 | + * that render a native component directly instead of a `View`: without it, |
| 17 | + * the native side drops these props. |
| 18 | + */ |
| 19 | +export default function resolveAriaProps<T extends {...ViewProps, ...}>( |
| 20 | + props: T, |
| 21 | +): T { |
| 22 | + const { |
| 23 | + accessibilityState, |
| 24 | + accessibilityValue, |
| 25 | + 'aria-busy': ariaBusy, |
| 26 | + 'aria-checked': ariaChecked, |
| 27 | + 'aria-disabled': ariaDisabled, |
| 28 | + 'aria-expanded': ariaExpanded, |
| 29 | + 'aria-hidden': ariaHidden, |
| 30 | + 'aria-label': ariaLabel, |
| 31 | + 'aria-labelledby': ariaLabelledBy, |
| 32 | + 'aria-live': ariaLive, |
| 33 | + 'aria-selected': ariaSelected, |
| 34 | + 'aria-valuemax': ariaValueMax, |
| 35 | + 'aria-valuemin': ariaValueMin, |
| 36 | + 'aria-valuenow': ariaValueNow, |
| 37 | + 'aria-valuetext': ariaValueText, |
| 38 | + id, |
| 39 | + tabIndex, |
| 40 | + ...otherProps |
| 41 | + } = props; |
| 42 | + |
| 43 | + const resolvedProps = otherProps as {...ViewProps, ...}; |
| 44 | + |
| 45 | + const parsedAriaLabelledBy = ariaLabelledBy?.split(/\s*,\s*/g); |
| 46 | + if (parsedAriaLabelledBy !== undefined) { |
| 47 | + resolvedProps.accessibilityLabelledBy = parsedAriaLabelledBy; |
| 48 | + } |
| 49 | + |
| 50 | + if (ariaLabel !== undefined) { |
| 51 | + resolvedProps.accessibilityLabel = ariaLabel; |
| 52 | + } |
| 53 | + |
| 54 | + if (ariaLive !== undefined) { |
| 55 | + resolvedProps.accessibilityLiveRegion = |
| 56 | + ariaLive === 'off' ? 'none' : ariaLive; |
| 57 | + } |
| 58 | + |
| 59 | + if (ariaHidden !== undefined) { |
| 60 | + resolvedProps.accessibilityElementsHidden = ariaHidden; |
| 61 | + if (ariaHidden === true) { |
| 62 | + resolvedProps.importantForAccessibility = 'no-hide-descendants'; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (id !== undefined) { |
| 67 | + resolvedProps.nativeID = id; |
| 68 | + } |
| 69 | + |
| 70 | + if (tabIndex !== undefined) { |
| 71 | + resolvedProps.focusable = !tabIndex; |
| 72 | + } |
| 73 | + |
| 74 | + if ( |
| 75 | + accessibilityState != null || |
| 76 | + ariaBusy != null || |
| 77 | + ariaChecked != null || |
| 78 | + ariaDisabled != null || |
| 79 | + ariaExpanded != null || |
| 80 | + ariaSelected != null |
| 81 | + ) { |
| 82 | + resolvedProps.accessibilityState = { |
| 83 | + busy: ariaBusy ?? accessibilityState?.busy, |
| 84 | + checked: ariaChecked ?? accessibilityState?.checked, |
| 85 | + disabled: ariaDisabled ?? accessibilityState?.disabled, |
| 86 | + expanded: ariaExpanded ?? accessibilityState?.expanded, |
| 87 | + selected: ariaSelected ?? accessibilityState?.selected, |
| 88 | + }; |
| 89 | + } |
| 90 | + |
| 91 | + if ( |
| 92 | + accessibilityValue != null || |
| 93 | + ariaValueMax != null || |
| 94 | + ariaValueMin != null || |
| 95 | + ariaValueNow != null || |
| 96 | + ariaValueText != null |
| 97 | + ) { |
| 98 | + resolvedProps.accessibilityValue = { |
| 99 | + max: ariaValueMax ?? accessibilityValue?.max, |
| 100 | + min: ariaValueMin ?? accessibilityValue?.min, |
| 101 | + now: ariaValueNow ?? accessibilityValue?.now, |
| 102 | + text: ariaValueText ?? accessibilityValue?.text, |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + // $FlowFixMe[incompatible-type] The aria-* props are replaced, the rest of T is kept |
| 107 | + return resolvedProps; |
| 108 | +} |
0 commit comments