@@ -3,6 +3,7 @@ import { useEffect, useRef, useState } from "react";
3
3
import getInputSelection , { setCaretPosition } from "./util" ;
4
4
import getCaretCoordinates from "textarea-caret" ;
5
5
import classes from "./styles.module.css" ;
6
+ import { Languages } from "./languages" ;
6
7
7
8
const KEY_UP = 38 ;
8
9
const KEY_DOWN = 40 ;
@@ -23,58 +24,25 @@ interface Props
23
24
containerStyles : React . CSSProperties ;
24
25
activeItemStyles : React . CSSProperties ;
25
26
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 ;
62
28
}
63
29
64
30
export const ReactTransliterate = ( {
65
31
Component = < input /> ,
66
32
lang = "hi" ,
67
33
offsetX = 0 ,
68
34
offsetY = 10 ,
35
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
69
36
onChange = ( ) => { } ,
70
37
value,
38
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
71
39
onKeyDown = ( ) => { } ,
72
40
containerClassName = "" ,
73
41
containerStyles = { } ,
74
42
activeItemStyles = { } ,
75
43
maxOptions = 5 ,
76
44
...rest
77
- } : Props ) => {
45
+ } : Props ) : JSX . Element => {
78
46
const [ options , setOptions ] = useState < string [ ] > ( [ ] ) ;
79
47
const [ left , setLeft ] = useState ( 0 ) ;
80
48
const [ top , setTop ] = useState ( 0 ) ;
@@ -83,6 +51,41 @@ export const ReactTransliterate = ({
83
51
const [ matchEnd , setMatchEnd ] = useState ( - 1 ) ;
84
52
const inputRef = useRef < HTMLInputElement > ( null ) ;
85
53
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
+
86
89
const getSuggestions = async ( lastWord : string ) => {
87
90
// fetch suggestion from api
88
91
// 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 = ({
204
207
// the helper on screen size change
205
208
} ;
206
209
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
-
241
210
useEffect ( ( ) => {
242
211
window . addEventListener ( "resize" , handleResize ) ;
243
212
@@ -263,13 +232,6 @@ export const ReactTransliterate = ({
263
232
value : value ,
264
233
...rest ,
265
234
} ) }
266
- { /* <Component
267
- onChange={handleChange}
268
- onKeyDown={handleKeyDown}
269
- ref={inputRef}
270
- value={value}
271
- {...rest}
272
- /> */ }
273
235
{ options . length > 0 && (
274
236
< ul
275
237
style = { {
0 commit comments