PromucFlow_constructor/app/client/src/components/editorComponents/EditableText.tsx

235 lines
5.9 KiB
TypeScript
Raw Normal View History

import React, { useState, useEffect, useRef, useCallback } from "react";
2020-02-21 12:16:49 +00:00
import {
EditableText as BlueprintEditableText,
Classes,
} from "@blueprintjs/core";
2020-01-27 08:24:58 +00:00
import styled from "styled-components";
2020-06-18 07:46:53 +00:00
import _ from "lodash";
import ErrorTooltip from "./ErrorTooltip";
2020-11-24 07:01:37 +00:00
import { Toaster } from "components/ads/Toast";
import { Variant } from "components/ads/common";
import Icon, { IconSize } from "components/ads/Icon";
2020-06-18 07:46:53 +00:00
export enum EditInteractionKind {
SINGLE,
DOUBLE,
}
2020-01-27 08:24:58 +00:00
type EditableTextProps = {
type: "text" | "password" | "email" | "phone" | "date";
defaultValue: string;
onTextChanged: (value: string) => void;
placeholder: string;
2020-03-30 14:21:21 +00:00
className?: string;
2020-06-18 07:46:53 +00:00
valueTransform?: (value: string) => string;
isEditingDefault?: boolean;
forceDefault?: boolean;
updating?: boolean;
isInvalid?: (value: string) => string | boolean;
editInteractionKind: EditInteractionKind;
hideEditIcon?: boolean;
minimal?: boolean;
onBlur?: (value?: string) => void;
beforeUnmount?: (value?: string) => void;
errorTooltipClass?: string;
maxLength?: number;
2022-05-10 12:08:08 +00:00
underline?: boolean;
2020-01-27 08:24:58 +00:00
};
const EditableTextWrapper = styled.div<{
isEditing: boolean;
minimal: boolean;
}>`
2020-01-27 08:24:58 +00:00
&& {
2020-06-18 07:46:53 +00:00
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: flex-start;
width: 100%;
2020-02-21 12:16:49 +00:00
& .${Classes.EDITABLE_TEXT} {
background: ${(props) =>
props.isEditing && !props.minimal
? props.theme.colors.editableText.bg
: "none"};
2020-01-27 08:24:58 +00:00
cursor: pointer;
2020-12-24 04:32:25 +00:00
padding: ${(props) => (!props.minimal ? "5px 5px" : "0px")};
2020-02-21 12:16:49 +00:00
text-transform: none;
2020-06-18 07:46:53 +00:00
flex: 1 0 100%;
max-width: 100%;
overflow: hidden;
2020-06-18 07:46:53 +00:00
display: flex;
2020-01-27 08:24:58 +00:00
&:before,
&:after {
display: none;
}
}
2020-02-21 12:16:49 +00:00
& div.${Classes.EDITABLE_TEXT_INPUT} {
text-transform: none;
2020-06-18 07:46:53 +00:00
width: 100%;
2020-02-21 12:16:49 +00:00
}
2020-01-27 08:24:58 +00:00
}
`;
// using the !important keyword here is mandatory because a style is being applied to that element using the style attribute
// which has higher specificity than other css selectors. It seems the overriding style is being applied by the package itself.
2022-05-10 12:08:08 +00:00
const TextContainer = styled.div<{
isValid: boolean;
minimal: boolean;
underline?: boolean;
}>`
2020-06-18 07:46:53 +00:00
display: flex;
&&&& .${Classes.EDITABLE_TEXT} {
& .${Classes.EDITABLE_TEXT_CONTENT} {
&:hover {
text-decoration: ${(props) => (props.minimal ? "underline" : "none")};
}
}
2020-06-18 07:46:53 +00:00
}
2022-05-10 12:08:08 +00:00
&&& .${Classes.EDITABLE_TEXT_CONTENT}:hover {
${(props) =>
props.underline
? `
border-bottom-style: solid;
border-bottom-width: 1px;
width: fit-content;
`
: null}
}
& span.bp3-editable-text-content {
height: auto !important;
}
2020-06-18 07:46:53 +00:00
`;
export function EditableText(props: EditableTextProps) {
const {
beforeUnmount,
className,
defaultValue,
editInteractionKind,
errorTooltipClass,
forceDefault,
hideEditIcon,
isEditingDefault,
isInvalid,
maxLength,
minimal,
onBlur,
onTextChanged,
placeholder,
updating,
valueTransform,
} = props;
const [isEditing, setIsEditing] = useState(!!isEditingDefault);
const [value, setStateValue] = useState(defaultValue);
const inputValRef = useRef("");
2020-12-30 09:03:24 +00:00
const setValue = useCallback((value) => {
inputValRef.current = value;
setStateValue(value);
}, []);
2020-06-18 07:46:53 +00:00
2020-01-27 08:24:58 +00:00
useEffect(() => {
setValue(defaultValue);
}, [defaultValue]);
useEffect(() => {
setIsEditing(!!isEditingDefault);
}, [defaultValue, isEditingDefault]);
2020-06-18 07:46:53 +00:00
useEffect(() => {
if (forceDefault === true) setValue(defaultValue);
}, [forceDefault, defaultValue]);
// at times onTextChange is not fired
// for example when the modal is closed on clicking the overlay
useEffect(() => {
return () => {
if (typeof beforeUnmount === "function")
beforeUnmount(inputValRef.current);
};
}, [beforeUnmount]);
2020-01-27 08:24:58 +00:00
const edit = (e: any) => {
setIsEditing(true);
e.preventDefault();
e.stopPropagation();
};
const onChange = useCallback(
(_value: string) => {
onBlur && onBlur();
const _isInvalid = isInvalid ? isInvalid(_value) : false;
if (!_isInvalid) {
onTextChanged(_value);
setIsEditing(false);
} else {
Toaster.show({
text: "Invalid name",
variant: Variant.danger,
});
}
},
[isInvalid],
);
2020-06-18 07:46:53 +00:00
const onInputchange = useCallback(
(_value: string) => {
let finalVal: string = _value;
if (valueTransform) {
finalVal = valueTransform(_value);
}
setValue(finalVal);
},
[valueTransform],
);
2020-06-18 07:46:53 +00:00
const errorMessage = isInvalid && isInvalid(value);
2020-06-18 07:46:53 +00:00
const error = errorMessage ? errorMessage : undefined;
2020-01-27 08:24:58 +00:00
return (
2020-06-18 07:46:53 +00:00
<EditableTextWrapper
isEditing={isEditing}
minimal={!!minimal}
2020-06-18 07:46:53 +00:00
onClick={
editInteractionKind === EditInteractionKind.SINGLE ? edit : _.noop
2020-06-18 07:46:53 +00:00
}
onDoubleClick={
editInteractionKind === EditInteractionKind.DOUBLE ? edit : _.noop
}
2020-06-18 07:46:53 +00:00
>
<ErrorTooltip
customClass={errorTooltipClass}
isOpen={!!error}
message={errorMessage as string}
>
2022-05-10 12:08:08 +00:00
<TextContainer
isValid={!error}
minimal={!!minimal}
underline={props.underline}
>
2020-06-18 07:46:53 +00:00
<BlueprintEditableText
className={className}
2020-06-18 07:46:53 +00:00
disabled={!isEditing}
isEditing={isEditing}
maxLength={maxLength}
onCancel={onBlur}
2020-06-18 07:46:53 +00:00
onChange={onInputchange}
onConfirm={onChange}
placeholder={placeholder}
2020-06-18 07:46:53 +00:00
selectAllOnFocus
value={value}
/>
{!minimal && !hideEditIcon && !updating && !isEditing && (
<Icon
className="t--action-name-edit-icon"
fillColor="#939090"
name="edit"
size={IconSize.XXL}
/>
)}
2020-06-18 07:46:53 +00:00
</TextContainer>
</ErrorTooltip>
2020-01-27 08:24:58 +00:00
</EditableTextWrapper>
);
}
2020-01-27 08:24:58 +00:00
export default EditableText;