@arkyn/components 1.3.121 → 1.3.123

Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +1 @@
1
- {"version":3,"file":"deserialize.d.ts","sourceRoot":"","sources":["../../../../src/components/RichText/functions/deserialize.ts"],"names":[],"mappings":"AA8CA,QAAA,MAAM,gBAAgB,SAAU,MAAM,mFAarC,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
1
+ {"version":3,"file":"deserialize.d.ts","sourceRoot":"","sources":["../../../../src/components/RichText/functions/deserialize.ts"],"names":[],"mappings":"AAiDA,QAAA,MAAM,gBAAgB,SAAU,MAAM,mFAarC,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
@@ -8,6 +8,7 @@ const deserialize = (el) => {
8
8
  const children = Array.isArray(el.props.children)
9
9
  ? parsedChildren(el.props.children)
10
10
  : [{ text: el.props.children }];
11
+ const childrenString = typeof el.props.children === "string" ? el.props.children : "";
11
12
  switch (el.type) {
12
13
  case "p":
13
14
  return { type: "paragraph", children };
@@ -24,15 +25,15 @@ const deserialize = (el) => {
24
25
  case "ol":
25
26
  return { type: "numberedList", children };
26
27
  case "strong":
27
- return { text: "", children, bold: true };
28
+ return { text: childrenString, bold: true };
28
29
  case "code":
29
- return { text: "", children, code: true };
30
+ return { text: childrenString, code: true };
30
31
  case "em":
31
- return { text: "", children, italic: true };
32
+ return { text: childrenString, italic: true };
32
33
  case "u":
33
- return { text: "", children, underline: true };
34
+ return { text: childrenString, underline: true };
34
35
  default:
35
- return { text: "", children };
36
+ return { text: childrenString };
36
37
  }
37
38
  };
38
39
  const getSlateFromHtml = (html) => {
@@ -0,0 +1,3 @@
1
+ declare const isHtml: (str: string) => boolean;
2
+ export { isHtml };
3
+ //# sourceMappingURL=isHtml.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isHtml.d.ts","sourceRoot":"","sources":["../../../../src/components/RichText/functions/isHtml.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,MAAM,QAAS,MAAM,KAAG,OAG7B,CAAC;AAEF,OAAO,EAAE,MAAM,EAAE,CAAC"}
@@ -0,0 +1,5 @@
1
+ const isHtml = (str) => {
2
+ const htmlRegex = /<\/?[a-z][\s\S]*>/i;
3
+ return htmlRegex.test(str);
4
+ };
5
+ export { isHtml };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/RichText/index.tsx"],"names":[],"mappings":"AAeA,OAAO,EAAgB,UAAU,EAAc,MAAM,OAAO,CAAC;AAc7D,OAAO,cAAc,CAAC;AAMtB,KAAK,aAAa,GAAG;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,uBAAuB,CAAC,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;IAC9C,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,IAAI,CAAC;IACzC,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACzC,CAAC;AAEF,iBAAS,QAAQ,CAAC,EAChB,IAAI,EACJ,YAAY,EACZ,qBAA6B,EAC7B,uBAAuB,EACvB,QAAe,EACf,aAAa,EACb,QAAQ,EACR,OAAO,EAAE,WAAW,GACrB,EAAE,aAAa,2CAwGf;AAED,OAAO,EAAE,QAAQ,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/RichText/index.tsx"],"names":[],"mappings":"AAeA,OAAO,EAAgB,UAAU,EAAc,MAAM,OAAO,CAAC;AAc7D,OAAO,cAAc,CAAC;AAOtB,KAAK,aAAa,GAAG;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,uBAAuB,CAAC,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;IAC9C,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,IAAI,CAAC;IACzC,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACzC,CAAC;AAEF,iBAAS,QAAQ,CAAC,EAChB,IAAI,EACJ,YAAY,EACZ,qBAA6B,EAC7B,uBAAuB,EACvB,QAAe,EACf,aAAa,EACb,QAAQ,EACR,OAAO,EAAE,WAAW,GACrB,EAAE,aAAa,2CA2Gf;AAED,OAAO,EAAE,QAAQ,EAAE,CAAC"}
@@ -18,16 +18,17 @@ import "./styles.css";
18
18
  import { getSlateFromHtml } from "./functions/deserialize";
19
19
  import { extractText } from "./functions/extractText";
20
20
  import { getHtmlFromSlate } from "./functions/serialize";
21
+ import { isHtml } from "./functions/isHtml";
21
22
  function RichText({ name, defaultValue, enforceCharacterLimit = false, onChangeCharactersCount, maxLimit = 2000, onValueChange, onChange, isError: baseIsError, }) {
22
23
  const editor = useMemo(() => withHistory(withReact(createEditor())), []);
23
24
  const { id, inputRef, error } = useFormController();
24
25
  const baseRef = useRef(null);
25
- const defaultNodes = defaultValue
26
+ const defaultNodes = isHtml(defaultValue)
26
27
  ? getSlateFromHtml(defaultValue)
27
28
  : INITIAL_VALUE;
28
29
  const textFromNodes = extractText(defaultNodes);
29
30
  const [charactersCount, setCharactersCount] = useState(textFromNodes.length);
30
- const [inputValue, setInputValue] = useState(defaultValue);
31
+ const [inputValue, setInputValue] = useState(isHtml(defaultValue) ? defaultValue : "");
31
32
  const [onFocus, setOnFocus] = useState(false);
32
33
  const ref = inputRef || baseRef;
33
34
  const isError = baseIsError || !!error;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkyn/components",
3
- "version": "1.3.121",
3
+ "version": "1.3.123",
4
4
  "main": "./dist/bundle.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Lucas Gonçalves",
@@ -16,6 +16,9 @@ const deserialize = (el: ParseElement): Descendant => {
16
16
  ? parsedChildren(el.props.children)
17
17
  : [{ text: el.props.children }];
18
18
 
19
+ const childrenString =
20
+ typeof el.props.children === "string" ? el.props.children : "";
21
+
19
22
  switch (el.type) {
20
23
  case "p":
21
24
  return { type: "paragraph", children };
@@ -32,15 +35,15 @@ const deserialize = (el: ParseElement): Descendant => {
32
35
  case "ol":
33
36
  return { type: "numberedList", children };
34
37
  case "strong":
35
- return { text: "", children, bold: true };
38
+ return { text: childrenString, bold: true };
36
39
  case "code":
37
- return { text: "", children, code: true };
40
+ return { text: childrenString, code: true };
38
41
  case "em":
39
- return { text: "", children, italic: true };
42
+ return { text: childrenString, italic: true };
40
43
  case "u":
41
- return { text: "", children, underline: true };
44
+ return { text: childrenString, underline: true };
42
45
  default:
43
- return { text: "", children };
46
+ return { text: childrenString };
44
47
  }
45
48
  };
46
49
 
@@ -0,0 +1,6 @@
1
+ const isHtml = (str: string): boolean => {
2
+ const htmlRegex = /<\/?[a-z][\s\S]*>/i;
3
+ return htmlRegex.test(str);
4
+ };
5
+
6
+ export { isHtml };
@@ -32,6 +32,7 @@ import "./styles.css";
32
32
  import { getSlateFromHtml } from "./functions/deserialize";
33
33
  import { extractText } from "./functions/extractText";
34
34
  import { getHtmlFromSlate } from "./functions/serialize";
35
+ import { isHtml } from "./functions/isHtml";
35
36
 
36
37
  type RichTextProps = {
37
38
  name: string;
@@ -59,14 +60,17 @@ function RichText({
59
60
 
60
61
  const baseRef = useRef<HTMLInputElement>(null);
61
62
 
62
- const defaultNodes = defaultValue
63
+ const defaultNodes = isHtml(defaultValue)
63
64
  ? getSlateFromHtml(defaultValue)
64
65
  : INITIAL_VALUE;
65
66
 
66
67
  const textFromNodes = extractText(defaultNodes);
67
68
 
68
69
  const [charactersCount, setCharactersCount] = useState(textFromNodes.length);
69
- const [inputValue, setInputValue] = useState(defaultValue);
70
+ const [inputValue, setInputValue] = useState(
71
+ isHtml(defaultValue) ? defaultValue : ""
72
+ );
73
+
70
74
  const [onFocus, setOnFocus] = useState(false);
71
75
 
72
76
  const ref = inputRef || baseRef;
@@ -77,8 +81,8 @@ function RichText({
77
81
 
78
82
  function handleChange(value: Descendant[]) {
79
83
  const text = extractText(value);
80
- setCharactersCount(text.length);
81
84
 
85
+ setCharactersCount(text.length);
82
86
  onChangeCharactersCount && onChangeCharactersCount(text.length);
83
87
 
84
88
  if (enforceCharacterLimit && text.length >= maxLimit) {