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

151 lines
3.6 KiB
TypeScript
Raw Normal View History

2019-12-10 13:30:16 +00:00
import React, { Component } from "react";
2019-11-13 07:34:59 +00:00
import styled from "styled-components";
2019-10-21 15:12:45 +00:00
import { WrappedFieldInputProps, WrappedFieldMetaProps } from "redux-form";
2019-12-06 13:16:08 +00:00
import {
IconName,
IInputGroupProps,
IIntentProps,
InputGroup,
MaybeElement,
} from "@blueprintjs/core";
2019-10-30 10:23:20 +00:00
import { ComponentProps } from "./BaseComponent";
2019-12-10 13:30:16 +00:00
import ErrorTooltip from "components/editorComponents/ErrorTooltip";
2019-10-21 15:12:45 +00:00
2019-12-10 13:30:16 +00:00
export const TextInput = styled(({ hasError, ...rest }) => (
<InputGroup {...rest} />
))<{ hasError: boolean }>`
2019-10-21 15:12:45 +00:00
flex: 1;
2019-11-14 09:01:23 +00:00
& input {
2019-12-10 13:30:16 +00:00
border: 1px solid
${props =>
props.hasError
? props.theme.colors.error
: props.theme.colors.inputInactiveBorders};
2019-11-13 07:34:59 +00:00
border-radius: 4px;
box-shadow: none;
2019-11-13 11:34:11 +00:00
height: 32px;
2019-10-21 15:12:45 +00:00
background-color: ${props => props.theme.colors.textOnDarkBG};
2019-11-13 07:34:59 +00:00
&:focus {
2019-12-10 13:30:16 +00:00
border-color: ${props =>
props.hasError
? props.theme.colors.error
: props.theme.colors.secondary};
2019-11-13 07:34:59 +00:00
background-color: ${props => props.theme.colors.textOnDarkBG};
outline: 0;
2019-11-25 09:15:11 +00:00
box-shadow: none;
2019-11-13 07:34:59 +00:00
}
}
2019-12-10 13:30:16 +00:00
&.bp3-input-group {
.bp3-input:not(:first-child) {
padding-left: 35px;
}
.bp3-icon {
border-radius: 4px 0 0 4px;
margin: 0;
height: 32px;
width: 30px;
display: flex;
align-items: center;
justify-content: center;
background-color: #eef2f5;
svg {
height: 20px;
width: 20px;
path {
fill: #979797;
}
2019-11-13 07:34:59 +00:00
}
}
2019-10-21 15:12:45 +00:00
}
`;
const InputContainer = styled.div`
display: flex;
flex: 1;
flex-direction: column;
2019-12-06 13:16:08 +00:00
position: relative;
`;
2019-12-06 13:16:08 +00:00
export interface TextInputProps extends IInputGroupProps {
2019-12-05 03:16:00 +00:00
/** TextInput Placeholder */
2019-11-25 09:15:11 +00:00
placeholder?: string;
2019-12-06 13:16:08 +00:00
intent?: IIntentProps["intent"];
2019-11-13 07:34:59 +00:00
input?: Partial<WrappedFieldInputProps>;
2019-12-05 03:16:00 +00:00
meta?: Partial<WrappedFieldMetaProps>;
2019-11-13 11:34:11 +00:00
icon?: IconName | MaybeElement;
2019-12-05 03:16:00 +00:00
/** Should show error when defined */
2019-11-13 11:34:11 +00:00
showError?: boolean;
2019-12-05 03:16:00 +00:00
/** Additional classname */
2019-11-14 09:01:23 +00:00
className?: string;
2019-12-16 08:49:10 +00:00
type?: string;
2019-12-06 13:16:08 +00:00
refHandler?: (ref: HTMLInputElement | null) => void;
2019-10-21 15:12:45 +00:00
}
2019-12-10 13:30:16 +00:00
interface TextInputState {
inputIsFocused: boolean;
}
export class BaseTextInput extends Component<TextInputProps, TextInputState> {
constructor(props: TextInputProps) {
super(props);
this.state = {
inputIsFocused: false,
};
}
handleFocus = (e: React.FocusEvent) => {
this.setState({ inputIsFocused: true });
if (this.props.input && this.props.input.onFocus) {
this.props.input.onFocus(e);
}
};
handleBlur = (e: React.FocusEvent) => {
this.setState({ inputIsFocused: false });
if (this.props.input && this.props.input.onBlur) {
this.props.input.onBlur(e);
}
};
render() {
const {
input,
meta,
showError,
className,
refHandler,
...rest
} = this.props;
const hasError = !!(
showError &&
meta &&
(meta.touched || meta.active) &&
meta.error
);
const errorIsOpen = hasError && this.state.inputIsFocused;
return (
<InputContainer className={className}>
<ErrorTooltip message={meta ? meta.error : ""} isOpen={errorIsOpen}>
<TextInput
hasError={hasError}
inputRef={refHandler}
{...input}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
autoComplete={"off"}
{...rest}
/>
</ErrorTooltip>
</InputContainer>
);
}
}
2019-10-21 15:12:45 +00:00
2019-12-05 03:16:00 +00:00
/**
* Text Input Component
* Has Icon, placholder, errors, etc.
*/
2019-10-21 15:12:45 +00:00
const TextInputComponent = (props: TextInputProps & ComponentProps) => {
return <BaseTextInput {...props} />;
2019-10-21 15:12:45 +00:00
};
export default TextInputComponent;