-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathinput-base.tsx
134 lines (124 loc) · 3.12 KB
/
input-base.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* External dependencies
*/
import type { ForwardedRef } from 'react';
/**
* WordPress dependencies
*/
import { useInstanceId } from '@wordpress/compose';
import { forwardRef, useMemo } from '@wordpress/element';
/**
* Internal dependencies
*/
import Backdrop from './backdrop';
import Label from './label';
import {
Container,
Root,
Prefix,
Suffix,
LabelWrapper,
getSizeConfig,
} from './styles/input-control-styles';
import type { InputBaseProps, LabelPosition } from './types';
import { ContextSystemProvider } from '../ui/context';
function useUniqueId( idProp?: string ) {
const instanceId = useInstanceId( InputBase );
const id = `input-base-control-${ instanceId }`;
return idProp || id;
}
// Adapter to map props for the new ui/flex compopnent.
function getUIFlexProps( labelPosition?: LabelPosition ) {
const props: { direction?: string; gap?: number; justify?: string } = {};
switch ( labelPosition ) {
case 'top':
props.direction = 'column';
props.gap = 0;
break;
case 'bottom':
props.direction = 'column-reverse';
props.gap = 0;
break;
case 'edge':
props.justify = 'space-between';
break;
}
return props;
}
export function InputBase(
{
__next36pxDefaultSize,
__unstableInputWidth,
children,
className,
disabled = false,
hideLabelFromVision = false,
labelPosition,
id: idProp,
isFocused = false,
label,
prefix,
size = 'default',
suffix,
...props
}: InputBaseProps,
ref: ForwardedRef< HTMLDivElement >
) {
const id = useUniqueId( idProp );
const hideLabel = hideLabelFromVision || ! label;
const { paddingLeft, paddingRight } = getSizeConfig( {
inputSize: size,
__next36pxDefaultSize,
} );
const prefixSuffixContextValue = useMemo( () => {
return {
InputControlPrefixWrapper: { paddingLeft },
InputControlSuffixWrapper: { paddingRight },
};
}, [ paddingLeft, paddingRight ] );
return (
// @ts-expect-error The `direction` prop from Flex (FlexDirection) conflicts with legacy SVGAttributes `direction` (string) that come from React intrinsic prop definitions.
<Root
{ ...props }
{ ...getUIFlexProps( labelPosition ) }
className={ className }
isFocused={ isFocused }
labelPosition={ labelPosition }
ref={ ref }
>
<LabelWrapper>
<Label
className="components-input-control__label"
hideLabelFromVision={ hideLabelFromVision }
labelPosition={ labelPosition }
htmlFor={ id }
>
{ label }
</Label>
</LabelWrapper>
<Container
__unstableInputWidth={ __unstableInputWidth }
className="components-input-control__container"
disabled={ disabled }
hideLabel={ hideLabel }
labelPosition={ labelPosition }
>
<ContextSystemProvider value={ prefixSuffixContextValue }>
{ prefix && (
<Prefix className="components-input-control__prefix">
{ prefix }
</Prefix>
) }
{ children }
{ suffix && (
<Suffix className="components-input-control__suffix">
{ suffix }
</Suffix>
) }
</ContextSystemProvider>
<Backdrop disabled={ disabled } isFocused={ isFocused } />
</Container>
</Root>
);
}
export default forwardRef( InputBase );