DocSpace-client/packages/components/textarea/index.js

203 lines
5.1 KiB
JavaScript
Raw Normal View History

2023-01-20 15:46:39 +00:00
import React, { useRef, useState, useEffect } from "react";
import PropTypes from "prop-types";
2023-01-20 15:46:39 +00:00
import {
StyledTextarea,
StyledScrollbar,
StyledCopyIcon,
Wrapper,
Numeration,
2023-01-20 15:46:39 +00:00
} from "./styled-textarea";
2022-08-12 15:07:44 +00:00
import { ColorTheme, ThemeType } from "@docspace/common/components/ColorTheme";
2023-01-22 11:45:41 +00:00
import Toast from "@docspace/components/toast";
import toastr from "@docspace/components/toast/toastr";
2023-01-20 15:46:39 +00:00
import { isJSON, beautifyJSON } from "./utils";
import copy from "copy-to-clipboard";
// eslint-disable-next-line react/prop-types, no-unused-vars
const Textarea = ({
className,
id,
isDisabled,
isReadOnly,
hasError,
heightScale,
maxLength,
name,
onChange,
placeholder,
style,
tabIndex,
value,
fontSize,
heightTextArea,
color,
theme,
autoFocus,
areaSelect,
2023-01-20 15:46:39 +00:00
isJSONField,
copyInfoText,
}) => {
const areaRef = useRef(null);
2023-01-20 15:46:39 +00:00
const [isError, setIsError] = useState(hasError);
const [modifiedValue, setModifiedValue] = useState(value);
const lineHeight = 1.5;
const padding = 7;
const numberOfLines = modifiedValue.split("\n").length;
const textareaHeight = isJSONField
? numberOfLines * fontSize * lineHeight + padding + 4
: heightTextArea;
const defaultPaddingLeft = 42;
const numberOfDigits =
String(numberOfLines).length - 2 > 0 ? String(numberOfLines).length : 0;
const paddingLeftProp = isJSONField
? fontSize < 13
? `${defaultPaddingLeft + numberOfDigits * 6}px`
: `${((defaultPaddingLeft + numberOfDigits * 4) * fontSize) / 13}px`
: "8px";
const numerationValue = [];
for (let i = 1; i <= numberOfLines; i++) {
numerationValue.push(i);
}
2023-02-13 11:36:56 +00:00
function onTextareaClick() {
areaRef.current.select();
}
2023-01-20 15:46:39 +00:00
useEffect(() => {
if (isJSONField) {
if (modifiedValue && isJSON(modifiedValue)) {
setModifiedValue(beautifyJSON(modifiedValue));
} else {
setIsError(true);
}
}
}, [isJSONField]);
2023-01-20 15:46:39 +00:00
useEffect(() => {
if (areaSelect && areaRef.current) {
areaRef.current.select();
}
}, [areaSelect]);
return (
2023-02-13 11:36:56 +00:00
<Wrapper
isJSONField={isJSONField}
onFocus={isJSONField ? onTextareaClick : undefined}
>
2023-01-20 15:46:39 +00:00
{isJSONField && (
2023-01-22 11:45:41 +00:00
<StyledCopyIcon
onClick={() => {
copy(modifiedValue);
toastr.success(copyInfoText);
}}
heightScale={heightScale}
isJSONField={isJSONField}
2023-01-22 11:45:41 +00:00
/>
2023-01-20 15:46:39 +00:00
)}
<ColorTheme
themeId={ThemeType.Textarea}
className={className}
style={style}
stype="preMediumBlack"
2019-12-17 10:49:53 +00:00
isDisabled={isDisabled}
hasError={isError}
heightScale={heightScale}
heighttextarea={textareaHeight}
>
<Toast />
{isJSONField && (
<Numeration fontSize={fontSize}>
{numerationValue.join("\n")}
</Numeration>
)}
<StyledTextarea
id={id}
paddingLeftProp={paddingLeftProp}
2023-02-13 11:36:56 +00:00
isJSONField={isJSONField}
placeholder={placeholder}
onChange={(e) => onChange && onChange(e)}
maxLength={maxLength}
name={name}
tabIndex={tabIndex}
isDisabled={isDisabled}
disabled={isDisabled}
readOnly={isReadOnly}
value={isJSONField ? modifiedValue : value}
fontSize={fontSize}
color={color}
autoFocus={autoFocus}
ref={areaRef}
heighttextarea={heightTextArea}
/>
</ColorTheme>
</Wrapper>
);
};
Textarea.propTypes = {
/** Class name */
2019-12-17 10:49:53 +00:00
className: PropTypes.string,
/** Used as HTML `id` property */
id: PropTypes.string,
/** Indicates that the field cannot be used */
isDisabled: PropTypes.bool,
/** Indicates that the field is displaying read-only content */
isReadOnly: PropTypes.bool,
/** Indicates the input field has an error */
2020-07-08 13:57:34 +00:00
hasError: PropTypes.bool,
/** Indicates the input field has scale */
2020-07-13 11:15:44 +00:00
heightScale: PropTypes.bool,
/** Max Length of value */
maxLength: PropTypes.number,
/** Used as HTML `name` property */
name: PropTypes.string,
/** Allow you to handle changing events of component */
onChange: PropTypes.func,
/** Placeholder for Textarea */
placeholder: PropTypes.string,
/** Accepts css style */
2019-12-17 10:49:53 +00:00
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
/** Used as HTML `tabindex` property */
tabIndex: PropTypes.number,
/** Value for Textarea */
value: PropTypes.string,
/** Value for font-size */
fontSize: PropTypes.number,
/** Value for height text-area */
heightTextArea: PropTypes.number,
/** Specifies the text color */
color: PropTypes.string,
2022-05-20 11:07:46 +00:00
autoFocus: PropTypes.bool,
areaSelect: PropTypes.bool,
2023-01-20 15:46:39 +00:00
/** Prettifies Json and adds lines numeration */
isJSONField: PropTypes.bool,
/** Indicates the text of toast/informational alarm */
copyInfoText: PropTypes.string,
};
Textarea.defaultProps = {
className: "",
isDisabled: false,
isReadOnly: false,
2020-07-08 13:57:34 +00:00
hasError: false,
2020-07-13 11:15:44 +00:00
heightScale: false,
placeholder: "",
tabIndex: -1,
value: "",
fontSize: 13,
2022-05-20 11:07:46 +00:00
isAutoFocussed: false,
areaSelect: false,
2023-01-20 15:46:39 +00:00
isJSONField: false,
copyInfoText: "Content was copied successfully!",
};
export default Textarea;