Debian Codegraph @

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

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