From 65cacd31c2f4089bc644d0d6f86372c8cadd5c57 Mon Sep 17 00:00:00 2001 From: Alex Khomenko Date: Sat, 15 May 2021 11:06:42 +0300 Subject: [PATCH] grafana/ui: Unify disabled styles for input components (#34126) * Select: Add disabled prop to SingleValue * TagsInput: Disable remove button with disabled prop * Checkbox: Add disabled styles --- .../src/components/Forms/Checkbox.tsx | 77 +++++++++++-------- .../src/components/Select/SelectBase.tsx | 4 +- .../src/components/Select/SingleValue.tsx | 15 ++-- .../src/components/TagsInput/TagsInput.tsx | 3 + 4 files changed, 60 insertions(+), 39 deletions(-) diff --git a/packages/grafana-ui/src/components/Forms/Checkbox.tsx b/packages/grafana-ui/src/components/Forms/Checkbox.tsx index 5073b186859..fabac95b6f2 100644 --- a/packages/grafana-ui/src/components/Forms/Checkbox.tsx +++ b/packages/grafana-ui/src/components/Forms/Checkbox.tsx @@ -1,7 +1,7 @@ import React, { HTMLProps, useCallback } from 'react'; import { GrafanaTheme2 } from '@grafana/data'; import { getLabelStyles } from './Label'; -import { useTheme2, stylesFactory } from '../../themes'; +import { stylesFactory, useStyles2 } from '../../themes'; import { css, cx } from '@emotion/css'; import { getFocusStyles, getMouseFocusStyles } from '../../themes/mixins'; @@ -11,6 +11,37 @@ export interface CheckboxProps extends Omit, 'value' value?: boolean; } +export const Checkbox = React.forwardRef( + ({ label, description, value, onChange, disabled, className, ...inputProps }, ref) => { + const handleOnChange = useCallback( + (e: React.ChangeEvent) => { + if (onChange) { + onChange(e); + } + }, + [onChange] + ); + const styles = useStyles2(getCheckboxStyles); + + return ( + + ); + } +); + export const getCheckboxStyles = stylesFactory((theme: GrafanaTheme2) => { const labelStyles = getLabelStyles(theme); const checkboxSize = '16px'; @@ -79,6 +110,18 @@ export const getCheckboxStyles = stylesFactory((theme: GrafanaTheme2) => { transform: rotate(45deg); } } + + &:disabled + span { + background-color: ${theme.colors.action.disabledBackground}; + cursor: not-allowed; + &:hover { + background-color: ${theme.colors.action.disabledBackground}; + } + + &:after { + border-color: ${theme.colors.action.disabledText}; + } + } `, checkmark: css` display: inline-block; @@ -100,36 +143,4 @@ export const getCheckboxStyles = stylesFactory((theme: GrafanaTheme2) => { }; }); -export const Checkbox = React.forwardRef( - ({ label, description, value, onChange, disabled, className, ...inputProps }, ref) => { - const theme = useTheme2(); - const handleOnChange = useCallback( - (e: React.ChangeEvent) => { - if (onChange) { - onChange(e); - } - }, - [onChange] - ); - const styles = getCheckboxStyles(theme); - - return ( - - ); - } -); - Checkbox.displayName = 'Checkbox'; diff --git a/packages/grafana-ui/src/components/Select/SelectBase.tsx b/packages/grafana-ui/src/components/Select/SelectBase.tsx index 432ddd58fe4..cf64db0f7e1 100644 --- a/packages/grafana-ui/src/components/Select/SelectBase.tsx +++ b/packages/grafana-ui/src/components/Select/SelectBase.tsx @@ -314,7 +314,9 @@ export function SelectBase({ DropdownIndicator(props: any) { return ; }, - SingleValue: SingleValue, + SingleValue(props: any) { + return ; + }, MultiValueContainer: MultiValueContainer, MultiValueRemove: MultiValueRemove, ...components, diff --git a/packages/grafana-ui/src/components/Select/SingleValue.tsx b/packages/grafana-ui/src/components/Select/SingleValue.tsx index e95278fbfbe..eb19839bb30 100644 --- a/packages/grafana-ui/src/components/Select/SingleValue.tsx +++ b/packages/grafana-ui/src/components/Select/SingleValue.tsx @@ -34,7 +34,11 @@ const getStyles = (theme: GrafanaTheme2) => { position: absolute; `; - return { singleValue, container, item }; + const disabled = css` + color: ${theme.colors.action.disabledText}; + `; + + return { singleValue, container, item, disabled }; }; type StylesType = ReturnType; @@ -44,17 +48,18 @@ interface Props imgUrl?: string; loading?: boolean; hideText?: boolean; - }> {} + }> { + disabled?: boolean; +} export const SingleValue = (props: Props) => { - const { children, data } = props; + const { children, data, disabled } = props; const styles = useStyles2(getStyles); - const loading = useDelayedSwitch(data.loading || false, { delay: 250, duration: 750 }); return ( -
+
{data.imgUrl ? ( ) : ( diff --git a/packages/grafana-ui/src/components/TagsInput/TagsInput.tsx b/packages/grafana-ui/src/components/TagsInput/TagsInput.tsx index d6c8d88836c..dc1268b6316 100644 --- a/packages/grafana-ui/src/components/TagsInput/TagsInput.tsx +++ b/packages/grafana-ui/src/components/TagsInput/TagsInput.tsx @@ -32,6 +32,9 @@ export const TagsInput: FC = ({ }; const onRemove = (tagToRemove: string) => { + if (disabled) { + return; + } onChange(tags?.filter((x) => x !== tagToRemove)); };