@telus-uds/components-base 0.0.2-prerelease.6 → 0.0.2-prerelease.7
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/.ultra.cache.json +1 -1
- package/CHANGELOG.md +20 -0
- package/__fixtures__/testTheme.js +424 -37
- package/__tests__/Button/ButtonBase.test.jsx +2 -31
- package/__tests__/Checkbox/Checkbox.test.jsx +94 -0
- package/__tests__/InputSupports/InputSupports.test.jsx +50 -0
- package/__tests__/List/List.test.jsx +60 -0
- package/__tests__/Radio/Radio.test.jsx +87 -0
- package/__tests__/Select/Select.test.jsx +93 -0
- package/__tests__/Skeleton/Skeleton.test.jsx +61 -0
- package/__tests__/Tags/Tags.test.jsx +328 -0
- package/__tests__/TextInput/TextArea.test.jsx +34 -0
- package/__tests__/TextInput/{TextInput.test.jsx → TextInputBase.test.jsx} +20 -46
- package/jest.config.js +3 -1
- package/lib/Button/Button.js +10 -3
- package/lib/Button/ButtonBase.js +73 -59
- package/lib/Button/ButtonGroup.js +11 -27
- package/lib/Button/ButtonLink.js +5 -0
- package/lib/Checkbox/Checkbox.js +308 -0
- package/lib/Checkbox/CheckboxInput.native.js +6 -0
- package/lib/Checkbox/CheckboxInput.web.js +57 -0
- package/lib/Checkbox/index.js +2 -0
- package/lib/Feedback/Feedback.js +20 -3
- package/lib/Icon/Icon.js +8 -5
- package/lib/Icon/IconText.js +72 -0
- package/lib/Icon/index.js +2 -1
- package/lib/InputSupports/InputSupports.js +90 -0
- package/lib/InputSupports/index.js +2 -0
- package/lib/InputSupports/propTypes.js +55 -0
- package/lib/Link/ChevronLink.js +23 -20
- package/lib/Link/InlinePressable.native.js +78 -0
- package/lib/Link/InlinePressable.web.js +32 -0
- package/lib/Link/Link.js +11 -10
- package/lib/Link/LinkBase.js +62 -123
- package/lib/Link/TextButton.js +20 -9
- package/lib/Link/index.js +2 -1
- package/lib/List/List.js +52 -0
- package/lib/List/ListItem.js +207 -0
- package/lib/List/index.js +2 -0
- package/lib/Pagination/PageButton.js +2 -25
- package/lib/Pagination/SideButton.js +27 -37
- package/lib/Radio/Radio.js +291 -0
- package/lib/Radio/RadioInput.native.js +6 -0
- package/lib/Radio/RadioInput.web.js +59 -0
- package/lib/Radio/index.js +2 -0
- package/lib/Select/Group.native.js +14 -0
- package/lib/Select/Group.web.js +18 -0
- package/lib/Select/Item.native.js +9 -0
- package/lib/Select/Item.web.js +15 -0
- package/lib/Select/Picker.native.js +87 -0
- package/lib/Select/Picker.web.js +63 -0
- package/lib/Select/Select.js +272 -0
- package/lib/Select/index.js +6 -0
- package/lib/Skeleton/Skeleton.js +119 -0
- package/lib/Skeleton/index.js +2 -0
- package/lib/Tags/Tags.js +217 -0
- package/lib/Tags/index.js +2 -0
- package/lib/TextInput/TextArea.js +82 -0
- package/lib/TextInput/TextInput.js +23 -304
- package/lib/TextInput/TextInputBase.js +229 -0
- package/lib/TextInput/index.js +2 -1
- package/lib/TextInput/propTypes.js +31 -0
- package/lib/ThemeProvider/useThemeTokens.js +54 -3
- package/lib/ToggleSwitch/ToggleSwitch.js +1 -1
- package/lib/Typography/Typography.js +4 -19
- package/lib/index.js +8 -1
- package/lib/utils/a11y/index.js +1 -0
- package/lib/utils/a11y/textSize.js +33 -0
- package/lib/utils/index.js +3 -0
- package/lib/utils/info/index.js +7 -0
- package/lib/utils/info/platform/index.js +11 -0
- package/lib/utils/info/platform/platform.android.js +1 -0
- package/lib/utils/info/platform/platform.ios.js +1 -0
- package/lib/utils/info/platform/platform.native.js +4 -0
- package/lib/utils/info/platform/platform.web.js +1 -0
- package/lib/utils/info/versions.js +5 -0
- package/lib/utils/pressability.js +92 -0
- package/lib/utils/propTypes.js +78 -17
- package/package.json +5 -4
- package/release-context.json +4 -4
- package/src/Button/Button.jsx +6 -3
- package/src/Button/ButtonBase.jsx +66 -57
- package/src/Button/ButtonGroup.jsx +9 -22
- package/src/Button/ButtonLink.jsx +11 -2
- package/src/Checkbox/Checkbox.jsx +275 -0
- package/src/Checkbox/CheckboxInput.native.jsx +6 -0
- package/src/Checkbox/CheckboxInput.web.jsx +55 -0
- package/src/Checkbox/index.js +3 -0
- package/src/Feedback/Feedback.jsx +13 -4
- package/src/Icon/Icon.jsx +9 -5
- package/src/Icon/IconText.jsx +63 -0
- package/src/Icon/index.js +2 -1
- package/src/InputSupports/InputSupports.jsx +86 -0
- package/src/InputSupports/index.js +3 -0
- package/src/InputSupports/propTypes.js +44 -0
- package/src/Link/ChevronLink.jsx +20 -17
- package/src/Link/InlinePressable.native.jsx +73 -0
- package/src/Link/InlinePressable.web.jsx +37 -0
- package/src/Link/Link.jsx +17 -13
- package/src/Link/LinkBase.jsx +57 -140
- package/src/Link/TextButton.jsx +25 -11
- package/src/Link/index.js +2 -1
- package/src/List/List.jsx +47 -0
- package/src/List/ListItem.jsx +187 -0
- package/src/List/index.js +3 -0
- package/src/Pagination/PageButton.jsx +2 -16
- package/src/Pagination/SideButton.jsx +23 -34
- package/src/Radio/Radio.jsx +270 -0
- package/src/Radio/RadioInput.native.jsx +6 -0
- package/src/Radio/RadioInput.web.jsx +57 -0
- package/src/Radio/index.js +3 -0
- package/src/Select/Group.native.jsx +14 -0
- package/src/Select/Group.web.jsx +15 -0
- package/src/Select/Item.native.jsx +10 -0
- package/src/Select/Item.web.jsx +11 -0
- package/src/Select/Picker.native.jsx +95 -0
- package/src/Select/Picker.web.jsx +67 -0
- package/src/Select/Select.jsx +265 -0
- package/src/Select/index.js +8 -0
- package/src/Skeleton/Skeleton.jsx +101 -0
- package/src/Skeleton/index.js +3 -0
- package/src/Tags/Tags.jsx +206 -0
- package/src/Tags/index.js +3 -0
- package/src/TextInput/TextArea.jsx +78 -0
- package/src/TextInput/TextInput.jsx +17 -284
- package/src/TextInput/TextInputBase.jsx +220 -0
- package/src/TextInput/index.js +2 -1
- package/src/TextInput/propTypes.js +29 -0
- package/src/ThemeProvider/useThemeTokens.js +54 -3
- package/src/ToggleSwitch/ToggleSwitch.jsx +1 -1
- package/src/Typography/Typography.jsx +4 -15
- package/src/index.js +8 -1
- package/src/utils/a11y/index.js +1 -0
- package/src/utils/a11y/textSize.js +30 -0
- package/src/utils/index.js +3 -0
- package/src/utils/info/index.js +8 -0
- package/src/utils/info/platform/index.js +11 -0
- package/src/utils/info/platform/platform.android.js +1 -0
- package/src/utils/info/platform/platform.ios.js +1 -0
- package/src/utils/info/platform/platform.native.js +4 -0
- package/src/utils/info/platform/platform.web.js +1 -0
- package/src/utils/info/versions.js +6 -0
- package/src/utils/pressability.js +92 -0
- package/src/utils/propTypes.js +97 -22
- package/stories/Button/Button.stories.jsx +5 -0
- package/stories/Checkbox/Checkbox.stories.jsx +71 -0
- package/stories/Feedback/Feedback.stories.jsx +5 -6
- package/stories/Link/Link.stories.jsx +15 -1
- package/stories/List/List.stories.jsx +117 -0
- package/stories/Radio/Radio.stories.jsx +113 -0
- package/stories/Select/Select.stories.jsx +55 -0
- package/stories/Skeleton/Skeleton.stories.jsx +36 -0
- package/stories/Tags/Tags.stories.jsx +69 -0
- package/stories/TextInput/TextArea.stories.jsx +100 -0
- package/stories/supports.jsx +1 -1
|
@@ -1,96 +1,10 @@
|
|
|
1
|
-
import React
|
|
2
|
-
import { Platform, StyleSheet, TextInput as NativeTextInput, View } from 'react-native'
|
|
1
|
+
import React from 'react'
|
|
3
2
|
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import useUniqueId from '../utils/useUniqueId'
|
|
10
|
-
import StackView from '../StackView'
|
|
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(' ')
|
|
3
|
+
import { getTokensPropType, variantProp } from '../utils'
|
|
4
|
+
import InputSupports from '../InputSupports'
|
|
5
|
+
import TextInputBase from './TextInputBase'
|
|
6
|
+
import inputSupportProps from '../InputSupports/propTypes'
|
|
7
|
+
import textInputPropTypes from './propTypes'
|
|
94
8
|
|
|
95
9
|
/**
|
|
96
10
|
* A basic text input component. Use in forms or individually to receive user's input.
|
|
@@ -110,210 +24,29 @@ const joinDefined = (array) => array.filter((item) => item !== undefined).join('
|
|
|
110
24
|
* supported props and <a href="https://reactnative.dev/docs/textinput" target="_blank">React Native Web documentation</a> for
|
|
111
25
|
* their implementation on the web.
|
|
112
26
|
*/
|
|
113
|
-
function TextInput({
|
|
114
|
-
|
|
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, space } = themeTokens
|
|
175
|
-
|
|
176
|
-
const hasValidationError = validation === 'error'
|
|
27
|
+
function TextInput({ tokens, variant = {}, ...remainingProps }) {
|
|
28
|
+
const { props: supportsProps, rest } = inputSupportProps.select(remainingProps)
|
|
177
29
|
|
|
178
30
|
const inputProps = {
|
|
179
|
-
...
|
|
180
|
-
|
|
181
|
-
|
|
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
|
-
const feedbackVariant = {}
|
|
201
|
-
|
|
202
|
-
if (hasValidationError) {
|
|
203
|
-
feedbackVariant.state = 'error'
|
|
204
|
-
} else if (validation === 'success') {
|
|
205
|
-
feedbackVariant.state = 'success'
|
|
31
|
+
...rest,
|
|
32
|
+
tokens,
|
|
33
|
+
variant: { ...variant, validation: supportsProps.validation }
|
|
206
34
|
}
|
|
207
35
|
|
|
208
36
|
return (
|
|
209
|
-
<
|
|
210
|
-
{
|
|
211
|
-
<
|
|
212
|
-
label={label}
|
|
213
|
-
hint={hint}
|
|
214
|
-
hintPosition={hintPosition}
|
|
215
|
-
hintId={hintId}
|
|
216
|
-
tooltip={tooltip}
|
|
217
|
-
forId={inputId}
|
|
218
|
-
/>
|
|
37
|
+
<InputSupports {...supportsProps}>
|
|
38
|
+
{({ a11yProps, inputId }) => (
|
|
39
|
+
<TextInputBase {...inputProps} {...a11yProps} nativeID={inputId} />
|
|
219
40
|
)}
|
|
220
|
-
|
|
221
|
-
<NativeTextInput style={selectInputStyles(themeTokens, inactive)} {...inputProps} />
|
|
222
|
-
{IconComponent && (
|
|
223
|
-
<View
|
|
224
|
-
pointerEvents="none" // avoid hijacking input press events
|
|
225
|
-
style={[staticStyles.iconContainer, selectIconContainerStyles(themeTokens)]}
|
|
226
|
-
>
|
|
227
|
-
<IconComponent tokens={selectIconTokens(themeTokens)} />
|
|
228
|
-
</View>
|
|
229
|
-
)}
|
|
230
|
-
</View>
|
|
231
|
-
{feedback && (
|
|
232
|
-
<Feedback
|
|
233
|
-
title={feedback}
|
|
234
|
-
variant={feedbackVariant}
|
|
235
|
-
accessibilityRole={hasValidationError ? 'alert' : undefined}
|
|
236
|
-
/>
|
|
237
|
-
)}
|
|
238
|
-
</StackView>
|
|
41
|
+
</InputSupports>
|
|
239
42
|
)
|
|
240
43
|
}
|
|
241
44
|
|
|
242
|
-
const selectIconContainerStyles = ({ paddingRight }) => ({
|
|
243
|
-
paddingRight
|
|
244
|
-
})
|
|
245
|
-
|
|
246
45
|
TextInput.propTypes = {
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
*/
|
|
250
|
-
label: PropTypes.string,
|
|
251
|
-
/**
|
|
252
|
-
* If the `TextInput's` state is to be controlled by a parent component, use this prop
|
|
253
|
-
* together with the `onChange` to pass down and update the lifted state.
|
|
254
|
-
*/
|
|
255
|
-
value: PropTypes.string,
|
|
256
|
-
/**
|
|
257
|
-
* Use this to set the initial value of an uncontrolled `TextInput`.
|
|
258
|
-
* Updating `initialValue` will **not** update the actual value.
|
|
259
|
-
*/
|
|
260
|
-
initialValue: PropTypes.string,
|
|
261
|
-
/**
|
|
262
|
-
* A short description of the expected input.
|
|
263
|
-
*/
|
|
264
|
-
hint: PropTypes.string,
|
|
265
|
-
/**
|
|
266
|
-
* Position of the hint relative to label.
|
|
267
|
-
*/
|
|
268
|
-
hintPosition: PropTypes.oneOf(['inline', 'below']),
|
|
269
|
-
/**
|
|
270
|
-
* A detailed description of validation error/success or additional instructions.
|
|
271
|
-
* Visual variant is determined based on the `validation` prop.
|
|
272
|
-
*/
|
|
273
|
-
feedback: PropTypes.string,
|
|
274
|
-
/**
|
|
275
|
-
* Content of an optional `Tooltip`. If set, a tooltip button will be shown next to the label.
|
|
276
|
-
*/
|
|
277
|
-
tooltip: PropTypes.string,
|
|
278
|
-
/**
|
|
279
|
-
* Use to visually mark an input as valid or invalid.
|
|
280
|
-
*/
|
|
281
|
-
validation: PropTypes.oneOf(['error', 'success']),
|
|
282
|
-
/**
|
|
283
|
-
* Disables all user interactions with the input.
|
|
284
|
-
*/
|
|
285
|
-
inactive: PropTypes.bool,
|
|
286
|
-
/**
|
|
287
|
-
* Makes it impossible to change the input's value.
|
|
288
|
-
*/
|
|
289
|
-
readOnly: PropTypes.bool,
|
|
290
|
-
/**
|
|
291
|
-
* Use to react upon input's value changes. Required when the `value` prop is set.
|
|
292
|
-
* Will receive the input's value as an argument.
|
|
293
|
-
*/
|
|
294
|
-
onChange: PropTypes.func,
|
|
295
|
-
/** @ignore */
|
|
296
|
-
onChangeText: PropTypes.func,
|
|
297
|
-
/** @ignore */
|
|
298
|
-
onFocus: PropTypes.func,
|
|
299
|
-
/** @ignore */
|
|
300
|
-
onBlur: PropTypes.func,
|
|
301
|
-
/** @ignore */
|
|
302
|
-
onMouseOver: PropTypes.func,
|
|
303
|
-
/** @ignore */
|
|
304
|
-
onMouseOut: PropTypes.func,
|
|
46
|
+
...inputSupportProps.types,
|
|
47
|
+
...textInputPropTypes,
|
|
305
48
|
tokens: getTokensPropType('TextInput'),
|
|
306
49
|
variant: variantProp.propType
|
|
307
50
|
}
|
|
308
51
|
|
|
309
52
|
export default TextInput
|
|
310
|
-
|
|
311
|
-
const staticStyles = StyleSheet.create({
|
|
312
|
-
iconContainer: {
|
|
313
|
-
position: 'absolute',
|
|
314
|
-
right: 0,
|
|
315
|
-
top: 0,
|
|
316
|
-
bottom: 0,
|
|
317
|
-
justifyContent: 'center'
|
|
318
|
-
}
|
|
319
|
-
})
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import React, { useState } from 'react'
|
|
2
|
+
import { Platform, StyleSheet, TextInput as NativeTextInput, View } from 'react-native'
|
|
3
|
+
|
|
4
|
+
import PropTypes from 'prop-types'
|
|
5
|
+
import { applyTextStyles, useThemeTokens } from '../ThemeProvider'
|
|
6
|
+
import { getTokensPropType, useInputValue, variantProp } from '../utils'
|
|
7
|
+
|
|
8
|
+
const selectInputStyles = (
|
|
9
|
+
{
|
|
10
|
+
backgroundColor,
|
|
11
|
+
color,
|
|
12
|
+
borderWidth = 0,
|
|
13
|
+
borderColor,
|
|
14
|
+
borderRadius,
|
|
15
|
+
paddingTop = 0,
|
|
16
|
+
paddingBottom = 0,
|
|
17
|
+
paddingLeft,
|
|
18
|
+
paddingRight = 0,
|
|
19
|
+
fontName,
|
|
20
|
+
fontSize,
|
|
21
|
+
fontWeight,
|
|
22
|
+
lineHeight,
|
|
23
|
+
icon,
|
|
24
|
+
iconSize = 0,
|
|
25
|
+
minLines,
|
|
26
|
+
maxLines,
|
|
27
|
+
width,
|
|
28
|
+
height
|
|
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' ? Math.max(0, value - borderWidth) : value
|
|
36
|
+
|
|
37
|
+
const textStyles = applyTextStyles({ fontName, fontSize, lineHeight, fontWeight })
|
|
38
|
+
|
|
39
|
+
function linesToHeight(lines) {
|
|
40
|
+
const { lineHeight: absoluteLineHeight } = textStyles
|
|
41
|
+
|
|
42
|
+
return lines !== undefined ? lines * absoluteLineHeight + paddingTop + paddingBottom : undefined
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const minHeight = linesToHeight(minLines)
|
|
46
|
+
const maxHeight = linesToHeight(maxLines)
|
|
47
|
+
|
|
48
|
+
const webStyles = Platform.select({
|
|
49
|
+
web: {
|
|
50
|
+
outline: 'none',
|
|
51
|
+
cursor: inactive ? 'not-allowed' : undefined,
|
|
52
|
+
resize: minHeight !== maxHeight ? 'vertical' : 'none' // does nothing for an input, only needed for textarea
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
const paddingWithIcon = iconSize + paddingRight
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
backgroundColor,
|
|
60
|
+
color,
|
|
61
|
+
borderWidth,
|
|
62
|
+
borderColor,
|
|
63
|
+
borderRadius,
|
|
64
|
+
paddingLeft: offsetBorder(paddingLeft),
|
|
65
|
+
paddingRight: icon ? offsetBorder(paddingWithIcon) : offsetBorder(paddingRight),
|
|
66
|
+
paddingTop: offsetBorder(paddingTop),
|
|
67
|
+
paddingBottom: offsetBorder(paddingBottom),
|
|
68
|
+
minHeight,
|
|
69
|
+
maxHeight,
|
|
70
|
+
width,
|
|
71
|
+
height,
|
|
72
|
+
...textStyles,
|
|
73
|
+
...webStyles
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const selectOuterBorderStyles = ({
|
|
78
|
+
outerBackgroundColor,
|
|
79
|
+
outerBorderWidth = 0,
|
|
80
|
+
outerBorderColor,
|
|
81
|
+
outerBorderRadius = 0
|
|
82
|
+
}) => {
|
|
83
|
+
// Use negative margins so that the outer border doesn't expand the input's bounding box
|
|
84
|
+
const margin = -1 * outerBorderWidth
|
|
85
|
+
|
|
86
|
+
// Account for the border width since we style it as an outline
|
|
87
|
+
const borderRadius = outerBorderRadius + outerBorderWidth
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
background: outerBackgroundColor,
|
|
91
|
+
borderWidth: outerBorderWidth,
|
|
92
|
+
borderColor: outerBorderColor,
|
|
93
|
+
borderRadius,
|
|
94
|
+
marginTop: margin,
|
|
95
|
+
marginBottom: margin,
|
|
96
|
+
marginLeft: margin,
|
|
97
|
+
marginRight: margin
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const selectIconTokens = ({ iconSize, iconColor }) => ({
|
|
102
|
+
size: iconSize,
|
|
103
|
+
color: iconColor
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
const selectIconContainerStyles = ({ paddingRight, paddingBottom }) => ({
|
|
107
|
+
paddingRight,
|
|
108
|
+
paddingBottom
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
function TextInputBase({
|
|
112
|
+
value,
|
|
113
|
+
height,
|
|
114
|
+
initialValue,
|
|
115
|
+
inactive,
|
|
116
|
+
readOnly,
|
|
117
|
+
onChange,
|
|
118
|
+
onChangeText,
|
|
119
|
+
onFocus,
|
|
120
|
+
onBlur,
|
|
121
|
+
onMouseOver,
|
|
122
|
+
onMouseOut,
|
|
123
|
+
tokens,
|
|
124
|
+
variant = {},
|
|
125
|
+
...remainingProps
|
|
126
|
+
}) {
|
|
127
|
+
const [isFocused, setIsFocused] = useState(false)
|
|
128
|
+
const handleFocus = (event) => {
|
|
129
|
+
setIsFocused(true)
|
|
130
|
+
if (typeof onFocus === 'function') onFocus(event)
|
|
131
|
+
}
|
|
132
|
+
const handleBlur = (event) => {
|
|
133
|
+
setIsFocused(false)
|
|
134
|
+
if (typeof onBlur === 'function') onBlur(event)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const [isHovered, setIsHovered] = useState(false)
|
|
138
|
+
const handleMouseOver = (event) => {
|
|
139
|
+
setIsHovered(true)
|
|
140
|
+
if (typeof onMouseOver === 'function') onMouseOver(event)
|
|
141
|
+
}
|
|
142
|
+
const handleMouseOut = (event) => {
|
|
143
|
+
setIsHovered(false)
|
|
144
|
+
if (typeof onMouseOut === 'function') onMouseOut(event)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const { currentValue, setValue, isControlled } = useInputValue({
|
|
148
|
+
value,
|
|
149
|
+
initialValue,
|
|
150
|
+
onChange,
|
|
151
|
+
readOnly
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
const handleChangeText = (text) => {
|
|
155
|
+
setValue(text)
|
|
156
|
+
if (typeof onChangeText === 'function') onChangeText(text)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const states = { focus: isFocused, hover: isHovered, inactive }
|
|
160
|
+
|
|
161
|
+
const themeTokens = useThemeTokens('TextInput', tokens, variant, states)
|
|
162
|
+
|
|
163
|
+
const { icon: IconComponent } = themeTokens
|
|
164
|
+
|
|
165
|
+
const inputProps = {
|
|
166
|
+
...remainingProps,
|
|
167
|
+
editable: !inactive,
|
|
168
|
+
onFocus: handleFocus,
|
|
169
|
+
onBlur: handleBlur,
|
|
170
|
+
onMouseOver: handleMouseOver,
|
|
171
|
+
onMouseOut: handleMouseOut,
|
|
172
|
+
onChangeText: handleChangeText,
|
|
173
|
+
defaultValue: initialValue,
|
|
174
|
+
// currentValue is being updated even if the input is not controlled, passing it down to the
|
|
175
|
+
// Input could lead to changing its state from uncontrolled to controlled
|
|
176
|
+
value: isControlled ? currentValue : undefined
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const nativeInputStyle = selectInputStyles({ ...themeTokens, height }, inactive)
|
|
180
|
+
|
|
181
|
+
return (
|
|
182
|
+
<View style={selectOuterBorderStyles(themeTokens)}>
|
|
183
|
+
<NativeTextInput style={nativeInputStyle} {...inputProps} />
|
|
184
|
+
{IconComponent && (
|
|
185
|
+
<View
|
|
186
|
+
pointerEvents="none" // avoid hijacking input press events
|
|
187
|
+
style={[staticStyles.iconContainer, selectIconContainerStyles(themeTokens)]}
|
|
188
|
+
>
|
|
189
|
+
<IconComponent tokens={selectIconTokens(themeTokens)} />
|
|
190
|
+
</View>
|
|
191
|
+
)}
|
|
192
|
+
</View>
|
|
193
|
+
)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
TextInputBase.propTypes = {
|
|
197
|
+
value: PropTypes.string,
|
|
198
|
+
height: PropTypes.number,
|
|
199
|
+
initialValue: PropTypes.string,
|
|
200
|
+
inactive: PropTypes.bool,
|
|
201
|
+
readOnly: PropTypes.bool,
|
|
202
|
+
onChange: PropTypes.func,
|
|
203
|
+
onChangeText: PropTypes.func,
|
|
204
|
+
onFocus: PropTypes.func,
|
|
205
|
+
onBlur: PropTypes.func,
|
|
206
|
+
onMouseOver: PropTypes.func,
|
|
207
|
+
onMouseOut: PropTypes.func,
|
|
208
|
+
tokens: getTokensPropType('TextInput', 'TextArea'),
|
|
209
|
+
variant: variantProp.propType
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export default TextInputBase
|
|
213
|
+
|
|
214
|
+
const staticStyles = StyleSheet.create({
|
|
215
|
+
iconContainer: {
|
|
216
|
+
position: 'absolute',
|
|
217
|
+
right: 0,
|
|
218
|
+
bottom: 0
|
|
219
|
+
}
|
|
220
|
+
})
|
package/src/TextInput/index.js
CHANGED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import PropTypes from 'prop-types'
|
|
2
|
+
|
|
3
|
+
const textInputPropTypes = {
|
|
4
|
+
/**
|
|
5
|
+
* If the input's state is to be controlled by a parent component, use this prop
|
|
6
|
+
* together with the `onChange` to pass down and update the lifted state.
|
|
7
|
+
*/
|
|
8
|
+
value: PropTypes.string,
|
|
9
|
+
/**
|
|
10
|
+
* Use this to set the initial value of an uncontrolled input.
|
|
11
|
+
* Updating `initialValue` will **not** update the actual value.
|
|
12
|
+
*/
|
|
13
|
+
initialValue: PropTypes.string,
|
|
14
|
+
/**
|
|
15
|
+
* Disables all user interactions with the input.
|
|
16
|
+
*/
|
|
17
|
+
inactive: PropTypes.bool,
|
|
18
|
+
/**
|
|
19
|
+
* Makes it impossible to change the input's value.
|
|
20
|
+
*/
|
|
21
|
+
readOnly: PropTypes.bool,
|
|
22
|
+
/**
|
|
23
|
+
* Use to react upon input's value changes. Required when the `value` prop is set.
|
|
24
|
+
* Will receive the input's value as an argument.
|
|
25
|
+
*/
|
|
26
|
+
onChange: PropTypes.func
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export default textInputPropTypes
|
|
@@ -11,6 +11,22 @@ import { getComponentTheme, getThemeTokens, resolveTokens, mergeAppearances } fr
|
|
|
11
11
|
* Returns a complete set of theme tokens for a component based on which of the
|
|
12
12
|
* component's theme rules apply to the current set of theme appearances.
|
|
13
13
|
*
|
|
14
|
+
* When applying theme tokens as styles to a component, don't spread the theme tokens
|
|
15
|
+
* object, explicitly select each property. This way, if new theme tokens are added to the
|
|
16
|
+
* component's theme in a minor or patch release of the theme package, the appearance
|
|
17
|
+
* of the component in sites and apps will only change when the component itself is updated.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const seletContainerTokens = ({ color, width, height }) => ({
|
|
21
|
+
* backgroundColor: color, width, height
|
|
22
|
+
* })
|
|
23
|
+
* const MyComponent = ({ tokens, variant, children }) => {
|
|
24
|
+
* const viewport = useViewport()
|
|
25
|
+
* const themeTokens = useThemeTokens('MyComponent', tokens, variant, { viewport })
|
|
26
|
+
* const style = seletContainerTokens(themeTokens)
|
|
27
|
+
* return <View style={style}>{children}</View>
|
|
28
|
+
* }
|
|
29
|
+
*
|
|
14
30
|
* @param {string} componentName - the name as defined in the theme schema of the component whose theme is to be used
|
|
15
31
|
* @param {TokensProp} [tokens] - every themed component should accept an optional `tokens` prop allowing theme tokens to be overridden
|
|
16
32
|
* @param {AppearanceSet} [variants] - every themed component should accept an optional `variants` prop specifying theme variants
|
|
@@ -26,13 +42,48 @@ export const useThemeTokens = (componentName, tokens = {}, variants = {}, states
|
|
|
26
42
|
|
|
27
43
|
/**
|
|
28
44
|
* Returns a memoised tokens getter function that gets tokens similar to calling useThemeTokens.
|
|
29
|
-
* Scenarios where useThemeTokensCallback should be used instead of useThemeTokens include:
|
|
45
|
+
* Scenarios where `useThemeTokensCallback` should be used instead of `useThemeTokens` include:
|
|
30
46
|
*
|
|
31
|
-
* - Where tokens to be obtained from state accessible only in scopes like callbacks
|
|
32
|
-
* where calling useThemeTokens directly would be disallowed by React's hook rules.
|
|
47
|
+
* - Where tokens are to be obtained from state that is accessible only in scopes like callbacks
|
|
48
|
+
* and render functions, where calling useThemeTokens directly would be disallowed by React's hook rules.
|
|
33
49
|
* - Passing a tokens getter down via a child component's `tokens` prop, applying rules using the
|
|
34
50
|
* child component's current state. Consider wrapping the returned tokens in `selectTokens()`.
|
|
35
51
|
*
|
|
52
|
+
* The function returned by `useThemeTokens` may be called with an object of state appearances to get an object
|
|
53
|
+
* of tokens, or may be passed as a `tokens` prop to other themed components or `useThemeTokens`/`useThemeTokensCallback`
|
|
54
|
+
* hooks.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* // Resolving tokens inside Pressable's style function, based on Pressable state
|
|
58
|
+
* const PressMe = ({ tokens, variant, children }) => {
|
|
59
|
+
* const getTokens = useThemeTokensCallback('PressMe', tokens, variant)
|
|
60
|
+
* const getPressableStyle = ({ pressed }) => {
|
|
61
|
+
* const { color, width, height } = getTokens({ pressed })
|
|
62
|
+
* return { backgroundColor: color, width, height }
|
|
63
|
+
* }
|
|
64
|
+
* return <Pressable style={getPressableStyle}>{children}</Pressable>
|
|
65
|
+
* }
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* // Setting the theme in a parent and resolving it in a child based on child's state
|
|
69
|
+
* const MenuButton = ({ tokens, variant, ...buttonProps }) => {
|
|
70
|
+
* // Define what theme, variant etc we want in this component...
|
|
71
|
+
* const getTokens = useThemeTokensCallback('Button', tokens, variant)
|
|
72
|
+
* // ...resolve them in another component based on its state (e.g. press, hover...)
|
|
73
|
+
* return <ButtonBase tokens={getTokens} accessibilityRole="menuitem" {...buttonProps} />
|
|
74
|
+
* }
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* // Chaining multiple themes together, sharing the same variants and state
|
|
78
|
+
* const GlowingLink = ({ tokens, variant, children }) => {
|
|
79
|
+
* // applies a small theme for a glowing effect, using same variants and states as "link"
|
|
80
|
+
* const getGlowTokens = useThemeCallback('GlowingLink', tokens, variant)
|
|
81
|
+
* // applies link state to get "Link" theme tokens and merge "GlowingLink" tokens on top
|
|
82
|
+
* const getTokens = useThemeCallback('Link', getGlowTokens, variant)
|
|
83
|
+
* // renders a link with both themes applied, both based on current link state
|
|
84
|
+
* return <LinkBase tokens={getTokens}>{children}</LinkBase>
|
|
85
|
+
* }
|
|
86
|
+
*
|
|
36
87
|
* @param {string} componentName - the name as defined in the theme schema of the component whose theme is to be used
|
|
37
88
|
* @param {TokensProp} [tokens] - every themed component should accept a `tokens` prop allowing theme tokens to be overridden
|
|
38
89
|
* @param {AppearanceSet} [variants] - variants passed in as props that don't change dynamically
|