@telus-uds/components-base 1.0.1 → 1.3.0
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/.storybook/main.js +4 -0
- package/.storybook/preview.js +37 -0
- package/.turbo/turbo-build.log +5 -0
- package/CHANGELOG.md +32 -0
- package/README.md +1 -1
- package/__fixtures__/Accessible.js +4 -2
- package/__fixtures__/Accessible.native.js +5 -2
- package/__fixtures__/testTheme.js +9 -0
- package/__tests__/FlexGrid/Col.test.jsx +6 -10
- package/__tests__/HorizontalScroll/HorizontalScroll.test.jsx +1 -0
- package/__tests__/ToggleSwitch/ToggleSwitch.test.jsx +10 -0
- package/__tests__/ToggleSwitch/ToggleSwitchGroup.test.jsx +192 -0
- package/__tests__/utils/props.test.js +36 -0
- package/__tests__/utils/semantics.test.jsx +1 -10
- package/babel.config.js +9 -16
- package/component-docs.json +10142 -0
- package/generate-component-docs.js +59 -0
- package/lib/A11yText/index.js +2 -2
- package/lib/ActivityIndicator/index.js +3 -3
- package/lib/Box/Box.js +4 -4
- package/lib/Button/Button.js +2 -2
- package/lib/Button/ButtonBase.js +20 -6
- package/lib/Button/ButtonGroup.js +10 -12
- package/lib/Button/ButtonLink.js +4 -4
- package/lib/Button/propTypes.js +4 -2
- package/lib/Card/Card.js +2 -2
- package/lib/Card/CardBase.js +6 -9
- package/lib/Card/PressableCardBase.js +12 -6
- package/lib/Checkbox/Checkbox.js +3 -5
- package/lib/Divider/Divider.js +2 -2
- package/lib/FlexGrid/FlexGrid.js +4 -4
- package/lib/FlexGrid/helpers/index.js +1 -4
- package/lib/IconButton/IconButton.js +11 -6
- package/lib/Link/LinkBase.js +19 -12
- package/lib/List/List.js +4 -5
- package/lib/List/ListItem.js +14 -27
- package/lib/List/index.js +15 -0
- package/lib/Pagination/PageButton.js +4 -8
- package/lib/Pagination/Pagination.js +16 -4
- package/lib/Pagination/SideButton.js +3 -1
- package/lib/Progress/Progress.js +2 -2
- package/lib/Progress/ProgressBar.js +2 -2
- package/lib/Radio/Radio.js +3 -5
- package/lib/Spacer/Spacer.js +2 -2
- package/lib/StackView/StackWrap.js +9 -5
- package/lib/StackView/getStackedContent.js +1 -1
- package/lib/StepTracker/StepTracker.js +2 -2
- package/lib/Tabs/Tabs.js +12 -4
- package/lib/Tabs/TabsItem.js +12 -6
- package/lib/Tags/Tags.js +9 -9
- package/lib/ThemeProvider/useThemeTokens.js +3 -3
- package/lib/ThemeProvider/utils/theme-tokens.js +3 -3
- package/lib/ToggleSwitch/ToggleSwitch.js +105 -43
- package/lib/ToggleSwitch/ToggleSwitchGroup.js +230 -0
- package/lib/ToggleSwitch/index.js +14 -4
- package/lib/index.js +28 -9
- package/lib/utils/a11y/semantics.js +4 -3
- package/lib/utils/index.js +14 -5
- package/lib/utils/pressability.js +2 -2
- package/lib/utils/props/a11yProps.js +153 -0
- package/lib/utils/props/clickProps.js +36 -0
- package/lib/utils/props/componentPropType.js +70 -0
- package/lib/utils/props/copyPropTypes.js +14 -0
- package/lib/utils/props/getPropSelector.js +13 -0
- package/lib/utils/props/hrefAttrsProp.js +41 -0
- package/lib/utils/props/index.js +149 -0
- package/lib/utils/props/linkProps.js +64 -0
- package/lib/utils/props/paddingProp.js +20 -0
- package/lib/utils/props/pressProps.js +57 -0
- package/lib/utils/props/rectProp.js +20 -0
- package/lib/utils/props/responsiveProps.js +40 -0
- package/lib/utils/props/selectSystemProps.js +31 -0
- package/lib/utils/props/spacingProps.js +71 -0
- package/lib/utils/props/tokens.js +145 -0
- package/lib/utils/props/variantProp.js +28 -0
- package/lib/utils/props/viewProps.js +34 -0
- package/lib/utils/useResponsiveProp.js +1 -1
- package/lib/utils/useSpacingScale.js +4 -4
- package/lib/utils/withLinkRouter.js +98 -0
- package/package.json +9 -5
- package/release-context.json +4 -4
- package/src/A11yText/index.jsx +1 -1
- package/src/ActivityIndicator/index.jsx +1 -1
- package/src/Box/Box.jsx +5 -4
- package/src/Button/Button.jsx +1 -1
- package/src/Button/ButtonBase.jsx +11 -4
- package/src/Button/ButtonGroup.jsx +17 -8
- package/src/Button/ButtonLink.jsx +1 -1
- package/src/Button/propTypes.js +2 -1
- package/src/Card/Card.jsx +1 -1
- package/src/Card/CardBase.jsx +6 -5
- package/src/Card/PressableCardBase.jsx +7 -5
- package/src/Checkbox/Checkbox.jsx +1 -3
- package/src/Divider/Divider.jsx +2 -2
- package/src/FlexGrid/FlexGrid.jsx +11 -5
- package/src/FlexGrid/helpers/index.js +1 -3
- package/src/IconButton/IconButton.jsx +7 -5
- package/src/Link/LinkBase.jsx +7 -5
- package/src/List/List.jsx +2 -4
- package/src/List/ListItem.jsx +11 -26
- package/src/List/index.js +5 -0
- package/src/Pagination/PageButton.jsx +5 -8
- package/src/Pagination/Pagination.jsx +29 -2
- package/src/Pagination/SideButton.jsx +2 -2
- package/src/Progress/Progress.jsx +1 -1
- package/src/Progress/ProgressBar.jsx +1 -1
- package/src/Radio/Radio.jsx +1 -3
- package/src/Spacer/Spacer.jsx +2 -2
- package/src/StackView/StackWrap.jsx +7 -6
- package/src/StackView/getStackedContent.jsx +1 -1
- package/src/StepTracker/StepTracker.jsx +1 -1
- package/src/Tabs/Tabs.jsx +49 -22
- package/src/Tabs/TabsItem.jsx +11 -7
- package/src/Tags/Tags.jsx +1 -7
- package/src/ThemeProvider/useThemeTokens.js +3 -3
- package/src/ThemeProvider/utils/theme-tokens.js +3 -3
- package/src/ToggleSwitch/ToggleSwitch.jsx +93 -41
- package/src/ToggleSwitch/ToggleSwitchGroup.jsx +203 -0
- package/src/ToggleSwitch/index.js +2 -1
- package/src/index.js +2 -2
- package/src/utils/a11y/semantics.js +3 -2
- package/src/utils/index.js +2 -1
- package/src/utils/pressability.js +1 -1
- package/src/utils/props/a11yProps.js +151 -0
- package/src/utils/props/clickProps.js +31 -0
- package/src/utils/props/componentPropType.js +67 -0
- package/src/utils/props/copyPropTypes.js +3 -0
- package/src/utils/props/getPropSelector.js +14 -0
- package/src/utils/props/hrefAttrsProp.js +25 -0
- package/src/utils/props/index.js +15 -0
- package/src/utils/props/linkProps.js +43 -0
- package/src/utils/props/paddingProp.js +10 -0
- package/src/utils/props/pressProps.js +45 -0
- package/src/utils/props/rectProp.js +10 -0
- package/src/utils/props/responsiveProps.js +30 -0
- package/src/utils/props/selectSystemProps.js +25 -0
- package/src/utils/props/spacingProps.js +58 -0
- package/src/utils/props/tokens.js +150 -0
- package/src/utils/props/variantProp.js +20 -0
- package/src/utils/props/viewProps.js +23 -0
- package/src/utils/useResponsiveProp.js +1 -1
- package/src/utils/useSpacingScale.js +4 -4
- package/src/utils/withLinkRouter.jsx +68 -0
- package/stories/A11yText/A11yText.stories.jsx +1 -1
- package/stories/ActivityIndicator/ActivityIndicator.stories.jsx +1 -1
- package/stories/Box/Box.stories.jsx +1 -1
- package/stories/Button/Button.stories.jsx +1 -1
- package/stories/Button/ButtonGroup.stories.jsx +1 -1
- package/stories/Button/ButtonLink.stories.jsx +1 -1
- package/stories/Card/Card.stories.jsx +1 -1
- package/stories/Checkbox/Checkbox.stories.jsx +1 -1
- package/stories/Divider/Divider.stories.jsx +1 -1
- package/stories/ExpandCollapse/ExpandCollapse.stories.jsx +1 -1
- package/stories/Feedback/Feedback.stories.jsx +1 -1
- package/stories/FlexGrid/01 FlexGrid.stories.jsx +1 -1
- package/stories/FlexGrid/02 Row.stories.jsx +1 -1
- package/stories/FlexGrid/03 Col.stories.jsx +1 -1
- package/stories/Icon/Icon.stories.jsx +1 -1
- package/stories/IconButton/IconButton.stories.jsx +1 -1
- package/stories/InputLabel/InputLabel.stories.jsx +1 -1
- package/stories/Link/ChevronLink.stories.jsx +1 -1
- package/stories/Link/Link.stories.jsx +1 -1
- package/stories/Link/TextButton.stories.jsx +1 -1
- package/stories/List/List.stories.jsx +1 -1
- package/stories/Modal/Modal.stories.jsx +1 -1
- package/stories/Notification/Notification.stories.jsx +1 -1
- package/stories/Pagination/Pagination.stories.jsx +1 -1
- package/stories/Progress/Progress.stories.jsx +1 -1
- package/stories/Radio/Radio.stories.jsx +1 -1
- package/stories/RadioCard/RadioCard.stories.jsx +1 -1
- package/stories/Search/Search.stories.jsx +1 -1
- package/stories/Select/Select.stories.jsx +1 -1
- package/stories/SideNav/SideNav.stories.jsx +1 -1
- package/stories/SideNav/SideNavItem.stories.jsx +1 -1
- package/stories/SideNav/SideNavItemsGroup.stories.jsx +1 -1
- package/stories/Skeleton/Skeleton.stories.jsx +1 -1
- package/stories/Spacer/Spacer.stories.jsx +1 -1
- package/stories/StackView/StackView.stories.jsx +1 -1
- package/stories/StackView/StackWrap.stories.jsx +1 -1
- package/stories/StepTracker/StepTracker.stories.jsx +1 -1
- package/stories/Tabs/Tabs.stories.jsx +1 -1
- package/stories/Tags/Tags.stories.jsx +1 -1
- package/stories/TextInput/TextArea.stories.jsx +2 -1
- package/stories/TextInput/TextInput.stories.jsx +1 -1
- package/stories/ToggleSwitch/ToggleSwitch.stories.jsx +6 -2
- package/stories/ToggleSwitch/ToggleSwitchGroup.stories.jsx +81 -0
- package/stories/Tooltip/Tooltip.stories.jsx +1 -1
- package/stories/TooltipButton/TooltipButton.stories.jsx +1 -1
- package/stories/Typography/Typography.stories.jsx +1 -1
- package/stories/supports.jsx +2 -3
- package/.ultra.cache.json +0 -1
- package/lib/utils/propTypes.js +0 -623
- package/src/utils/propTypes.js +0 -640
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import PropTypes from 'prop-types'
|
|
2
|
+
import { components as tokenKeys } from '@telus-uds/system-theme-tokens'
|
|
3
|
+
|
|
4
|
+
// Tokens can be primitive values (e.g. `'rgba(0,0,0,0'`, `12`), or objects of such values,
|
|
5
|
+
// such as tokens that describe shadow (e.g. shadow: { inset: true, color: 'rgba(...)' ... }),
|
|
6
|
+
// or components (e.g. Icon tokens)
|
|
7
|
+
const tokenValue = PropTypes.oneOfType([
|
|
8
|
+
PropTypes.string,
|
|
9
|
+
PropTypes.number,
|
|
10
|
+
PropTypes.bool,
|
|
11
|
+
PropTypes.elementType
|
|
12
|
+
])
|
|
13
|
+
const tokenValueType = PropTypes.oneOfType([tokenValue, PropTypes.objectOf(tokenValue)])
|
|
14
|
+
|
|
15
|
+
export const getTokenNames = (componentName) => {
|
|
16
|
+
const componentTokenSchema = tokenKeys[componentName]
|
|
17
|
+
if (!componentTokenSchema) {
|
|
18
|
+
throw new Error(`No "${componentName}" tokenKeys in @telus-uds/system-theme-tokens`)
|
|
19
|
+
}
|
|
20
|
+
return Object.keys(componentTokenSchema)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Returns the subset of a set of tokens that may be accepted by the `tokens` prop of a named component
|
|
25
|
+
* or by a provided array of tokens. A prefix may be provided, for example:
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```jsx
|
|
29
|
+
* // returns tokens from `themeTokens` that exist in the theme schema for `Button`.
|
|
30
|
+
* selectTokens('Button', themeTokens)
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```jsx
|
|
35
|
+
* // returns `{ backgroundColor, width }` where the values of those keys come from
|
|
36
|
+
* // the source object's `{ containerBackgroundColor, containerWidth }` properties.
|
|
37
|
+
* selectTokens(['backgroundColor', 'width'], themeTokens, 'container')
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```jsx
|
|
42
|
+
* // returns tokens that are defined in the `Button` theme schema, from tokens with
|
|
43
|
+
* // a prefix `'button'` e.g. a token `buttonBorderWidth` outputs as `borderWidth`.
|
|
44
|
+
* selectTokens('Button', themeTokens, 'button')
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @param {string[]|string} specifier - a name of a component in the theme schema, or an array of token names
|
|
48
|
+
* @param {object} tokens - a source object of theme tokens
|
|
49
|
+
* @param {string} [prefix] - if provided, matches keys in the source object with this as the first camelCase item
|
|
50
|
+
* @returns {object} - subset of theme tokens
|
|
51
|
+
*/
|
|
52
|
+
export const selectTokens = (specifier, tokens, prefix) => {
|
|
53
|
+
const tokenNames = typeof specifier === 'string' ? getTokenNames(specifier) : specifier
|
|
54
|
+
const filteredTokens = tokenNames.reduce((validTokens, key) => {
|
|
55
|
+
const prefixedKey = prefix ? `${prefix}${key[0].toUpperCase()}${key.slice(1)}` : key
|
|
56
|
+
const token = tokens[prefixedKey]
|
|
57
|
+
return token !== undefined ? { ...validTokens, [key]: token } : validTokens
|
|
58
|
+
}, {})
|
|
59
|
+
return filteredTokens
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @typedef {string|number|boolean|{[key: string]:string|number|boolean}} TokenValue
|
|
64
|
+
* @typedef {{[key: string]: TokenValue}} TokensSet
|
|
65
|
+
* @typedef {(AppearanceSet) => TokensSet} TokensGetter
|
|
66
|
+
* @typedef {TokensSet|TokensGetter} TokensProp
|
|
67
|
+
*/
|
|
68
|
+
/**
|
|
69
|
+
* 'tokens' is an optional object prop on all themable components. Its keys must match the
|
|
70
|
+
* token keys in the component's theme schema.
|
|
71
|
+
*
|
|
72
|
+
* This prop is intended to be used as an 'escape hatch' for difficult or exceptional cases
|
|
73
|
+
* where the main theming system doesn't apply. It is intentionally permissive about values.
|
|
74
|
+
*
|
|
75
|
+
* @param {...string} componentsNames - one or more ComponentName, which tokens keys are accepted
|
|
76
|
+
* @return {function} - a custom PropTypes validator
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* Component.propTypes = {
|
|
80
|
+
* // accepts all tokens keys defined in Component schema
|
|
81
|
+
* tokens: getTokensPropType('Component')
|
|
82
|
+
* }
|
|
83
|
+
*
|
|
84
|
+
* Component.propTypes = {
|
|
85
|
+
* // accepts all tokens keys defined in schemas for Component1 and Component2
|
|
86
|
+
* tokens: getTokensPropType('Component1', 'Component2')
|
|
87
|
+
* }
|
|
88
|
+
*/
|
|
89
|
+
export const getTokensPropType = (...componentsNames) => (props, propName, componentName) => {
|
|
90
|
+
PropTypes.checkPropTypes(
|
|
91
|
+
{
|
|
92
|
+
[propName]: PropTypes.oneOfType([PropTypes.object, PropTypes.func])
|
|
93
|
+
},
|
|
94
|
+
props,
|
|
95
|
+
propName,
|
|
96
|
+
componentName
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
if (typeof props[propName] !== 'function') {
|
|
100
|
+
PropTypes.checkPropTypes(
|
|
101
|
+
{
|
|
102
|
+
[propName]: PropTypes.exact(
|
|
103
|
+
Object.fromEntries(
|
|
104
|
+
componentsNames.flatMap((component) =>
|
|
105
|
+
getTokenNames(component).map((key) => [key, tokenValueType])
|
|
106
|
+
)
|
|
107
|
+
)
|
|
108
|
+
)
|
|
109
|
+
},
|
|
110
|
+
props,
|
|
111
|
+
propName,
|
|
112
|
+
componentName
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Get a proptypes validator for a set of tokens that is not based on a component in the theme schema.
|
|
119
|
+
*
|
|
120
|
+
* For example, for a set of tokens used in a common style, or for a set of tokens required by
|
|
121
|
+
* a themeless component whose tokens are set by a parent but requires tokens of a certain shape.
|
|
122
|
+
*
|
|
123
|
+
* By default, requires an object with a complete set of tokens (allowing `null`, but not `undefined`).
|
|
124
|
+
*
|
|
125
|
+
* @param {string[]} componentTokenKeys - array of strings of token names
|
|
126
|
+
* @param {object} [options]
|
|
127
|
+
* @param {boolean} [options.partial] - if true, allows tokens to be undefined
|
|
128
|
+
* @param {boolean} [options.allowFunction] - if true, allows functions as well as tokens objects
|
|
129
|
+
* @returns
|
|
130
|
+
*/
|
|
131
|
+
export const getTokensSetPropType = (
|
|
132
|
+
componentTokenKeys,
|
|
133
|
+
{ partial = false, allowFunction = false } = {}
|
|
134
|
+
) => {
|
|
135
|
+
const tokensObjectValidator = PropTypes.exact(
|
|
136
|
+
Object.fromEntries(
|
|
137
|
+
componentTokenKeys.map((key) => [
|
|
138
|
+
key,
|
|
139
|
+
partial
|
|
140
|
+
? tokenValueType
|
|
141
|
+
: // Some theme tokens can validly resolve to `null`, but .isRequired treats null as undefined
|
|
142
|
+
(props, propName, ...args) =>
|
|
143
|
+
props[propName] !== null && tokenValueType.isRequired(props, propName, ...args)
|
|
144
|
+
])
|
|
145
|
+
)
|
|
146
|
+
)
|
|
147
|
+
return allowFunction
|
|
148
|
+
? PropTypes.oneOfType([tokensObjectValidator, PropTypes.func])
|
|
149
|
+
: tokensObjectValidator
|
|
150
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import PropTypes from 'prop-types'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {{[key: string]: string|number|boolean}} AppearanceSet
|
|
5
|
+
* @typedef {AppearanceSet} VariantProp
|
|
6
|
+
*/
|
|
7
|
+
export default {
|
|
8
|
+
/**
|
|
9
|
+
* 'variant' is an optional object prop on all themable components.
|
|
10
|
+
*
|
|
11
|
+
* Contains an object with keys that correspond to the current component theme's allowed
|
|
12
|
+
* appearances and values that correspond to the allowed values of that component.
|
|
13
|
+
*
|
|
14
|
+
* Since the particular keys and values depend on which theme is currently active,
|
|
15
|
+
* the exact shape of variant props is not enforced using PropTypes.
|
|
16
|
+
*/
|
|
17
|
+
propType: PropTypes.objectOf(
|
|
18
|
+
PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool])
|
|
19
|
+
)
|
|
20
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import PropTypes from 'prop-types'
|
|
2
|
+
import getPropSelector from './getPropSelector'
|
|
3
|
+
|
|
4
|
+
const viewPropTypes = {
|
|
5
|
+
pointerEvents: PropTypes.oneOf(['all', 'none', 'box-only', 'box-none']),
|
|
6
|
+
onLayout: PropTypes.func,
|
|
7
|
+
nativeID: PropTypes.string,
|
|
8
|
+
testID: PropTypes.string,
|
|
9
|
+
dataSet: PropTypes.object
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default {
|
|
13
|
+
/**
|
|
14
|
+
* Subset of `View` proptypes that could conceivably be needed on any component
|
|
15
|
+
* that renders a single View.
|
|
16
|
+
*/
|
|
17
|
+
types: viewPropTypes,
|
|
18
|
+
/**
|
|
19
|
+
* Filters a props object, returning only cross-platform View props that are potentially
|
|
20
|
+
* relevant to any basic layout component that renders one View.
|
|
21
|
+
*/
|
|
22
|
+
select: getPropSelector(viewPropTypes)
|
|
23
|
+
}
|
|
@@ -36,7 +36,7 @@ export const resolveResponsiveProp = (prop, viewport, defaultValue) => {
|
|
|
36
36
|
* For example, if passed `{ sm: 4, lg: 8 }`, it will return 8 on `xl` and `lg` viewports,
|
|
37
37
|
* 4 on `md` and `sm` viewports, and `undefined` (or a provided `defaultValue`) on `xs` viewports.
|
|
38
38
|
*
|
|
39
|
-
* To generate propTypes for responsive props, see `responsiveProps` in `./utils/
|
|
39
|
+
* To generate propTypes for responsive props, see `responsiveProps` in `./utils/props/responsiveProps.js`.
|
|
40
40
|
*
|
|
41
41
|
* @param {*} prop - any value which may be an object with viewport keys
|
|
42
42
|
* @param {*} [defaultValue] - default value to provide if no matches found for the current viewport
|
|
@@ -3,10 +3,10 @@ import { useThemeTokens } from '../ThemeProvider'
|
|
|
3
3
|
import { resolveResponsiveProp } from './useResponsiveProp'
|
|
4
4
|
/**
|
|
5
5
|
* @typedef {import('@telus-uds/system-constants/viewports').Viewport} Viewport
|
|
6
|
-
* @typedef {import('./
|
|
7
|
-
* @typedef {import('./
|
|
8
|
-
* @typedef {import('./
|
|
9
|
-
* @typedef {import('./
|
|
6
|
+
* @typedef {import('./props/spacingProps.js').SpacingValue} SpacingValue
|
|
7
|
+
* @typedef {import('./props/spacingProps.js').SpacingIndex} SpacingIndex
|
|
8
|
+
* @typedef {import('./props/spacingProps.js').SpacingObject} SpacingObject
|
|
9
|
+
* @typedef {import('./props/spacingProps.js').SpacingOptions} SpacingOptions
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
const resolveSpacingOptions = (space) => {
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import React, { forwardRef } from 'react'
|
|
2
|
+
import PropTypes from 'prop-types'
|
|
3
|
+
|
|
4
|
+
// Prototype-safe alternative to (linter-forbidden) someObject.hasOwnProperty()
|
|
5
|
+
const hasOwnProperty = (object, prop) => Object.prototype.hasOwnProperty.call(object, prop)
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Higher-order component that has no effect unless an additional prop `LinkRouter` is passed.
|
|
9
|
+
* This may be used to provide custom wrappers for integrations with third party libraries.
|
|
10
|
+
*
|
|
11
|
+
* If LinkRouter is passed, LinkRouter is rendered in place of the main component and is passed:
|
|
12
|
+
*
|
|
13
|
+
* - `linkRouterProps`: an optional object passed alongside LinkRouter, for props needed by the wrapper
|
|
14
|
+
* that are not valid props for the wrapped component.
|
|
15
|
+
* - `Component`: automatically provided, the original component to render inside the wrapper.
|
|
16
|
+
* - `ref`: forwarded `ref` passed down to `Component`
|
|
17
|
+
* - All other props passed to the outer component
|
|
18
|
+
*
|
|
19
|
+
* @example A LinkRouter component to be used with link-like components might look like:
|
|
20
|
+
*
|
|
21
|
+
* ```jsx
|
|
22
|
+
* const LinkLinkRouter = forwardRef(({ Component, linkRouterProps: { to, options }, href, ...rest }, ref) => {
|
|
23
|
+
* const { href: resolvedHref, onClick } = useSomeRouterHook({ to, href, options })
|
|
24
|
+
* return <Component href={resolvedHref} onPress={onClick} {...rest} />
|
|
25
|
+
* })
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* Any component that takes href and onPress props may then use this wrapper:
|
|
29
|
+
*
|
|
30
|
+
* ```jsx
|
|
31
|
+
* <Link href={href} LinkRouter={LinkLinkRouter} linkRouterProps={{ to, options }}>Some link</Link>
|
|
32
|
+
* <IconButton icon={SomeIcon} LinkRouter={LinkLinkRouter} linkRouterProps={{ to, options }} ref={iconRef} />
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
const withLinkRouter = (Component) => {
|
|
36
|
+
const wrappedComponent = forwardRef(({ LinkRouter, linkRouterProps, ...props }, ref) => {
|
|
37
|
+
if (!LinkRouter) return <Component {...props} ref={ref} />
|
|
38
|
+
return (
|
|
39
|
+
<LinkRouter linkRouterProps={linkRouterProps} Component={Component} ref={ref} {...props} />
|
|
40
|
+
)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
// Ensure the returned component has appropriate outer properties set:
|
|
44
|
+
/* eslint-disable-next-line react/forbid-foreign-prop-types */
|
|
45
|
+
const { displayName, name, propTypes, ...otherProperties } = Component
|
|
46
|
+
|
|
47
|
+
// Apply unique component name as a displayName
|
|
48
|
+
wrappedComponent.displayName = Component.displayName || Component.name
|
|
49
|
+
|
|
50
|
+
// Apply proptypes including wrapper props - is safely { ...undefined, ...undefined } in prod
|
|
51
|
+
wrappedComponent.propTypes = { ...Component.propTypes, ...withLinkRouter.propTypes }
|
|
52
|
+
|
|
53
|
+
// Forward any other properties explicitly set e.g. Component.SubComponent
|
|
54
|
+
Object.keys(otherProperties).forEach((key) => {
|
|
55
|
+
// Skip internal React properties from wrappedComponent's forwardRef (render, $$typeof, etc)
|
|
56
|
+
if (hasOwnProperty(Component, key) && !hasOwnProperty(wrappedComponent, key)) {
|
|
57
|
+
wrappedComponent[key] = Component[key]
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
return wrappedComponent
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
withLinkRouter.propTypes = {
|
|
64
|
+
LinkRouter: PropTypes.elementType,
|
|
65
|
+
linkRouterProps: PropTypes.object
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export default withLinkRouter
|
|
@@ -6,7 +6,7 @@ import { Container, EachParentType, parentTypesParams } from '../supports'
|
|
|
6
6
|
import { useMultipleInputValues } from '../../src/utils'
|
|
7
7
|
|
|
8
8
|
export default {
|
|
9
|
-
title: '
|
|
9
|
+
title: 'ExpandCollapse',
|
|
10
10
|
component: ExpandCollapse,
|
|
11
11
|
args: {
|
|
12
12
|
children: () => {}
|
|
@@ -5,7 +5,7 @@ import { ChevronLink } from '../../src'
|
|
|
5
5
|
import { Container, EachParentType, parentTypesParams, useVariants } from '../supports'
|
|
6
6
|
|
|
7
7
|
export default {
|
|
8
|
-
title: '
|
|
8
|
+
title: 'ChevronLink',
|
|
9
9
|
component: ChevronLink,
|
|
10
10
|
args: {
|
|
11
11
|
href: 'https://example.com'
|
|
@@ -8,7 +8,7 @@ import { Link, Typography, variantProp } from '../../src'
|
|
|
8
8
|
import { Container, EachIcon, EachParentType, parentTypesParams, useVariants } from '../supports'
|
|
9
9
|
|
|
10
10
|
export default {
|
|
11
|
-
title: '
|
|
11
|
+
title: 'Link',
|
|
12
12
|
component: Link,
|
|
13
13
|
args: {
|
|
14
14
|
href: `https://telus.com/?unvisited:${Date.now()}`,
|
|
@@ -10,7 +10,7 @@ import { TextButton, variantProp } from '../../src'
|
|
|
10
10
|
import { Container, EachParentType, useVariants, parentTypesParams } from '../supports'
|
|
11
11
|
|
|
12
12
|
export default {
|
|
13
|
-
title: '
|
|
13
|
+
title: 'TextButton',
|
|
14
14
|
component: TextButton,
|
|
15
15
|
args: {
|
|
16
16
|
// eslint-disable-next-line no-console
|
|
@@ -5,7 +5,7 @@ import { Progress, StackView, Typography } from '../../src'
|
|
|
5
5
|
import { Container, EachParentType, parentTypesParams } from '../supports'
|
|
6
6
|
|
|
7
7
|
export default {
|
|
8
|
-
title: '
|
|
8
|
+
title: 'Progress',
|
|
9
9
|
component: Progress.Bar,
|
|
10
10
|
args: { accessibilityLabel: 'Progress Bar' }
|
|
11
11
|
}
|