2020-08-26 09:04:58 +00:00
|
|
|
import React, { useState, useEffect, useMemo, useCallback } from "react";
|
2020-11-25 12:24:14 +00:00
|
|
|
import {
|
|
|
|
|
EditableText as BlueprintEditableText,
|
|
|
|
|
Classes as BlueprintClasses,
|
|
|
|
|
} from "@blueprintjs/core";
|
2020-08-26 09:04:58 +00:00
|
|
|
import styled from "styled-components";
|
|
|
|
|
import Text, { TextType } from "./Text";
|
|
|
|
|
import Spinner from "./Spinner";
|
2020-09-23 14:06:50 +00:00
|
|
|
import { Classes, CommonComponentProps } from "./common";
|
2020-08-26 09:04:58 +00:00
|
|
|
import { noop } from "lodash";
|
2020-09-01 07:24:53 +00:00
|
|
|
import Icon, { IconSize } from "./Icon";
|
2020-09-16 11:50:47 +00:00
|
|
|
import { getThemeDetails } from "selectors/themeSelectors";
|
|
|
|
|
import { useSelector } from "react-redux";
|
2020-08-26 09:04:58 +00:00
|
|
|
|
|
|
|
|
export enum EditInteractionKind {
|
|
|
|
|
SINGLE = "SINGLE",
|
|
|
|
|
DOUBLE = "DOUBLE",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export enum SavingState {
|
2020-09-16 11:50:47 +00:00
|
|
|
STARTED = "STARTED",
|
2020-08-26 09:04:58 +00:00
|
|
|
NOT_STARTED = "NOT_STARTED",
|
|
|
|
|
SUCCESS = "SUCCESS",
|
|
|
|
|
ERROR = "ERROR",
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-31 06:40:51 +00:00
|
|
|
export type EditableTextProps = CommonComponentProps & {
|
2020-08-26 09:04:58 +00:00
|
|
|
defaultValue: string;
|
2020-10-31 06:40:51 +00:00
|
|
|
placeholder?: string;
|
|
|
|
|
editInteractionKind: EditInteractionKind;
|
|
|
|
|
savingState: SavingState;
|
|
|
|
|
onBlur: (value: string) => void;
|
|
|
|
|
onTextChanged?: (value: string) => void;
|
2020-08-26 09:04:58 +00:00
|
|
|
className?: string;
|
|
|
|
|
valueTransform?: (value: string) => string;
|
|
|
|
|
isEditingDefault?: boolean;
|
|
|
|
|
forceDefault?: boolean;
|
|
|
|
|
updating?: boolean;
|
|
|
|
|
isInvalid?: (value: string) => string | boolean;
|
|
|
|
|
hideEditIcon?: boolean;
|
|
|
|
|
fill?: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const EditableTextWrapper = styled.div<{
|
|
|
|
|
fill?: boolean;
|
|
|
|
|
}>`
|
|
|
|
|
width: ${props => (!props.fill ? "234px" : "100%")};
|
2020-10-31 06:40:51 +00:00
|
|
|
.error-message {
|
2020-08-28 10:51:41 +00:00
|
|
|
margin-left: ${props => props.theme.spaces[5]}px;
|
2020-08-26 09:04:58 +00:00
|
|
|
color: ${props => props.theme.colors.danger.main};
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const editModeBgcolor = (
|
|
|
|
|
isInvalid: boolean,
|
|
|
|
|
isEditing: boolean,
|
2020-09-16 11:50:47 +00:00
|
|
|
savingState: SavingState,
|
|
|
|
|
theme: any,
|
2020-08-26 09:04:58 +00:00
|
|
|
): string => {
|
2020-09-16 11:50:47 +00:00
|
|
|
if ((isInvalid && isEditing) || savingState === SavingState.ERROR) {
|
2020-09-23 14:06:50 +00:00
|
|
|
return theme.colors.editableText.dangerBg;
|
2020-08-26 09:04:58 +00:00
|
|
|
} else if (!isInvalid && isEditing) {
|
2020-09-23 14:06:50 +00:00
|
|
|
return theme.colors.editableText.bg;
|
2020-08-26 09:04:58 +00:00
|
|
|
} else {
|
|
|
|
|
return "transparent";
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const TextContainer = styled.div<{
|
|
|
|
|
isInvalid: boolean;
|
|
|
|
|
isEditing: boolean;
|
|
|
|
|
bgColor: string;
|
|
|
|
|
}>`
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
.bp3-editable-text.bp3-editable-text-editing::before,
|
|
|
|
|
.bp3-editable-text.bp3-disabled::before {
|
|
|
|
|
display: none;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-25 12:24:14 +00:00
|
|
|
&&&
|
|
|
|
|
.${BlueprintClasses.EDITABLE_TEXT_CONTENT},
|
|
|
|
|
&&&
|
|
|
|
|
.${BlueprintClasses.EDITABLE_TEXT_INPUT} {
|
2020-08-26 09:04:58 +00:00
|
|
|
font-size: ${props => props.theme.typography.p1.fontSize}px;
|
|
|
|
|
line-height: ${props => props.theme.typography.p1.lineHeight}px;
|
|
|
|
|
letter-spacing: ${props => props.theme.typography.p1.letterSpacing}px;
|
|
|
|
|
font-weight: ${props => props.theme.typography.p1.fontWeight}px;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-25 12:24:14 +00:00
|
|
|
&&& .${BlueprintClasses.EDITABLE_TEXT_CONTENT} {
|
2020-08-26 09:04:58 +00:00
|
|
|
cursor: pointer;
|
2020-09-23 14:06:50 +00:00
|
|
|
color: ${props => props.theme.colors.editableText.color};
|
2020-08-26 09:04:58 +00:00
|
|
|
overflow: hidden;
|
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
|
${props => (props.isEditing ? "display: none" : "display: block")};
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-25 12:24:14 +00:00
|
|
|
&&& .${BlueprintClasses.EDITABLE_TEXT_INPUT} {
|
2020-08-26 09:04:58 +00:00
|
|
|
border: none;
|
|
|
|
|
outline: none;
|
|
|
|
|
height: ${props => props.theme.spaces[13] + 3}px;
|
2020-09-23 14:06:50 +00:00
|
|
|
color: ${props => props.theme.colors.editableText.color};
|
2020-08-26 09:04:58 +00:00
|
|
|
min-width: 100%;
|
|
|
|
|
border-radius: ${props => props.theme.spaces[0]}px;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-25 12:24:14 +00:00
|
|
|
&&& .${BlueprintClasses.EDITABLE_TEXT} {
|
2020-08-26 09:04:58 +00:00
|
|
|
overflow: hidden;
|
2020-09-16 11:50:47 +00:00
|
|
|
height: ${props => props.theme.spaces[13] + 3}px;
|
2020-08-26 09:04:58 +00:00
|
|
|
padding: ${props => props.theme.spaces[4]}px
|
|
|
|
|
${props => props.theme.spaces[5]}px;
|
|
|
|
|
width: calc(100% - 40px);
|
|
|
|
|
background-color: ${props => props.bgColor};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.icon-wrapper {
|
|
|
|
|
background-color: ${props => props.bgColor};
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const IconWrapper = styled.div`
|
|
|
|
|
width: ${props => props.theme.spaces[13] + 4}px;
|
|
|
|
|
padding-right: ${props => props.theme.spaces[5]}px;
|
|
|
|
|
height: ${props => props.theme.spaces[13] + 3}px;
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: flex-end;
|
|
|
|
|
`;
|
|
|
|
|
|
2020-08-28 10:51:41 +00:00
|
|
|
export const EditableText = (props: EditableTextProps) => {
|
2020-10-12 07:15:35 +00:00
|
|
|
const {
|
|
|
|
|
onBlur,
|
|
|
|
|
onTextChanged,
|
|
|
|
|
isInvalid: inputValidation,
|
|
|
|
|
defaultValue,
|
|
|
|
|
isEditingDefault,
|
|
|
|
|
} = props;
|
|
|
|
|
const [isEditing, setIsEditing] = useState(!!isEditingDefault);
|
|
|
|
|
const [value, setValue] = useState(defaultValue);
|
|
|
|
|
const [lastValidValue, setLastValidValue] = useState(defaultValue);
|
2020-08-26 09:04:58 +00:00
|
|
|
const [isInvalid, setIsInvalid] = useState<string | boolean>(false);
|
|
|
|
|
const [changeStarted, setChangeStarted] = useState<boolean>(false);
|
2020-09-16 11:50:47 +00:00
|
|
|
const [savingState, setSavingState] = useState<SavingState>(
|
|
|
|
|
SavingState.NOT_STARTED,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setSavingState(props.savingState);
|
2020-10-06 06:02:43 +00:00
|
|
|
}, [props.savingState]);
|
2020-08-26 09:04:58 +00:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2020-10-12 07:15:35 +00:00
|
|
|
setValue(defaultValue);
|
|
|
|
|
setIsEditing(!!isEditingDefault);
|
|
|
|
|
}, [defaultValue, isEditingDefault]);
|
2020-08-26 09:04:58 +00:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2020-10-12 07:15:35 +00:00
|
|
|
if (props.forceDefault === true) setValue(defaultValue);
|
|
|
|
|
}, [props.forceDefault, defaultValue]);
|
2020-08-26 09:04:58 +00:00
|
|
|
|
2020-09-16 11:50:47 +00:00
|
|
|
const themeDetails = useSelector(getThemeDetails);
|
2020-08-26 09:04:58 +00:00
|
|
|
const bgColor = useMemo(
|
2020-09-16 11:50:47 +00:00
|
|
|
() =>
|
|
|
|
|
editModeBgcolor(!!isInvalid, isEditing, savingState, themeDetails.theme),
|
2020-09-22 11:56:11 +00:00
|
|
|
[isInvalid, isEditing, savingState, themeDetails],
|
2020-08-26 09:04:58 +00:00
|
|
|
);
|
|
|
|
|
|
2020-08-31 13:04:34 +00:00
|
|
|
const editMode = useCallback(
|
|
|
|
|
(e: React.MouseEvent) => {
|
|
|
|
|
setIsEditing(true);
|
2020-10-12 07:15:35 +00:00
|
|
|
const errorMessage = inputValidation && inputValidation(defaultValue);
|
2020-08-31 13:04:34 +00:00
|
|
|
setIsInvalid(errorMessage ? errorMessage : false);
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
},
|
2020-10-12 07:15:35 +00:00
|
|
|
[inputValidation, defaultValue],
|
2020-08-31 13:04:34 +00:00
|
|
|
);
|
2020-08-26 09:04:58 +00:00
|
|
|
|
2020-09-28 05:06:06 +00:00
|
|
|
const onConfirm = useCallback(
|
|
|
|
|
(_value: string) => {
|
2020-10-31 06:40:51 +00:00
|
|
|
if (savingState === SavingState.ERROR || isInvalid || _value === "") {
|
2020-09-28 05:06:06 +00:00
|
|
|
setValue(lastValidValue);
|
2020-10-12 07:15:35 +00:00
|
|
|
onBlur(lastValidValue);
|
2020-09-28 05:06:06 +00:00
|
|
|
setSavingState(SavingState.NOT_STARTED);
|
|
|
|
|
} else if (changeStarted) {
|
2020-10-31 06:40:51 +00:00
|
|
|
onTextChanged && onTextChanged(_value);
|
2020-09-28 05:06:06 +00:00
|
|
|
}
|
2020-12-01 22:00:29 +00:00
|
|
|
if (_value !== defaultValue) {
|
|
|
|
|
onBlur(_value);
|
|
|
|
|
}
|
2020-09-28 05:06:06 +00:00
|
|
|
setIsEditing(false);
|
|
|
|
|
setChangeStarted(false);
|
|
|
|
|
},
|
2020-10-12 07:15:35 +00:00
|
|
|
[
|
|
|
|
|
changeStarted,
|
|
|
|
|
savingState,
|
|
|
|
|
isInvalid,
|
|
|
|
|
lastValidValue,
|
|
|
|
|
onBlur,
|
|
|
|
|
onTextChanged,
|
|
|
|
|
],
|
2020-09-28 05:06:06 +00:00
|
|
|
);
|
2020-08-26 09:04:58 +00:00
|
|
|
|
2020-08-31 13:04:34 +00:00
|
|
|
const onInputchange = useCallback(
|
|
|
|
|
(_value: string) => {
|
2020-09-16 11:50:47 +00:00
|
|
|
const finalVal: string = _value;
|
2020-10-12 07:15:35 +00:00
|
|
|
const errorMessage = inputValidation && inputValidation(finalVal);
|
2020-08-31 13:04:34 +00:00
|
|
|
const error = errorMessage ? errorMessage : false;
|
2020-10-31 06:40:51 +00:00
|
|
|
if (!error && _value !== "") {
|
2020-08-31 13:04:34 +00:00
|
|
|
setLastValidValue(finalVal);
|
2020-10-31 06:40:51 +00:00
|
|
|
onTextChanged && onTextChanged(finalVal);
|
2020-08-31 13:04:34 +00:00
|
|
|
}
|
2020-09-16 11:50:47 +00:00
|
|
|
setValue(finalVal);
|
2020-08-31 13:04:34 +00:00
|
|
|
setIsInvalid(error);
|
|
|
|
|
setChangeStarted(true);
|
|
|
|
|
},
|
2020-10-14 10:35:19 +00:00
|
|
|
[inputValidation, onTextChanged],
|
2020-08-31 13:04:34 +00:00
|
|
|
);
|
2020-08-26 09:04:58 +00:00
|
|
|
|
|
|
|
|
const iconName =
|
2020-09-16 11:50:47 +00:00
|
|
|
!isEditing && savingState === SavingState.NOT_STARTED && !props.hideEditIcon
|
2020-09-01 07:24:53 +00:00
|
|
|
? "edit"
|
2020-09-16 11:50:47 +00:00
|
|
|
: !isEditing && savingState === SavingState.SUCCESS
|
2020-09-01 07:24:53 +00:00
|
|
|
? "success"
|
2020-09-16 11:50:47 +00:00
|
|
|
: savingState === SavingState.ERROR || (isEditing && !!isInvalid)
|
2020-09-01 07:24:53 +00:00
|
|
|
? "error"
|
2020-08-26 09:04:58 +00:00
|
|
|
: undefined;
|
|
|
|
|
|
|
|
|
|
const nonEditMode = () => {
|
2020-09-16 11:50:47 +00:00
|
|
|
if (!isEditing && savingState === SavingState.SUCCESS) {
|
|
|
|
|
setSavingState(SavingState.NOT_STARTED);
|
2020-08-26 09:04:58 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<EditableTextWrapper
|
|
|
|
|
fill={props.fill}
|
|
|
|
|
onMouseEnter={nonEditMode}
|
|
|
|
|
onDoubleClick={
|
|
|
|
|
props.editInteractionKind === EditInteractionKind.DOUBLE
|
|
|
|
|
? editMode
|
|
|
|
|
: noop
|
|
|
|
|
}
|
|
|
|
|
onClick={
|
|
|
|
|
props.editInteractionKind === EditInteractionKind.SINGLE
|
|
|
|
|
? editMode
|
|
|
|
|
: noop
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<TextContainer
|
2020-11-05 05:57:27 +00:00
|
|
|
className="editable-text-container"
|
2020-10-14 10:35:19 +00:00
|
|
|
data-cy={props.cypressSelector}
|
2020-08-26 09:04:58 +00:00
|
|
|
isInvalid={!!isInvalid}
|
|
|
|
|
isEditing={isEditing}
|
|
|
|
|
bgColor={bgColor}
|
|
|
|
|
>
|
|
|
|
|
<BlueprintEditableText
|
|
|
|
|
disabled={!isEditing}
|
|
|
|
|
isEditing={isEditing}
|
|
|
|
|
onChange={onInputchange}
|
|
|
|
|
onConfirm={onConfirm}
|
|
|
|
|
value={value}
|
2020-10-31 06:40:51 +00:00
|
|
|
selectAllOnFocus={true}
|
|
|
|
|
placeholder={props.placeholder || defaultValue}
|
2020-08-26 09:04:58 +00:00
|
|
|
className={props.className}
|
|
|
|
|
onCancel={onConfirm}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<IconWrapper className="icon-wrapper">
|
2020-09-16 11:50:47 +00:00
|
|
|
{savingState === SavingState.STARTED ? (
|
2020-08-31 04:59:18 +00:00
|
|
|
<Spinner size={IconSize.XL} />
|
2020-10-31 06:40:51 +00:00
|
|
|
) : value ? (
|
2020-08-31 04:59:18 +00:00
|
|
|
<Icon name={iconName} size={IconSize.XL} />
|
2020-10-31 06:40:51 +00:00
|
|
|
) : null}
|
2020-08-26 09:04:58 +00:00
|
|
|
</IconWrapper>
|
|
|
|
|
</TextContainer>
|
|
|
|
|
{isEditing && !!isInvalid ? (
|
2020-10-31 06:40:51 +00:00
|
|
|
<Text className="error-message" type={TextType.P2}>
|
|
|
|
|
{isInvalid}
|
|
|
|
|
</Text>
|
2020-08-26 09:04:58 +00:00
|
|
|
) : null}
|
|
|
|
|
</EditableTextWrapper>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2020-08-28 10:51:41 +00:00
|
|
|
EditableText.defaultProps = {
|
2020-08-26 09:04:58 +00:00
|
|
|
fill: false,
|
|
|
|
|
};
|
|
|
|
|
|
2020-08-28 10:51:41 +00:00
|
|
|
export default EditableText;
|