-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
45 lines (40 loc) · 1.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import React, { useState, useRef } from 'react';
import { toKana, toRomaji, toHiragana, toKatakana } from 'wanakana';
// Need IMEMode option to handle single kana characters like ん and い
function translateValue(string, type) {
switch (type) {
case 'romaji':
return toRomaji(string, { IMEMode: true });
case 'hiragana':
return toHiragana(string, { IMEMode: true });
case 'katakana':
return toKatakana(string, { IMEMode: true });
case 'kana':
default:
return toKana(string, { IMEMode: true });
}
}
const WanakanaInput = ({ component, value, to, onChange, ...props }) => {
const inputRef = useRef(null);
const [parsedValue, setValue] = useState(translateValue(value, to));
const handleChange = e => {
const updatedValue = translateValue(e.target.value, to);
setValue(updatedValue);
inputRef.current.value = updatedValue;
onChange(e);
};
return React.createElement(component, {
ref: inputRef,
value: parsedValue,
onChange: handleChange,
...props,
});
};
WanakanaInput.defaultProps = {
component: 'input',
type: 'text',
value: '',
to: 'kana',
onChange: e => e,
};
export default WanakanaInput;