Commit fa4fddf7 by Torkel Ödegaard

React-select refactorings

parent 1d7d72b4
...@@ -4,19 +4,40 @@ import { OptionProps } from 'react-select/lib/components/Option'; ...@@ -4,19 +4,40 @@ import { OptionProps } from 'react-select/lib/components/Option';
// https://github.com/JedWatson/react-select/issues/3038 // https://github.com/JedWatson/react-select/issues/3038
interface ExtendedOptionProps extends OptionProps<any> { interface ExtendedOptionProps extends OptionProps<any> {
data: any; data: {
description?: string;
imgUrl?: string;
};
} }
export const PickerOption = (props: ExtendedOptionProps) => { export const Option = (props: ExtendedOptionProps) => {
const { children, data, className } = props; const { children, isSelected, data } = props;
return ( return (
<components.Option {...props}> <components.Option {...props}>
<div className={`description-picker-option__button btn btn-link ${className}`}> <div className="gf-form-select-box__desc-option">
{data.avatarUrl && <img src={data.avatarUrl} alt={data.label} className="user-picker-option__avatar" />} {data.imgUrl && <img className="gf-form-select-box__desc-option__img" src={data.imgUrl} />}
{children} <div className="gf-form-select-box__desc-option__body">
<div>{children}</div>
{data.description && <div className="gf-form-select-box__desc-option__desc">{data.description}</div>}
</div>
{isSelected && <i className="fa fa-check" aria-hidden="true" />}
</div> </div>
</components.Option> </components.Option>
); );
}; };
export default PickerOption; export const SingleValue = (props: ExtendedOptionProps) => {
const { children, data } = props;
return (
<components.SingleValue {...props}>
<div className="gf-form-select-box__img-value">
{data.imgUrl && <img className="gf-form-select-box__desc-option__img" src={data.imgUrl} />}
{children}
</div>
</components.SingleValue>
);
};
export default Option;
import React, { SFC } from 'react'; import React, { SFC } from 'react';
import Select from 'react-select'; import Select from 'react-select';
import DescriptionOption from './DescriptionOption'; import DescriptionOption from './DescriptionOption';
import IndicatorsContainer from './IndicatorsContainer';
import ResetStyles from './ResetStyles'; import ResetStyles from './ResetStyles';
interface Props { interface Props {
...@@ -32,6 +33,7 @@ const SimplePicker: SFC<Props> = ({ ...@@ -32,6 +33,7 @@ const SimplePicker: SFC<Props> = ({
className={`width-${width} gf-form-input gf-form-input--form-dropdown ${className || ''}`} className={`width-${width} gf-form-input gf-form-input--form-dropdown ${className || ''}`}
components={{ components={{
Option: DescriptionOption, Option: DescriptionOption,
IndicatorsContainer,
}} }}
defaultValue={defaultValue} defaultValue={defaultValue}
value={value} value={value}
......
...@@ -47,7 +47,7 @@ export class TeamPicker extends Component<Props, State> { ...@@ -47,7 +47,7 @@ export class TeamPicker extends Component<Props, State> {
id: team.id, id: team.id,
label: team.name, label: team.name,
name: team.name, name: team.name,
avatarUrl: team.avatarUrl, imgUrl: team.avatarUrl,
}; };
}); });
......
...@@ -41,7 +41,7 @@ export class UserPicker extends Component<Props, State> { ...@@ -41,7 +41,7 @@ export class UserPicker extends Component<Props, State> {
return result.map(user => ({ return result.map(user => ({
id: user.userId, id: user.userId,
label: user.login === user.email ? user.login : `${user.login} - ${user.email}`, label: user.login === user.email ? user.login : `${user.login} - ${user.email}`,
avatarUrl: user.avatarUrl, imgUrl: user.avatarUrl,
login: user.login, login: user.login,
})); }));
}) })
......
...@@ -4,7 +4,7 @@ import _ from 'lodash'; ...@@ -4,7 +4,7 @@ import _ from 'lodash';
// Components // Components
import ResetStyles from 'app/core/components/Picker/ResetStyles'; import ResetStyles from 'app/core/components/Picker/ResetStyles';
import PickerOption from 'app/core/components/Picker/PickerOption'; import { Option, SingleValue } from 'app/core/components/Picker/PickerOption';
import IndicatorsContainer from 'app/core/components/Picker/IndicatorsContainer'; import IndicatorsContainer from 'app/core/components/Picker/IndicatorsContainer';
import Select from 'react-select'; import Select from 'react-select';
...@@ -35,10 +35,14 @@ export class DataSourcePicker extends PureComponent<Props> { ...@@ -35,10 +35,14 @@ export class DataSourcePicker extends PureComponent<Props> {
const options = datasources.map(ds => ({ const options = datasources.map(ds => ({
value: ds.name, value: ds.name,
label: ds.name, label: ds.name,
avatarUrl: ds.meta.info.logos.small, imgUrl: ds.meta.info.logos.small,
})); }));
const value = { label: current.name, label: current.name }; const value = {
label: current.name,
value: current.name,
imgUrl: current.meta.info.logos.small,
};
return ( return (
<div className="gf-form-inline"> <div className="gf-form-inline">
...@@ -57,7 +61,8 @@ export class DataSourcePicker extends PureComponent<Props> { ...@@ -57,7 +61,8 @@ export class DataSourcePicker extends PureComponent<Props> {
noOptionsMessage={() => 'No datasources found'} noOptionsMessage={() => 'No datasources found'}
value={value} value={value}
components={{ components={{
Option: PickerOption, Option,
SingleValue,
IndicatorsContainer, IndicatorsContainer,
}} }}
/> />
......
...@@ -96,8 +96,6 @@ ...@@ -96,8 +96,6 @@
@import 'components/empty_list_cta'; @import 'components/empty_list_cta';
@import 'components/popper'; @import 'components/popper';
@import 'components/form_select_box'; @import 'components/form_select_box';
@import 'components/user-picker';
@import 'components/description-picker';
@import 'components/panel_editor'; @import 'components/panel_editor';
@import 'components/toolbar'; @import 'components/toolbar';
@import 'components/delete_button'; @import 'components/delete_button';
......
.description-picker-option__button {
position: relative;
text-align: left;
width: 100%;
display: block;
border-radius: 0;
white-space: normal;
i.fa-check {
padding-left: 2px;
}
}
.gf-form-select-box__desc-option {
display: flex;
align-items: center;
justify-content: flex-start;
justify-items: center;
cursor: pointer;
padding: 7px 10px;
width: 100%;
}
.gf-form-select-box__desc-option__body {
display: flex;
flex-direction: column;
flex-grow: 1;
padding-right: 10px;
font-weight: 500;
}
.gf-form-select-box__desc-option__desc {
font-weight: normal;
font-size: $font-size-sm;
color: $text-muted;
}
...@@ -47,6 +47,9 @@ $select-input-bg-disabled: $input-bg-disabled; ...@@ -47,6 +47,9 @@ $select-input-bg-disabled: $input-bg-disabled;
.gf-form-select-box__input { .gf-form-select-box__input {
padding-left: 5px; padding-left: 5px;
input {
line-height: inherit;
}
} }
.gf-form-select-box__menu { .gf-form-select-box__menu {
...@@ -140,3 +143,32 @@ $select-input-bg-disabled: $input-bg-disabled; ...@@ -140,3 +143,32 @@ $select-input-bg-disabled: $input-bg-disabled;
padding-left: 30px; padding-left: 30px;
} }
} }
.gf-form-select-box__desc-option {
display: flex;
align-items: center;
justify-content: flex-start;
justify-items: center;
cursor: pointer;
padding: 7px 10px;
width: 100%;
}
.gf-form-select-box__desc-option__body {
display: flex;
flex-direction: column;
flex-grow: 1;
padding-right: 10px;
font-weight: 500;
}
.gf-form-select-box__desc-option__desc {
font-weight: normal;
font-size: $font-size-sm;
color: $text-muted;
}
.gf-form-select-box__desc-option__img {
width: 20px;
margin-right: 10px;
}
.user-picker-option__button {
position: relative;
text-align: left;
width: 100%;
display: block;
border-radius: 0;
}
.user-picker-option__avatar {
width: 20px;
display: inline-block;
margin-right: 10px;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment