PromucFlow_constructor/app/client/src/components/propertyControls/InputTextControl.tsx

129 lines
3.4 KiB
TypeScript
Raw Normal View History

2020-05-18 06:47:45 +00:00
import React, { lazy, Suspense } from "react";
import styled from "styled-components";
import BaseControl, { ControlProps } from "./BaseControl";
import { StyledDynamicInput } from "./StyledControls";
2019-11-25 05:07:27 +00:00
import { InputType } from "widgets/InputWidget";
2019-12-06 13:16:08 +00:00
import DynamicAutocompleteInput from "components/editorComponents/DynamicAutocompleteInput";
2020-05-19 11:27:42 +00:00
import CodeMirror from "codemirror";
2020-05-18 06:47:45 +00:00
const LightningMenu = lazy(() =>
import("components/editorComponents/LightningMenu"),
);
const InputControlWrapper = styled.div`
width: 100%;
position: relative;
& > span:first-of-type {
position: absolute;
right: 0;
top: 2px;
width: 14px;
z-index: 10;
}
`;
2020-05-19 11:27:42 +00:00
2020-01-23 07:53:36 +00:00
export function InputText(props: {
label: string;
value: string;
onChange: (event: React.ChangeEvent<HTMLTextAreaElement> | string) => void;
isValid: boolean;
validationMessage?: string;
2020-03-16 15:42:39 +00:00
placeholder?: string;
2020-05-19 06:53:37 +00:00
innerRef?: React.Ref<HTMLTextAreaElement>;
2020-01-23 07:53:36 +00:00
}) {
2020-03-16 15:42:39 +00:00
const { validationMessage, value, isValid, onChange, placeholder } = props;
2020-05-19 06:53:37 +00:00
console.log("ref inside InputText", props.innerRef);
2020-01-23 07:53:36 +00:00
return (
<StyledDynamicInput>
<DynamicAutocompleteInput
input={{
value: value,
onChange: onChange,
}}
meta={{
error: isValid ? "" : validationMessage,
touched: true,
}}
theme={"DARK"}
singleLine={false}
2020-03-16 15:42:39 +00:00
placeholder={placeholder}
2020-05-19 06:53:37 +00:00
forwardRef={props.innerRef as React.RefObject<HTMLTextAreaElement>}
/>
</StyledDynamicInput>
2020-01-23 07:53:36 +00:00
);
}
class InputTextControl extends BaseControl<InputControlProps> {
2020-05-19 06:53:37 +00:00
private inputElement: React.RefObject<HTMLTextAreaElement> = React.createRef<
HTMLTextAreaElement
>();
updatePropertyValue = (value: string) => {
console.log(this.inputElement);
if (this.inputElement.current) {
console.log(this.inputElement.current);
2020-05-19 11:27:42 +00:00
const editor = CodeMirror.fromTextArea(this.inputElement.current);
console.log("editor", editor);
editor.setValue("value");
2020-05-19 06:53:37 +00:00
}
};
render() {
2020-03-16 15:42:39 +00:00
const {
validationMessage,
propertyValue,
isValid,
label,
placeholderText,
} = this.props;
2020-05-19 06:53:37 +00:00
console.log("ref inside InputTextControl", this.inputElement);
return (
2020-05-18 06:47:45 +00:00
<InputControlWrapper>
<Suspense fallback={<div />}>
2020-05-19 06:53:37 +00:00
<LightningMenu updatePropertyValue={this.updatePropertyValue} />
2020-05-18 06:47:45 +00:00
</Suspense>
<InputText
label={label}
value={propertyValue}
onChange={this.onTextChange}
isValid={isValid}
validationMessage={validationMessage}
placeholder={placeholderText}
2020-05-19 06:53:37 +00:00
innerRef={this.inputElement}
2020-05-18 06:47:45 +00:00
/>
</InputControlWrapper>
);
}
2019-11-19 12:44:58 +00:00
isNumberType(): boolean {
const { inputType } = this.props;
2019-10-31 05:28:11 +00:00
switch (inputType) {
case "CURRENCY":
case "INTEGER":
case "NUMBER":
case "PHONE_NUMBER":
return true;
default:
return false;
}
}
2019-12-30 07:35:16 +00:00
onTextChange = (event: React.ChangeEvent<HTMLTextAreaElement> | string) => {
2019-12-06 13:16:08 +00:00
let value = event;
if (typeof event !== "string") {
value = event.target.value;
}
2019-11-14 14:26:23 +00:00
this.updateProperty(this.props.propertyName, value);
};
static getControlType() {
return "INPUT_TEXT";
}
}
export interface InputControlProps extends ControlProps {
placeholderText: string;
2019-11-19 12:44:58 +00:00
inputType: InputType;
isDisabled?: boolean;
}
export default InputTextControl;