@telus-uds/components-base 0.0.2-prerelease.4 → 0.0.2-prerelease.5

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.
Files changed (96) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/__fixtures__/testTheme.js +83 -11
  3. package/__tests__/Divider/Divider.test.jsx +26 -5
  4. package/__tests__/Feedback/Feedback.test.jsx +42 -0
  5. package/__tests__/Spacer/Spacer.test.jsx +63 -0
  6. package/__tests__/StackView/StackView.test.jsx +242 -0
  7. package/__tests__/StackView/StackWrap.test.jsx +47 -0
  8. package/__tests__/StackView/getStackedContent.test.jsx +295 -0
  9. package/__tests__/TextInput/TextInput.test.jsx +146 -0
  10. package/__tests__/utils/useUniqueId.test.js +31 -0
  11. package/lib/Box/Box.js +7 -2
  12. package/lib/Button/ButtonBase.js +6 -16
  13. package/lib/Button/ButtonGroup.js +13 -22
  14. package/lib/Divider/Divider.js +40 -2
  15. package/lib/Feedback/Feedback.js +110 -0
  16. package/lib/Feedback/index.js +2 -0
  17. package/lib/Icon/Icon.js +1 -1
  18. package/lib/InputLabel/InputLabel.js +86 -0
  19. package/lib/InputLabel/LabelContent.native.js +8 -0
  20. package/lib/InputLabel/LabelContent.web.js +17 -0
  21. package/lib/InputLabel/index.js +2 -0
  22. package/lib/Link/LinkBase.js +9 -3
  23. package/lib/Spacer/Spacer.js +98 -0
  24. package/lib/Spacer/index.js +2 -0
  25. package/lib/StackView/StackView.js +105 -0
  26. package/lib/StackView/StackWrap.js +32 -0
  27. package/lib/StackView/StackWrap.native.js +3 -0
  28. package/lib/StackView/StackWrapBox.js +85 -0
  29. package/lib/StackView/StackWrapGap.js +45 -0
  30. package/lib/StackView/common.js +30 -0
  31. package/lib/StackView/getStackedContent.js +111 -0
  32. package/lib/StackView/index.js +5 -0
  33. package/lib/TextInput/TextInput.js +337 -0
  34. package/lib/TextInput/index.js +2 -0
  35. package/lib/Typography/Typography.js +0 -4
  36. package/lib/index.js +6 -1
  37. package/lib/utils/input.js +3 -1
  38. package/lib/utils/propTypes.js +9 -1
  39. package/lib/utils/useUniqueId.js +12 -0
  40. package/package.json +2 -2
  41. package/release-context.json +4 -4
  42. package/src/Box/Box.jsx +4 -2
  43. package/src/Button/ButtonBase.jsx +6 -18
  44. package/src/Button/ButtonGroup.jsx +13 -17
  45. package/src/Divider/Divider.jsx +38 -3
  46. package/src/Feedback/Feedback.jsx +99 -0
  47. package/src/Feedback/index.js +3 -0
  48. package/src/Icon/Icon.jsx +2 -1
  49. package/src/InputLabel/InputLabel.jsx +99 -0
  50. package/src/InputLabel/LabelContent.native.jsx +6 -0
  51. package/src/InputLabel/LabelContent.web.jsx +13 -0
  52. package/src/InputLabel/index.js +3 -0
  53. package/src/Link/LinkBase.jsx +9 -3
  54. package/src/Spacer/Spacer.jsx +91 -0
  55. package/src/Spacer/index.js +3 -0
  56. package/src/StackView/StackView.jsx +103 -0
  57. package/src/StackView/StackWrap.jsx +33 -0
  58. package/src/StackView/StackWrap.native.jsx +4 -0
  59. package/src/StackView/StackWrapBox.jsx +82 -0
  60. package/src/StackView/StackWrapGap.jsx +39 -0
  61. package/src/StackView/common.jsx +28 -0
  62. package/src/StackView/getStackedContent.jsx +106 -0
  63. package/src/StackView/index.js +6 -0
  64. package/src/TextInput/TextInput.jsx +325 -0
  65. package/src/TextInput/index.js +3 -0
  66. package/src/Typography/Typography.jsx +0 -4
  67. package/src/index.js +6 -1
  68. package/src/utils/input.js +2 -1
  69. package/src/utils/propTypes.js +9 -0
  70. package/src/utils/useUniqueId.js +14 -0
  71. package/stories/A11yText/A11yText.stories.jsx +11 -5
  72. package/stories/ActivityIndicator/ActivityIndicator.stories.jsx +11 -2
  73. package/stories/Box/Box.stories.jsx +29 -2
  74. package/stories/Button/Button.stories.jsx +17 -21
  75. package/stories/Button/ButtonGroup.stories.jsx +2 -1
  76. package/stories/Button/ButtonLink.stories.jsx +6 -4
  77. package/stories/Card/Card.stories.jsx +13 -1
  78. package/stories/Divider/Divider.stories.jsx +26 -2
  79. package/stories/ExpandCollapse/ExpandCollapse.stories.jsx +74 -79
  80. package/stories/Feedback/Feedback.stories.jsx +97 -0
  81. package/stories/FlexGrid/01 FlexGrid.stories.jsx +20 -7
  82. package/stories/Icon/Icon.stories.jsx +11 -3
  83. package/stories/InputLabel/InputLabel.stories.jsx +37 -0
  84. package/stories/Link/ChevronLink.stories.jsx +20 -4
  85. package/stories/Link/Link.stories.jsx +24 -2
  86. package/stories/Link/TextButton.stories.jsx +24 -2
  87. package/stories/Pagination/Pagination.stories.jsx +28 -14
  88. package/stories/SideNav/SideNav.stories.jsx +17 -2
  89. package/stories/Spacer/Spacer.stories.jsx +33 -0
  90. package/stories/StackView/StackView.stories.jsx +65 -0
  91. package/stories/StackView/StackWrap.stories.jsx +52 -0
  92. package/stories/TextInput/TextInput.stories.jsx +103 -0
  93. package/stories/ToggleSwitch/ToggleSwitch.stories.jsx +16 -3
  94. package/stories/Typography/Typography.stories.jsx +12 -3
  95. package/stories/platform-supports.web.jsx +1 -1
  96. package/stories/supports.jsx +109 -13
