Repository navigation
Expand file tree
/
Copy pathTextInput.ts
More file actions
211 lines (178 loc) · 7.49 KB
/
Copy pathTextInput.ts
File metadata and controls
211 lines (178 loc) · 7.49 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
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* This file belongs to Hoist, an application development toolkit
* developed by Extremely Heavy Industries (www.xh.io | info@xh.io)
*
* Copyright © 2026 Extremely Heavy Industries Inc.
*/
import type {HoistInputProps, PasswordManagerSupportProps} from '@xh/hoist/cmp/input';
import {getPasswordManagerAttrs, HoistInputModel, useHoistInputModel} from '@xh/hoist/cmp/input';
import {div} from '@xh/hoist/cmp/layout';
import type {HoistProps, LayoutProps, StyleProps} from '@xh/hoist/core';
import {hoistCmp} from '@xh/hoist/core';
import {button} from '@xh/hoist/desktop/cmp/button';
import '@xh/hoist/desktop/register';
import {Icon} from '@xh/hoist/icon';
import {inputGroup} from '@xh/hoist/kit/blueprint';
import {getTestId, TEST_ID, withDefault} from '@xh/hoist/utils/js';
import {useComposedRefs, getLayoutProps} from '@xh/hoist/utils/react';
import type {Property} from 'csstype';
import {isEmpty} from 'lodash';
import type {FocusEvent, KeyboardEventHandler, ReactElement, ReactNode, Ref} from 'react';
export interface TextInputProps
extends HoistProps, HoistInputProps, PasswordManagerSupportProps, LayoutProps, StyleProps {
value?: string;
/**
* HTML `autocomplete` attribute to set on underlying <input> element.
*
* Defaults to 'off' for fields of type text and 'new-password' for fields of type 'password'
* to defeat browser auto-completion, which is typically not desired in Hoist applications.
* Set to 'on' or a more specific autocomplete token to enable.
*
* @see https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofilling-form-controls%3A-the-autocomplete-attribute
* @see https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion
*/
autoComplete?: string;
/** True to focus the control on render. */
autoFocus?: boolean;
/** True to commit on every change/keystroke, default false. */
commitOnChange?: boolean;
/** True to show a "clear" button as the right element. default false */
enableClear?: boolean;
/** Ref handler that receives HTML <input> element backing this component. */
inputRef?: Ref<HTMLInputElement>;
/**
* Element to display inline on the left side of the input. Unlike `leftIcon`, the input's
* padding tracks its width, so it can hold wider content. Use one or the other, not both.
*/
leftElement?: ReactNode;
/** Icon to display inline on the left side of the input. */
leftIcon?: ReactElement;
/** Callback for normalized keydown event. */
onKeyDown?: (e: KeyboardEvent) => void;
/** Text to display when control is empty. */
placeholder?: string;
/** Element to display inline on the right side of the input. */
rightElement?: ReactNode;
/** True to display with rounded caps. */
round?: boolean;
/** True to select contents when control receives focus. */
selectOnFocus?: boolean;
/** Alignment of entry text within control, default 'left'. */
textAlign?: Property.TextAlign;
/** True to allow browser spell check, default false. */
spellCheck?: boolean;
/**
* True to trim leading/trailing whitespace from this input's value as committed to any bound
* model and reported to `onChange` / `onCommit`. Default true, except for `password` type
* inputs, where such whitespace can be intentional. A value that trims away to nothing commits
* null, as per an input the user cleared.
*/
trimWhitespace?: boolean;
/** Underlying HTML <input> element type. */
type?: 'text' | 'password';
}
/**
* A single-line text input with additional support for embedded icons/elements.
*/
export const [TextInput, textInput] = hoistCmp.withFactory<TextInputProps>({
displayName: 'TextInput',
className: 'xh-text-input',
render(props, ref) {
return useHoistInputModel(cmp, props, ref, TextInputModel);
}
});
(TextInput as any).hasLayoutSupport = true;
//-----------------------
// Implementation
//-----------------------
export class TextInputModel extends HoistInputModel {
override xhImpl = true;
override get commitOnChange() {
return withDefault(this.componentProps.commitOnChange, false);
}
override get trimWhitespace() {
const {trimWhitespace, type} = this.componentProps;
// Passwords can legitimately carry leading/trailing whitespace - never trim by default.
return withDefault(trimWhitespace, type !== 'password');
}
override toExternal(internal: string): string {
// Normalize a value that trims away to nothing to null, as per an input the user cleared.
return super.toExternal(internal) || null;
}
onChange = ev => {
let {value} = ev.target;
if (value === '') value = null;
this.noteValueChange(value);
};
onKeyDown: KeyboardEventHandler = ev => {
if (ev.key === 'Enter') this.doCommit();
this.componentProps.onKeyDown?.(ev);
};
override onFocus = (ev: FocusEvent) => {
const target = ev.target as HTMLElement;
if (this.componentProps.selectOnFocus && target.nodeName === 'INPUT') {
(target as HTMLInputElement).select();
}
this.noteFocused();
};
}
const cmp = hoistCmp.factory<TextInputProps & {model: TextInputModel}>(
({model, className, ...props}, ref) => {
const {width, flex, ...layoutProps} = getLayoutProps(props);
const isClearable = !isEmpty(model.internalValue);
return div({
children: inputGroup({
value: model.renderValue || '',
autoComplete: withDefault(
props.autoComplete,
props.type === 'password' ? 'new-password' : 'off'
),
autoFocus: props.autoFocus,
disabled: props.disabled,
inputRef: useComposedRefs(model.inputRef as Ref<HTMLInputElement>, props.inputRef),
leftElement: props.leftElement as ReactElement,
leftIcon: props.leftIcon,
placeholder: props.placeholder,
rightElement:
(props.rightElement as ReactElement) ||
(props.enableClear && !props.disabled && isClearable ? clearButton() : null),
round: withDefault(props.round, false),
spellCheck: withDefault(props.spellCheck, false),
tabIndex: props.tabIndex,
type: props.type,
id: props.id,
style: {
...props.style,
...layoutProps,
textAlign: withDefault(props.textAlign, 'left')
},
[TEST_ID]: props.testId,
...getPasswordManagerAttrs(props.enablePasswordManagers),
...props.domAttrs,
onChange: model.onChange,
onKeyDown: model.onKeyDown
}),
className,
style: {
width: withDefault(width, 200),
flex: withDefault(flex, null)
},
onBlur: model.onBlur,
onFocus: model.onFocus,
ref
});
}
);
const clearButton = hoistCmp.factory<TextInputModel>(({model}) =>
button({
icon: Icon.cross(),
tabIndex: -1,
minimal: true,
testId: getTestId(model.componentProps, 'clear-btn'),
onClick: () => {
model.noteValueChange(null);
model.doCommit();
model.focus();
}
})
);