2022-06-23 04:49:44 +08:00
|
|
|
import { css } from '@emotion/css';
|
2020-03-27 04:48:46 +08:00
|
|
|
|
2023-12-07 19:03:18 +08:00
|
|
|
import { StandardEditorProps, GrafanaTheme2, UnitFieldConfigSettings } from '@grafana/data';
|
2025-05-15 15:17:14 +08:00
|
|
|
import { useTranslate } from '@grafana/i18n';
|
2022-06-23 04:49:44 +08:00
|
|
|
import { IconButton, UnitPicker, useStyles2 } from '@grafana/ui';
|
2020-03-27 04:48:46 +08:00
|
|
|
|
2023-12-07 19:03:18 +08:00
|
|
|
type Props = StandardEditorProps<string, UnitFieldConfigSettings>;
|
2022-06-23 04:49:44 +08:00
|
|
|
|
|
|
|
export function UnitValueEditor({ value, onChange, item }: Props) {
|
|
|
|
const styles = useStyles2(getStyles);
|
2025-05-15 15:17:14 +08:00
|
|
|
const { t } = useTranslate();
|
2022-06-23 04:49:44 +08:00
|
|
|
if (item?.settings?.isClearable && value != null) {
|
|
|
|
return (
|
|
|
|
<div className={styles.wrapper}>
|
|
|
|
<span className={styles.first}>
|
|
|
|
<UnitPicker value={value} onChange={onChange} />
|
|
|
|
</span>
|
2025-04-07 16:48:03 +08:00
|
|
|
<IconButton
|
|
|
|
name="times"
|
|
|
|
onClick={() => onChange(undefined)}
|
|
|
|
tooltip={t('options-ui.units.clear-tooltip', 'Clear unit selection')}
|
|
|
|
/>
|
2022-06-23 04:49:44 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2020-05-07 16:54:05 +08:00
|
|
|
return <UnitPicker value={value} onChange={onChange} />;
|
2022-06-23 04:49:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const getStyles = (theme: GrafanaTheme2) => ({
|
2023-11-29 23:26:19 +08:00
|
|
|
wrapper: css({
|
|
|
|
width: '100%',
|
|
|
|
display: 'flex',
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
}),
|
|
|
|
first: css({
|
|
|
|
marginRight: theme.spacing(1),
|
|
|
|
flexGrow: 2,
|
|
|
|
}),
|
2022-06-23 04:49:44 +08:00
|
|
|
});
|