@semcore/inline-input 1.0.0 → 2.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.
@@ -9,37 +9,36 @@ import CloseM from '@semcore/icon/Close/m';
9
9
  import Spin from '@semcore/spin';
10
10
 
11
11
  const isFocusOutsideOf = (element: HTMLElement) => {
12
- let traversed = document.activeElement;
12
+ let traversed: Element | null | undefined = document.activeElement;
13
13
  for (let i = 0; i < 1000000; i++) {
14
14
  if (traversed === element) return false;
15
15
  if (traversed === document.body) return true;
16
- traversed = traversed.parentElement;
16
+ traversed = traversed?.parentElement;
17
17
  }
18
18
 
19
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.',
20
+ 'Failed to traverse parents chain in utilite `isFocusOutsideOf` in 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
21
  );
22
22
  };
23
23
 
24
- type AsProps = {
25
- state?: 'normal' | 'valid' | 'invalid' | 'disabled';
24
+ type OnConfirm = (
25
+ value: string,
26
+ event: React.MouseEvent | React.FocusEvent | React.KeyboardEvent,
27
+ ) => void;
28
+ type OnCancel = (
29
+ prevValue: string,
30
+ event: React.MouseEvent | React.FocusEvent | React.KeyboardEvent,
31
+ ) => void;
32
+ type RootAsProps = {
33
+ state?: 'normal' | 'valid' | 'invalid';
26
34
  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
+ disabled?: boolean;
36
+ onConfirm?: OnConfirm;
37
+ onCancel?: OnCancel;
35
38
  value?: string;
36
39
  defaultValue?: string;
37
- confirmText?: string;
38
- cancelText?: string;
39
- tooltipsProps?: ITooltipProps;
40
40
  autoFocus?: boolean;
41
41
  placeholder?: string;
42
- inputId?: string;
43
42
  onChange?: (value: string, event: React.ChangeEvent) => void;
44
43
  onBlur?: (event: React.FocusEvent) => void;
45
44
  onFocus?: (event: React.FocusEvent) => void;
@@ -48,22 +47,35 @@ type AsProps = {
48
47
  Children: React.FC;
49
48
  };
50
49
 
51
- class InlineInputBase extends Component<AsProps> {
50
+ type AddonAsProps = {
51
+ styles?: React.CSSProperties;
52
+ Children: React.FC;
53
+ };
54
+
55
+ type ControlAsProps = {
56
+ Children: React.FC;
57
+ styles?: React.CSSProperties;
58
+ title?: string;
59
+ $tooltipsProps?: ITooltipProps;
60
+ loading?: boolean;
61
+ disabled?: boolean;
62
+ onCancel?: OnCancel;
63
+ value?: string;
64
+ icon?: React.FC;
65
+ };
66
+ type ConfirmControlAsProps = ControlAsProps & {
67
+ onConfirm?: OnConfirm;
68
+ };
69
+ type CancelControlAsProps = ControlAsProps & {
70
+ onCancel?: OnCancel;
71
+ };
72
+
73
+ class InlineInputBase extends Component<RootAsProps> {
52
74
  static displayName = 'InlineInput';
53
75
 
54
76
  static defaultProps = {
55
77
  state: 'normal',
56
- loading: false,
57
- defaultValue: '',
58
- __excludeProps: [
59
- 'defaultValue',
60
- 'placeholder',
61
- 'tooltipsProps',
62
- 'confirmText',
63
- 'cancelText',
64
- 'autoFocus',
65
- 'inputId',
66
- ],
78
+ onBlurBehavior: 'confirm',
67
79
  };
68
80
  static style = style;
69
81
 
@@ -72,138 +84,97 @@ class InlineInputBase extends Component<AsProps> {
72
84
  };
73
85
 
74
86
  rootRef = React.createRef<HTMLElement>();
75
- inputRef = React.createRef();
87
+ inputRef = React.createRef<HTMLInputElement>();
76
88
  initValue: string = '';
77
89
 
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
90
  componentDidMount() {
94
- this.initValue = ((this.asProps.value ?? this.asProps.defaultValue) as string) || '';
91
+ this.initValue = this.inputRef.current?.value || '';
95
92
  }
96
93
 
97
- getContextProps() {
98
- const {
99
- loading,
100
- value,
101
- defaultValue,
102
- state,
103
- confirmText = 'Confirm',
104
- cancelText = 'Cancel',
105
- tooltipsProps = {},
106
- } = this.asProps;
94
+ getAddonProps() {
95
+ return {
96
+ onMouseDown: this.handleMouseDownAddon,
97
+ };
98
+ }
107
99
 
100
+ getConfirmControlProps() {
101
+ const { loading } = this.asProps;
108
102
  return {
103
+ value: this.inputRef.current?.value,
109
104
  loading,
110
105
  onConfirm: this.handleConfirm,
111
- onCancel: this.handleCancel,
112
- value,
113
- defaultValue,
114
- state,
115
- confirmText,
116
- cancelText,
117
- tooltipsProps,
118
106
  };
119
107
  }
120
108
 
121
- geAddonProps() {
122
- return this.getContextProps();
123
- }
124
- getConfirmControlProps() {
125
- return this.getContextProps();
126
- }
127
109
  getCancelControlProps() {
128
- return this.getContextProps();
129
- }
130
- getControlsProps() {
131
- return this.getContextProps();
110
+ const { loading, disabled } = this.asProps;
111
+ return {
112
+ value: this.initValue,
113
+ // because double disabled(root disabled and addon disabled)
114
+ disabled: loading && !disabled,
115
+ onCancel: this.handleCancel,
116
+ };
132
117
  }
133
118
 
134
119
  getValueProps() {
135
- const { autoFocus, value, inputId, defaultValue, onChange, state, loading, placeholder } =
136
- this.asProps;
137
-
120
+ const { state } = this.asProps;
138
121
  return {
139
122
  ref: this.inputRef,
140
- autoFocus,
141
- value,
142
- defaultValue,
143
- onChange,
144
123
  state,
145
- loading,
146
- placeholder,
147
- id: inputId,
124
+ onKeyDown: this.handleKeyDown,
125
+ onFocus: this.bindHandlerValueFocused(true),
126
+ onBlur: this.bindHandlerValueFocused(false),
148
127
  };
149
128
  }
150
129
 
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
- }
130
+ bindHandlerValueFocused = (focused: boolean) => () => {
131
+ this.setState({ focused });
132
+ };
161
133
 
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 });
134
+ handleMouseDownAddon = (event: React.MouseEvent) => {
135
+ event.preventDefault();
136
+ this.inputRef.current?.focus();
137
+ };
138
+
139
+ handleConfirm = (
140
+ text: string,
141
+ event: React.MouseEvent | React.FocusEvent | React.KeyboardEvent,
142
+ ) => {
143
+ this.asProps.onConfirm?.(text, event);
144
+ };
145
+ handleCancel = (
146
+ prevText: string,
147
+ event: React.MouseEvent | React.FocusEvent | React.KeyboardEvent,
148
+ ) => {
149
+ this.asProps.onCancel?.(prevText, event);
150
+ };
151
+
152
+ handleBlur = (event: React.FocusEvent) => {
153
+ const { onConfirm, onCancel, onBlurBehavior } = this.asProps;
172
154
  if (onBlurBehavior) {
173
155
  setTimeout(() => {
174
- if (isFocusOutsideOf(this.rootRef.current)) {
175
- if (onBlurBehavior === 'confirm') onConfirm?.(value, event);
156
+ if (this.rootRef.current && isFocusOutsideOf(this.rootRef.current)) {
157
+ if (onBlurBehavior === 'confirm') onConfirm?.(this.inputRef.current?.value ?? '', event);
176
158
  if (onBlurBehavior === 'cancel') onCancel?.(this.initValue, event);
177
159
  }
178
160
  }, 0);
179
161
  }
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);
162
+ };
163
+
164
+ handleKeyDown = (event: React.KeyboardEvent) => {
165
+ const { onConfirm, onCancel } = this.asProps;
166
+ if (event.code === 'Enter') onConfirm?.(this.inputRef.current?.value ?? '', event);
187
167
  if (event.code === 'Escape') onCancel?.(this.initValue, event);
188
- }
168
+ };
189
169
 
190
170
  render() {
191
171
  const SInlineInput = Root;
192
172
  const SUnderline = 'div';
193
- const { Children: providedChildren, children: hasChildren, styles, loading } = this.asProps;
194
- const Children = hasChildren ? providedChildren : DefaultChildren;
173
+ const { Children, styles } = this.asProps;
195
174
  const { focused } = this.state;
196
175
 
197
176
  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
- >
177
+ <SInlineInput render={Box} ref={this.rootRef} focused={focused} onBlur={this.handleBlur}>
207
178
  <SUnderline>
208
179
  <Children />
209
180
  </SUnderline>
@@ -212,167 +183,132 @@ class InlineInputBase extends Component<AsProps> {
212
183
  }
213
184
  }
214
185
 
215
- class Value extends Component<AsProps> {
186
+ class Value extends Component<RootAsProps> {
216
187
  static defaultProps = {
217
188
  defaultValue: '',
218
189
  };
219
190
  static enhance = [autoFocusEnhance()];
191
+ static hoistProps = ['disabled'];
220
192
 
221
193
  uncontrolledProps() {
222
194
  return {
223
- value: (event) => {
224
- event.stopPropagation();
225
- return event.target.value;
226
- },
195
+ value: (event: React.ChangeEvent<HTMLInputElement>) => event.target.value,
227
196
  };
228
197
  }
229
198
 
230
199
  render() {
231
200
  const SValue = Root;
232
201
 
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
- );
202
+ return sstyled(this.asProps.styles)(<SValue render={Box} tag="input" type="text" />);
241
203
  }
242
204
  }
243
205
 
244
- const Addon: React.FC<AsProps> = (props) => {
206
+ const Addon: React.FC<AddonAsProps> = (props) => {
245
207
  const SAddon = Root;
246
208
  return sstyled(props.styles)(<SAddon render={Box} />) as React.ReactElement;
247
209
  };
248
210
 
249
- const ConfirmControl: React.FC<AsProps> = (props) => {
250
- const SConfirmControl = Root;
251
- const { Children, children: hasChildren } = props;
211
+ const ConfirmControl: React.FC<ConfirmControlAsProps> = (props) => {
212
+ const SAddon = Root;
213
+ const { Children, children: hasChildren, title } = props;
252
214
 
253
215
  const handleConfirm = React.useCallback(
254
216
  (event: React.MouseEvent | React.KeyboardEvent) => {
255
- props.onConfirm?.(props.value, event);
217
+ props.onConfirm?.(props.value ?? '', event);
256
218
  },
257
219
  [props.onConfirm, props.value],
258
220
  );
221
+
259
222
  const handleKeydown = React.useCallback(
260
223
  (event: React.KeyboardEvent) => {
261
- if (props.loading) return;
262
224
  if (event.code === 'Enter' || event.code === 'Space') {
263
225
  event.preventDefault();
264
226
  event.stopPropagation();
265
227
  handleConfirm(event);
266
228
  }
267
229
  },
268
- [props.loading, handleConfirm],
230
+ [handleConfirm],
269
231
  );
270
232
 
271
233
  if (props.loading) {
272
234
  return sstyled(props.styles)(
273
- <SConfirmControl render={Box} className="controls-icon">
274
- {hasChildren ? <Children /> : <Spin size="xxs" className="spin" />}
275
- </SConfirmControl>,
235
+ <SAddon render={Box}>{hasChildren ? <Children /> : <Spin size="xxs" />}</SAddon>,
276
236
  ) as React.ReactElement;
277
237
  }
278
238
 
279
239
  return sstyled(props.styles)(
280
- <SConfirmControl render={Box} onKeyDown={handleKeydown}>
240
+ <SAddon render={Box} onKeyDown={handleKeydown}>
281
241
  {hasChildren ? (
282
242
  <Children />
283
243
  ) : (
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>
244
+ <Tooltip {...props.$tooltipsProps}>
245
+ <Tooltip.Trigger
246
+ tag={props.icon ?? CheckM}
247
+ aria-label={`${title} ${props.value ?? ''}`}
248
+ role="button"
249
+ interactive
250
+ color="green-300"
251
+ onClick={handleConfirm}
252
+ />
253
+ <Tooltip.Popper p={3}>{props.title}</Tooltip.Popper>
294
254
  </Tooltip>
295
255
  )}
296
- </SConfirmControl>,
256
+ </SAddon>,
297
257
  ) as React.ReactElement;
298
258
  };
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);
259
+ ConfirmControl.defaultProps = {
260
+ title: 'Confirm',
261
+ };
262
+ const CancelControl: React.FC<CancelControlAsProps> = (props) => {
263
+ const SAddon = Root;
264
+ const { Children, children: hasChildren, title } = props;
265
+
303
266
  const handleCancel = React.useCallback(
304
267
  (event: React.MouseEvent | React.KeyboardEvent) => {
305
- if (props.loading || !props.onCancel) return;
306
- props.onCancel?.(initValue.current, event);
268
+ props.onCancel?.(props.value ?? '', event);
307
269
  },
308
- [props.loading, props.onCancel],
270
+ [props.onCancel, props.value],
309
271
  );
272
+
310
273
  const handleKeydown = React.useCallback(
311
274
  (event: React.KeyboardEvent) => {
312
- if (props.loading) return;
313
275
  if (event.code === 'Enter' || event.code === 'Space') {
314
276
  event.preventDefault();
315
277
  event.stopPropagation();
316
278
  handleCancel(event);
317
279
  }
318
280
  },
319
- [props.loading, handleCancel],
281
+ [handleCancel],
320
282
  );
321
283
 
322
- if (props.loading) {
284
+ if (props.disabled) {
323
285
  return sstyled(props.styles)(
324
- <SCancelControl render={Box} className="controls-icon">
325
- {hasChildren ? <Children /> : <CloseM disabled={true} />}
326
- </SCancelControl>,
286
+ <SAddon render={Box}>{hasChildren ? <Children /> : <CloseM disabled />}</SAddon>,
327
287
  ) as React.ReactElement;
328
288
  }
329
289
 
330
290
  return sstyled(props.styles)(
331
- <SCancelControl render={Box} onKeyDown={handleKeydown}>
291
+ <SAddon render={Box} onKeyDown={handleKeydown}>
332
292
  {hasChildren ? (
333
293
  <Children />
334
294
  ) : (
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>
295
+ <Tooltip {...props.$tooltipsProps}>
296
+ <Tooltip.Trigger
297
+ tag={props.icon ?? CloseM}
298
+ aria-label={title}
299
+ role="button"
300
+ interactive
301
+ color="gray-300"
302
+ onClick={handleCancel}
303
+ />
304
+ <Tooltip.Popper p={3}>{title}</Tooltip.Popper>
345
305
  </Tooltip>
346
306
  )}
347
- </SCancelControl>,
307
+ </SAddon>,
348
308
  ) as React.ReactElement;
349
309
  };
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;
310
+ CancelControl.defaultProps = {
311
+ title: 'Cancel',
376
312
  };
377
313
 
378
314
  /** `createComponent` currently exposes unrelated junk instead of typings, that the reason of to any cast */
@@ -381,7 +317,6 @@ const InlineInput = createComponent(InlineInputBase, {
381
317
  Value,
382
318
  ConfirmControl,
383
319
  CancelControl,
384
- Controls,
385
320
  }) as any;
386
321
 
387
322
  export default InlineInput;