2021-07-15 10:56:18 +00:00
|
|
|
import React, { useState, useCallback } from "react";
|
|
|
|
|
|
|
|
|
|
import styled from "styled-components";
|
2020-08-26 09:04:58 +00:00
|
|
|
import { noop } from "lodash";
|
|
|
|
|
|
2021-07-15 10:56:18 +00:00
|
|
|
import { CommonComponentProps } from "./common";
|
|
|
|
|
import EditableTextSubComponent, {
|
|
|
|
|
EditInteractionKind,
|
|
|
|
|
SavingState,
|
|
|
|
|
} from "./EditableTextSubComponent";
|
2020-08-26 09:04:58 +00:00
|
|
|
|
2021-07-15 10:56:18 +00:00
|
|
|
export { EditInteractionKind, SavingState };
|
2020-08-26 09:04:58 +00:00
|
|
|
|
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;
|
2021-02-11 12:54:00 +00:00
|
|
|
onBlur?: (value: string) => void;
|
2020-10-31 06:40:51 +00:00
|
|
|
onTextChanged?: (value: string) => void;
|
2020-08-26 09:04:58 +00:00
|
|
|
valueTransform?: (value: string) => string;
|
|
|
|
|
isEditingDefault?: boolean;
|
|
|
|
|
forceDefault?: boolean;
|
|
|
|
|
updating?: boolean;
|
|
|
|
|
isInvalid?: (value: string) => string | boolean;
|
|
|
|
|
hideEditIcon?: boolean;
|
|
|
|
|
fill?: boolean;
|
2021-02-11 12:54:00 +00:00
|
|
|
underline?: boolean;
|
2021-06-24 06:32:09 +00:00
|
|
|
isError?: boolean;
|
2020-08-26 09:04:58 +00:00
|
|
|
};
|
|
|
|
|
|
2021-02-22 05:32:19 +00:00
|
|
|
export const EditableTextWrapper = styled.div<{
|
2021-02-16 10:29:08 +00:00
|
|
|
filled: boolean;
|
2020-08-26 09:04:58 +00:00
|
|
|
}>`
|
2021-02-22 05:32:19 +00:00
|
|
|
${(props) =>
|
|
|
|
|
!props.filled
|
|
|
|
|
? `
|
|
|
|
|
width: 243px;
|
|
|
|
|
`
|
|
|
|
|
: `
|
|
|
|
|
width: 100%;
|
|
|
|
|
flex: 1;
|
|
|
|
|
`}
|
2020-10-31 06:40:51 +00:00
|
|
|
.error-message {
|
2020-12-24 04:32:25 +00:00
|
|
|
margin-left: ${(props) => props.theme.spaces[5]}px;
|
|
|
|
|
color: ${(props) => props.theme.colors.danger.main};
|
2020-08-26 09:04:58 +00:00
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2021-04-28 10:28:39 +00:00
|
|
|
export function EditableText(props: EditableTextProps) {
|
2020-10-12 07:15:35 +00:00
|
|
|
const {
|
|
|
|
|
defaultValue,
|
|
|
|
|
isEditingDefault,
|
2021-05-13 08:35:39 +00:00
|
|
|
isInvalid: inputValidation,
|
2021-07-15 10:56:18 +00:00
|
|
|
savingState: defaultSavingState,
|
|
|
|
|
...others
|
2020-10-12 07:15:35 +00:00
|
|
|
} = props;
|
|
|
|
|
const [isEditing, setIsEditing] = useState(!!isEditingDefault);
|
2020-08-26 09:04:58 +00:00
|
|
|
const [isInvalid, setIsInvalid] = useState<string | boolean>(false);
|
2020-09-16 11:50:47 +00:00
|
|
|
const [savingState, setSavingState] = useState<SavingState>(
|
|
|
|
|
SavingState.NOT_STARTED,
|
|
|
|
|
);
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
2021-02-16 10:29:08 +00:00
|
|
|
filled={!!props.fill}
|
2021-04-28 10:28:39 +00:00
|
|
|
onClick={
|
|
|
|
|
props.editInteractionKind === EditInteractionKind.SINGLE
|
2020-08-26 09:04:58 +00:00
|
|
|
? editMode
|
|
|
|
|
: noop
|
|
|
|
|
}
|
2021-04-28 10:28:39 +00:00
|
|
|
onDoubleClick={
|
|
|
|
|
props.editInteractionKind === EditInteractionKind.DOUBLE
|
2020-08-26 09:04:58 +00:00
|
|
|
? editMode
|
|
|
|
|
: noop
|
|
|
|
|
}
|
2021-04-28 10:28:39 +00:00
|
|
|
onMouseEnter={nonEditMode}
|
2020-08-26 09:04:58 +00:00
|
|
|
>
|
2021-07-15 10:56:18 +00:00
|
|
|
<EditableTextSubComponent
|
|
|
|
|
defaultSavingState={defaultSavingState}
|
|
|
|
|
defaultValue={defaultValue}
|
|
|
|
|
inputValidation={inputValidation}
|
2020-08-26 09:04:58 +00:00
|
|
|
isEditing={isEditing}
|
2021-07-15 10:56:18 +00:00
|
|
|
isEditingDefault={isEditingDefault}
|
|
|
|
|
isInvalid={isInvalid}
|
|
|
|
|
savingState={savingState}
|
|
|
|
|
setIsEditing={setIsEditing}
|
|
|
|
|
setIsInvalid={setIsInvalid}
|
|
|
|
|
setSavingState={setSavingState}
|
|
|
|
|
{...others}
|
|
|
|
|
/>
|
2020-08-26 09:04:58 +00:00
|
|
|
</EditableTextWrapper>
|
|
|
|
|
);
|
2021-04-28 10:28:39 +00:00
|
|
|
}
|
2020-08-26 09:04:58 +00:00
|
|
|
|
2020-08-28 10:51:41 +00:00
|
|
|
export default EditableText;
|