Files
grafana/public/app/features/serviceaccounts/components/ServiceAccountProfileRow.tsx
Ashley Harrison 47f8717149 React: Use new JSX transform (#88802)
* update eslint, tsconfig + esbuild to handle new jsx transform

* remove thing that breaks the new jsx transform

* remove react imports

* adjust grafana-icons build

* is this the correct syntax?

* try this

* well this was much easier than expected...

* change grafana-plugin-configs webpack config

* fixes

* fix lockfile

* fix 2 more violations

* use path.resolve instead of require.resolve

* remove react import

* fix react imports

* more fixes

* remove React import

* remove import React from docs

* remove another react import
2024-06-25 12:43:47 +01:00

108 lines
2.6 KiB
TypeScript

import { css, cx } from '@emotion/css';
import { useEffect, useRef, useState } from 'react';
import * as React from 'react';
import { GrafanaTheme2 } from '@grafana/data';
import { ConfirmButton, Input, Label, LegacyInputStatus, useStyles2 } from '@grafana/ui';
interface Props {
label: string;
value?: string;
inputType?: string;
disabled?: boolean;
onChange?: (value: string) => void;
}
export const ServiceAccountProfileRow = ({ label, value, inputType, disabled, onChange }: Props): JSX.Element => {
const inputElem = useRef<HTMLInputElement>(null);
const [inputValue, setInputValue] = useState(value);
const [isEditing, setIsEditing] = useState(false);
const styles = useStyles2(getStyles);
const inputId = `${label}-input`;
useEffect(() => {
if (isEditing) {
focusInput();
}
}, [isEditing]);
const onEditClick = () => {
setIsEditing(true);
};
const onCancelClick = () => {
setIsEditing(false);
setInputValue(value || '');
};
const onInputChange = (event: React.ChangeEvent<HTMLInputElement>, status?: LegacyInputStatus) => {
if (status === LegacyInputStatus.Invalid) {
return;
}
setInputValue(event.target.value);
};
const onInputBlur = (event: React.FocusEvent<HTMLInputElement>, status?: LegacyInputStatus) => {
if (status === LegacyInputStatus.Invalid) {
return;
}
setInputValue(event.target.value);
};
const focusInput = () => {
inputElem?.current?.focus();
};
const onSave = () => {
setIsEditing(false);
if (onChange) {
onChange(inputValue!);
}
};
return (
<tr>
<td>
<Label htmlFor={inputId}>{label}</Label>
</td>
<td className="width-25" colSpan={2}>
{!disabled && isEditing ? (
<Input
id={inputId}
type={inputType}
defaultValue={value}
onBlur={onInputBlur}
onChange={onInputChange}
ref={inputElem}
width={30}
/>
) : (
<span className={cx({ [styles.disabled]: disabled })}>{value}</span>
)}
</td>
<td>
{onChange && (
<ConfirmButton
closeOnConfirm
confirmText="Save"
onConfirm={onSave}
onClick={onEditClick}
onCancel={onCancelClick}
disabled={disabled}
>
Edit
</ConfirmButton>
)}
</td>
</tr>
);
};
const getStyles = (theme: GrafanaTheme2) => {
return {
disabled: css`
color: ${theme.colors.text.secondary};
`,
};
};