/* Credits: https://github.com/Katello/katello/blob/631d5bb83dc5d87320ee9002a6de33809a281b3e/webpack/components/EditableTextInput/EditableTextInput.js */ import React, { useState, useEffect } from 'react'; import { TextInput, TextArea, Text, Button, Split, SplitItem, } from '@patternfly/react-core'; import { EyeIcon, EyeSlashIcon, TimesIcon, CheckIcon, } from '@patternfly/react-icons'; import { translate as __ } from 'foremanReact/common/I18n'; import PropTypes from 'prop-types'; import './editableTextInput.scss'; import PencilEditButton from './PencilEditButton'; const PASSWORD_MASK = '••••••••'; const EditableTextInput = ({ onEdit, value, textArea, attribute, placeholder, isPassword, hasPassword, component, currentAttribute, setCurrentAttribute, disabled, ouiaId, valid, }) => { const [inputValue, setInputValue] = useState(value); const [editing, setEditing] = useState(false); const [passwordPlaceholder, setPasswordPlaceholder] = useState( hasPassword ? PASSWORD_MASK : null ); const [showPassword, setShowPassword] = useState(false); useEffect(() => { if (setCurrentAttribute && currentAttribute) { if (attribute !== currentAttribute) { setEditing(false); } } }, [attribute, currentAttribute, setCurrentAttribute]); const onEditClick = () => { setEditing(true); if (isPassword) setPasswordPlaceholder(null); if (setCurrentAttribute && attribute !== currentAttribute) setCurrentAttribute(attribute); }; const onSubmit = async () => { setEditing(false); if (isPassword) { if (inputValue?.length > 0) { setPasswordPlaceholder(PASSWORD_MASK); } } await onEdit(inputValue, attribute); }; const onClear = () => { if (isPassword) { if (hasPassword || inputValue?.length > 0) { setPasswordPlaceholder(PASSWORD_MASK); } } setInputValue(value); setEditing(false); }; const toggleShowPassword = () => { setShowPassword(prevShowPassword => !prevShowPassword); }; const onKeyUp = ({ key, charCode }) => (key === 'Enter' || charCode === '13') && onSubmit(); const inputProps = { onKeyUp, component, value: inputValue || '', onChange: setInputValue, validated: valid, }; return editing ? ( {textArea ? (