@telus-uds/components-base 0.0.2-prerelease.1 → 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.
- package/CHANGELOG.md +36 -0
- package/__fixtures__/testTheme.js +264 -84
- package/__tests__/Box/Box.test.jsx +81 -58
- package/__tests__/Card/Card.test.jsx +63 -0
- package/__tests__/Divider/Divider.test.jsx +26 -5
- package/__tests__/Feedback/Feedback.test.jsx +42 -0
- package/__tests__/FlexGrid/Col.test.jsx +5 -0
- package/__tests__/Pagination/Pagination.test.jsx +160 -0
- package/__tests__/Spacer/Spacer.test.jsx +63 -0
- package/__tests__/StackView/StackView.test.jsx +242 -0
- package/__tests__/StackView/StackWrap.test.jsx +47 -0
- package/__tests__/StackView/getStackedContent.test.jsx +295 -0
- package/__tests__/TextInput/TextInput.test.jsx +146 -0
- package/__tests__/ThemeProvider/useThemeTokens.test.jsx +5 -3
- package/__tests__/utils/spacing.test.jsx +273 -0
- package/__tests__/utils/useUniqueId.test.js +31 -0
- package/babel.config.json +8 -0
- package/jest.config.js +7 -6
- package/lib/A11yInfoProvider/index.js +2 -2
- package/lib/A11yText/index.js +1 -3
- package/lib/ActivityIndicator/Spinner.web.js +3 -5
- package/lib/Box/Box.js +117 -82
- package/lib/Button/Button.js +1 -3
- package/lib/Button/ButtonBase.js +9 -21
- package/lib/Button/ButtonGroup.js +14 -25
- package/lib/Button/ButtonLink.js +1 -3
- package/lib/Card/Card.js +103 -0
- package/lib/Card/index.js +2 -0
- package/lib/Divider/Divider.js +40 -4
- package/lib/ExpandCollapse/Accordion.js +1 -3
- package/lib/ExpandCollapse/Control.js +3 -5
- package/lib/ExpandCollapse/Panel.js +2 -4
- package/lib/Feedback/Feedback.js +110 -0
- package/lib/Feedback/index.js +2 -0
- package/lib/FlexGrid/Col/Col.js +3 -5
- package/lib/FlexGrid/FlexGrid.js +1 -3
- package/lib/FlexGrid/Row/Row.js +1 -3
- package/lib/FlexGrid/providers/GutterContext.js +1 -1
- package/lib/Icon/Icon.js +1 -1
- package/lib/InputLabel/InputLabel.js +86 -0
- package/lib/InputLabel/LabelContent.native.js +8 -0
- package/lib/InputLabel/LabelContent.web.js +17 -0
- package/lib/InputLabel/index.js +2 -0
- package/lib/Link/ChevronLink.js +1 -3
- package/lib/Link/Link.js +1 -3
- package/lib/Link/LinkBase.js +11 -7
- package/lib/Link/TextButton.js +1 -3
- package/lib/Pagination/PageButton.js +85 -0
- package/lib/Pagination/Pagination.js +118 -0
- package/lib/Pagination/SideButton.js +108 -0
- package/lib/Pagination/dictionary.js +18 -0
- package/lib/Pagination/index.js +2 -0
- package/lib/Pagination/useCopy.js +10 -0
- package/lib/Pagination/usePagination.js +70 -0
- package/lib/SideNav/Item.js +4 -6
- package/lib/SideNav/ItemsGroup.js +11 -11
- package/lib/SideNav/SideNav.js +2 -4
- package/lib/Spacer/Spacer.js +98 -0
- package/lib/Spacer/index.js +2 -0
- package/lib/StackView/StackView.js +105 -0
- package/lib/StackView/StackWrap.js +32 -0
- package/lib/StackView/StackWrap.native.js +3 -0
- package/lib/StackView/StackWrapBox.js +85 -0
- package/lib/StackView/StackWrapGap.js +45 -0
- package/lib/StackView/common.js +30 -0
- package/lib/StackView/getStackedContent.js +111 -0
- package/lib/StackView/index.js +5 -0
- package/lib/TextInput/TextInput.js +337 -0
- package/lib/TextInput/index.js +2 -0
- package/lib/ThemeProvider/ThemeProvider.js +2 -2
- package/lib/ThemeProvider/useThemeTokens.js +34 -6
- package/lib/ThemeProvider/utils/theme-tokens.js +37 -9
- package/lib/ToggleSwitch/ToggleSwitch.js +17 -47
- package/lib/Typography/Typography.js +1 -7
- package/lib/ViewportProvider/index.js +1 -1
- package/lib/index.js +8 -1
- package/lib/utils/index.js +2 -1
- package/lib/utils/input.js +3 -1
- package/lib/utils/propTypes.js +103 -8
- package/lib/utils/spacing/index.js +2 -0
- package/lib/utils/spacing/useSpacingScale.js +102 -0
- package/lib/utils/spacing/utils.js +32 -0
- package/lib/utils/useUniqueId.js +12 -0
- package/package.json +6 -9
- package/release-context.json +4 -4
- package/src/Box/Box.jsx +117 -80
- package/src/Button/ButtonBase.jsx +8 -21
- package/src/Button/ButtonGroup.jsx +13 -17
- package/src/Card/Card.jsx +101 -0
- package/src/Card/index.js +3 -0
- package/src/Divider/Divider.jsx +38 -3
- package/src/ExpandCollapse/Control.jsx +2 -3
- package/src/Feedback/Feedback.jsx +99 -0
- package/src/Feedback/index.js +3 -0
- package/src/FlexGrid/Col/Col.jsx +4 -2
- package/src/Icon/Icon.jsx +2 -1
- package/src/InputLabel/InputLabel.jsx +99 -0
- package/src/InputLabel/LabelContent.native.jsx +6 -0
- package/src/InputLabel/LabelContent.web.jsx +13 -0
- package/src/InputLabel/index.js +3 -0
- package/src/Link/LinkBase.jsx +9 -3
- package/src/Pagination/PageButton.jsx +80 -0
- package/src/Pagination/Pagination.jsx +135 -0
- package/src/Pagination/SideButton.jsx +93 -0
- package/src/Pagination/dictionary.js +18 -0
- package/src/Pagination/index.js +3 -0
- package/src/Pagination/useCopy.js +7 -0
- package/src/Pagination/usePagination.js +69 -0
- package/src/SideNav/Item.jsx +3 -3
- package/src/SideNav/ItemsGroup.jsx +11 -13
- package/src/Spacer/Spacer.jsx +91 -0
- package/src/Spacer/index.js +3 -0
- package/src/StackView/StackView.jsx +103 -0
- package/src/StackView/StackWrap.jsx +33 -0
- package/src/StackView/StackWrap.native.jsx +4 -0
- package/src/StackView/StackWrapBox.jsx +82 -0
- package/src/StackView/StackWrapGap.jsx +39 -0
- package/src/StackView/common.jsx +28 -0
- package/src/StackView/getStackedContent.jsx +106 -0
- package/src/StackView/index.js +6 -0
- package/src/TextInput/TextInput.jsx +325 -0
- package/src/TextInput/index.js +3 -0
- package/src/ThemeProvider/useThemeTokens.js +34 -7
- package/src/ThemeProvider/utils/theme-tokens.js +37 -8
- package/src/ToggleSwitch/ToggleSwitch.jsx +23 -43
- package/src/Typography/Typography.jsx +0 -4
- package/src/index.js +8 -1
- package/src/utils/index.js +1 -0
- package/src/utils/input.js +2 -1
- package/src/utils/propTypes.js +105 -16
- package/src/utils/spacing/index.js +3 -0
- package/src/utils/spacing/useSpacingScale.js +93 -0
- package/src/utils/spacing/utils.js +28 -0
- package/src/utils/useUniqueId.js +14 -0
- package/stories/A11yText/A11yText.stories.jsx +11 -5
- package/stories/ActivityIndicator/ActivityIndicator.stories.jsx +11 -2
- package/stories/Box/Box.stories.jsx +46 -17
- package/stories/Button/Button.stories.jsx +17 -21
- package/stories/Button/ButtonGroup.stories.jsx +2 -1
- package/stories/Button/ButtonLink.stories.jsx +6 -4
- package/stories/Card/Card.stories.jsx +62 -0
- package/stories/Divider/Divider.stories.jsx +26 -2
- package/stories/ExpandCollapse/ExpandCollapse.stories.jsx +74 -79
- package/stories/Feedback/Feedback.stories.jsx +97 -0
- package/stories/FlexGrid/01 FlexGrid.stories.jsx +20 -7
- package/stories/Icon/Icon.stories.jsx +11 -3
- package/stories/InputLabel/InputLabel.stories.jsx +37 -0
- package/stories/Link/ChevronLink.stories.jsx +20 -4
- package/stories/Link/Link.stories.jsx +24 -3
- package/stories/Link/TextButton.stories.jsx +24 -3
- package/stories/Pagination/Pagination.stories.jsx +64 -0
- package/stories/SideNav/SideNav.stories.jsx +17 -2
- package/stories/Spacer/Spacer.stories.jsx +33 -0
- package/stories/StackView/StackView.stories.jsx +65 -0
- package/stories/StackView/StackWrap.stories.jsx +52 -0
- package/stories/TextInput/TextInput.stories.jsx +103 -0
- package/stories/ToggleSwitch/ToggleSwitch.stories.jsx +16 -3
- package/stories/Typography/Typography.stories.jsx +12 -3
- package/stories/platform-supports.web.jsx +1 -1
- package/stories/supports.jsx +113 -13
- package/babel.config.js +0 -3
|
@@ -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,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,325 @@
|
|
|
1
|
+
import React, { useState } from 'react'
|
|
2
|
+
import { View, TextInput as NativeTextInput, Platform, StyleSheet } from 'react-native'
|
|
3
|
+
|
|
4
|
+
import PropTypes from 'prop-types'
|
|
5
|
+
import { useThemeTokens, applyTextStyles } from '../ThemeProvider'
|
|
6
|
+
import { getTokensPropType, useInputValue, variantProp } from '../utils'
|
|
7
|
+
import InputLabel from '../InputLabel'
|
|
8
|
+
import Feedback from '../Feedback'
|
|
9
|
+
import Box from '../Box'
|
|
10
|
+
import useUniqueId from '../utils/useUniqueId'
|
|
11
|
+
|
|
12
|
+
const selectInputStyles = (
|
|
13
|
+
{
|
|
14
|
+
backgroundColor,
|
|
15
|
+
color,
|
|
16
|
+
borderWidth,
|
|
17
|
+
borderColor,
|
|
18
|
+
borderRadius,
|
|
19
|
+
paddingTop,
|
|
20
|
+
paddingBottom,
|
|
21
|
+
paddingLeft,
|
|
22
|
+
paddingRight = 0,
|
|
23
|
+
fontName,
|
|
24
|
+
fontSize,
|
|
25
|
+
fontWeight,
|
|
26
|
+
lineHeight,
|
|
27
|
+
icon,
|
|
28
|
+
iconSize = 0
|
|
29
|
+
},
|
|
30
|
+
inactive
|
|
31
|
+
) => {
|
|
32
|
+
// Subtract border width from padding so overall input width/height doesn't
|
|
33
|
+
// jump around if the border width changes (avoiding NaN and negative padding)
|
|
34
|
+
const offsetBorder = (value) =>
|
|
35
|
+
typeof value === 'number' && typeof borderWidth === 'number'
|
|
36
|
+
? Math.max(0, value - borderWidth)
|
|
37
|
+
: value
|
|
38
|
+
|
|
39
|
+
const textStyles = applyTextStyles({ fontName, fontSize, lineHeight, fontWeight })
|
|
40
|
+
const webStyles = Platform.select({
|
|
41
|
+
web: {
|
|
42
|
+
outline: 'none',
|
|
43
|
+
cursor: inactive ? 'not-allowed' : undefined
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
const paddingWithIcon = iconSize + paddingRight
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
backgroundColor,
|
|
51
|
+
color,
|
|
52
|
+
borderWidth,
|
|
53
|
+
borderColor,
|
|
54
|
+
borderRadius,
|
|
55
|
+
paddingLeft: offsetBorder(paddingLeft),
|
|
56
|
+
paddingRight: icon ? offsetBorder(paddingWithIcon) : offsetBorder(paddingRight),
|
|
57
|
+
paddingTop: offsetBorder(paddingTop),
|
|
58
|
+
paddingBottom: offsetBorder(paddingBottom),
|
|
59
|
+
...textStyles,
|
|
60
|
+
...webStyles
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const selectOuterBorderStyles = ({
|
|
65
|
+
outerBackgroundColor,
|
|
66
|
+
outerBorderWidth = 0,
|
|
67
|
+
outerBorderColor,
|
|
68
|
+
outerBorderRadius = 0
|
|
69
|
+
}) => {
|
|
70
|
+
// Use negative margins so that the outer border doesn't expand the input's bounding box
|
|
71
|
+
const margin = -1 * outerBorderWidth
|
|
72
|
+
|
|
73
|
+
// Account for the border width since we style it as an outline
|
|
74
|
+
const borderRadius = outerBorderRadius + outerBorderWidth
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
background: outerBackgroundColor,
|
|
78
|
+
borderWidth: outerBorderWidth,
|
|
79
|
+
borderColor: outerBorderColor,
|
|
80
|
+
borderRadius,
|
|
81
|
+
marginTop: margin,
|
|
82
|
+
marginBottom: margin,
|
|
83
|
+
marginLeft: margin,
|
|
84
|
+
marginRight: margin
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const selectIconTokens = ({ iconSize, iconColor }) => ({
|
|
89
|
+
size: iconSize,
|
|
90
|
+
color: iconColor
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
const joinDefined = (array) => array.filter((item) => item !== undefined).join(' ')
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* A basic text input component. Use in forms or individually to receive user's input.
|
|
97
|
+
* Due to React Native's implementation of `TextInput` it's not possible to access the current value by passing a ref.
|
|
98
|
+
*
|
|
99
|
+
* ## Controlled component
|
|
100
|
+
* If it is required that the state of the `TextInput` be controlled by the application or other external methods,
|
|
101
|
+
* `value` and `onChange` props must be passed to the `TextInput`.
|
|
102
|
+
* If the `TextInput` value should not be changed by user input, a `readOnly` prop must be provided.
|
|
103
|
+
*
|
|
104
|
+
* ## Uncontrolled component
|
|
105
|
+
* If it is not necessary to control the `TextInput` state, you can create one without a value prop.
|
|
106
|
+
* In this case its value can be accessed within the `onChange` callback.
|
|
107
|
+
*
|
|
108
|
+
* NOTE: In order to support more advanced usages all additional props will be passed down to the React Native's
|
|
109
|
+
* `TextInput` component. See <a href="https://reactnative.dev/docs/textinput" target="_blank">React Native documentation</a> for
|
|
110
|
+
* supported props and <a href="https://reactnative.dev/docs/textinput" target="_blank">React Native Web documentation</a> for
|
|
111
|
+
* their implementation on the web.
|
|
112
|
+
*/
|
|
113
|
+
function TextInput({
|
|
114
|
+
value,
|
|
115
|
+
initialValue,
|
|
116
|
+
label,
|
|
117
|
+
hint,
|
|
118
|
+
hintPosition = 'inline',
|
|
119
|
+
feedback,
|
|
120
|
+
tooltip,
|
|
121
|
+
validation,
|
|
122
|
+
inactive,
|
|
123
|
+
readOnly,
|
|
124
|
+
onChange,
|
|
125
|
+
onChangeText,
|
|
126
|
+
onFocus,
|
|
127
|
+
onBlur,
|
|
128
|
+
onMouseOver,
|
|
129
|
+
onMouseOut,
|
|
130
|
+
tokens,
|
|
131
|
+
variant = {},
|
|
132
|
+
...remainingProps
|
|
133
|
+
}) {
|
|
134
|
+
const inputId = useUniqueId('text-input')
|
|
135
|
+
const hintId = useUniqueId('text-input-hint')
|
|
136
|
+
const feedbackId = useUniqueId('text-input-feedback')
|
|
137
|
+
|
|
138
|
+
const [isFocused, setIsFocused] = useState(false)
|
|
139
|
+
const handleFocus = (event) => {
|
|
140
|
+
setIsFocused(true)
|
|
141
|
+
if (typeof onFocus === 'function') onFocus(event)
|
|
142
|
+
}
|
|
143
|
+
const handleBlur = (event) => {
|
|
144
|
+
setIsFocused(false)
|
|
145
|
+
if (typeof onBlur === 'function') onBlur(event)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const [isHovered, setIsHovered] = useState(false)
|
|
149
|
+
const handleMouseOver = (event) => {
|
|
150
|
+
setIsHovered(true)
|
|
151
|
+
if (typeof onMouseOver === 'function') onMouseOver(event)
|
|
152
|
+
}
|
|
153
|
+
const handleMouseOut = (event) => {
|
|
154
|
+
setIsHovered(false)
|
|
155
|
+
if (typeof onMouseOut === 'function') onMouseOut(event)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const { currentValue, setValue, isControlled } = useInputValue({
|
|
159
|
+
value,
|
|
160
|
+
initialValue,
|
|
161
|
+
onChange,
|
|
162
|
+
readOnly
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
const handleChangeText = (text) => {
|
|
166
|
+
setValue(text)
|
|
167
|
+
if (typeof onChangeText === 'function') onChangeText(text)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const states = { focus: isFocused, hover: isHovered, inactive }
|
|
171
|
+
|
|
172
|
+
const themeTokens = useThemeTokens('TextInput', tokens, { ...variant, validation }, states)
|
|
173
|
+
|
|
174
|
+
const { icon: IconComponent } = themeTokens
|
|
175
|
+
|
|
176
|
+
const hasValidationError = validation === 'error'
|
|
177
|
+
|
|
178
|
+
const inputProps = {
|
|
179
|
+
...remainingProps,
|
|
180
|
+
editable: !inactive,
|
|
181
|
+
onFocus: handleFocus,
|
|
182
|
+
onBlur: handleBlur,
|
|
183
|
+
onMouseOver: handleMouseOver,
|
|
184
|
+
onMouseOut: handleMouseOut,
|
|
185
|
+
onChangeText: handleChangeText,
|
|
186
|
+
accessibilityLabel: label,
|
|
187
|
+
accessibilityHint: joinDefined([!hasValidationError && feedback, hint]), // native only -> replaced with describedBy on web
|
|
188
|
+
accessibilityDescribedBy: joinDefined([
|
|
189
|
+
!hasValidationError && feedback && feedbackId, // feedback receives a11yRole=alert, so there's no need to include it here
|
|
190
|
+
hint && hintId
|
|
191
|
+
]), // introduced in RNW 0.15.0
|
|
192
|
+
accessibilityInvalid: hasValidationError, // introduced in RNW 0.15.0
|
|
193
|
+
nativeID: inputId,
|
|
194
|
+
defaultValue: initialValue,
|
|
195
|
+
// currentValue is being updated even if the input is not controlled, passing it down to the
|
|
196
|
+
// Input could lead to changing its state from uncontrolled to controlled
|
|
197
|
+
value: isControlled ? currentValue : undefined
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// TODO: use Stack instead of the Boxes
|
|
201
|
+
|
|
202
|
+
const feedbackVariant = {}
|
|
203
|
+
|
|
204
|
+
if (hasValidationError) {
|
|
205
|
+
feedbackVariant.state = 'error'
|
|
206
|
+
} else if (validation === 'success') {
|
|
207
|
+
feedbackVariant.state = 'success'
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return (
|
|
211
|
+
<View>
|
|
212
|
+
{label && (
|
|
213
|
+
<Box bottom={1}>
|
|
214
|
+
<InputLabel
|
|
215
|
+
label={label}
|
|
216
|
+
hint={hint}
|
|
217
|
+
hintPosition={hintPosition}
|
|
218
|
+
hintId={hintId}
|
|
219
|
+
tooltip={tooltip}
|
|
220
|
+
forId={inputId}
|
|
221
|
+
/>
|
|
222
|
+
</Box>
|
|
223
|
+
)}
|
|
224
|
+
<View style={selectOuterBorderStyles(themeTokens)}>
|
|
225
|
+
<NativeTextInput style={selectInputStyles(themeTokens, inactive)} {...inputProps} />
|
|
226
|
+
{IconComponent && (
|
|
227
|
+
<View
|
|
228
|
+
pointerEvents="none" // avoid hijacking input press events
|
|
229
|
+
style={[staticStyles.iconContainer, selectIconContainerStyles(themeTokens)]}
|
|
230
|
+
>
|
|
231
|
+
<IconComponent tokens={selectIconTokens(themeTokens)} />
|
|
232
|
+
</View>
|
|
233
|
+
)}
|
|
234
|
+
</View>
|
|
235
|
+
{feedback && (
|
|
236
|
+
<Box top={1}>
|
|
237
|
+
<Feedback
|
|
238
|
+
title={feedback}
|
|
239
|
+
variant={feedbackVariant}
|
|
240
|
+
accessibilityRole={hasValidationError ? 'alert' : undefined}
|
|
241
|
+
/>
|
|
242
|
+
</Box>
|
|
243
|
+
)}
|
|
244
|
+
</View>
|
|
245
|
+
)
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const selectIconContainerStyles = ({ paddingRight }) => ({
|
|
249
|
+
paddingRight
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
TextInput.propTypes = {
|
|
253
|
+
/**
|
|
254
|
+
* The input label.
|
|
255
|
+
*/
|
|
256
|
+
label: PropTypes.string,
|
|
257
|
+
/**
|
|
258
|
+
* If the `TextInput's` state is to be controlled by a parent component, use this prop
|
|
259
|
+
* together with the `onChange` to pass down and update the lifted state.
|
|
260
|
+
*/
|
|
261
|
+
value: PropTypes.string,
|
|
262
|
+
/**
|
|
263
|
+
* Use this to set the initial value of an uncontrolled `TextInput`.
|
|
264
|
+
* Updating `initialValue` will **not** update the actual value.
|
|
265
|
+
*/
|
|
266
|
+
initialValue: PropTypes.string,
|
|
267
|
+
/**
|
|
268
|
+
* A short description of the expected input.
|
|
269
|
+
*/
|
|
270
|
+
hint: PropTypes.string,
|
|
271
|
+
/**
|
|
272
|
+
* Position of the hint relative to label.
|
|
273
|
+
*/
|
|
274
|
+
hintPosition: PropTypes.oneOf(['inline', 'below']),
|
|
275
|
+
/**
|
|
276
|
+
* A detailed description of validation error/success or additional instructions.
|
|
277
|
+
* Visual variant is determined based on the `validation` prop.
|
|
278
|
+
*/
|
|
279
|
+
feedback: PropTypes.string,
|
|
280
|
+
/**
|
|
281
|
+
* Content of an optional `Tooltip`. If set, a tooltip button will be shown next to the label.
|
|
282
|
+
*/
|
|
283
|
+
tooltip: PropTypes.string,
|
|
284
|
+
/**
|
|
285
|
+
* Use to visually mark an input as valid or invalid.
|
|
286
|
+
*/
|
|
287
|
+
validation: PropTypes.oneOf(['error', 'success']),
|
|
288
|
+
/**
|
|
289
|
+
* Disables all user interactions with the input.
|
|
290
|
+
*/
|
|
291
|
+
inactive: PropTypes.bool,
|
|
292
|
+
/**
|
|
293
|
+
* Makes it impossible to change the input's value.
|
|
294
|
+
*/
|
|
295
|
+
readOnly: PropTypes.bool,
|
|
296
|
+
/**
|
|
297
|
+
* Use to react upon input's value changes. Required when the `value` prop is set.
|
|
298
|
+
* Will receive the input's value as an argument.
|
|
299
|
+
*/
|
|
300
|
+
onChange: PropTypes.func,
|
|
301
|
+
/** @ignore */
|
|
302
|
+
onChangeText: PropTypes.func,
|
|
303
|
+
/** @ignore */
|
|
304
|
+
onFocus: PropTypes.func,
|
|
305
|
+
/** @ignore */
|
|
306
|
+
onBlur: PropTypes.func,
|
|
307
|
+
/** @ignore */
|
|
308
|
+
onMouseOver: PropTypes.func,
|
|
309
|
+
/** @ignore */
|
|
310
|
+
onMouseOut: PropTypes.func,
|
|
311
|
+
tokens: getTokensPropType('TextInput'),
|
|
312
|
+
variant: variantProp.propType
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export default TextInput
|
|
316
|
+
|
|
317
|
+
const staticStyles = StyleSheet.create({
|
|
318
|
+
iconContainer: {
|
|
319
|
+
position: 'absolute',
|
|
320
|
+
right: 0,
|
|
321
|
+
top: 0,
|
|
322
|
+
bottom: 0,
|
|
323
|
+
justifyContent: 'center'
|
|
324
|
+
}
|
|
325
|
+
})
|