Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: select secrets #906

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import usePressEnter from '@hooks/usePressEnter';

import {Option} from '@models/form';

import {customStyles, customTheme} from './CreatableMultiSelect.styled';
import {
DefaultDropdownIndicator,
DefaultMultiValueLabel,
DefaultMultiValueRemove,
DefaultOptionComponent,
} from './DefaultComponents';
} from '../DefaultComponents';
import {customMultiValueStyles, customTheme} from '../ReactSelect.styled';

type MultiSelectProps = {
id?: string;
Expand Down Expand Up @@ -91,7 +91,7 @@ const CreatableMultiSelect: React.FC<MultiSelectProps> = props => {
}}
formatCreateLabel={formatCreateLabel}
theme={customTheme}
styles={customStyles(validation, stylePlaceholderAsValue)}
styles={customMultiValueStyles(validation, stylePlaceholderAsValue)}
components={{
Option: CustomOptionComponent,
MultiValueLabel: CustomMultiValueLabelComponent,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import React, {KeyboardEvent, useRef} from 'react';
import {OptionProps} from 'react-select';
import CreatableSelect from 'react-select/creatable';

import usePressEnter from '@hooks/usePressEnter';

import {Option} from '@models/form';

import CustomSingleValue from '../CustomComponents/SingleValue';
import {DefaultClearIndicator, DefaultOptionComponent} from '../DefaultComponents';
import {customSingleValueStyles, customTheme} from '../ReactSelect.styled';

type SingleSelectProps = {
options?: Option[];
placeholder: string;
formatCreateLabel: (input: string) => string;
value?: Option;
defaultValue?: Option;
onChange?: (value: any) => void;
onBlur?: (event: any) => void;
onCreateOption?: (input: string) => void;
validateCreation?: (inputValue: string) => boolean;
CustomOptionComponent?: (props: OptionProps<Option>) => JSX.Element;
isLoading?: boolean;
validation?: boolean;
dataTest?: string;
disabled?: boolean;
menuPlacement?: 'auto' | 'bottom' | 'top';
stylePlaceholderAsValue?: boolean;
};

const CreatableSingleSelect: React.FC<SingleSelectProps> = props => {
const {
options,
placeholder,
formatCreateLabel,
value,
defaultValue,
onChange,
onBlur,
onCreateOption,
validateCreation,
CustomOptionComponent = DefaultOptionComponent,
validation,
dataTest,
disabled = false,
menuPlacement = 'bottom',
stylePlaceholderAsValue = false,
} = props;

const ref = useRef(null);

const onEvent = usePressEnter();

const handleKeyDown = (event: KeyboardEvent) => {
if (validateCreation) {
// @ts-ignore
if (!validateCreation(ref.current.props.inputValue)) {
event.preventDefault();
}
} else {
event.preventDefault();
}
};

return (
<CreatableSelect
ref={ref}
value={value}
defaultValue={defaultValue}
menuPlacement={menuPlacement}
isClearable
onChange={onChange}
onBlur={onBlur}
onCreateOption={onCreateOption}
placeholder={placeholder}
options={options}
createOptionPosition="first"
onKeyDown={event => {
onEvent(event, () => {
handleKeyDown(event);
});
}}
formatCreateLabel={formatCreateLabel}
theme={customTheme}
styles={customSingleValueStyles(validation, stylePlaceholderAsValue)}
components={{
Option: CustomOptionComponent,
// removing default dropdown indicator
DropdownIndicator: () => null,
ClearIndicator: DefaultClearIndicator,
SingleValue: CustomSingleValue,
}}
data-test={dataTest}
isDisabled={disabled}
noOptionsMessage={() => null}
/>
);
};
export default CreatableSingleSelect;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {default} from './CreatableSingleSelect';
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {SplitLabelText} from '@atoms';

import {Option} from '@models/form';

import {StyledMultiLabel} from '../CreatableMultiSelect.styled';
import {StyledMultiLabel} from '../ReactSelect.styled';

const LabelsMultiValueLabel = (props: MultiValueGenericProps<Option>) => {
const {children} = props;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {OptionProps} from 'react-select';

import {SplitLabelText} from '@atoms';

import {Option} from '@models/form';

import {labelRegex} from '@molecules/LabelsSelect/utils';

import {StyledOption} from '../ReactSelect.styled';

const LabelsOption = (props: OptionProps<Option>) => {
// @ts-ignore
const {children, innerRef, innerProps, value} = props;

const isChildren = typeof children === 'string';
const allowClick = labelRegex.test(value);

if (allowClick && isChildren) {
return (
// @ts-ignore
<StyledOption ref={innerRef} {...innerProps} data-test={`label-option-${children}`}>
<SplitLabelText value={children} />
</StyledOption>
);
}

return (
// @ts-ignore
<StyledOption ref={innerRef}>{children}</StyledOption>
);
};

export default LabelsOption;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {OptionProps} from 'react-select';

import {SplitLabelText} from '@atoms';

import {Option} from '@models/form';

import {labelRegex} from '@molecules/LabelsSelect/utils';

import {StyledOption} from '../ReactSelect.styled';

const SecretOption = (props: OptionProps<Option>) => {
// @ts-ignore
const {children, innerRef, innerProps, value} = props;

const isChildren = typeof children === 'string';
const isKeyValuePair = labelRegex.test(value);

return (
// @ts-ignore
<StyledOption ref={innerRef} {...innerProps} data-test={`secret-option-${children}`}>
{isKeyValuePair && isChildren ? <SplitLabelText value={children} isBordered /> : children}
</StyledOption>
);
};

export default SecretOption;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {SingleValueProps, components} from 'react-select';

import SplitLabelText from '@atoms/SplitLabelText';

import {Option} from '@models/form';

const CustomSingleValue = (props: SingleValueProps<Option>) => {
const {children} = props;
return (
<components.SingleValue {...props}>
<SplitLabelText value={String(children)} />
</components.SingleValue>
);
};

export default CustomSingleValue;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {ClearIndicatorProps, components} from 'react-select';

import {ReactComponent as RemoveIcon} from '@assets/closeCircle.svg';

import {Option} from '@models/form';

const ClearIndicator = (props: ClearIndicatorProps<Option>) => {
return (
<components.ClearIndicator {...props}>
<RemoveIcon width={16} height={16} />
</components.ClearIndicator>
);
};

export default ClearIndicator;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {DownOutlined} from '@ant-design/icons';

import {DropdownWrapper} from '../CreatableMultiSelect.styled';
import {DropdownWrapper} from '../ReactSelect.styled';

const DropdownIndicator = () => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export {default as DefaultMultiValueLabel} from './MultiValueLabel';
export {default as DefaultOptionComponent} from './Option';
export {default as DefaultMultiValueRemove} from './MultiValueRemove';
export {default as DefaultDropdownIndicator} from './DropdownIndicator';
export {default as DefaultClearIndicator} from './ClearIndicator';
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,35 @@ export const StyledMultiLabel = styled.div`
padding: 3px 5px;
`;

export const customStyles: (
const singleValueStyles: StylesConfig<Option, true, GroupBase<Option>> = {
singleValue: styles => ({
...styles,
color: Colors.slate200,
}),
};

const multiValueStyles: StylesConfig<Option, true, GroupBase<Option>> = {
multiValue: styles => ({
...styles,
background: 'transparent',
border: `1px solid ${Colors.slate700}`,
}),
multiValueLabel: styles => ({
...styles,
color: Colors.slate200,
fontWeight: 400,
fontSize: 12,
}),
multiValueRemove: styles => ({
...styles,
'&:hover': {
backgroundColor: 'transparent',
cursor: 'pointer',
},
}),
};

const customStyles: (
validation?: boolean,
stylePlaceholderAsValue?: boolean
) => StylesConfig<Option, true, GroupBase<Option>> = (validation = true, stylePlaceholderAsValue = false) => ({
Expand Down Expand Up @@ -65,24 +93,6 @@ export const customStyles: (
}),
menu: styles => ({...styles, backgroundColor: Colors.slate800}),
menuList: styles => ({...styles, padding: 0}),
multiValue: styles => ({
...styles,
background: 'transparent',
border: `1px solid ${Colors.slate700}`,
}),
multiValueLabel: styles => ({
...styles,
color: Colors.slate200,
fontWeight: 400,
fontSize: 12,
}),
multiValueRemove: styles => ({
...styles,
'&:hover': {
backgroundColor: 'transparent',
cursor: 'pointer',
},
}),
noOptionsMessage: styles => ({
...styles,
fontWeight: 400,
Expand All @@ -102,6 +112,22 @@ export const customStyles: (
}),
});

export const customMultiValueStyles: (
validation?: boolean,
stylePlaceholderAsValue?: boolean
) => StylesConfig<Option, true, GroupBase<Option>> = (validation = true, stylePlaceholderAsValue = false) => ({
...customStyles(validation, stylePlaceholderAsValue),
...multiValueStyles,
});

export const customSingleValueStyles: (
validation?: boolean,
stylePlaceholderAsValue?: boolean
) => StylesConfig<Option, true, GroupBase<Option>> = (validation = true, stylePlaceholderAsValue = false) => ({
...customStyles(validation, stylePlaceholderAsValue),
...singleValueStyles,
});

export const customTheme: ThemeConfig = theme => {
const {colors, ...rest} = theme;

Expand Down
2 changes: 2 additions & 0 deletions packages/web/src/components/atoms/ReactSelect/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {default as CreatableMultiSelect} from './CreatableMultiSelect';
export {default as CreatableSingleSelect} from './CreatableSingleSelect';
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import styled from 'styled-components';

export const SplitLabelTextContainer = styled.div`
import Colors from '@styles/Colors';

export const SplitLabelTextContainer = styled.div<{$isBordered: boolean}>`
display: inline-flex;
gap: 3px;

${({$isBordered}) =>
$isBordered
? `
border: 1px solid ${Colors.slate700};
&:hover {
border: 1px solid ${Colors.slate500};
}
`
: ''}
border-radius: 4px;

padding: 0 5px;
`;
Loading
Loading