UNPKG

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