PromucFlow_constructor/app/client/src/components/designSystems/appsmith/CreatableDropdown.tsx

98 lines
2.5 KiB
TypeScript
Raw Normal View History

2019-10-21 15:12:45 +00:00
import React from "react";
import Creatable from "react-select/creatable";
import { WrappedFieldInputProps, WrappedFieldMetaProps } from "redux-form";
2019-11-25 05:07:27 +00:00
import { theme } from "constants/DefaultTheme";
2019-10-21 15:12:45 +00:00
type DropdownProps = {
options: Array<{
value: string;
label: string;
}>;
placeholder: string;
isLoading?: boolean;
input: WrappedFieldInputProps;
meta: WrappedFieldMetaProps;
onCreateOption: (inputValue: string) => void;
formatCreateLabel?: (value: string) => React.ReactNode;
2020-01-02 13:36:35 +00:00
noOptionsMessage?: (obj: { inputValue: string }) => string;
2019-10-21 15:12:45 +00:00
};
const selectStyles = {
2020-01-02 13:36:35 +00:00
placeholder: (provided: any) => ({
...provided,
color: "#a3b3bf",
}),
singleValue: (provided: any) => ({
...provided,
backgroundColor: "rgba(104,113,239,0.1)",
border: "1px solid rgba(104, 113, 239, 0.5)",
2019-11-14 09:01:23 +00:00
borderRadius: `${theme.radii[1]}px`,
2019-11-13 11:34:11 +00:00
padding: "2px 5px",
fontSize: "14px",
maxWidth: "95%",
2019-12-11 10:35:07 +00:00
position: "relative",
display: "inline-block",
transform: "none",
}),
2019-10-21 15:12:45 +00:00
container: (styles: any) => ({
...styles,
flex: 1,
}),
2019-11-14 09:01:23 +00:00
control: (styles: any, state: any) => ({
2019-11-13 11:34:11 +00:00
...styles,
minHeight: "32px",
2019-11-14 09:01:23 +00:00
border: state.isFocused
? `${theme.colors.secondary} solid 1px`
: `${theme.colors.inputInactiveBorders} solid 1px`,
2019-12-11 10:35:07 +00:00
boxShadow: state.isFocused ? "none" : "none",
2019-11-14 09:01:23 +00:00
"&:hover": {
border: `${theme.colors.secondary} solid 1px`,
},
2019-11-13 11:34:11 +00:00
}),
indicatorsContainer: (provided: any) => ({
...provided,
height: "30px",
}),
clearIndicator: (provided: any) => ({
...provided,
padding: "5px",
}),
dropdownIndicator: (provided: any) => ({
...provided,
padding: "5px",
}),
2019-10-21 15:12:45 +00:00
};
class CreatableDropdown extends React.Component<DropdownProps> {
render() {
const {
placeholder,
options,
isLoading,
onCreateOption,
input,
formatCreateLabel,
2020-01-02 13:36:35 +00:00
noOptionsMessage,
} = this.props;
const optionalProps: Partial<DropdownProps> = {};
if (formatCreateLabel) optionalProps.formatCreateLabel = formatCreateLabel;
2020-01-02 13:36:35 +00:00
if (noOptionsMessage) optionalProps.noOptionsMessage = noOptionsMessage;
2019-10-21 15:12:45 +00:00
return (
2019-12-11 10:35:07 +00:00
<Creatable
placeholder={placeholder}
options={options}
styles={selectStyles}
isLoading={isLoading}
onCreateOption={onCreateOption}
{...input}
onChange={value => input.onChange(value)}
onBlur={() => input.value}
isClearable
{...optionalProps}
/>
2019-10-21 15:12:45 +00:00
);
}
}
export default CreatableDropdown;