The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
grafana/packages/grafana-ui/src/components/SecretFormField/SecretFormField.tsx

83 lines
2.5 KiB

import omit from 'lodash/omit';
import React, { InputHTMLAttributes, FunctionComponent } from 'react';
import { FormField } from '../FormField/FormField';
import { Button } from '../Button/Button';
import { css, cx } from 'emotion';
export interface Props extends Omit<InputHTMLAttributes<HTMLInputElement>, 'onReset'> {
6 years ago
// Function to use when reset is clicked. Means you have to reset the input value yourself as this is uncontrolled
// component (or do something else if required).
onReset: (event: React.SyntheticEvent<HTMLButtonElement>) => void;
isConfigured: boolean;
label?: string;
tooltip?: string;
labelWidth?: number;
inputWidth?: number;
6 years ago
// Placeholder of the input field when in non configured state.
placeholder?: string;
}
const getSecretFormFieldStyles = () => {
return {
noRadiusInput: css`
border-bottom-right-radius: 0 !important;
border-top-right-radius: 0 !important;
`,
noRadiusButton: css`
border-bottom-left-radius: 0 !important;
border-top-left-radius: 0 !important;
`,
};
6 years ago
};
/**
* Form field that has 2 states configured and not configured. If configured it will not show its contents and adds
* a reset button that will clear the input and makes it accessible. In non configured state it behaves like normal
* form field. This is used for passwords or anything that is encrypted on the server and is later returned encrypted
* to the user (like datasource passwords).
*/
export const SecretFormField: FunctionComponent<Props> = ({
label = 'Password',
labelWidth,
inputWidth = 12,
onReset,
isConfigured,
tooltip,
placeholder = 'Password',
...inputProps
}: Props) => {
const styles = getSecretFormFieldStyles();
return (
<FormField
6 years ago
label={label!}
tooltip={tooltip!}
labelWidth={labelWidth}
inputEl={
isConfigured ? (
<>
<input
type="text"
className={cx(`gf-form-input width-${inputWidth! - 2}`, styles.noRadiusInput)}
disabled={true}
value="configured"
{...omit(inputProps, 'value')}
/>
<Button onClick={onReset} variant="secondary">
Reset
</Button>
</>
) : (
<input
type="password"
className={`gf-form-input width-${inputWidth}`}
6 years ago
placeholder={placeholder}
{...inputProps}
/>
)
}
/>
);
};
SecretFormField.displayName = 'SecretFormField';