Skip to content

Commit 6484005

Browse files
committed
make ts happy
1 parent 9d953ce commit 6484005

File tree

2 files changed

+44
-80
lines changed

2 files changed

+44
-80
lines changed

.eslintrc

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"plugin:prettier/recommended",
77
"prettier/standard",
88
"prettier/react",
9-
"plugin:@typescript-eslint/eslint-recommended"
9+
"plugin:@typescript-eslint/eslint-recommended",
10+
"plugin:@typescript-eslint/recommended"
1011
],
1112
"env": {
1213
"browser": true,
@@ -30,6 +31,7 @@
3031
"react/jsx-handler-names": 0,
3132
"react/jsx-fragments": 0,
3233
"react/no-unused-prop-types": 0,
33-
"import/export": 0
34+
"import/export": 0,
35+
"@typescript-eslint/explicit-function-return-type": "off"
3436
}
3537
}

src/index.tsx

+40-78
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useEffect, useRef, useState } from "react";
33
import getInputSelection, { setCaretPosition } from "./util";
44
import getCaretCoordinates from "textarea-caret";
55
import classes from "./styles.module.css";
6+
import { Languages } from "./languages";
67

78
const KEY_UP = 38;
89
const KEY_DOWN = 40;
@@ -23,58 +24,25 @@ interface Props
2324
containerStyles: React.CSSProperties;
2425
activeItemStyles: React.CSSProperties;
2526
maxOptions: number;
26-
lang:
27-
| "am"
28-
| "ar"
29-
| "bn"
30-
| "be"
31-
| "bg"
32-
| "yue-hant"
33-
| "zh"
34-
| "zh-hant"
35-
| "fr"
36-
| "de"
37-
| "el"
38-
| "gu"
39-
| "he"
40-
| "hi"
41-
| "it"
42-
| "ja"
43-
| "kn"
44-
| "ml"
45-
| "mr"
46-
| "ne"
47-
| "or"
48-
| "fa"
49-
| "pt"
50-
| "pa"
51-
| "ru"
52-
| "sa"
53-
| "sr"
54-
| "si"
55-
| "es"
56-
| "ta"
57-
| "te"
58-
| "ti"
59-
| "uk"
60-
| "ur"
61-
| "vi";
27+
lang: Languages;
6228
}
6329

6430
export const ReactTransliterate = ({
6531
Component = <input />,
6632
lang = "hi",
6733
offsetX = 0,
6834
offsetY = 10,
35+
// eslint-disable-next-line @typescript-eslint/no-empty-function
6936
onChange = () => {},
7037
value,
38+
// eslint-disable-next-line @typescript-eslint/no-empty-function
7139
onKeyDown = () => {},
7240
containerClassName = "",
7341
containerStyles = {},
7442
activeItemStyles = {},
7543
maxOptions = 5,
7644
...rest
77-
}: Props) => {
45+
}: Props): JSX.Element => {
7846
const [options, setOptions] = useState<string[]>([]);
7947
const [left, setLeft] = useState(0);
8048
const [top, setTop] = useState(0);
@@ -83,6 +51,41 @@ export const ReactTransliterate = ({
8351
const [matchEnd, setMatchEnd] = useState(-1);
8452
const inputRef = useRef<HTMLInputElement>(null);
8553

54+
const reset = () => {
55+
// reset the component
56+
setSelection(0);
57+
setOptions([]);
58+
};
59+
60+
const handleSelection = (index: number) => {
61+
const currentString = value;
62+
// create a new string with the currently typed word
63+
// replaced with the word in transliterated language
64+
if (typeof currentString !== "string") return;
65+
const newValue =
66+
currentString.substring(0, matchStart) +
67+
options[index] +
68+
" " +
69+
currentString.substring(matchEnd + 1, currentString.length);
70+
71+
// set the position of the caret (cursor) one character after the
72+
// the position of the new word
73+
setTimeout(() => {
74+
setCaretPosition(
75+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
76+
inputRef.current!,
77+
matchStart + options[index].length + 1,
78+
);
79+
}, 1);
80+
81+
// bubble up event to the parent component
82+
const e = ({ target: { value: newValue } } as unknown) as React.FormEvent<
83+
HTMLInputElement
84+
>;
85+
onChange(e);
86+
reset();
87+
};
88+
8689
const getSuggestions = async (lastWord: string) => {
8790
// fetch suggestion from api
8891
// const url = `https://www.google.com/inputtools/request?ime=transliteration_en_${lang}&num=5&cp=0&cs=0&ie=utf-8&oe=utf-8&app=jsapi&text=${lastWord}`;
@@ -204,40 +207,6 @@ export const ReactTransliterate = ({
204207
// the helper on screen size change
205208
};
206209

207-
const handleSelection = (index: number) => {
208-
const currentString = value;
209-
// create a new string with the currently typed word
210-
// replaced with the word in transliterated language
211-
if (typeof currentString !== "string") return;
212-
const newValue =
213-
currentString.substring(0, matchStart) +
214-
options[index] +
215-
" " +
216-
currentString.substring(matchEnd + 1, currentString.length);
217-
218-
// set the position of the caret (cursor) one character after the
219-
// the position of the new word
220-
setTimeout(() => {
221-
setCaretPosition(
222-
inputRef.current!,
223-
matchStart + options[index].length + 1,
224-
);
225-
}, 1);
226-
227-
// bubble up event to the parent component
228-
const e = ({ target: { value: newValue } } as unknown) as React.FormEvent<
229-
HTMLInputElement
230-
>;
231-
onChange(e);
232-
reset();
233-
};
234-
235-
const reset = () => {
236-
// reset the component
237-
setSelection(0);
238-
setOptions([]);
239-
};
240-
241210
useEffect(() => {
242211
window.addEventListener("resize", handleResize);
243212

@@ -263,13 +232,6 @@ export const ReactTransliterate = ({
263232
value: value,
264233
...rest,
265234
})}
266-
{/* <Component
267-
onChange={handleChange}
268-
onKeyDown={handleKeyDown}
269-
ref={inputRef}
270-
value={value}
271-
{...rest}
272-
/> */}
273235
{options.length > 0 && (
274236
<ul
275237
style={{

0 commit comments

Comments
 (0)