@@ -0,0 +1,103 @@
1
+ import React from 'react'
2
+ import PropTypes from 'prop-types'
3
+ import { View } from 'react-native'
4
+
5
+ import Divider from '../Divider'
6
+ import { spacingProps, a11yProps } from '../utils'
7
+ import { useThemeTokens } from '../ThemeProvider'
8
+ import getStackedContent from './getStackedContent'
9
+ import { staticStyles, selectFlexStyles } from './common'
10
+
11
+ /**
12
+ * StackView evenly spaces its children, by default placing `Spacer` components between them.
13
+ *
14
+ * ## Spacing
15
+ *
16
+ * StackView takes the same spacing props as `Spacer` and passes them directly to the layout components
17
+ * it uses for spacing.
18
+ *
19
+ * ## Direction
20
+ *
21
+ * Use `direction` to control how the children are stacked:
22
+ *
23
+ * - `'column'` (default) stacks and spaces the children vertically.
24
+ * - `'row'` stacks and spaces the children horizontally.
25
+ * - `'column-reverse'` acts the same as `'column'`, but reverses the direction.
26
+ * - `'row-reverse'` acts the same as `'row'`, but reverses the direction.
27
+ *
28
+ * ## Theming, alignment and justification
29
+ *
30
+ * `StackView` is a pure layout component and has no theme styles beyond a small set of layout styles
31
+ * (`alignItems`, `justifyContent`, `flexGrow`) which may be set using the `tokens` prop.
32
+ *
33
+ * All other styling such as borders, background colours, padding etc should be applied by wrapping
34
+ * the `StackView` in other components. For example, a `StackView` may be the child of a `Card`.
35
+ *
36
+ * ## Separator options
37
+ *
38
+ * By default, StackView inserts a `<Spacer>` component between each rendered child.
39
+ *
40
+ * With the `divider` prop, it instead renders a `Divider` instead of a `Spacer`. The gap between
41
+ * items will be double what it would be without this prop plus the width of the divider.
42
+ *
43
+ * ## Accessibility
44
+ *
45
+ * `StackView` may take any React Native accessibility props and applies them to the outer `View`.
46
+ * It applies no accessibility props of its own, unless `divider` prop is passed (`Divider` has a
47
+ * semantic role but only on web, not within native apps).
48
+ */
49
+ const StackView = ({
50
+ space = 1,
51
+ divider,
52
+ direction = 'column',
53
+ inherit,
54
+ children,
55
+ variant,
56
+ tokens,
57
+ ...rest
58
+ }) => {
59
+ const a11y = a11yProps.select({ ...rest })
60
+ const content = getStackedContent(children, { direction, divider, space })
61
+ const themeTokens = useThemeTokens('StackView', tokens, variant)
62
+ const flexStyles = selectFlexStyles(themeTokens)
63
+
64
+ return inherit ? (
65
+ content
66
+ ) : (
67
+ <View {...a11y} style={[flexStyles, staticStyles[direction]]}>
68
+ {content}
69
+ </View>
70
+ )
71
+ }
72
+
73
+ StackView.propTypes = {
74
+ ...a11yProps.propTypes,
75
+ /**
76
+ * Sets the `flexDirection` of the container and, if `divider` is used, gives the Divider the appropriate direction.
77
+ */
78
+ direction: PropTypes.oneOf(['column', 'row', 'column-reverse', 'row-reverse']),
79
+ /**
80
+ * If true, renders a UDS `Divider` component between each item. If an object is passed,
81
+ * this object is passes as props to each Divider.
82
+ */
83
+ divider: PropTypes.oneOfType([PropTypes.bool, PropTypes.shape(Divider.propTypes)]),
84
+ /**
85
+ * If true, does not render any container and returns an array of spaced children, inheriting
86
+ * flex styles from the StackView's parent. That parent must have a `flexDirection` style corresponding
87
+ * to the StackView's `direction` prop for `divider` prop to behave correctly.
88
+ */
89
+ inherit: PropTypes.bool,
90
+ /**
91
+ * The size of the spacer according to the theme's spacing scale.
92
+ * Either a number corresponding to a position on the theme's spacing scale (1 is smallest, 2 is second smallest, etc),
93
+ * or, a SpacingObject with viewport keys and options (see `useSpacingScale` for details).
94
+ */
95
+ space: spacingProps.types.spacingValue,
96
+ /**
97
+ * A StackView may take any children, but will have no effect if it is only passed one child or is passed children
98
+ * wrapped in a component. If necessary, children may be wrapped in one React Fragment.
99
+ */
100
+ children: PropTypes.node
101
+ }
102
+
103
+ export default StackView
@@ -0,0 +1,33 @@
1
+ import React from 'react'
2
+
3
+ import StackWrapBox from './StackWrapBox'
4
+ import StackWrapGap from './StackWrapGap'
5
+
6
+ // CSS.supports needs an example of the type of value you intend to use.
7
+ // Will be an integer appended `px` after hooks and JSX styles are resolved.
8
+ const exampleGapValue = '1px'
9
+
10
+ /**
11
+ * StackWrap is an alternative to StackView where the spaced items are allowed to wrap.
12
+ *
13
+ * As well as providing space between the items, StackWrap provides `gap` space between
14
+ * wrapped lines of items. By default, this `gap` is the same as the gap between spaced items.
15
+ * If a different spacing is desired between wrapped lines, pass a spacing value to the `gap` prop.
16
+ */
17
+ const StackWrap = (props) => {
18
+ const { space } = props
19
+ // Don't apply separate gap if `null` or `undefined`, so can be unset in Storybook etc
20
+ // eslint-disable-next-line react/destructuring-assignment
21
+ const gap = props.gap ?? space
22
+
23
+ return gap === space && CSS?.supports('gap', exampleGapValue) ? (
24
+ // If possible, use the cleaner implementation that applies CSS `gap` styles to the container.
25
+ <StackWrapGap {...props} />
26
+ ) : (
27
+ // Else, use the fallback implementation which renders a `Box` component around each child.
28
+ <StackWrapBox {...props} />
29
+ )
30
+ }
31
+ StackWrap.propTypes = StackWrapBox.propTypes
32
+
33
+ export default StackWrap
@@ -0,0 +1,4 @@
1
+ import StackWrapBox from './StackWrapBox'
2
+
3
+ // No support for CSS `gap` on React Native, so always use the fallback "box" implementation.
4
+ export default StackWrapBox
@@ -0,0 +1,82 @@
1
+ import React from 'react'
2
+ import PropTypes from 'prop-types'
3
+ import { View } from 'react-native'
4
+
5
+ import { spacingProps, a11yProps, useSpacingScale } from '../utils'
6
+ import { useThemeTokens } from '../ThemeProvider'
7
+ import getStackedContent from './getStackedContent'
8
+ import { staticStyles, selectFlexStyles } from './common'
9
+
10
+ const offsetSides = {
11
+ row: 'marginBottom',
12
+ column: 'marginRight'
13
+ }
14
+
15
+ const gapSides = {
16
+ row: 'bottom',
17
+ column: 'right'
18
+ }
19
+
20
+ const spaceSides = {
21
+ row: 'right',
22
+ column: 'bottom'
23
+ }
24
+
25
+ /**
26
+ * An alternative fallback implementation of `StackWrap` that doesn't use CSS `gap`.
27
+ *
28
+ * This is not intended to be used directly. It will automatically be used instead of StackWrap in:
29
+ *
30
+ * - Native apps (no `gap` style prop in React Native).
31
+ * - Older (~<=2020) browsers which don't support CSS `gap`.
32
+ * - Cases where the `space` between items should be different to the `gap` between wrapped rows .
33
+ *
34
+ * Unlike the cleaner and more side-effect-free CSS gap StackWrap implementation, this renders a Box (View)
35
+ * between the container and the stacked children, and requires a negative margin on the container.
36
+ */
37
+ const StackWrapBox = ({
38
+ space = 1,
39
+ gap = space,
40
+ direction = 'row',
41
+ children,
42
+ tokens,
43
+ variant,
44
+ ...rest
45
+ }) => {
46
+ const themeTokens = useThemeTokens('StackView', tokens, variant)
47
+ const flexStyles = selectFlexStyles(themeTokens)
48
+ const a11y = a11yProps.select({ ...rest })
49
+
50
+ // Mimic CSS `gap` using box spacing on the side after a wrapped row then offsetting it on the last row.
51
+ const gapSize = useSpacingScale(gap)
52
+ const offsetStyle = { [offsetSides[direction]]: -1 * gapSize }
53
+ const boxProps = { [gapSides[direction]]: gap, [spaceSides[direction]]: space }
54
+ const content = getStackedContent(children, { direction, space: 0, box: boxProps })
55
+
56
+ return (
57
+ <View {...a11y} style={[flexStyles, staticStyles.wrap, staticStyles[direction], offsetStyle]}>
58
+ {content}
59
+ </View>
60
+ )
61
+ }
62
+
63
+ StackWrapBox.propTypes = {
64
+ ...a11yProps.propTypes,
65
+ /**
66
+ * Sets the `flexDirection` of the container, or if 'inherit' returns spaced children as an array.
67
+ */
68
+ direction: PropTypes.oneOf(['column', 'row']),
69
+ /**
70
+ * The size of the spacer according to the theme's spacing scale.
71
+ * Either a number corresponding to a position on the theme's spacing scale (1 is smallest, 2 is second smallest, etc),
72
+ * or, a SpacingObject with viewport keys and options (see `useSpacingScale` for details).
73
+ */
74
+ space: spacingProps.types.spacingValue,
75
+ /**
76
+ * A StackWrap may take any children, but will have no effect if it is only passed one child or is passed children
77
+ * wrapped in a component. If necessary, children may be wrapped in one React Fragment.
78
+ */
79
+ children: PropTypes.node
80
+ }
81
+
82
+ export default StackWrapBox
@@ -0,0 +1,39 @@
1
+ import React from 'react'
2
+ import { View } from 'react-native'
3
+
4
+ import StackWrapBox from './StackWrapBox'
5
+ import { a11yProps, useSpacingScale } from '../utils'
6
+ import { useThemeTokens } from '../ThemeProvider'
7
+ import getStackedContent from './getStackedContent'
8
+ import { staticStyles, selectFlexStyles } from './common'
9
+
10
+ /**
11
+ * The primary implementation of StackWrap.
12
+ *
13
+ * This is not intended to be used directly. It will automatically be used instead of StackWrap if:
14
+ *
15
+ * - On web, and,
16
+ * - On a browser that supports CSS `gap` (most browser versions from 2020 onwards), and,
17
+ * - The `space` between items is the same as the `gap` between wrapped rows (this is the
18
+ * default if `gap` prop is undefined)
19
+ */
20
+ const StackWrapGap = ({ space = 1, tokens, variant, direction = 'row', children, ...rest }) => {
21
+ const themeTokens = useThemeTokens('StackView', tokens, variant)
22
+ const flexStyles = selectFlexStyles(themeTokens)
23
+ const a11y = a11yProps.select({ ...rest })
24
+
25
+ const size = useSpacingScale(space)
26
+ const gapStyle = { gap: size }
27
+
28
+ const content = getStackedContent(children, { direction, space: 0 })
29
+
30
+ return (
31
+ <View {...a11y} style={[flexStyles, staticStyles.wrap, staticStyles[direction], gapStyle]}>
32
+ {content}
33
+ </View>
34
+ )
35
+ }
36
+
37
+ StackWrapGap.propTypes = StackWrapBox.propTypes
38
+
39
+ export default StackWrapGap
@@ -0,0 +1,28 @@
1
+ import { StyleSheet } from 'react-native'
2
+
3
+ export const selectFlexStyles = ({ alignItems, justifyContent, flexGrow }) => ({
4
+ alignItems,
5
+ justifyContent,
6
+ flexGrow
7
+ })
8
+
9
+ export const staticStyles = StyleSheet.create({
10
+ wrap: {
11
+ flexShrink: 1,
12
+ flexWrap: 'wrap'
13
+ },
14
+ row: {
15
+ flexDirection: 'row'
16
+ },
17
+ 'row-reverse': {
18
+ flexDirection: 'row-reverse'
19
+ },
20
+ column: {
21
+ // This is the React Native View default, but also it's very fundamental to the behaviour,
22
+ // so make it explicit and allow it to be tested for.
23
+ flexDirection: 'column'
24
+ },
25
+ 'column-reverse': {
26
+ flexDirection: 'column-reverse'
27
+ }
28
+ })
@@ -0,0 +1,106 @@
1
+ import React, { Children, Fragment } from 'react'
2
+
3
+ import Box from '../Box'
4
+ import Divider from '../Divider'
5
+ import Spacer from '../Spacer'
6
+
7
+ /**
8
+ * @typedef {import('react').ReactChildren} ReactChildren
9
+ * @typedef {import('react').ReactElement} ReactElement
10
+ * @typedef {import('../utils/propTypes.js').SpacingValue} SpacingValue
11
+ */
12
+
13
+ /**
14
+ * Takes valid React Children and inserts a specified amount of space between each valid (not nullish) top
15
+ * level element, according to the provided `options`.
16
+ *
17
+ * Returns an array of the original elements plus inserted spacing elements.
18
+ * This array of elements is keyed and may be used as a component's `children` without mapping.
19
+ *
20
+ * @param {ReactChildren} children
21
+ * @param {object} [options] -
22
+ * - `space`: amount of space to insert using the theme spacing scale (see `useSpacingScale`)
23
+ * - `direction`: if 'row' or 'row-reverse', applies space horizontally, if 'column' or 'column-reverse', applies space vertically
24
+ * - `box`: if truthy, wraps each valid child in a Box component; if an object, passes it to Box as props
25
+ * - `divider`: if truthy, inserts Divider components; if an object, passes it to Divider as props
26
+ * @param {SpacingValue} [options.space]
27
+ * @param {boolean | object} [options.divider]
28
+ * @param {"row" | "column" | "row-reverse" | "column-reverse"} [options.direction]
29
+ * @returns {ReactElement[]}
30
+ */
31
+ const getStackedContent = (children, { divider, space, direction = 'column', box }) => {
32
+ const boxProps = box && typeof box === 'object' ? box : { space }
33
+ const dividerProps = divider && typeof divider === 'object' ? divider : {}
34
+
35
+ // Avoid surprises if children contains comments, nulls, or is a variable wrapped as a fragment
36
+ const validChildren = Children.toArray(unpackFragment(children)).filter(Boolean)
37
+ const content = validChildren.reduce((newChildren, child, index) => {
38
+ const boxID = `Stack-Box-${index}`
39
+ const item = box ? (
40
+ // If wrapped in Box, that Box needs a key.
41
+ // If possible, use an existing content key; use an index-based key only if necessary.
42
+ <Box {...boxProps} key={child.key || boxID} testID={boxID}>
43
+ {child}
44
+ </Box>
45
+ ) : (
46
+ child
47
+ )
48
+ if (!index || (!space && !divider)) return [...newChildren, item]
49
+
50
+ const testID = `Stack-${divider ? 'Divider' : 'Spacer'}-${index}`
51
+ const commonProps = { testID, key: testID, space }
52
+ const separator = divider ? (
53
+ <Divider vertical={direction.startsWith('row')} {...dividerProps} {...commonProps} />
54
+ ) : (
55
+ <Spacer direction={direction.startsWith('row') ? 'row' : 'column'} {...commonProps} />
56
+ )
57
+ return [...newChildren, separator, item]
58
+ }, [])
59
+
60
+ return content
61
+ }
62
+
63
+ /**
64
+ * Unpacks top-level fragments, so that common compositional patterns such as the following examples
65
+ * can be iterated as flat siblings (as if they were `<Child1 /><Child2 /><Child3 />`):
66
+ *
67
+ * - Setting `children` as a property wrapped in a fragment:
68
+ *
69
+ * ```jsx
70
+ * args.children = <><Child1 /><Child2 /><Child3 /></>
71
+ * ```
72
+ *
73
+ * - Defining `children` as a variable wrapped in a fragment:
74
+ *
75
+ * ```jsx
76
+ * const content = <><Child1 /><Child2 /><Child3 /></>
77
+ * if (someCondition) return <SomeWrapper>{content}</SomeWrapper>
78
+ * ```
79
+ *
80
+ * - Using fragments at the top level of a JSX block for conditional rendering:
81
+ *
82
+ * ```jsx
83
+ * <Child1 />
84
+ * {someCondition && (
85
+ * <>
86
+ * <Child2 />
87
+ * <Child3 />
88
+ * </>
89
+ * )}
90
+ * ```
91
+ *
92
+ * @param {ReactChildren} child
93
+ * @returns {ReactChildren}
94
+ */
95
+ const unpackFragment = (child) => {
96
+ // If this level is a set of top-level siblings rather than one child, check each in turn
97
+ if (Children.count(child) > 1) return Children.map(child, unpackFragment)
98
+
99
+ // When a fragment is found, unpack its children to the top level and check them
100
+ if (child?.type === Fragment) return unpackFragment(child.props?.children)
101
+
102
+ // Stop unpacking as soon as any non-fragment child is found
103
+ return child
104
+ }
105
+
106
+ export default getStackedContent
@@ -0,0 +1,6 @@
1
+ import StackView from './StackView'
2
+ import StackWrap from './StackWrap'
3
+
4
+ export default StackView
5
+ export { StackWrap }
6
+ export { default as getStackedContent } from './getStackedContent'