-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathInput.tsx
More file actions
198 lines (179 loc) · 5.04 KB
/
Copy pathInput.tsx
File metadata and controls
198 lines (179 loc) · 5.04 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { NeighborLocation, Box, InvalidStateBox } from '@semcore/base-components';
import type { Intergalactic } from '@semcore/core';
import { createComponent, Component, sstyled, Root, lastInteraction } from '@semcore/core';
import React from 'react';
import type { NSInput } from './Input.type';
import style from './style/input.shadow.css';
class Input extends Component<
Intergalactic.InternalTypings.InferComponentProps<NSInput.Component>,
[],
{},
{},
{},
NSInput.DefaultProps
> {
static displayName = 'Input';
static defaultProps = {
size: 'm',
state: 'normal',
} as const;
static style = style;
inputRef = React.createRef<HTMLInputElement>();
handleMouseDownAddon = (event: React.MouseEvent) => {
event.preventDefault();
this.inputRef.current?.focus();
};
handleClick = () => {
if (!lastInteraction.isKeyboard) return;
setTimeout(() => {
if (document.activeElement === document.body) {
this.inputRef.current?.focus();
}
}, 0);
};
getAddonProps() {
const { disabled, size } = this.asProps;
return {
disabled,
onMouseDown: this.handleMouseDownAddon,
onClick: this.handleClick,
size,
};
}
getValueProps() {
const {
size,
disabled,
state,
role,
placeholder,
// these props might be passed from the <Select /> component
'aria-haspopup': ariaHaspopup,
'aria-controls': ariaControls,
'aria-expanded': ariaExpanded,
'aria-autocomplete': ariaAutocomplete,
'aria-owns': ariaOwns,
'aria-activedescendant': ariaActivedescendant,
} = this.asProps;
return {
'ref': this.inputRef,
size,
disabled,
state,
role,
placeholder,
'aria-haspopup': ariaHaspopup,
'aria-controls': ariaControls,
'aria-expanded': ariaExpanded,
'aria-autocomplete': ariaAutocomplete,
'aria-owns': ariaOwns,
'aria-activedescendant': ariaActivedescendant,
};
}
render() {
const SInput = Root;
const SOutline = 'div';
const { Children, styles, neighborLocation, controlsLength, state } = this.asProps;
return (
<NeighborLocation.Detect neighborLocation={neighborLocation}>
{(neighborLocation) =>
sstyled(styles)(
<SInput
render={Box}
neighborLocation={neighborLocation}
__excludeProps={[
'role',
'aria-haspopup',
'aria-controls',
'aria-expanded',
'placeholder',
'aria-autocomplete',
'aria-owns',
'aria-activedescendant',
'tabIndex',
]}
>
<NeighborLocation controlsLength={controlsLength}>
<Children />
</NeighborLocation>
<SOutline>{state === 'invalid' && <InvalidStateBox />}</SOutline>
</SInput>,
)}
</NeighborLocation.Detect>
);
}
}
class Value extends Component<
Intergalactic.InternalTypings.InferChildComponentProps<NSInput.Value.Component, typeof Input, 'Value'>,
[],
NSInput.Value.Handlers,
{},
{},
NSInput.Value.DefaultProps
> {
static defaultProps = {
defaultValue: '',
};
inputRef = React.createRef<HTMLInputElement>();
componentDidMount() {
if (this.asProps.autoFocus) {
setTimeout(() => {
this.inputRef.current?.focus();
}, 10); // in autoFocusEnhance it was boolean `true`. In FF and Safari was floating bug with focus, so, I set 10.
}
}
uncontrolledProps() {
return {
value: (e: React.ChangeEvent<HTMLInputElement>) => e.target.value,
};
}
render() {
const SValue = Root;
const { styles, neighborLocation, state } = this.asProps;
return (
<NeighborLocation.Detect neighborLocation={neighborLocation}>
{(neighborLocation) =>
sstyled(styles)(
<SValue
ref={this.inputRef}
render={Box}
inAfterOutline
neighborLocation={neighborLocation}
tag='input'
type='text'
aria-invalid={state === 'invalid'}
use:autoFocus={false}
/>,
)}
</NeighborLocation.Detect>
);
}
}
function Addon(
props: Intergalactic.InternalTypings.InferChildComponentProps<NSInput.Addon.Component, typeof Input, 'Addon'>,
) {
const SAddon = Root;
const { Children, styles, neighborLocation } = props;
return (
<NeighborLocation.Detect neighborLocation={neighborLocation}>
{(neighborLocation) =>
sstyled(styles)(
<SAddon render={Box} neighborLocation={neighborLocation}>
<Children />
</SAddon>,
)}
</NeighborLocation.Detect>
);
}
/**
* Input
*
* {@link https://developer.semrush.com/intergalactic/components/input/input-api/|API} | {@link https://developer.semrush.com/intergalactic/components/input/input-code/|Examples}
*/
export default createComponent<
NSInput.Component,
typeof Input
>(Input, {
Addon,
Value,
});