forked from primer/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_TextInputInnerVisualSlot.tsx
45 lines (41 loc) · 1.55 KB
/
_TextInputInnerVisualSlot.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import React from 'react'
import {Box, Spinner} from '.'
import {TextInputNonPassthroughProps} from './TextInput'
const TextInputInnerVisualSlot: React.FC<{
/** Whether the input is expected to ever show a loading indicator */
hasLoadingIndicator: boolean
/** Whether the to show the loading indicator */
showLoadingIndicator: TextInputNonPassthroughProps['loading']
/** Which side of this visual is being rendered */
visualPosition: 'leading' | 'trailing'
}> = ({children, hasLoadingIndicator, showLoadingIndicator, visualPosition}) => {
if ((!children && !hasLoadingIndicator) || (visualPosition === 'leading' && !children && !showLoadingIndicator)) {
return null
}
if (!hasLoadingIndicator) {
return <span className="TextInput-icon">{children}</span>
}
return (
<span className="TextInput-icon">
<Box display="flex" position="relative">
{children && <Box sx={{visibility: showLoadingIndicator ? 'hidden' : 'visible'}}>{children}</Box>}
<Spinner
sx={
children
? {
position: 'absolute',
top: 0,
height: '100%',
maxWidth: '100%',
visibility: showLoadingIndicator ? 'visible' : 'hidden',
...(visualPosition === 'leading' ? {left: 0} : {right: 0})
}
: {visibility: showLoadingIndicator ? 'visible' : 'hidden'}
}
size={children ? undefined : 'small'}
/>
</Box>
</span>
)
}
export default TextInputInnerVisualSlot