GithubHelp home page GithubHelp logo

burhanuday / react-transliterate Goto Github PK

View Code? Open in Web Editor NEW
186.0 4.0 31.0 3.56 MB

Transliteration component for React with support for over 30 langauges. Burhanuddin → बुरहानुद्दीन

Home Page: https://burhanuday.github.io/react-transliterate/

HTML 7.08% CSS 7.21% TypeScript 85.71%
react transliteration-component react-component works-with-react

react-transliterate's Introduction

React Transliterate

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

Prop Required? Default Description
onChangeText Yes Listener for the current value from the component. (text: string) => void
value Yes value prop to pass to the component
enabled true Control whether suggestions should be shown
renderComponent (props) => <input {...props} /> Component to render. You can pass components from your component library as this prop
lang hi Language you want to transliterate. See the following section for language codes
maxOptions 5 Maximum number of suggestions to show in helper
offsetY 0 Extra space between the top of the helper and bottom of the caret
offsetX 0 Extra space between the caret and left of the helper
containerClassName empty string Classname 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
hideSuggestionBoxOnMobileDevices false Should the suggestions be visible on mobile devices since keyboards like Gboard and Swiftkey support typing in multiple languages
hideSuggestionBoxBreakpoint 450 type: number. To be used when hideSuggestionBoxOnMobileDevices is true. Suggestion box will not be shown below this device width
triggerKeys KEY_SPACE, KEY_ENTER, KEY_TAB, KEY_RETURN Keys which when pressed, input the current selection to the textbox
insertCurrentSelectionOnBlur true Should the current selection be inserted when blur event occurs
showCurrentWordAsLastSuggestion true Show current input as the last option in the suggestion box

Supported Languages

Language Code
Amharic am
Arabic ar
Bangla bn
Belarusian be
Bulgarian bg
Chinese (Hong Kong) yue-hant
Chinese (Simplified) zh
Chinese (Traditional) zh-hant
French fr
German de
Greek el
Gujarati gu
Hebrew he
Hindi hi
Italian it
Japanese ja
Kannada kn
Malayalam ml
Marathi mr
Nepali ne
Odia or
Persian fa
Portuguese (Brazil) pt
Punjabi pa
Russian ru
Sanskrit sa
Serbian sr
Sinhala si
Spanish es
Tamil ta
Telugu te
Tigrinya ti
Ukrainian uk
Urdu ur
Vietnamese vi

License

MIT © burhanuday

react-transliterate's People

Contributors

burhanuday avatar dependabot[bot] avatar sifulbd avatar sttronn avatar utk09 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-transliterate's Issues

Position of suggestion box

I am using one textarea and that is scrollable. When I have to scroll the textarea for typing text then that time the position of the suggestion box goes bottom of the whole webpage and that goes down accordingly with respect to the height of the whole textarea and scroll position.
Please solve this issue.

Suggestion onChange

Add a little bit more functionality.
Like without pressing the enter key. That will automatically put the highest probability word inside the search bar.
Thanks.

Originally posted by @vishalvikal in #9 (comment)

How to use this script along with Jodit editor

First of all, thanks for the amazing api.
Secondly, this api only works with form input tags and not rich text format. If you could suggest a way we can use this for jodit editor, that would be great.

Global CSS cannot be imported from within node_modules.

I want to add react-transliterate textbox inside my component in NextJS. But it's showing error 'Global CSS cannot be imported from within node_modules'. I searched this issue on internet, and this is a common issue with NextJS. So can you please tell me how can I use your package?

incorrect `event.keyCode` values on android devices

Android gives incorrect event.keyCode values of 229 or 0. The component uses event.keyCode to detect which key was pressed in order to auto insert the current selection

if (triggerKeys.includes(event.keyCode)) {
event.preventDefault();
handleSelection(selection);

as a result, trigger keys are not detected correctly and selections have to be pressed resulting in poor UX

Handle space key press

As the input tool on the demo page works https://www.google.com/inputtools/try/ I believe to achieve exactly like that we need to add a spacebar functionality. For example, when someone hits the space then the first word from the queue would be put in the input field. Currently, just KEY_TAB and KEY_ENTER are working.

If you add something like
var KEY_SPACEBAR = 32;

then add
case KEY_SPACEBAR:

would this functionality will achieve the desired result or not. If it is possible can you please update the module?

Thanks.

Originally posted by @rustamveer in #9 (comment)

[Bug] Caret does not get set to correct position on selection

See util.js for implementation of function setCaretPosition

export function setCaretPosition(elem, caretPos) {
  if (elem) {
    if (elem.createTextRange) {
      const range = elem.createTextRange();

      range.move("character", caretPos);
      range.select();
    } else {
      if (elem.selectionStart) {
        elem.focus();
        elem.setSelectionRange(caretPos, caretPos);
      } else {
        elem.focus();
      }
    }
  }
}

elem.setSelectionRange does not seem to be working as expected when a ref is passed to the element

Space key not working in android chrome browser

I am using your React Transliterate project in our project. but the space key is not working in the android chrome browser, even your project is also not working in chrome. but in the iphone chrome browser its working fine. please help me

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.