@semcore/inline-input 1.0.0

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.
@@ -0,0 +1,387 @@
1
+ import React from 'react';
2
+ import createComponent, { Component, sstyled, Root } from '@semcore/core';
3
+ import { Box } from '@semcore/flex-box';
4
+ import autoFocusEnhance from '@semcore/utils/lib/enhances/autoFocusEnhance';
5
+ import Tooltip, { ITooltipProps } from '@semcore/tooltip';
6
+ import style from './style/inline-input.shadow.css';
7
+ import CheckM from '@semcore/icon/Check/m';
8
+ import CloseM from '@semcore/icon/Close/m';
9
+ import Spin from '@semcore/spin';
10
+
11
+ const isFocusOutsideOf = (element: HTMLElement) => {
12
+ let traversed = document.activeElement;
13
+ for (let i = 0; i < 1000000; i++) {
14
+ if (traversed === element) return false;
15
+ if (traversed === document.body) return true;
16
+ traversed = traversed.parentElement;
17
+ }
18
+
19
+ throw new Error(
20
+ 'Failed to traverse parents chain in utilite `isFocusOutsideOf` reasonable count of iterations. Possibly Shadow DOM or come other uncommon DOM-way is used, that is not supported by utilite yet. If you are not doing weird things or that is reasonable, contact developers team for yours case support.',
21
+ );
22
+ };
23
+
24
+ type AsProps = {
25
+ state?: 'normal' | 'valid' | 'invalid' | 'disabled';
26
+ loading?: boolean;
27
+ onConfirm?: (
28
+ value: string,
29
+ event: React.MouseEvent | React.FocusEvent | React.KeyboardEvent,
30
+ ) => void;
31
+ onCancel?: (
32
+ prevValue: string,
33
+ event: React.MouseEvent | React.FocusEvent | React.KeyboardEvent,
34
+ ) => void;
35
+ value?: string;
36
+ defaultValue?: string;
37
+ confirmText?: string;
38
+ cancelText?: string;
39
+ tooltipsProps?: ITooltipProps;
40
+ autoFocus?: boolean;
41
+ placeholder?: string;
42
+ inputId?: string;
43
+ onChange?: (value: string, event: React.ChangeEvent) => void;
44
+ onBlur?: (event: React.FocusEvent) => void;
45
+ onFocus?: (event: React.FocusEvent) => void;
46
+ onBlurBehavior?: 'cancel' | 'confirm';
47
+ styles?: React.CSSProperties;
48
+ Children: React.FC;
49
+ };
50
+
51
+ class InlineInputBase extends Component<AsProps> {
52
+ static displayName = 'InlineInput';
53
+
54
+ static defaultProps = {
55
+ state: 'normal',
56
+ loading: false,
57
+ defaultValue: '',
58
+ __excludeProps: [
59
+ 'defaultValue',
60
+ 'placeholder',
61
+ 'tooltipsProps',
62
+ 'confirmText',
63
+ 'cancelText',
64
+ 'autoFocus',
65
+ 'inputId',
66
+ ],
67
+ };
68
+ static style = style;
69
+
70
+ state = {
71
+ focused: false,
72
+ };
73
+
74
+ rootRef = React.createRef<HTMLElement>();
75
+ inputRef = React.createRef();
76
+ initValue: string = '';
77
+
78
+ uncontrolledProps() {
79
+ return {
80
+ value: null,
81
+ };
82
+ }
83
+
84
+ constructor(props) {
85
+ super(props);
86
+ this.handleConfirm = this.handleConfirm.bind(this);
87
+ this.handleCancel = this.handleCancel.bind(this);
88
+ this.handleFocus = this.handleFocus.bind(this);
89
+ this.handleBlur = this.handleBlur.bind(this);
90
+ this.handleKeyDown = this.handleKeyDown.bind(this);
91
+ }
92
+
93
+ componentDidMount() {
94
+ this.initValue = ((this.asProps.value ?? this.asProps.defaultValue) as string) || '';
95
+ }
96
+
97
+ getContextProps() {
98
+ const {
99
+ loading,
100
+ value,
101
+ defaultValue,
102
+ state,
103
+ confirmText = 'Confirm',
104
+ cancelText = 'Cancel',
105
+ tooltipsProps = {},
106
+ } = this.asProps;
107
+
108
+ return {
109
+ loading,
110
+ onConfirm: this.handleConfirm,
111
+ onCancel: this.handleCancel,
112
+ value,
113
+ defaultValue,
114
+ state,
115
+ confirmText,
116
+ cancelText,
117
+ tooltipsProps,
118
+ };
119
+ }
120
+
121
+ geAddonProps() {
122
+ return this.getContextProps();
123
+ }
124
+ getConfirmControlProps() {
125
+ return this.getContextProps();
126
+ }
127
+ getCancelControlProps() {
128
+ return this.getContextProps();
129
+ }
130
+ getControlsProps() {
131
+ return this.getContextProps();
132
+ }
133
+
134
+ getValueProps() {
135
+ const { autoFocus, value, inputId, defaultValue, onChange, state, loading, placeholder } =
136
+ this.asProps;
137
+
138
+ return {
139
+ ref: this.inputRef,
140
+ autoFocus,
141
+ value,
142
+ defaultValue,
143
+ onChange,
144
+ state,
145
+ loading,
146
+ placeholder,
147
+ id: inputId,
148
+ };
149
+ }
150
+
151
+ handleConfirm(text: string, event: React.MouseEvent | React.FocusEvent | React.KeyboardEvent) {
152
+ const { onConfirm } = this.asProps;
153
+ onConfirm?.(text, event);
154
+ return false;
155
+ }
156
+ handleCancel(prevText: string, event: React.MouseEvent | React.FocusEvent | React.KeyboardEvent) {
157
+ const { onCancel } = this.asProps;
158
+ onCancel?.(prevText, event);
159
+ return false;
160
+ }
161
+
162
+ handleFocus(event: React.FocusEvent) {
163
+ const { onFocus } = this.asProps;
164
+ this.setState({ focused: true });
165
+ if (onFocus) {
166
+ onFocus(event);
167
+ }
168
+ }
169
+ handleBlur(event: React.FocusEvent) {
170
+ const { onConfirm, onCancel, onBlur, onBlurBehavior, value } = this.asProps;
171
+ this.setState({ focused: false });
172
+ if (onBlurBehavior) {
173
+ setTimeout(() => {
174
+ if (isFocusOutsideOf(this.rootRef.current)) {
175
+ if (onBlurBehavior === 'confirm') onConfirm?.(value, event);
176
+ if (onBlurBehavior === 'cancel') onCancel?.(this.initValue, event);
177
+ }
178
+ }, 0);
179
+ }
180
+ if (onBlur) {
181
+ onBlur(event);
182
+ }
183
+ }
184
+ handleKeyDown(event: React.KeyboardEvent) {
185
+ const { onConfirm, onCancel, value } = this.asProps;
186
+ if (event.code === 'Enter') onConfirm?.(value, event);
187
+ if (event.code === 'Escape') onCancel?.(this.initValue, event);
188
+ }
189
+
190
+ render() {
191
+ const SInlineInput = Root;
192
+ const SUnderline = 'div';
193
+ const { Children: providedChildren, children: hasChildren, styles, loading } = this.asProps;
194
+ const Children = hasChildren ? providedChildren : DefaultChildren;
195
+ const { focused } = this.state;
196
+
197
+ return sstyled(styles)(
198
+ <SInlineInput
199
+ ref={this.rootRef}
200
+ onFocus={this.handleFocus}
201
+ onBlur={this.handleBlur}
202
+ onKeyDown={this.handleKeyDown}
203
+ render={Box}
204
+ focused={focused}
205
+ loading={loading}
206
+ >
207
+ <SUnderline>
208
+ <Children />
209
+ </SUnderline>
210
+ </SInlineInput>,
211
+ );
212
+ }
213
+ }
214
+
215
+ class Value extends Component<AsProps> {
216
+ static defaultProps = {
217
+ defaultValue: '',
218
+ };
219
+ static enhance = [autoFocusEnhance()];
220
+
221
+ uncontrolledProps() {
222
+ return {
223
+ value: (event) => {
224
+ event.stopPropagation();
225
+ return event.target.value;
226
+ },
227
+ };
228
+ }
229
+
230
+ render() {
231
+ const SValue = Root;
232
+
233
+ return sstyled(this.asProps.styles)(
234
+ <SValue
235
+ render={Box}
236
+ tag="input"
237
+ type="text"
238
+ disabled={this.asProps.state === 'disabled' || this.asProps.loading}
239
+ />,
240
+ );
241
+ }
242
+ }
243
+
244
+ const Addon: React.FC<AsProps> = (props) => {
245
+ const SAddon = Root;
246
+ return sstyled(props.styles)(<SAddon render={Box} />) as React.ReactElement;
247
+ };
248
+
249
+ const ConfirmControl: React.FC<AsProps> = (props) => {
250
+ const SConfirmControl = Root;
251
+ const { Children, children: hasChildren } = props;
252
+
253
+ const handleConfirm = React.useCallback(
254
+ (event: React.MouseEvent | React.KeyboardEvent) => {
255
+ props.onConfirm?.(props.value, event);
256
+ },
257
+ [props.onConfirm, props.value],
258
+ );
259
+ const handleKeydown = React.useCallback(
260
+ (event: React.KeyboardEvent) => {
261
+ if (props.loading) return;
262
+ if (event.code === 'Enter' || event.code === 'Space') {
263
+ event.preventDefault();
264
+ event.stopPropagation();
265
+ handleConfirm(event);
266
+ }
267
+ },
268
+ [props.loading, handleConfirm],
269
+ );
270
+
271
+ if (props.loading) {
272
+ return sstyled(props.styles)(
273
+ <SConfirmControl render={Box} className="controls-icon">
274
+ {hasChildren ? <Children /> : <Spin size="xxs" className="spin" />}
275
+ </SConfirmControl>,
276
+ ) as React.ReactElement;
277
+ }
278
+
279
+ return sstyled(props.styles)(
280
+ <SConfirmControl render={Box} onKeyDown={handleKeydown}>
281
+ {hasChildren ? (
282
+ <Children />
283
+ ) : (
284
+ <Tooltip {...props.tooltipsProps}>
285
+ <Tooltip.Trigger className="controls-icon">
286
+ <CheckM
287
+ tabIndex={0}
288
+ aria-label={`${props.confirmText} ${props.value ?? ''}`}
289
+ role="button"
290
+ onClick={handleConfirm}
291
+ />
292
+ </Tooltip.Trigger>
293
+ <Tooltip.Popper p={2}>{props.confirmText}</Tooltip.Popper>
294
+ </Tooltip>
295
+ )}
296
+ </SConfirmControl>,
297
+ ) as React.ReactElement;
298
+ };
299
+ const CancelControl: React.FC<AsProps> = (props) => {
300
+ const SCancelControl = Root;
301
+ const { Children, children: hasChildren } = props;
302
+ const initValue = React.useRef(props.value ?? props.defaultValue);
303
+ const handleCancel = React.useCallback(
304
+ (event: React.MouseEvent | React.KeyboardEvent) => {
305
+ if (props.loading || !props.onCancel) return;
306
+ props.onCancel?.(initValue.current, event);
307
+ },
308
+ [props.loading, props.onCancel],
309
+ );
310
+ const handleKeydown = React.useCallback(
311
+ (event: React.KeyboardEvent) => {
312
+ if (props.loading) return;
313
+ if (event.code === 'Enter' || event.code === 'Space') {
314
+ event.preventDefault();
315
+ event.stopPropagation();
316
+ handleCancel(event);
317
+ }
318
+ },
319
+ [props.loading, handleCancel],
320
+ );
321
+
322
+ if (props.loading) {
323
+ return sstyled(props.styles)(
324
+ <SCancelControl render={Box} className="controls-icon">
325
+ {hasChildren ? <Children /> : <CloseM disabled={true} />}
326
+ </SCancelControl>,
327
+ ) as React.ReactElement;
328
+ }
329
+
330
+ return sstyled(props.styles)(
331
+ <SCancelControl render={Box} onKeyDown={handleKeydown}>
332
+ {hasChildren ? (
333
+ <Children />
334
+ ) : (
335
+ <Tooltip {...props.tooltipsProps}>
336
+ <Tooltip.Trigger className="controls-icon">
337
+ <CloseM
338
+ tabIndex={0}
339
+ aria-label={props.cancelText}
340
+ role="button"
341
+ onClick={handleCancel}
342
+ />
343
+ </Tooltip.Trigger>
344
+ <Tooltip.Popper p={2}>{props.cancelText}</Tooltip.Popper>
345
+ </Tooltip>
346
+ )}
347
+ </SCancelControl>,
348
+ ) as React.ReactElement;
349
+ };
350
+
351
+ const Controls: React.FC<AsProps> = (props) => {
352
+ const SControls = Root;
353
+ return sstyled(props.styles)(
354
+ <SControls render={Box}>
355
+ <InlineInput.ConfirmControl
356
+ tooltipsProps={props.tooltipsProps}
357
+ confirmText={props.confirmText}
358
+ onConfirm={props.onConfirm}
359
+ />
360
+ <InlineInput.CancelControl
361
+ tooltipsProps={props.tooltipsProps}
362
+ cancelText={props.cancelText}
363
+ onCancel={props.onCancel}
364
+ />
365
+ </SControls>,
366
+ ) as React.ReactElement;
367
+ };
368
+
369
+ const DefaultChildren: React.FC<AsProps> = (props) => {
370
+ return (
371
+ <>
372
+ <InlineInput.Value {...props} />
373
+ <InlineInput.Controls />
374
+ </>
375
+ ) as React.ReactElement;
376
+ };
377
+
378
+ /** `createComponent` currently exposes unrelated junk instead of typings, that the reason of to any cast */
379
+ const InlineInput = createComponent(InlineInputBase, {
380
+ Addon,
381
+ Value,
382
+ ConfirmControl,
383
+ CancelControl,
384
+ Controls,
385
+ }) as any;
386
+
387
+ export default InlineInput;
package/src/index.d.ts ADDED
@@ -0,0 +1,199 @@
1
+ import React from 'react';
2
+ import { PropGetterFn } from '@semcore/core';
3
+ import { ITooltipProps } from '@semcore/tooltip';
4
+ import { IBoxProps } from '@semcore/flex-box';
5
+
6
+ /* utils type */
7
+ type CProps<Props, Ctx = {}, UCProps = {}> = Props & {
8
+ children?: ((props: Props & Ctx, handlers: UCProps) => React.ReactElement) | React.ReactElement;
9
+ };
10
+ /* utils type */
11
+
12
+ export interface IInlineInputProps extends IBoxProps {
13
+ /**
14
+ * Visual state of inline input
15
+ * @default normal
16
+ */
17
+ state?: 'normal' | 'valid' | 'invalid' | 'disabled';
18
+ /**
19
+ * Disabled inputs and shows spinner instead of confirm control
20
+ * @default false
21
+ */
22
+ loading?: boolean;
23
+ /**
24
+ * Fired with entered value when user clicks confirm control or hits `Enter` or `Space`
25
+ */
26
+ onConfirm?: (value: string, event: React.MouseEvent | React.KeyboardEvent) => void;
27
+ /**
28
+ * Fired with value (or defaultValue) that was provided during component mount when user clicks cancel control or hits `Escape`
29
+ */
30
+ onCancel?: (prevValue: string) => void;
31
+ /**
32
+ * Text value of input. Should be used with `onChange` property together
33
+ */
34
+ value?: string;
35
+ /**
36
+ * Initial text value of input
37
+ */
38
+ defaultValue?: string;
39
+ /**
40
+ * Tooltip and aria-label text of the confirm control
41
+ * @default Confirm
42
+ */
43
+ confirmText?: string;
44
+ /**
45
+ * Tooltip and aria-label text of the cancel control
46
+ * @default Cancel
47
+ */
48
+ cancelText?: string;
49
+ /**
50
+ * Properties of control's tooltips
51
+ */
52
+ tooltipsProps?: ITooltipProps;
53
+ /**
54
+ * Makes component to catch browser focus on component mount
55
+ * @default false
56
+ */
57
+ autoFocus?: boolean;
58
+ /**
59
+ * Gray text displayed in empty input
60
+ */
61
+ placeholder?: string;
62
+ /**
63
+ * `id` attribute of `<input />` dom tag. Use it to attach `<label />` via `htmlFor` attribute to `<input />` tag
64
+ */
65
+ inputId?: string;
66
+ /**
67
+ * Fired on every input value change. Should be used with `value` property together
68
+ */
69
+ onChange?: (value: string, event: React.ChangeEvent) => void;
70
+ /**
71
+ * Fired when browser focus leaves component.
72
+ */
73
+ onBlur?: (event: React.FocusEvent) => void;
74
+ /**
75
+ * Fired when browser focus enters component.
76
+ */
77
+ onFocus?: (event: React.FocusEvent) => void;
78
+ /**
79
+ * @description defines callback (`onCancel` or `onConfirm`) triggered when `blur` event out of container fired
80
+ * Triggered after all previous macrotasks completed (internally called inside of `setTimeout`)
81
+ */
82
+ onBlurBehavior?: 'cancel' | 'confirm';
83
+ }
84
+
85
+ export interface IInlineInputAddonProps extends IBoxProps {
86
+ size?: 's' | 'm' | 'l' | 'xl';
87
+ }
88
+ export interface IInlineInputUnderlineProps extends IBoxProps {}
89
+ export interface IInlineInputValueProps extends IBoxProps {
90
+ id?: string;
91
+ size?: 's' | 'm' | 'l' | 'xl';
92
+ autoFocus?: boolean;
93
+ value?: string;
94
+ defaultValue?: string;
95
+ onChange?: (value: string, event: React.ChangeEvent) => void;
96
+ state?: 'normal' | 'valid' | 'invalid' | 'disabled';
97
+ loading?: boolean;
98
+ placeholder?: string;
99
+ }
100
+ export interface IInlineInputConfirmIconProps extends IBoxProps {
101
+ onConfirm?: (value: string) => void;
102
+ /**
103
+ * @default "Confirm"
104
+ */
105
+ confirmText?: string;
106
+ tooltipsProps?: ITooltipProps;
107
+ }
108
+ export interface IInlineInputCancelIconProps extends IBoxProps {
109
+ onCancel?: (prevValue: string) => void;
110
+ /**
111
+ * @default "Cancel"
112
+ */
113
+ cancelText?: string;
114
+ tooltipsProps?: ITooltipProps;
115
+ }
116
+ export interface IInlineInputControlIconsProps extends IBoxProps {
117
+ onConfirm?: (value: string) => void;
118
+ onCancel?: (prevValue: string) => void;
119
+ /**
120
+ * @default "Confirm"
121
+ */
122
+ confirmText?: string;
123
+ /**
124
+ * @default "Cancel"
125
+ */
126
+ cancelText?: string;
127
+ tooltipsProps?: ITooltipProps;
128
+ }
129
+ export interface IInlineInputConfirmButtonProps extends IBoxProps {
130
+ size?: 's' | 'm' | 'l' | 'xl';
131
+
132
+ onConfirm?: (value: string) => void;
133
+ /**
134
+ * @default "Confirm"
135
+ */
136
+ confirmText?: string;
137
+ }
138
+ export interface IInlineInputCancelButtonProps extends IBoxProps {
139
+ size?: 's' | 'm' | 'l' | 'xl';
140
+
141
+ onCancel?: (prevValue: string) => void;
142
+ /**
143
+ * @default "Confirm"
144
+ */
145
+ cancelText?: string;
146
+ }
147
+ export interface IInlineInputControlButtonsProps extends IBoxProps {
148
+ size?: 's' | 'm' | 'l' | 'xl';
149
+
150
+ onConfirm?: (value: string) => void;
151
+ onCancel?: (prevValue: string) => void;
152
+ /**
153
+ * @default "Confirm"
154
+ */
155
+ confirmText?: string;
156
+ /**
157
+ * @default "Cancel"
158
+ */
159
+ cancelText?: string;
160
+ }
161
+
162
+ interface IInlineInputCtx {
163
+ geAddonProps: PropGetterFn;
164
+ getConfirmIconProps: PropGetterFn;
165
+ getCancelIconProps: PropGetterFn;
166
+ getControlIconsProps: PropGetterFn;
167
+ getConfirmButtonProps: PropGetterFn;
168
+ getCancelButtonProps: PropGetterFn;
169
+ getControlButtonsProps: PropGetterFn;
170
+ }
171
+
172
+ declare const InlineInput: (<T>(
173
+ props: CProps<IInlineInputProps & T, IInlineInputCtx>,
174
+ ) => React.ReactElement) & {
175
+ Addon: <T>(props: CProps<IInlineInputAddonProps & T, IInlineInputProps>) => React.ReactElement;
176
+ Underline: <T>(
177
+ props: CProps<IInlineInputUnderlineProps & T, IInlineInputProps>,
178
+ ) => React.ReactElement;
179
+ Value: <T>(props: CProps<IInlineInputValueProps & T, IInlineInputProps>) => React.ReactElement;
180
+ ConfirmIcon: <T>(
181
+ props: CProps<IInlineInputConfirmIconProps & T, IInlineInputProps>,
182
+ ) => React.ReactElement;
183
+ CancelIcon: <T>(
184
+ props: CProps<IInlineInputCancelIconProps & T, IInlineInputProps>,
185
+ ) => React.ReactElement;
186
+ ControlIcons: <T>(
187
+ props: CProps<IInlineInputControlIconsProps & T, IInlineInputProps>,
188
+ ) => React.ReactElement;
189
+ ConfirmButton: <T>(
190
+ props: CProps<IInlineInputConfirmButtonProps & T, IInlineInputProps>,
191
+ ) => React.ReactElement;
192
+ CancelButton: <T>(
193
+ props: CProps<IInlineInputCancelButtonProps & T, IInlineInputProps>,
194
+ ) => React.ReactElement;
195
+ ControlButtons: <T>(
196
+ props: CProps<IInlineInputControlButtonsProps & T, IInlineInputProps>,
197
+ ) => React.ReactElement;
198
+ };
199
+ export default InlineInput;
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ import InlineInputBase from './InlineInput';
2
+ export default InlineInputBase;