Debian Codegraph @

node/@patternfly/react-core/dist/esm/components/TextInput/TextInput.js

raw
1 import { __rest } from "tslib";
2 import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3 import { createRef, Component, forwardRef } from 'react';
4 import styles from '@patternfly/react-styles/css/components/FormControl/form-control.mjs';
5 import { css } from '@patternfly/react-styles';
6 import { ValidatedOptions } from '../../helpers/constants';
7 import { trimLeft } from '../../helpers/util';
8 import { getOUIAProps } from '../../helpers';
9 import { SSRSafeIds } from '../../helpers/SSRSafeIds/SSRSafeIds';
10 import { getResizeObserver } from '../../helpers/resizeObserver';
11 import { FormControlIcon } from '../FormControl/FormControlIcon';
12 export var TextInputTypes;
13 (function (TextInputTypes) {
14 TextInputTypes["text"] = "text";
15 TextInputTypes["date"] = "date";
16 TextInputTypes["datetimeLocal"] = "datetime-local";
17 TextInputTypes["email"] = "email";
18 TextInputTypes["month"] = "month";
19 TextInputTypes["number"] = "number";
20 TextInputTypes["password"] = "password";
21 TextInputTypes["search"] = "search";
22 TextInputTypes["tel"] = "tel";
23 TextInputTypes["time"] = "time";
24 TextInputTypes["url"] = "url";
25 })(TextInputTypes || (TextInputTypes = {}));
26 export var TextInputReadOnlyVariant;
27 (function (TextInputReadOnlyVariant) {
28 TextInputReadOnlyVariant["default"] = "default";
29 TextInputReadOnlyVariant["plain"] = "plain";
30 })(TextInputReadOnlyVariant || (TextInputReadOnlyVariant = {}));
31 export class TextInputBase extends Component {
32 constructor(props) {
33 super(props);
34 this.inputRef = createRef();
35 this.observer = () => { };
36 this.handleChange = (event) => {
37 if (this.props.onChange) {
38 this.props.onChange(event, event.currentTarget.value);
39 }
40 };
41 this.handleResize = () => {
42 const inputRef = this.props.innerRef || this.inputRef;
43 if (inputRef && inputRef.current) {
44 trimLeft(inputRef.current, String(this.props.value));
45 }
46 };
47 this.restoreText = () => {
48 const inputRef = this.props.innerRef || this.inputRef;
49 // restore the value
50 inputRef.current.value = String(this.props.value);
51 // make sure we still see the rightmost value to preserve cursor click position
52 inputRef.current.scrollLeft = inputRef.current.scrollWidth;
53 };
54 this.onFocus = (event) => {
55 const { isLeftTruncated, isStartTruncated, onFocus } = this.props;
56 if (isLeftTruncated || isStartTruncated) {
57 this.restoreText();
58 }
59 onFocus && onFocus(event);
60 };
61 this.onBlur = (event) => {
62 const { isLeftTruncated, isStartTruncated, onBlur } = this.props;
63 if (isLeftTruncated || isStartTruncated) {
64 this.handleResize();
65 }
66 onBlur && onBlur(event);
67 };
68 this.sanitizeInputValue = (value) => typeof value === 'string' ? value.replace(/\n/g, ' ') : value;
69 if (!props.id && !props['aria-label'] && !props['aria-labelledby']) {
70 // eslint-disable-next-line no-console
71 console.error('Text input:', 'Text input requires either an id or aria-label to be specified');
72 }
73 }
74 componentDidMount() {
75 if (this.props.isLeftTruncated || this.props.isStartTruncated) {
76 const inputRef = this.props.innerRef || this.inputRef;
77 this.observer = getResizeObserver(inputRef.current, this.handleResize, true);
78 this.handleResize();
79 }
80 }
81 componentWillUnmount() {
82 if (this.props.isLeftTruncated || this.props.isStartTruncated) {
83 this.observer();
84 }
85 }
86 render() {
87 const _a = this.props, { innerRef, className, type, value, placeholder, validated,
88 /* eslint-disable @typescript-eslint/no-unused-vars */
89 onChange, onFocus, onBlur, isLeftTruncated, isStartTruncated, isExpanded, expandedProps, readOnly, readOnlyVariant, isRequired, isDisabled, customIcon, ouiaId, ouiaSafe } = _a, props = __rest(_a, ["innerRef", "className", "type", "value", "placeholder", "validated", "onChange", "onFocus", "onBlur", "isLeftTruncated", "isStartTruncated", "isExpanded", "expandedProps", "readOnly", "readOnlyVariant", "isRequired", "isDisabled", "customIcon", "ouiaId", "ouiaSafe"]);
90 const hasStatusIcon = ['success', 'error', 'warning'].includes(validated);
91 const ariaExpandedProps = expandedProps
92 ? { 'aria-expanded': expandedProps === null || expandedProps === void 0 ? void 0 : expandedProps.isExpanded, 'aria-controls': expandedProps === null || expandedProps === void 0 ? void 0 : expandedProps.ariaControls, role: 'combobox' }
93 : {};
94 return (_jsx(SSRSafeIds, { prefix: "pf-", ouiaComponentType: TextInputBase.displayName, children: (_, generatedOuiaId) => (_jsxs("span", { className: css(styles.formControl, readOnlyVariant && styles.modifiers.readonly, readOnlyVariant === 'plain' && styles.modifiers.plain, isDisabled && styles.modifiers.disabled, (isExpanded || (expandedProps === null || expandedProps === void 0 ? void 0 : expandedProps.isExpanded)) && styles.modifiers.expanded, customIcon && styles.modifiers.icon, hasStatusIcon && styles.modifiers[validated], className), children: [_jsx("input", Object.assign({}, props, { onFocus: this.onFocus, onBlur: this.onBlur, onChange: this.handleChange, type: type, value: this.sanitizeInputValue(value), "aria-invalid": props['aria-invalid'] ? props['aria-invalid'] : validated === ValidatedOptions.error }, ariaExpandedProps, { required: isRequired, disabled: isDisabled, readOnly: !!readOnlyVariant || readOnly, ref: innerRef || this.inputRef, placeholder: placeholder }, getOUIAProps(TextInput.displayName, ouiaId !== undefined ? ouiaId : generatedOuiaId, ouiaSafe))), (customIcon || hasStatusIcon) && (_jsxs("span", { className: css(styles.formControlUtilities), children: [customIcon && _jsx(FormControlIcon, { customIcon: customIcon }), hasStatusIcon && _jsx(FormControlIcon, { status: validated })] }))] })) }));
95 }
96 }
97 TextInputBase.displayName = 'TextInputBase';
98 TextInputBase.defaultProps = {
99 'aria-label': null,
100 isRequired: false,
101 validated: 'default',
102 isDisabled: false,
103 isExpanded: false,
104 type: TextInputTypes.text,
105 isLeftTruncated: false,
106 isStartTruncated: false,
107 onChange: () => undefined,
108 ouiaSafe: true
109 };
110 export const TextInput = forwardRef((props, ref) => (_jsx(TextInputBase, Object.assign({}, props, { innerRef: ref }))));
111 TextInput.displayName = 'TextInput';
112 //# sourceMappingURL=TextInput.js.map