-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathmap-props.ts
More file actions
90 lines (77 loc) · 2.1 KB
/
map-props.ts
File metadata and controls
90 lines (77 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import type { ViewStyle } from 'react-native';
import { StyleSheet } from 'react-native';
import { removeUndefinedKeys } from './object';
export type MapPropsFunction = (props: Record<string, unknown>) => Record<string, unknown>;
const propsToDisplay = [
'accessible',
'accessibilityElementsHidden',
'accessibilityHint',
'accessibilityLabel',
'accessibilityLabelledBy',
'accessibilityRole',
'accessibilityViewIsModal',
'alt',
'aria-busy',
'aria-checked',
'aria-disabled',
'aria-expanded',
'aria-hidden',
'aria-label',
'aria-labelledby',
'aria-modal',
'aria-selected',
'aria-valuemax',
'aria-valuemin',
'aria-valuenow',
'aria-valuetext',
'defaultValue',
'disabled',
'editable',
'importantForAccessibility',
'nativeID',
'placeholder',
'pointerEvents',
'role',
'testID',
'title',
'value',
] as const;
/**
* Preserve props that are helpful in diagnosing test failures, while stripping rest
*/
export function defaultMapProps(props: Record<string, unknown>): Record<string, unknown> {
const result: Record<string, unknown> = {};
const styles = StyleSheet.flatten(props.style as ViewStyle);
const styleToDisplay = extractStyle(styles);
if (styleToDisplay !== undefined) {
result.style = styleToDisplay;
}
const accessibilityState = removeUndefinedKeys(props.accessibilityState);
if (accessibilityState !== undefined) {
result.accessibilityState = accessibilityState;
}
const accessibilityValue = removeUndefinedKeys(props.accessibilityValue);
if (accessibilityValue !== undefined) {
result.accessibilityValue = accessibilityValue;
}
propsToDisplay.forEach((propName) => {
if (propName in props) {
result[propName] = props[propName];
}
});
return result;
}
function extractStyle(style: ViewStyle | undefined) {
if (style == null) {
return undefined;
}
const result: Record<string, unknown> = {};
if (style.display === 'none') {
result.display = 'none';
}
if (style.opacity === 0) {
result.opacity = 0;
}
const hasAnyKeys = Object.keys(result).length > 0;
return hasAnyKeys ? result : undefined;
}