@telus-uds/components-base 1.3.0 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +5 -2
- package/CHANGELOG.json +38 -0
- package/CHANGELOG.md +15 -2
- package/__tests__/ActivityIndicator/ActivityIndicator.test.jsx +35 -0
- package/__tests__/ActivityIndicator/__snapshots__/ActivityIndicator.test.jsx.snap +287 -0
- package/babel.config.js +27 -5
- package/component-docs.json +88 -424
- package/lib/A11yText/index.js +5 -4
- package/lib/ActivityIndicator/Spinner.js +46 -37
- package/lib/ActivityIndicator/Spinner.native.js +26 -15
- package/lib/Box/Box.js +4 -4
- package/lib/Button/ButtonBase.js +7 -8
- package/lib/Card/Card.js +5 -4
- package/lib/Card/PressableCardBase.js +6 -11
- package/lib/Checkbox/Checkbox.js +4 -6
- package/lib/Divider/Divider.js +12 -11
- package/lib/ExpandCollapse/Control.js +7 -7
- package/lib/ExpandCollapse/ExpandCollapse.js +6 -2
- package/lib/ExpandCollapse/Panel.js +6 -7
- package/lib/Feedback/Feedback.js +6 -5
- package/lib/Fieldset/Fieldset.js +1 -3
- package/lib/HorizontalScroll/HorizontalScroll.js +5 -6
- package/lib/IconButton/IconButton.js +6 -6
- package/lib/InputLabel/InputLabel.js +7 -2
- package/lib/InputSupports/InputSupports.js +2 -2
- package/lib/Link/LinkBase.js +5 -6
- package/lib/List/List.js +4 -6
- package/lib/List/ListItem.js +6 -2
- package/lib/Modal/Modal.js +6 -2
- package/lib/Notification/Notification.js +7 -2
- package/lib/Pagination/Pagination.js +8 -3
- package/lib/Progress/Progress.js +5 -4
- package/lib/Progress/ProgressBar.js +8 -7
- package/lib/Radio/Radio.js +4 -6
- package/lib/Radio/RadioButton.js +6 -3
- package/lib/Radio/RadioGroup.js +7 -2
- package/lib/RadioCard/RadioCard.js +4 -4
- package/lib/RadioCard/RadioCardGroup.js +9 -6
- package/lib/Search/Search.js +7 -2
- package/lib/Select/Select.js +10 -9
- package/lib/SideNav/Item.js +7 -7
- package/lib/SideNav/SideNav.js +7 -3
- package/lib/Skeleton/Skeleton.js +6 -2
- package/lib/Spacer/Spacer.js +7 -18
- package/lib/StackView/StackView.js +7 -7
- package/lib/StackView/StackWrapBox.js +6 -7
- package/lib/StackView/StackWrapGap.js +6 -5
- package/lib/StepTracker/Step.js +7 -3
- package/lib/StepTracker/StepTracker.js +7 -7
- package/lib/Tabs/Tabs.js +6 -2
- package/lib/Tabs/TabsItem.js +6 -5
- package/lib/Tags/Tags.js +5 -6
- package/lib/TextInput/TextArea.js +13 -14
- package/lib/TextInput/TextInput.js +13 -14
- package/lib/TextInput/TextInputBase.js +5 -3
- package/lib/ToggleSwitch/ToggleSwitch.js +10 -9
- package/lib/ToggleSwitch/ToggleSwitchGroup.js +5 -7
- package/lib/Tooltip/Tooltip.js +7 -2
- package/lib/TooltipButton/TooltipButton.js +7 -2
- package/lib/Typography/Typography.js +8 -6
- package/lib/index.js +7 -0
- package/lib/utils/children.js +5 -1
- package/lib/utils/index.js +14 -0
- package/lib/utils/props/index.js +9 -0
- package/lib/{InputSupports/propTypes.js → utils/props/inputSupportsProps.js} +2 -3
- package/lib/utils/ssr.js +51 -0
- package/lib-module/A11yInfoProvider/index.js +62 -0
- package/lib-module/A11yText/index.js +55 -0
- package/lib-module/ActivityIndicator/Spinner.js +76 -0
- package/lib-module/ActivityIndicator/Spinner.native.js +143 -0
- package/lib-module/ActivityIndicator/index.js +40 -0
- package/lib-module/ActivityIndicator/shared.js +12 -0
- package/lib-module/BaseProvider/index.js +26 -0
- package/lib-module/Box/Box.js +243 -0
- package/lib-module/Box/index.js +2 -0
- package/lib-module/Button/Button.js +25 -0
- package/lib-module/Button/ButtonBase.js +254 -0
- package/lib-module/Button/ButtonGroup.js +164 -0
- package/lib-module/Button/ButtonLink.js +39 -0
- package/lib-module/Button/index.js +4 -0
- package/lib-module/Button/propTypes.js +36 -0
- package/lib-module/Card/Card.js +83 -0
- package/lib-module/Card/CardBase.js +62 -0
- package/lib-module/Card/PressableCardBase.js +113 -0
- package/lib-module/Card/index.js +4 -0
- package/lib-module/Checkbox/Checkbox.js +321 -0
- package/lib-module/Checkbox/CheckboxGroup.js +218 -0
- package/lib-module/Checkbox/CheckboxInput.js +58 -0
- package/lib-module/Checkbox/CheckboxInput.native.js +6 -0
- package/lib-module/Checkbox/index.js +3 -0
- package/lib-module/Divider/Divider.js +123 -0
- package/lib-module/Divider/index.js +2 -0
- package/lib-module/ExpandCollapse/Accordion.js +15 -0
- package/lib-module/ExpandCollapse/Control.js +130 -0
- package/lib-module/ExpandCollapse/ExpandCollapse.js +94 -0
- package/lib-module/ExpandCollapse/Panel.js +158 -0
- package/lib-module/ExpandCollapse/index.js +7 -0
- package/lib-module/Feedback/Feedback.js +144 -0
- package/lib-module/Feedback/index.js +2 -0
- package/lib-module/Fieldset/Fieldset.js +145 -0
- package/lib-module/Fieldset/FieldsetContainer.js +29 -0
- package/lib-module/Fieldset/FieldsetContainer.native.js +20 -0
- package/lib-module/Fieldset/Legend.js +21 -0
- package/lib-module/Fieldset/Legend.native.js +28 -0
- package/lib-module/Fieldset/cssReset.js +14 -0
- package/lib-module/Fieldset/index.js +2 -0
- package/lib-module/FlexGrid/Col/Col.js +265 -0
- package/lib-module/FlexGrid/Col/index.js +2 -0
- package/lib-module/FlexGrid/FlexGrid.js +147 -0
- package/lib-module/FlexGrid/Row/Row.js +177 -0
- package/lib-module/FlexGrid/Row/index.js +2 -0
- package/lib-module/FlexGrid/helpers/index.js +18 -0
- package/lib-module/FlexGrid/index.js +2 -0
- package/lib-module/FlexGrid/providers/GutterContext.js +3 -0
- package/lib-module/HorizontalScroll/HorizontalScroll.js +171 -0
- package/lib-module/HorizontalScroll/HorizontalScrollButton.js +102 -0
- package/lib-module/HorizontalScroll/ScrollViewEnd.js +47 -0
- package/lib-module/HorizontalScroll/ScrollViewEnd.native.js +24 -0
- package/lib-module/HorizontalScroll/dictionary.js +11 -0
- package/lib-module/HorizontalScroll/index.js +11 -0
- package/lib-module/HorizontalScroll/itemPositions.js +106 -0
- package/lib-module/Icon/Icon.js +61 -0
- package/lib-module/Icon/IconText.js +81 -0
- package/lib-module/Icon/index.js +4 -0
- package/lib-module/IconButton/IconButton.js +115 -0
- package/lib-module/IconButton/index.js +2 -0
- package/lib-module/InputLabel/InputLabel.js +131 -0
- package/lib-module/InputLabel/LabelContent.js +24 -0
- package/lib-module/InputLabel/LabelContent.native.js +16 -0
- package/lib-module/InputLabel/index.js +2 -0
- package/lib-module/InputSupports/InputSupports.js +88 -0
- package/lib-module/InputSupports/index.js +2 -0
- package/lib-module/InputSupports/useInputSupports.js +31 -0
- package/lib-module/Link/ChevronLink.js +51 -0
- package/lib-module/Link/InlinePressable.js +37 -0
- package/lib-module/Link/InlinePressable.native.js +85 -0
- package/lib-module/Link/Link.js +27 -0
- package/lib-module/Link/LinkBase.js +210 -0
- package/lib-module/Link/TextButton.js +43 -0
- package/lib-module/Link/index.js +5 -0
- package/lib-module/List/List.js +55 -0
- package/lib-module/List/ListItem.js +213 -0
- package/lib-module/List/index.js +5 -0
- package/lib-module/Modal/Modal.js +208 -0
- package/lib-module/Modal/dictionary.js +9 -0
- package/lib-module/Modal/index.js +2 -0
- package/lib-module/Notification/Notification.js +196 -0
- package/lib-module/Notification/dictionary.js +8 -0
- package/lib-module/Notification/index.js +2 -0
- package/lib-module/Pagination/PageButton.js +65 -0
- package/lib-module/Pagination/Pagination.js +140 -0
- package/lib-module/Pagination/SideButton.js +103 -0
- package/lib-module/Pagination/dictionary.js +18 -0
- package/lib-module/Pagination/index.js +2 -0
- package/lib-module/Pagination/usePagination.js +72 -0
- package/lib-module/Progress/Progress.js +85 -0
- package/lib-module/Progress/ProgressBar.js +134 -0
- package/lib-module/Progress/ProgressBarBackground.js +41 -0
- package/lib-module/Progress/index.js +4 -0
- package/lib-module/Radio/Radio.js +263 -0
- package/lib-module/Radio/RadioButton.js +128 -0
- package/lib-module/Radio/RadioGroup.js +225 -0
- package/lib-module/Radio/RadioInput.js +60 -0
- package/lib-module/Radio/RadioInput.native.js +6 -0
- package/lib-module/Radio/index.js +3 -0
- package/lib-module/RadioCard/RadioCard.js +218 -0
- package/lib-module/RadioCard/RadioCardGroup.js +232 -0
- package/lib-module/RadioCard/index.js +3 -0
- package/lib-module/Search/Search.js +233 -0
- package/lib-module/Search/dictionary.js +12 -0
- package/lib-module/Search/index.js +2 -0
- package/lib-module/Select/Group.js +20 -0
- package/lib-module/Select/Group.native.js +14 -0
- package/lib-module/Select/Item.js +17 -0
- package/lib-module/Select/Item.native.js +9 -0
- package/lib-module/Select/Picker.js +67 -0
- package/lib-module/Select/Picker.native.js +110 -0
- package/lib-module/Select/Select.js +317 -0
- package/lib-module/Select/index.js +6 -0
- package/lib-module/SideNav/Item.js +139 -0
- package/lib-module/SideNav/ItemContent.js +45 -0
- package/lib-module/SideNav/ItemsGroup.js +115 -0
- package/lib-module/SideNav/SideNav.js +133 -0
- package/lib-module/SideNav/index.js +1 -0
- package/lib-module/Skeleton/Skeleton.js +117 -0
- package/lib-module/Skeleton/index.js +2 -0
- package/lib-module/Skeleton/skeleton.constant.js +3 -0
- package/lib-module/Skeleton/skeletonWebAnimation.js +18 -0
- package/lib-module/Skeleton/useSkeletonNativeAnimation.js +24 -0
- package/lib-module/Spacer/Spacer.js +97 -0
- package/lib-module/Spacer/index.js +2 -0
- package/lib-module/StackView/StackView.js +124 -0
- package/lib-module/StackView/StackWrap.js +48 -0
- package/lib-module/StackView/StackWrap.native.js +3 -0
- package/lib-module/StackView/StackWrapBox.js +114 -0
- package/lib-module/StackView/StackWrapGap.js +58 -0
- package/lib-module/StackView/common.js +32 -0
- package/lib-module/StackView/getStackedContent.js +123 -0
- package/lib-module/StackView/index.js +5 -0
- package/lib-module/StepTracker/Step.js +229 -0
- package/lib-module/StepTracker/StepTracker.js +175 -0
- package/lib-module/StepTracker/dictionary.js +10 -0
- package/lib-module/StepTracker/index.js +2 -0
- package/lib-module/Tabs/Tabs.js +113 -0
- package/lib-module/Tabs/TabsItem.js +215 -0
- package/lib-module/Tabs/index.js +2 -0
- package/lib-module/Tags/Tags.js +238 -0
- package/lib-module/Tags/index.js +2 -0
- package/lib-module/TextInput/TextArea.js +88 -0
- package/lib-module/TextInput/TextInput.js +60 -0
- package/lib-module/TextInput/TextInputBase.js +233 -0
- package/lib-module/TextInput/index.js +3 -0
- package/lib-module/TextInput/propTypes.js +31 -0
- package/lib-module/ThemeProvider/ThemeProvider.js +35 -0
- package/lib-module/ThemeProvider/index.js +6 -0
- package/lib-module/ThemeProvider/useSetTheme.js +22 -0
- package/lib-module/ThemeProvider/useTheme.js +14 -0
- package/lib-module/ThemeProvider/useThemeTokens.js +105 -0
- package/lib-module/ThemeProvider/utils/index.js +2 -0
- package/lib-module/ThemeProvider/utils/styles.js +174 -0
- package/lib-module/ThemeProvider/utils/theme-tokens.js +151 -0
- package/lib-module/ToggleSwitch/ToggleSwitch.js +224 -0
- package/lib-module/ToggleSwitch/ToggleSwitchGroup.js +202 -0
- package/lib-module/ToggleSwitch/index.js +3 -0
- package/lib-module/Tooltip/Backdrop.js +52 -0
- package/lib-module/Tooltip/Backdrop.native.js +43 -0
- package/lib-module/Tooltip/Tooltip.js +332 -0
- package/lib-module/Tooltip/dictionary.js +8 -0
- package/lib-module/Tooltip/getTooltipPosition.js +164 -0
- package/lib-module/Tooltip/index.js +2 -0
- package/lib-module/TooltipButton/TooltipButton.js +71 -0
- package/lib-module/TooltipButton/index.js +2 -0
- package/lib-module/Typography/Typography.js +118 -0
- package/lib-module/Typography/index.js +2 -0
- package/lib-module/ViewportProvider/ViewportProvider.js +28 -0
- package/lib-module/ViewportProvider/index.js +3 -0
- package/lib-module/ViewportProvider/useViewport.js +3 -0
- package/lib-module/ViewportProvider/useViewportListener.js +43 -0
- package/lib-module/index.js +48 -0
- package/lib-module/utils/a11y/index.js +2 -0
- package/lib-module/utils/a11y/semantics.js +154 -0
- package/lib-module/utils/a11y/textSize.js +34 -0
- package/lib-module/utils/animation/index.js +2 -0
- package/lib-module/utils/animation/useVerticalExpandAnimation.js +49 -0
- package/lib-module/utils/children.js +118 -0
- package/lib-module/utils/index.js +15 -0
- package/lib-module/utils/info/index.js +7 -0
- package/lib-module/utils/info/platform/index.js +11 -0
- package/lib-module/utils/info/platform/platform.android.js +1 -0
- package/lib-module/utils/info/platform/platform.ios.js +1 -0
- package/lib-module/utils/info/platform/platform.js +1 -0
- package/lib-module/utils/info/platform/platform.native.js +4 -0
- package/lib-module/utils/info/versions.js +5 -0
- package/lib-module/utils/input.js +180 -0
- package/lib-module/utils/pressability.js +97 -0
- package/lib-module/utils/props/a11yProps.js +140 -0
- package/lib-module/utils/props/clickProps.js +25 -0
- package/lib-module/utils/props/componentPropType.js +63 -0
- package/lib-module/utils/props/copyPropTypes.js +2 -0
- package/lib-module/utils/props/getPropSelector.js +6 -0
- package/lib-module/utils/props/hrefAttrsProp.js +30 -0
- package/lib-module/utils/props/index.js +16 -0
- package/lib-module/utils/props/inputSupportsProps.js +54 -0
- package/lib-module/utils/props/linkProps.js +47 -0
- package/lib-module/utils/props/paddingProp.js +9 -0
- package/lib-module/utils/props/pressProps.js +42 -0
- package/lib-module/utils/props/rectProp.js +9 -0
- package/lib-module/utils/props/responsiveProps.js +30 -0
- package/lib-module/utils/props/selectSystemProps.js +24 -0
- package/lib-module/utils/props/spacingProps.js +56 -0
- package/lib-module/utils/props/tokens.js +120 -0
- package/lib-module/utils/props/variantProp.js +18 -0
- package/lib-module/utils/props/viewProps.js +22 -0
- package/lib-module/utils/ssr.js +35 -0
- package/lib-module/utils/useCopy.js +42 -0
- package/lib-module/utils/useHash.js +44 -0
- package/lib-module/utils/useHash.native.js +7 -0
- package/lib-module/utils/useResponsiveProp.js +47 -0
- package/lib-module/utils/useSpacingScale.js +123 -0
- package/lib-module/utils/useUniqueId.js +12 -0
- package/lib-module/utils/withLinkRouter.js +82 -0
- package/package.json +9 -5
- package/src/A11yText/index.jsx +7 -3
- package/src/ActivityIndicator/Spinner.jsx +56 -44
- package/src/ActivityIndicator/Spinner.native.jsx +20 -12
- package/src/Box/Box.jsx +10 -9
- package/src/Button/ButtonBase.jsx +9 -8
- package/src/Card/Card.jsx +7 -4
- package/src/Card/PressableCardBase.jsx +12 -12
- package/src/Checkbox/Checkbox.jsx +12 -5
- package/src/Divider/Divider.jsx +17 -10
- package/src/ExpandCollapse/Control.jsx +12 -7
- package/src/ExpandCollapse/ExpandCollapse.jsx +13 -3
- package/src/ExpandCollapse/Panel.jsx +15 -5
- package/src/Feedback/Feedback.jsx +13 -3
- package/src/Fieldset/Fieldset.jsx +1 -1
- package/src/HorizontalScroll/HorizontalScroll.jsx +12 -5
- package/src/IconButton/IconButton.jsx +12 -8
- package/src/InputLabel/InputLabel.jsx +16 -2
- package/src/InputSupports/InputSupports.jsx +1 -1
- package/src/Link/LinkBase.jsx +10 -9
- package/src/List/List.jsx +12 -5
- package/src/List/ListItem.jsx +16 -2
- package/src/Modal/Modal.jsx +63 -50
- package/src/Notification/Notification.jsx +17 -3
- package/src/Pagination/Pagination.jsx +10 -3
- package/src/Progress/Progress.jsx +5 -3
- package/src/Progress/ProgressBar.jsx +8 -6
- package/src/Radio/Radio.jsx +13 -5
- package/src/Radio/RadioButton.jsx +13 -3
- package/src/Radio/RadioGroup.jsx +14 -2
- package/src/RadioCard/RadioCard.jsx +9 -6
- package/src/RadioCard/RadioCardGroup.jsx +16 -4
- package/src/Search/Search.jsx +15 -3
- package/src/Select/Select.jsx +21 -7
- package/src/SideNav/Item.jsx +15 -7
- package/src/SideNav/SideNav.jsx +13 -2
- package/src/Skeleton/Skeleton.jsx +55 -43
- package/src/Spacer/Spacer.jsx +8 -16
- package/src/StackView/StackView.jsx +13 -12
- package/src/StackView/StackWrapBox.jsx +12 -12
- package/src/StackView/StackWrapGap.jsx +9 -7
- package/src/StepTracker/Step.jsx +7 -4
- package/src/StepTracker/StepTracker.jsx +8 -8
- package/src/Tabs/Tabs.jsx +16 -3
- package/src/Tabs/TabsItem.jsx +13 -9
- package/src/Tags/Tags.jsx +20 -5
- package/src/TextInput/TextArea.jsx +20 -8
- package/src/TextInput/TextInput.jsx +20 -8
- package/src/TextInput/TextInputBase.jsx +13 -3
- package/src/ToggleSwitch/ToggleSwitch.jsx +16 -5
- package/src/ToggleSwitch/ToggleSwitchGroup.jsx +14 -6
- package/src/Tooltip/Tooltip.jsx +13 -3
- package/src/TooltipButton/TooltipButton.jsx +6 -4
- package/src/Typography/Typography.jsx +10 -6
- package/src/index.js +1 -1
- package/src/utils/children.jsx +2 -1
- package/src/utils/index.js +1 -0
- package/src/utils/props/index.js +1 -0
- package/src/{InputSupports/propTypes.js → utils/props/inputSupportsProps.js} +2 -4
- package/src/utils/ssr.js +35 -0
- package/release-context.json +0 -7
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { useCallback, useRef, useState } from 'react';
|
|
2
|
+
const pluralHooks = ['useMultipleInputValues'];
|
|
3
|
+
|
|
4
|
+
const validateProps = ({
|
|
5
|
+
value,
|
|
6
|
+
onChange,
|
|
7
|
+
readOnly,
|
|
8
|
+
initialValue
|
|
9
|
+
}, {
|
|
10
|
+
isCurrentlyControlled,
|
|
11
|
+
isControlled
|
|
12
|
+
}, hookName) => {
|
|
13
|
+
const s = pluralHooks.includes(hookName) ? 's' : '';
|
|
14
|
+
|
|
15
|
+
const usageError = error => {
|
|
16
|
+
// Errors inside hooks in React Native get incomplete stack traces pointing at parent component only.
|
|
17
|
+
// Help devs out by telling them exactly which hook threw the error as well as why.
|
|
18
|
+
throw new Error(`${hookName} ${error}.
|
|
19
|
+
|
|
20
|
+
Consumers of this hook must be one of:
|
|
21
|
+
1. An "uncontrolled" component responding directly to user actions, with an optional \`initialValue${s}\` but no \`value${s}\` prop,
|
|
22
|
+
2. A "controlled" component where an always-defined \`value${s}\` prop is managed by an \`onChange\` handler, with no \`initialValue${s}\`,
|
|
23
|
+
3. A "read-only" component, with \`readOnly\` prop set as \`true\`.
|
|
24
|
+
`);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
if (value && !onChange && !readOnly) {
|
|
28
|
+
usageError(`has \`value${s}\` prop without \`onChange\` or \`readOnly\``);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (initialValue && value) {
|
|
32
|
+
usageError(`has both \`initialValue${s}\` and \`value${s}\``);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (isControlled && !isCurrentlyControlled) {
|
|
36
|
+
usageError(`stopped receiving \`value${s}\` from parent after delegating state management`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!isControlled && isCurrentlyControlled) {
|
|
40
|
+
usageError(`started receiving \`value${s}\` from parent after starting managing own state`);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Hook used for controlling the input state of input components that have a singular value.
|
|
45
|
+
* If `value` prop is passed in, this delegates state management to a parent using a passed-in `onChange` prop.
|
|
46
|
+
* If `value` prop is undefined, the hook manages its own state.
|
|
47
|
+
*
|
|
48
|
+
* TODO: when implementing full UDS forms, integrate validation support etc and test alongside common forms
|
|
49
|
+
* management tools such as Formik and React Hooks Form.
|
|
50
|
+
*
|
|
51
|
+
* @param {object} props
|
|
52
|
+
* @param {string|number|null} [props.value] - for a controlled input, the active values as set by a parent
|
|
53
|
+
* @param {string|number} [props.initialValue] - for an uncontrolled input, the default active values
|
|
54
|
+
* @param {function} [props.onChange] - function called when values change (required for controlled inputs)
|
|
55
|
+
* @param {boolean} [props.readOnly] - if true, stops the input values from changing
|
|
56
|
+
*
|
|
57
|
+
* @param {string} hookName - optional, used for tailoring error messages
|
|
58
|
+
*
|
|
59
|
+
* @typedef {(oldValue: string|number|null) => string|number|null} UpdaterFunction - `setValue` takes a value or
|
|
60
|
+
* a function returning a new value from the old value
|
|
61
|
+
* @returns {{
|
|
62
|
+
* currentValue: string|number|null
|
|
63
|
+
* setValue: (newValue: string|number|null|UpdaterFunction) => void
|
|
64
|
+
* resetValue: () => void
|
|
65
|
+
* isControlled: bool
|
|
66
|
+
* }}
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
export const useInputValue = (props = {}, hookName = 'useInputValue') => {
|
|
71
|
+
const isCurrentlyControlled = props.value !== undefined;
|
|
72
|
+
const [isControlled] = useState(isCurrentlyControlled);
|
|
73
|
+
validateProps(props, {
|
|
74
|
+
isControlled,
|
|
75
|
+
isCurrentlyControlled
|
|
76
|
+
}, hookName);
|
|
77
|
+
const {
|
|
78
|
+
value,
|
|
79
|
+
initialValue,
|
|
80
|
+
onChange,
|
|
81
|
+
readOnly = false
|
|
82
|
+
} = props;
|
|
83
|
+
const [ownValue, setOwnValue] = useState(!isControlled && initialValue);
|
|
84
|
+
const currentValue = isControlled ? value : ownValue; // Store the first valid value, for resetting input to default. If behaviour like Formik's `enableReinitialize`
|
|
85
|
+
// is needed, add useEffect calling a set fn if !deepEqual(initializedValues, initialValues)
|
|
86
|
+
|
|
87
|
+
const valueRef = useRef({
|
|
88
|
+
initial: currentValue,
|
|
89
|
+
value: currentValue
|
|
90
|
+
}); // Make current value accessible inside useCallback without rememoizing every time the value changes
|
|
91
|
+
|
|
92
|
+
valueRef.current.value = currentValue;
|
|
93
|
+
const setValue = useCallback((arg, event) => {
|
|
94
|
+
if (readOnly) return;
|
|
95
|
+
const newValue = typeof arg === 'function' ? arg(valueRef.current.value) : arg;
|
|
96
|
+
if (!isControlled) setOwnValue(newValue); // Call onChange handler if there's something for it to handle (event or a changed value)
|
|
97
|
+
|
|
98
|
+
if (onChange && (event || valueRef.current.value !== newValue)) onChange(newValue, event);
|
|
99
|
+
}, [isControlled, onChange, readOnly]);
|
|
100
|
+
const resetValue = useCallback(event => setValue(valueRef.current.initial, event), [setValue]);
|
|
101
|
+
return {
|
|
102
|
+
currentValue,
|
|
103
|
+
setValue,
|
|
104
|
+
resetValue,
|
|
105
|
+
isControlled
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Hook used for controlling the input state of input components that have multiple named on/off states.
|
|
110
|
+
* If `values` are passed in, this delegates state management to a parent using a passed-in `onChange` prop.
|
|
111
|
+
* If `values` prop is not passed in, the hook manages its own state.
|
|
112
|
+
*
|
|
113
|
+
* TODO: when implementing full UDS forms, integrate validation support etc and test alongside common forms
|
|
114
|
+
* management tools such as Formik and React Hooks Form.
|
|
115
|
+
*
|
|
116
|
+
* @param {object} props
|
|
117
|
+
* @param {string[]|number[]} [props.values] - for a controlled input, the active values as set by a parent
|
|
118
|
+
* @param {string[]|number[]} [props.initialValues] - for an uncontrolled input, the default active values
|
|
119
|
+
* @param {number?} [props.maxValues] - maximum number of values that may be selected at once (no limit if null)
|
|
120
|
+
* @param {function} [props.onChange] - function called when values change (required for controlled inputs)
|
|
121
|
+
* @param {boolean} [props.readOnly] - if true, stops the input values from changing
|
|
122
|
+
*
|
|
123
|
+
* @param {string} componentName - optional, used in error messages
|
|
124
|
+
*
|
|
125
|
+
* @typedef {(oldValues: string[]|number[]) => string[]|number[]} UpdaterFunction - `setValues` takes values or
|
|
126
|
+
* a function returning new values from old values
|
|
127
|
+
* @returns {{
|
|
128
|
+
* currentValues: any
|
|
129
|
+
* resetValues: () => void
|
|
130
|
+
* setValues: (newValues: string[]|number[]|UpdaterFunction) => void
|
|
131
|
+
* toggleOneValue: (value: string|number) => void
|
|
132
|
+
* unsetValues: () => void
|
|
133
|
+
* }}
|
|
134
|
+
*/
|
|
135
|
+
|
|
136
|
+
export const useMultipleInputValues = ({
|
|
137
|
+
values,
|
|
138
|
+
initialValues,
|
|
139
|
+
maxValues,
|
|
140
|
+
onChange,
|
|
141
|
+
readOnly
|
|
142
|
+
} = {}) => {
|
|
143
|
+
const {
|
|
144
|
+
currentValue,
|
|
145
|
+
setValue,
|
|
146
|
+
resetValue
|
|
147
|
+
} = useInputValue({
|
|
148
|
+
readOnly,
|
|
149
|
+
onChange,
|
|
150
|
+
value: values,
|
|
151
|
+
// if we're controlling our own state, always start with an array
|
|
152
|
+
initialValue: initialValues ?? (values === undefined ? [] : undefined)
|
|
153
|
+
}, 'useMultipleInputValues');
|
|
154
|
+
const enforceMaxValues = useCallback(newValues => {
|
|
155
|
+
if (!maxValues) return newValues;
|
|
156
|
+
const excess = newValues.length - maxValues;
|
|
157
|
+
return excess > 0 ? newValues.slice(excess) : newValues;
|
|
158
|
+
}, [maxValues]);
|
|
159
|
+
const currentValues = enforceMaxValues(currentValue);
|
|
160
|
+
const setValues = useCallback((newValues, event) => {
|
|
161
|
+
const validNewValues = enforceMaxValues(newValues);
|
|
162
|
+
setValue(validNewValues, event);
|
|
163
|
+
}, [setValue, enforceMaxValues]);
|
|
164
|
+
const resetValues = resetValue;
|
|
165
|
+
const unsetValues = useCallback(event => setValues([], event), [setValues]);
|
|
166
|
+
const toggleOneValue = useCallback((newValue, event) => {
|
|
167
|
+
if (readOnly) return; // This will only work with primitive values (e.g. strings, numbers), swap for .some() and
|
|
168
|
+
// a deepEqual() function if there's any use case for toggling stored objects or arrays.
|
|
169
|
+
|
|
170
|
+
const newValues = currentValues.includes(newValue) ? currentValues.filter(oldValue => oldValue !== newValue) : [...currentValues, newValue];
|
|
171
|
+
setValues(newValues, event);
|
|
172
|
+
}, [currentValues, readOnly, setValues]);
|
|
173
|
+
return {
|
|
174
|
+
currentValues,
|
|
175
|
+
resetValues,
|
|
176
|
+
setValues,
|
|
177
|
+
toggleOneValue,
|
|
178
|
+
unsetValues
|
|
179
|
+
};
|
|
180
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import Platform from "react-native-web/dist/exports/Platform";
|
|
2
|
+
import StyleSheet from "react-native-web/dist/exports/StyleSheet";
|
|
3
|
+
import pressProps from './props/pressProps';
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {import('react').ReactNode} ReactNode
|
|
6
|
+
*/
|
|
7
|
+
// These roles should result in cursor: pointer but don't in current RNW releases
|
|
8
|
+
|
|
9
|
+
const shouldUseCursor = ['checkbox', 'radio', 'switch'];
|
|
10
|
+
/**
|
|
11
|
+
* React Native Web has some built-in logic for applying cursor styles based on accessibility roles;
|
|
12
|
+
* however, it misses certain cases. This fills in known cases where widely used versions of RNW
|
|
13
|
+
* fail to apply an expected cursor style.
|
|
14
|
+
*
|
|
15
|
+
* @param {object} props
|
|
16
|
+
* @param {boolean} [props.inactive]
|
|
17
|
+
* @param {boolean} [props.disabled]
|
|
18
|
+
* @param {string} [props.accessibilityRole]
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
export const getCursorStyle = ({
|
|
23
|
+
inactive,
|
|
24
|
+
disabled,
|
|
25
|
+
accessibilityRole
|
|
26
|
+
}) => {
|
|
27
|
+
if (Platform.OS !== 'web') return undefined;
|
|
28
|
+
if (inactive || disabled) return staticStyles.notAllowed;
|
|
29
|
+
if (shouldUseCursor.includes(accessibilityRole)) return staticStyles.pointer;
|
|
30
|
+
return undefined; // allows React Native Web's built-in logic to apply
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* @typedef {{ pressed: boolean, focused: boolean, hovered?: boolean }} PressableState
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Maps the state object given by the React Native `Pressable` component to the set of
|
|
38
|
+
* equivalent appearance names used in UDS.
|
|
39
|
+
*
|
|
40
|
+
* @param {PressableState} pressableState - state object passed by React Native's `<Pressable>` into
|
|
41
|
+
* render functions passed to its `style` or `children` props.
|
|
42
|
+
* @param {object} [additionalState] - properties specific to the current component,
|
|
43
|
+
* such as `inactive`, `selected`, etc.
|
|
44
|
+
* @returns {object}
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
export const resolvePressableState = ({
|
|
48
|
+
pressed = false,
|
|
49
|
+
focused = false,
|
|
50
|
+
hovered = false
|
|
51
|
+
} = {}, additionalState) => ({ ...additionalState,
|
|
52
|
+
focus: focused,
|
|
53
|
+
hover: hovered,
|
|
54
|
+
pressed
|
|
55
|
+
});
|
|
56
|
+
/**
|
|
57
|
+
* Takes a UDS `tokens` prop and, if it is a function, resolves it based on a state
|
|
58
|
+
* object passed from the React Native `Pressable` component and optional extra properties.
|
|
59
|
+
*
|
|
60
|
+
* @param {object|function} tokens - UDS system tokens prop
|
|
61
|
+
* @param {PressableState} pressableState - state object passed by React Native's `<Pressable>`
|
|
62
|
+
* @param {object} [additionalState] - properties specific to the current component
|
|
63
|
+
* @returns {object} - resolved tokens object
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
export const resolvePressableTokens = (tokens, pressableState, additionalState) => {
|
|
67
|
+
if (typeof tokens !== 'function') return tokens;
|
|
68
|
+
const udsPressableState = resolvePressableState(pressableState, additionalState);
|
|
69
|
+
return tokens(udsPressableState);
|
|
70
|
+
};
|
|
71
|
+
const staticStyles = StyleSheet.create(Platform.select({
|
|
72
|
+
web: {
|
|
73
|
+
notAllowed: {
|
|
74
|
+
cursor: 'not-allowed'
|
|
75
|
+
},
|
|
76
|
+
pointer: {
|
|
77
|
+
cursor: 'pointer'
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
default: {}
|
|
81
|
+
}));
|
|
82
|
+
/**
|
|
83
|
+
* From an object of props, extracts any platform-appropriate press handler functions and wraps
|
|
84
|
+
* them in a function that passes in some provided args. Allows components containing a Pressable
|
|
85
|
+
* to pass in press handlers that are then called with state or values that is otherwise internal.
|
|
86
|
+
*/
|
|
87
|
+
|
|
88
|
+
export const getPressHandlersWithArgs = (pressableProps = {}, args = []) => {
|
|
89
|
+
// Allow handlers to be passed down for blur, hover, focus, pressIn, etc
|
|
90
|
+
const pressHandlers = Object.fromEntries(Object.entries(pressProps.selectHandlers(pressableProps)).map(([key, handler]) => ({
|
|
91
|
+
[key]: (...defaultArgs) => {
|
|
92
|
+
// Pass each handler data on this button and current selection
|
|
93
|
+
handler(...args, ...defaultArgs);
|
|
94
|
+
}
|
|
95
|
+
})));
|
|
96
|
+
return pressHandlers;
|
|
97
|
+
};
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import PropTypes from 'prop-types';
|
|
2
|
+
import Platform from "react-native-web/dist/exports/Platform";
|
|
3
|
+
import getPropSelector from './getPropSelector'; // React Native exports a11y prop definitions as TypeScript Interfaces, but no longer exports PropTypes
|
|
4
|
+
// so we have to define them ourselves.
|
|
5
|
+
|
|
6
|
+
const nativeA11yPropTypes = {
|
|
7
|
+
accessible: PropTypes.bool,
|
|
8
|
+
focusable: PropTypes.bool,
|
|
9
|
+
accessibilityElementsHidden: PropTypes.bool,
|
|
10
|
+
accessibilityHint: PropTypes.string,
|
|
11
|
+
// react-native-web ignores `accessibilityHint`
|
|
12
|
+
accessibilityIgnoresInvertColors: PropTypes.bool,
|
|
13
|
+
accessibilityLabel: PropTypes.string,
|
|
14
|
+
accessibilityRole: PropTypes.string,
|
|
15
|
+
accessibilityActions: PropTypes.arrayOf(PropTypes.shape({
|
|
16
|
+
name: PropTypes.string.isRequired,
|
|
17
|
+
label: PropTypes.string
|
|
18
|
+
})),
|
|
19
|
+
accessibilityLiveRegion: PropTypes.oneOf(['none', 'polite', 'assertive']),
|
|
20
|
+
accessibilityState: PropTypes.shape({
|
|
21
|
+
disabled: PropTypes.bool,
|
|
22
|
+
selected: PropTypes.bool,
|
|
23
|
+
checked: PropTypes.oneOf([true, false, 'mixed']),
|
|
24
|
+
busy: PropTypes.bool,
|
|
25
|
+
expanded: PropTypes.bool
|
|
26
|
+
}),
|
|
27
|
+
accessibilityValue: PropTypes.shape({
|
|
28
|
+
min: PropTypes.number,
|
|
29
|
+
max: PropTypes.number,
|
|
30
|
+
now: PropTypes.number,
|
|
31
|
+
text: PropTypes.string
|
|
32
|
+
}),
|
|
33
|
+
accessibilityViewIsModal: PropTypes.bool,
|
|
34
|
+
importantForAccessibility: PropTypes.oneOf(['auto', 'yes', 'no', 'no-hide-descendants']),
|
|
35
|
+
onAccessibilityAction: PropTypes.func,
|
|
36
|
+
onAccessibilityEscape: PropTypes.func,
|
|
37
|
+
onAccessibilityTap: PropTypes.func
|
|
38
|
+
};
|
|
39
|
+
const a11yPropTypes = { ...nativeA11yPropTypes,
|
|
40
|
+
// React Native Web adds many a11y props that alias aria-* attributes
|
|
41
|
+
// Types based on https://necolas.github.io/react-native-web/docs/accessibility/
|
|
42
|
+
accessibilityActiveDescendant: PropTypes.string,
|
|
43
|
+
accessibilityAtomic: PropTypes.bool,
|
|
44
|
+
accessibilityAutoComplete: PropTypes.string,
|
|
45
|
+
accessibilityBusy: PropTypes.bool,
|
|
46
|
+
accessibilityChecked: PropTypes.oneOf([true, false, 'mixed']),
|
|
47
|
+
accessibilityColumnCount: PropTypes.number,
|
|
48
|
+
accessibilityColumnIndex: PropTypes.number,
|
|
49
|
+
accessibilityColumnSpan: PropTypes.number,
|
|
50
|
+
accessibilityControls: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
|
|
51
|
+
accessibilityCurrent: PropTypes.oneOf([true, false, 'page', 'step', 'location', 'date', 'time']),
|
|
52
|
+
accessibilityDescribedBy: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
|
|
53
|
+
accessibilityDetails: PropTypes.string,
|
|
54
|
+
accessibilityDisabled: PropTypes.bool,
|
|
55
|
+
accessibilityErrorMessage: PropTypes.string,
|
|
56
|
+
accessibilityExpanded: PropTypes.bool,
|
|
57
|
+
accessibilityFlowTo: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
|
|
58
|
+
accessibilityHasPopup: PropTypes.string,
|
|
59
|
+
accessibilityHidden: PropTypes.bool,
|
|
60
|
+
accessibilityInvalid: PropTypes.bool,
|
|
61
|
+
accessibilityKeyShortcuts: PropTypes.string,
|
|
62
|
+
accessibilityLabelledBy: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
|
|
63
|
+
accessibilityLevel: PropTypes.number,
|
|
64
|
+
accessibilityModal: PropTypes.bool,
|
|
65
|
+
accessibilityMultiline: PropTypes.bool,
|
|
66
|
+
accessibilityMultiSelectable: PropTypes.bool,
|
|
67
|
+
accessibilityOrientation: PropTypes.oneOf(['horizontal', 'vertical']),
|
|
68
|
+
accessibilityOwns: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
|
|
69
|
+
accessibilityPlaceholder: PropTypes.string,
|
|
70
|
+
accessibilityPosInSet: PropTypes.number,
|
|
71
|
+
accessibilityPressed: PropTypes.bool,
|
|
72
|
+
accessibilityReadOnly: PropTypes.bool,
|
|
73
|
+
accessibilityRequired: PropTypes.bool,
|
|
74
|
+
accessibilityRoleDescription: PropTypes.string,
|
|
75
|
+
accessibilityRowCount: PropTypes.number,
|
|
76
|
+
accessibilityRowIndex: PropTypes.number,
|
|
77
|
+
accessibilityRowSpan: PropTypes.number,
|
|
78
|
+
accessibilitySelected: PropTypes.bool,
|
|
79
|
+
accessibilitySetSize: PropTypes.number,
|
|
80
|
+
accessibilitySort: PropTypes.oneOf(['ascending', 'descending', 'none', 'other']),
|
|
81
|
+
accessibilityValueMax: PropTypes.number,
|
|
82
|
+
accessibilityValueMin: PropTypes.number,
|
|
83
|
+
accessibilityValueNow: PropTypes.number,
|
|
84
|
+
accessibilityValueText: PropTypes.string
|
|
85
|
+
};
|
|
86
|
+
export default {
|
|
87
|
+
/**
|
|
88
|
+
* Proptypes for recognised React Native accessiblity (a11y) props.
|
|
89
|
+
* Spread this in the propTypes of components that accept React Native a11y props.
|
|
90
|
+
*/
|
|
91
|
+
types: a11yPropTypes,
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Filters a props object, returning only recognised React Native accessiblity (a11y) props.
|
|
95
|
+
*
|
|
96
|
+
* Where components accept React Native a11y props, pass { ...rest } from its props to this,
|
|
97
|
+
* then spread the returned object into the component's props (usually its outer container).
|
|
98
|
+
*/
|
|
99
|
+
select: getPropSelector(a11yPropTypes, /^aria-/),
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Use this to disable focus for elements which are visually hidden but still rendered.
|
|
103
|
+
*/
|
|
104
|
+
nonFocusableProps: {
|
|
105
|
+
focusable: false,
|
|
106
|
+
// for android, and for keyboard nav in web
|
|
107
|
+
...Platform.select({
|
|
108
|
+
web: {
|
|
109
|
+
accessibilityHidden: true // web screenreaders
|
|
110
|
+
|
|
111
|
+
},
|
|
112
|
+
android: {
|
|
113
|
+
importantForAccessibility: 'no-hide-descendants'
|
|
114
|
+
},
|
|
115
|
+
ios: {
|
|
116
|
+
accessibilityElementsHidden: true
|
|
117
|
+
}
|
|
118
|
+
})
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Generates an object of platform-appropriate a11y props describing an item that has an
|
|
123
|
+
* ordered position in a set. Examples of usage by accessibility tools includes screenreaders
|
|
124
|
+
* saying "Item X of Y" when this item is select.
|
|
125
|
+
*
|
|
126
|
+
* @param {number} itemsCount - the number of items in the set
|
|
127
|
+
* @param {number} index - the current item's index in the set
|
|
128
|
+
* @returns {object} - platform-applicable a11y props describing this position (if available)
|
|
129
|
+
*/
|
|
130
|
+
getPositionInSet: (itemsCount, index) => Platform.select({
|
|
131
|
+
web: {
|
|
132
|
+
// accessibilityPosInSet etc exists in React Native Web main branch
|
|
133
|
+
// but not in a release compatible with Expo etc; just use `aria-*`
|
|
134
|
+
'aria-setsize': itemsCount,
|
|
135
|
+
'aria-posinset': index + 1
|
|
136
|
+
},
|
|
137
|
+
// No equivalents exist on the native side
|
|
138
|
+
default: {}
|
|
139
|
+
})
|
|
140
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import PropTypes from 'prop-types';
|
|
2
|
+
const clickHandlerMapping = {
|
|
3
|
+
onClick: 'onPress',
|
|
4
|
+
mouseDown: 'onPressIn',
|
|
5
|
+
mouseUp: 'onPressOut'
|
|
6
|
+
};
|
|
7
|
+
export default {
|
|
8
|
+
/**
|
|
9
|
+
* Web-oriented HTML click handlers that may be mapped to React Native press handlers
|
|
10
|
+
*/
|
|
11
|
+
types: Object.fromEntries(Object.keys(clickHandlerMapping).map(mouseName => [mouseName, PropTypes.func])),
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Takes a set of props and converts HTML mouse click oriented event handlers to closest
|
|
15
|
+
* equivalent React Native press event handler.
|
|
16
|
+
*
|
|
17
|
+
* Use this when a component that expects press-oriented props may need to support third-party
|
|
18
|
+
* web-oriented tooling that injects web-oriented event handlers directly. For example, for
|
|
19
|
+
* to support use with NextJS's 'next/link' component, which injects `onClick` prop into its child.
|
|
20
|
+
*/
|
|
21
|
+
toPressProps: props => Object.fromEntries(Object.entries(props).map(([originalName, value]) => {
|
|
22
|
+
const translatedName = clickHandlerMapping[originalName];
|
|
23
|
+
return translatedName ? [translatedName, value] : [originalName, value];
|
|
24
|
+
}))
|
|
25
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a prop type validator which checks whether a prop is either a component or an array of
|
|
3
|
+
* components of a given type, based on their `name` or `displayName` properties.
|
|
4
|
+
* Use an array of strings for `passedName` to accept more than one component type.
|
|
5
|
+
* For an array the validation fails on the first occurrence of an invalid element.
|
|
6
|
+
*
|
|
7
|
+
* @param {string|string[]} passedName
|
|
8
|
+
* @return {function}
|
|
9
|
+
*/
|
|
10
|
+
export default function componentPropType(passedName) {
|
|
11
|
+
const passedNames = typeof passedName === 'string' ? [passedName] : passedName;
|
|
12
|
+
|
|
13
|
+
const checkProp = (props, propName, componentName) => {
|
|
14
|
+
var _props$propName$type, _props$propName$type2;
|
|
15
|
+
|
|
16
|
+
if (props[propName] === undefined || props[propName] === null) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (Array.isArray(props[propName])) {
|
|
21
|
+
// Iterates through every child and try to find the first element that does not match the passed name
|
|
22
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
|
23
|
+
return props[propName].map((_, index) => checkProp(props[propName], index, componentName)).find(Boolean);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const nameInProp = ((_props$propName$type = props[propName].type) === null || _props$propName$type === void 0 ? void 0 : _props$propName$type.displayName) || ((_props$propName$type2 = props[propName].type) === null || _props$propName$type2 === void 0 ? void 0 : _props$propName$type2.name);
|
|
27
|
+
|
|
28
|
+
if (!nameInProp || !passedNames.includes(nameInProp)) {
|
|
29
|
+
const propDescription = nameInProp ? `Component ${nameInProp}` : typeof props[propName];
|
|
30
|
+
return new Error(`${componentName}: ${propDescription} was passed to \`${propName}\` prop; should be ${passedNames.join(' or ')}`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return undefined;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const checkRequired = (props, propName, componentName) => {
|
|
37
|
+
if (props[propName] === undefined) {
|
|
38
|
+
return new Error(`The prop \`${propName}\` is marked as required in \`${componentName}\`, but its value is ${props[propName]}.`);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return undefined;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const createValidate = isRequired => {
|
|
45
|
+
if (isRequired) {
|
|
46
|
+
return (props, propName, componentName) => {
|
|
47
|
+
const checkForError = checkProp(props, propName, componentName);
|
|
48
|
+
|
|
49
|
+
if (checkForError) {
|
|
50
|
+
return checkForError;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return checkRequired(props, propName, componentName);
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return checkProp;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const validate = createValidate();
|
|
61
|
+
validate.isRequired = createValidate(true);
|
|
62
|
+
return validate;
|
|
63
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export default function getPropSelector(propTypes, regexp) {
|
|
2
|
+
const keys = Object.keys(propTypes);
|
|
3
|
+
return props => Object.entries(props).reduce((items, [key, value]) => keys.includes(key) || regexp && regexp.test(key) ? { ...items,
|
|
4
|
+
[key]: value
|
|
5
|
+
} : items, {});
|
|
6
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import PropTypes from 'prop-types'; // Props related to HTML <a> anchor link attributes.
|
|
2
|
+
|
|
3
|
+
const targetValues = ['_self', '_blank', '_parent', '_top'];
|
|
4
|
+
export default {
|
|
5
|
+
types: {
|
|
6
|
+
// React Native Web >= 0.15.0 supports hrefAttrs prop with only these properties
|
|
7
|
+
// and passes them to <a> if an element has a href prop or accessibilityRole "link"
|
|
8
|
+
download: PropTypes.string,
|
|
9
|
+
rel: PropTypes.string,
|
|
10
|
+
target: PropTypes.oneOf(targetValues)
|
|
11
|
+
},
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Takes a props object, bundles any hrefAttrs props present into one object
|
|
15
|
+
* and returns that keyed as `hrefAttrs` and non-hrefAttrs props keyed as `rest`
|
|
16
|
+
*/
|
|
17
|
+
bundle: ({
|
|
18
|
+
download,
|
|
19
|
+
rel,
|
|
20
|
+
target,
|
|
21
|
+
...rest
|
|
22
|
+
}) => ({
|
|
23
|
+
hrefAttrs: {
|
|
24
|
+
download,
|
|
25
|
+
rel,
|
|
26
|
+
target
|
|
27
|
+
},
|
|
28
|
+
rest
|
|
29
|
+
})
|
|
30
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export * from './tokens';
|
|
2
|
+
export { default as a11yProps } from './a11yProps';
|
|
3
|
+
export { default as clickProps } from './clickProps';
|
|
4
|
+
export { default as copyPropTypes } from './copyPropTypes';
|
|
5
|
+
export { default as componentPropType } from './componentPropType';
|
|
6
|
+
export { default as hrefAttrsProp } from './hrefAttrsProp';
|
|
7
|
+
export { default as inputSupportsProps } from './inputSupportsProps';
|
|
8
|
+
export { default as linkProps } from './linkProps';
|
|
9
|
+
export { default as pressProps } from './pressProps';
|
|
10
|
+
export { default as paddingProp } from './paddingProp';
|
|
11
|
+
export { default as rectProp } from './rectProp';
|
|
12
|
+
export { default as responsiveProps } from './responsiveProps';
|
|
13
|
+
export { default as spacingProps } from './spacingProps';
|
|
14
|
+
export { default as selectSystemProps } from './selectSystemProps';
|
|
15
|
+
export { default as variantProp } from './variantProp';
|
|
16
|
+
export { default as viewProps } from './viewProps';
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import PropTypes from 'prop-types';
|
|
2
|
+
export default {
|
|
3
|
+
types: {
|
|
4
|
+
/**
|
|
5
|
+
* The input label.
|
|
6
|
+
*/
|
|
7
|
+
label: PropTypes.string,
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A short description of the expected input.
|
|
11
|
+
*/
|
|
12
|
+
hint: PropTypes.string,
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Position of the hint relative to label. Use `below` to display a larger hint below the label.
|
|
16
|
+
*/
|
|
17
|
+
hintPosition: PropTypes.oneOf(['inline', 'below']),
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A detailed description of validation error/success or additional instructions.
|
|
21
|
+
* Visual variant is determined based on the `validation` prop.
|
|
22
|
+
*/
|
|
23
|
+
feedback: PropTypes.string,
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Content of an optional `Tooltip`. If set, a tooltip button will be shown next to the label.
|
|
27
|
+
*/
|
|
28
|
+
tooltip: PropTypes.string,
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Use to visually mark an input as valid or invalid.
|
|
32
|
+
*/
|
|
33
|
+
validation: PropTypes.oneOf(['error', 'success'])
|
|
34
|
+
},
|
|
35
|
+
select: ({
|
|
36
|
+
label,
|
|
37
|
+
hint,
|
|
38
|
+
hintPosition,
|
|
39
|
+
feedback,
|
|
40
|
+
tooltip,
|
|
41
|
+
validation,
|
|
42
|
+
...rest
|
|
43
|
+
}) => ({
|
|
44
|
+
props: {
|
|
45
|
+
label,
|
|
46
|
+
hint,
|
|
47
|
+
hintPosition,
|
|
48
|
+
feedback,
|
|
49
|
+
tooltip,
|
|
50
|
+
validation
|
|
51
|
+
},
|
|
52
|
+
...rest
|
|
53
|
+
})
|
|
54
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import Linking from "react-native-web/dist/exports/Linking";
|
|
2
|
+
import Platform from "react-native-web/dist/exports/Platform";
|
|
3
|
+
import PropTypes from 'prop-types';
|
|
4
|
+
import { pressPropTypes } from './pressProps';
|
|
5
|
+
import a11yProps from './a11yProps';
|
|
6
|
+
import hrefAttrsProp from './hrefAttrsProp';
|
|
7
|
+
import getPropSelector from './getPropSelector';
|
|
8
|
+
const linkPropTypes = { ...pressPropTypes,
|
|
9
|
+
href: PropTypes.string,
|
|
10
|
+
hrefAttrs: PropTypes.shape(hrefAttrsProp.types),
|
|
11
|
+
...a11yProps.types
|
|
12
|
+
};
|
|
13
|
+
export default {
|
|
14
|
+
/**
|
|
15
|
+
* Proptypes for components that, on Web, can output <a href="..."> link elements
|
|
16
|
+
*/
|
|
17
|
+
types: linkPropTypes,
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Filters a props object, returning only the platform-relevant link props defined above
|
|
21
|
+
*/
|
|
22
|
+
select: getPropSelector(linkPropTypes),
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Turn hrefs into press handlers on Native and throw if not given `onPress` or `href`.
|
|
26
|
+
*
|
|
27
|
+
* @param {({ onPress?: () => void, href?: string })}
|
|
28
|
+
* @returns {(() => void)|undefined} Returns a press handler, or undefined on web if href
|
|
29
|
+
* string is provided. Expects calling component to use href string on web (e.g. in `<a>`).
|
|
30
|
+
*/
|
|
31
|
+
handleHref: ({
|
|
32
|
+
onPress,
|
|
33
|
+
href
|
|
34
|
+
}) => {
|
|
35
|
+
if (!href && !onPress) {
|
|
36
|
+
throw new Error('handleHref requires either href or onPress');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return Platform.select({
|
|
40
|
+
web: onPress,
|
|
41
|
+
default: (...args) => {
|
|
42
|
+
if (onPress) onPress(...args);
|
|
43
|
+
if (href) Linking.openURL(href);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
};
|