2019-11-13 07:00:25 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import styled from "styled-components";
|
2020-03-06 09:45:21 +00:00
|
|
|
import { IntentColors, labelStyle } from "constants/DefaultTheme";
|
2019-11-25 05:07:27 +00:00
|
|
|
import { ComponentProps } from "components/designSystems/appsmith/BaseComponent";
|
2019-10-30 10:23:20 +00:00
|
|
|
import {
|
|
|
|
|
Intent,
|
|
|
|
|
NumericInput,
|
|
|
|
|
IconName,
|
|
|
|
|
InputGroup,
|
|
|
|
|
Button,
|
2019-10-31 05:28:11 +00:00
|
|
|
Label,
|
2020-01-14 09:50:42 +00:00
|
|
|
Classes,
|
2020-01-21 12:48:42 +00:00
|
|
|
ControlGroup,
|
2020-02-06 07:01:25 +00:00
|
|
|
TextArea,
|
2019-10-30 10:23:20 +00:00
|
|
|
} from "@blueprintjs/core";
|
2019-11-25 05:07:27 +00:00
|
|
|
import { InputType } from "widgets/InputWidget";
|
2020-01-21 12:48:42 +00:00
|
|
|
import { WIDGET_PADDING } from "constants/WidgetConstants";
|
2020-02-06 07:01:25 +00:00
|
|
|
import { Colors } from "constants/Colors";
|
2020-03-06 09:45:21 +00:00
|
|
|
import ErrorTooltip from "components/editorComponents/ErrorTooltip";
|
|
|
|
|
import _ from "lodash";
|
|
|
|
|
import { INPUT_WIDGET_DEFAULT_VALIDATION_ERROR } from "constants/messages";
|
2019-10-30 10:23:20 +00:00
|
|
|
/**
|
|
|
|
|
* All design system component specific logic goes here.
|
2020-03-06 09:45:21 +00:00
|
|
|
* Ex. Blueprint has a separate numeric input and text input so switching between them goes here
|
2019-10-30 10:23:20 +00:00
|
|
|
* Ex. To set the icon as currency, blue print takes in a set of defined types
|
|
|
|
|
* All generic logic like max characters for phone numbers should be 10, should go in the widget
|
|
|
|
|
*/
|
|
|
|
|
|
2020-12-24 04:32:25 +00:00
|
|
|
const InputComponentWrapper = styled((props) => (
|
2020-03-13 07:24:03 +00:00
|
|
|
<ControlGroup {..._.omit(props, ["hasError", "numeric"])} />
|
2020-03-06 09:45:21 +00:00
|
|
|
))<{
|
2020-03-13 07:24:03 +00:00
|
|
|
numeric: boolean;
|
2020-03-06 09:45:21 +00:00
|
|
|
multiline: string;
|
|
|
|
|
hasError: boolean;
|
|
|
|
|
}>`
|
2020-01-14 09:50:42 +00:00
|
|
|
&&&& {
|
2020-02-06 07:01:25 +00:00
|
|
|
.${Classes.INPUT} {
|
2020-01-17 12:34:58 +00:00
|
|
|
box-shadow: none;
|
2020-03-06 09:45:21 +00:00
|
|
|
border: 1px solid;
|
|
|
|
|
border-color: ${({ hasError }) =>
|
|
|
|
|
hasError ? IntentColors.danger : Colors.GEYSER_LIGHT};
|
2020-12-24 04:32:25 +00:00
|
|
|
border-radius: ${(props) => props.theme.radii[1]}px;
|
|
|
|
|
height: ${(props) => (props.multiline === "true" ? "100%" : "inherit")};
|
2020-03-06 09:45:21 +00:00
|
|
|
width: 100%;
|
2020-12-24 04:32:25 +00:00
|
|
|
${(props) =>
|
2020-03-13 07:24:03 +00:00
|
|
|
props.numeric &&
|
|
|
|
|
`
|
|
|
|
|
border-top-right-radius: 0px;
|
|
|
|
|
border-bottom-right-radius: 0px;
|
|
|
|
|
border-right-width: 0px;
|
|
|
|
|
`}
|
2020-02-06 07:01:25 +00:00
|
|
|
&:active {
|
2020-03-06 09:45:21 +00:00
|
|
|
border-color: ${({ hasError }) =>
|
|
|
|
|
hasError ? IntentColors.danger : Colors.HIT_GRAY};
|
2020-02-06 07:01:25 +00:00
|
|
|
}
|
|
|
|
|
&:focus {
|
2020-03-06 09:45:21 +00:00
|
|
|
border-color: ${({ hasError }) =>
|
|
|
|
|
hasError ? IntentColors.danger : Colors.MYSTIC};
|
2020-02-06 07:01:25 +00:00
|
|
|
}
|
2020-01-17 12:34:58 +00:00
|
|
|
}
|
2020-02-06 07:01:25 +00:00
|
|
|
.${Classes.INPUT_GROUP} {
|
2020-01-14 09:50:42 +00:00
|
|
|
display: block;
|
|
|
|
|
margin: 0;
|
|
|
|
|
}
|
2020-02-06 07:01:25 +00:00
|
|
|
.${Classes.CONTROL_GROUP} {
|
2020-01-14 09:50:42 +00:00
|
|
|
justify-content: flex-start;
|
|
|
|
|
}
|
2020-03-06 09:33:20 +00:00
|
|
|
height: 100%;
|
2020-01-21 12:48:42 +00:00
|
|
|
align-items: center;
|
|
|
|
|
label {
|
2020-01-28 08:21:22 +00:00
|
|
|
${labelStyle}
|
2020-01-21 12:48:42 +00:00
|
|
|
flex: 0 1 30%;
|
2020-02-06 07:01:25 +00:00
|
|
|
margin: 7px ${WIDGET_PADDING * 2}px 0 0;
|
2020-01-21 12:48:42 +00:00
|
|
|
text-align: right;
|
2020-02-06 07:01:25 +00:00
|
|
|
align-self: flex-start;
|
2020-03-06 09:45:21 +00:00
|
|
|
max-width: calc(30% - ${WIDGET_PADDING}px);
|
2020-01-21 12:48:42 +00:00
|
|
|
}
|
2019-11-13 07:00:25 +00:00
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2019-10-30 10:23:20 +00:00
|
|
|
class InputComponent extends React.Component<
|
|
|
|
|
InputComponentProps,
|
|
|
|
|
InputComponentState
|
|
|
|
|
> {
|
|
|
|
|
constructor(props: InputComponentProps) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = { showPassword: false };
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-06 09:45:21 +00:00
|
|
|
setFocusState = (isFocused: boolean) => {
|
|
|
|
|
this.props.onFocusChange(isFocused);
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-06 07:01:25 +00:00
|
|
|
onTextChange = (
|
|
|
|
|
event:
|
|
|
|
|
| React.ChangeEvent<HTMLInputElement>
|
|
|
|
|
| React.ChangeEvent<HTMLTextAreaElement>,
|
|
|
|
|
) => {
|
2019-10-31 05:28:11 +00:00
|
|
|
this.props.onValueChange(event.target.value);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onNumberChange = (valueAsNum: number, valueAsString: string) => {
|
|
|
|
|
this.props.onValueChange(valueAsString);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
isNumberInputType(inputType: InputType) {
|
|
|
|
|
return (
|
2019-11-05 05:09:50 +00:00
|
|
|
inputType === "INTEGER" ||
|
|
|
|
|
inputType === "NUMBER" ||
|
|
|
|
|
inputType === "CURRENCY"
|
2019-10-31 05:28:11 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getIcon(inputType: InputType) {
|
|
|
|
|
switch (inputType) {
|
|
|
|
|
case "PHONE_NUMBER":
|
|
|
|
|
return "phone";
|
|
|
|
|
case "SEARCH":
|
|
|
|
|
return "search";
|
|
|
|
|
case "EMAIL":
|
|
|
|
|
return "envelope";
|
|
|
|
|
default:
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getType(inputType: InputType) {
|
|
|
|
|
switch (inputType) {
|
|
|
|
|
case "PASSWORD":
|
2020-01-14 09:50:42 +00:00
|
|
|
return this.state.showPassword ? "text" : "password";
|
2019-10-31 05:28:11 +00:00
|
|
|
case "EMAIL":
|
|
|
|
|
return "email";
|
|
|
|
|
case "SEARCH":
|
|
|
|
|
return "search";
|
|
|
|
|
default:
|
|
|
|
|
return "text";
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-06 07:01:25 +00:00
|
|
|
private numericInputComponent = () => (
|
|
|
|
|
<NumericInput
|
2020-03-06 09:45:21 +00:00
|
|
|
value={this.props.value}
|
2020-02-06 07:01:25 +00:00
|
|
|
placeholder={this.props.placeholder}
|
|
|
|
|
min={this.props.minNum}
|
|
|
|
|
max={this.props.maxNum}
|
|
|
|
|
maxLength={this.props.maxChars}
|
|
|
|
|
disabled={this.props.disabled}
|
|
|
|
|
intent={this.props.intent}
|
|
|
|
|
className={this.props.isLoading ? "bp3-skeleton" : Classes.FILL}
|
|
|
|
|
onValueChange={this.onNumberChange}
|
|
|
|
|
leftIcon={
|
|
|
|
|
this.props.inputType === "PHONE_NUMBER" ? "phone" : this.props.leftIcon
|
|
|
|
|
}
|
|
|
|
|
type={this.props.inputType === "PHONE_NUMBER" ? "tel" : undefined}
|
|
|
|
|
allowNumericCharactersOnly
|
|
|
|
|
stepSize={this.props.stepSize}
|
2020-03-06 09:45:21 +00:00
|
|
|
onFocus={() => this.setFocusState(true)}
|
|
|
|
|
onBlur={() => this.setFocusState(false)}
|
2020-02-06 07:01:25 +00:00
|
|
|
/>
|
|
|
|
|
);
|
2020-03-13 07:24:03 +00:00
|
|
|
private textAreaInputComponent = () => (
|
2020-02-06 07:01:25 +00:00
|
|
|
<TextArea
|
2020-03-06 09:45:21 +00:00
|
|
|
value={this.props.value}
|
2020-02-06 07:01:25 +00:00
|
|
|
placeholder={this.props.placeholder}
|
|
|
|
|
disabled={this.props.disabled}
|
|
|
|
|
maxLength={this.props.maxChars}
|
|
|
|
|
intent={this.props.intent}
|
|
|
|
|
onChange={this.onTextChange}
|
|
|
|
|
className={this.props.isLoading ? "bp3-skeleton" : ""}
|
|
|
|
|
growVertically={false}
|
2020-03-06 09:45:21 +00:00
|
|
|
onFocus={() => this.setFocusState(true)}
|
|
|
|
|
onBlur={() => this.setFocusState(false)}
|
2020-02-06 07:01:25 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
private textInputComponent = (isTextArea: boolean) =>
|
|
|
|
|
isTextArea ? (
|
2020-03-13 07:24:03 +00:00
|
|
|
this.textAreaInputComponent()
|
2020-02-06 07:01:25 +00:00
|
|
|
) : (
|
|
|
|
|
<InputGroup
|
2020-03-06 09:45:21 +00:00
|
|
|
value={this.props.value}
|
2020-02-06 07:01:25 +00:00
|
|
|
placeholder={this.props.placeholder}
|
|
|
|
|
disabled={this.props.disabled}
|
|
|
|
|
maxLength={this.props.maxChars}
|
|
|
|
|
intent={this.props.intent}
|
|
|
|
|
onChange={this.onTextChange}
|
|
|
|
|
className={this.props.isLoading ? "bp3-skeleton" : ""}
|
|
|
|
|
rightElement={
|
|
|
|
|
this.props.inputType === "PASSWORD" ? (
|
|
|
|
|
<Button
|
|
|
|
|
icon={"lock"}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
this.setState({ showPassword: !this.state.showPassword });
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
undefined
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
type={this.getType(this.props.inputType)}
|
2020-03-06 09:45:21 +00:00
|
|
|
onFocus={() => this.setFocusState(true)}
|
|
|
|
|
onBlur={() => this.setFocusState(false)}
|
2020-02-06 07:01:25 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
private renderInputComponent = (inputType: InputType, isTextArea: boolean) =>
|
|
|
|
|
this.isNumberInputType(inputType)
|
|
|
|
|
? this.numericInputComponent()
|
|
|
|
|
: this.textInputComponent(isTextArea);
|
2019-10-31 05:28:11 +00:00
|
|
|
|
2019-10-30 10:23:20 +00:00
|
|
|
render() {
|
|
|
|
|
return (
|
2020-03-06 09:45:21 +00:00
|
|
|
<InputComponentWrapper
|
|
|
|
|
fill
|
|
|
|
|
multiline={this.props.multiline.toString()}
|
2020-03-13 07:24:03 +00:00
|
|
|
numeric={this.isNumberInputType(this.props.inputType)}
|
2020-03-06 09:45:21 +00:00
|
|
|
hasError={this.props.isInvalid}
|
|
|
|
|
>
|
2020-01-28 11:46:04 +00:00
|
|
|
{this.props.label && (
|
2020-01-31 11:13:16 +00:00
|
|
|
<Label
|
|
|
|
|
className={
|
|
|
|
|
this.props.isLoading
|
|
|
|
|
? Classes.SKELETON
|
|
|
|
|
: Classes.TEXT_OVERFLOW_ELLIPSIS
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
{this.props.label}
|
2020-01-28 11:46:04 +00:00
|
|
|
</Label>
|
|
|
|
|
)}
|
2020-03-06 09:45:21 +00:00
|
|
|
<ErrorTooltip
|
|
|
|
|
isOpen={this.props.isInvalid && this.props.showError}
|
|
|
|
|
message={
|
|
|
|
|
this.props.errorMessage || INPUT_WIDGET_DEFAULT_VALIDATION_ERROR
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
{this.renderInputComponent(
|
|
|
|
|
this.props.inputType,
|
|
|
|
|
this.props.multiline,
|
|
|
|
|
)}
|
|
|
|
|
</ErrorTooltip>
|
2019-11-13 07:00:25 +00:00
|
|
|
</InputComponentWrapper>
|
2019-10-30 10:23:20 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface InputComponentState {
|
|
|
|
|
showPassword?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface InputComponentProps extends ComponentProps {
|
2020-03-06 09:45:21 +00:00
|
|
|
value: string;
|
2019-10-31 05:28:11 +00:00
|
|
|
inputType: InputType;
|
2019-10-30 10:23:20 +00:00
|
|
|
disabled?: boolean;
|
|
|
|
|
intent?: Intent;
|
|
|
|
|
defaultValue?: string;
|
2019-10-31 05:28:11 +00:00
|
|
|
label: string;
|
2019-10-30 10:23:20 +00:00
|
|
|
leftIcon?: IconName;
|
|
|
|
|
allowNumericCharactersOnly?: boolean;
|
|
|
|
|
fill?: boolean;
|
2019-10-31 05:28:11 +00:00
|
|
|
errorMessage?: string;
|
|
|
|
|
maxChars?: number;
|
2019-10-30 10:23:20 +00:00
|
|
|
maxNum?: number;
|
|
|
|
|
minNum?: number;
|
2019-10-31 05:28:11 +00:00
|
|
|
onValueChange: (valueAsString: string) => void;
|
2019-10-30 10:23:20 +00:00
|
|
|
stepSize?: number;
|
|
|
|
|
placeholder?: string;
|
2019-12-03 04:41:10 +00:00
|
|
|
isLoading: boolean;
|
2020-02-13 09:32:24 +00:00
|
|
|
multiline: boolean;
|
2020-03-06 09:45:21 +00:00
|
|
|
isInvalid: boolean;
|
|
|
|
|
showError: boolean;
|
|
|
|
|
onFocusChange: (state: boolean) => void;
|
2019-10-30 10:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default InputComponent;
|