1.2.0 • Published 2 years ago

react-transliterate-updated v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Transliteration component for React with support for over 30 languages. Uses API from Google Input Tools

NPM

Demo

See Demo

Install

npm install --save react-transliterate

OR

yarn add react-transliterate

Usage

Basic example

import React, { useState } from "react";

import { ReactTransliterate } from "react-transliterate";
import "react-transliterate/dist/index.css";

const App = () => {
  const [text, setText] = useState("");

  return (
    <ReactTransliterate
      value={text}
      onChangeText={(text) => {
        setText(text);
      }}
      lang="hi"
    />
  );
};

export default App;

With custom component

import React, { useState } from "react";

import { ReactTransliterate } from "react-transliterate";
import "react-transliterate/dist/index.css";

const App = () => {
  const [text, setText] = useState("");

  return (
    <ReactTransliterate
      renderComponent={(props) => <textarea {...props} />}
      value={text}
      onChangeText={(text) => {
        setText(text);
      }}
      lang="hi"
    />
  );
};

export default App;

Usage with TypeScript

import React, { useState } from "react";

import { ReactTransliterate, Language } from "react-transliterate";
import "react-transliterate/dist/index.css";

const App = () => {
  const [text, setText] = useState("");
  const [lang, setLang] = useState<Language>("hi");

  return (
    <ReactTransliterate
      renderComponent={(props) => <textarea {...props} />}
      value={text}
      onChangeText={(text) => {
        setText(text);
      }}
      lang={lang}
    />
  );
};

export default App;

With material ui

import React, { useState } from "react";

import { ReactTransliterate, Language } from "react-transliterate";
import "react-transliterate/dist/index.css";

import Input from "@material-ui/core/Input";

const App = () => {
  const [text, setText] = useState("");
  const [lang, setLang] = useState<Language>("hi");

  return (
    <ReactTransliterate
      renderComponent={(props) => {
        const inputRef = props.ref;
        delete props["ref"];
        return <Input {...props} inputRef={inputRef} />;
      }}
      value={text}
      onChangeText={(text) => {
        setText(text);
      }}
      lang={lang}
    />
  );
};

export default App;

Custom trigger keys

Keys which when pressed, input the current selection to the textbox

React Transliterate uses the event.keycode property to detect keys. Here are some predefined keys you can use. Or, you can enter the integer codes for any other key you'd like to use as the trigger

import React, { useState } from "react";

import { ReactTransliterate, TriggerKeys } from "react-transliterate";
import "react-transliterate/dist/index.css";

import Input from "@material-ui/core/Input";

const App = () => {
  const [text, setText] = useState("");

  return (
    <ReactTransliterate
      value={text}
      onChangeText={(text) => {
        setText(text);
      }}
      lang="hi"
      triggerKeys={[
        TriggerKeys.KEY_RETURN,
        TriggerKeys.KEY_ENTER,
        TriggerKeys.KEY_SPACE,
        TriggerKeys.KEY_TAB,
      ]}
    />
  );
};

export default App;

Get transliteration suggestions

import { getTransliterateSuggestions } from "react-transliterate";

const data = await getTransliterateSuggestions(
  word, // word to fetch suggestions for
  {
    numOptions: 5, // number of suggestions to fetch
    showCurrentWordAsLastSuggestion: true, // add the word as the last suggestion
    lang: "hi", // target language
  },
);

For a full example, take a look at the example folder

Props

PropRequired?DefaultDescription
onChangeTextYesListener for the current value from the component. (text: string) => void
valueYesvalue prop to pass to the component
enabledtrueControl whether suggestions should be shown
renderComponent(props) => <input {...props} />Component to render. You can pass components from your component library as this prop
langhiLanguage you want to transliterate. See the following section for language codes
maxOptions5Maximum number of suggestions to show in helper
offsetY0Extra space between the top of the helper and bottom of the caret
offsetX0Extra space between the caret and left of the helper
containerClassNameempty stringClassname passed to the container of the component
containerStyles{}CSS styles object passed to the container
activeItemStyles{}CSS styles object passed to the active item <li> tag
hideSuggestionBoxOnMobileDevicesfalseShould the suggestions be visible on mobile devices since keyboards like Gboard and Swiftkey support typing in multiple languages
hideSuggestionBoxBreakpoint450type: number. To be used when hideSuggestionBoxOnMobileDevices is true. Suggestion box will not be shown below this device width
triggerKeysKEY_SPACE, KEY_ENTER, KEY_TAB, KEY_RETURNKeys which when pressed, input the current selection to the textbox
insertCurrentSelectionOnBlurtrueShould the current selection be inserted when blur event occurs
showCurrentWordAsLastSuggestiontrueShow current input as the last option in the suggestion box

Supported Languages

LanguageCode
Amharicam
Arabicar
Banglabn
Belarusianbe
Bulgarianbg
Chinese (Hong Kong)yue-hant
Chinese (Simplified)zh
Chinese (Traditional)zh-hant
Frenchfr
Germande
Greekel
Gujaratigu
Hebrewhe
Hindihi
Italianit
Japaneseja
Kannadakn
Malayalamml
Marathimr
Nepaline
Odiaor
Persianfa
Portuguese (Brazil)pt
Punjabipa
Russianru
Sanskritsa
Serbiansr
Sinhalasi
Spanishes
Tamilta
Telugute
Tigrinyati
Ukrainianuk
Urduur
Vietnamesevi

License

MIT © burhanuday

1.2.0

2 years ago