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

107 lines
2.6 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";
import styled from "styled-components";
const Wrapper = styled.div`
display: flex;
flex: 1;
flex-direction: column;
`;
const Error = styled.span`
color: ${props => props.theme.colors.error};
fontsize: ${props => props.theme.fontSizes[1]};
`;
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;
2019-10-21 15:12:45 +00:00
};
const selectStyles = {
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-11-14 13:13:35 +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`,
boxShadow: state.isFocused ? 0 : 0,
"&: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,
meta,
formatCreateLabel,
} = this.props;
const optionalProps: Partial<DropdownProps> = {};
if (formatCreateLabel) optionalProps.formatCreateLabel = formatCreateLabel;
2019-10-21 15:12:45 +00:00
return (
<Wrapper>
<Creatable
placeholder={placeholder}
options={options}
styles={selectStyles}
isLoading={isLoading}
onCreateOption={onCreateOption}
{...input}
onChange={value => input.onChange(value)}
onBlur={() => input.value}
isClearable
{...optionalProps}
/>
{meta && meta.touched && meta.error && <Error>{meta.error}</Error>}
</Wrapper>
2019-10-21 15:12:45 +00:00
);
}
}
export default CreatableDropdown;