Skip to content

Commit

Permalink
fixup! feat: japanese input
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuyapega committed Dec 13, 2024
1 parent 7a7babb commit ff8645c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 41 deletions.
9 changes: 8 additions & 1 deletion src/components/Pega_Extensions_JapaneseInput/Docs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import * as DemoStories from './demo.stories';

# Overview

This component that converts Japanese input, such as kana characters and full-width characters.
Japanese input component has the following features.

- Lowercase to Uppercase
Converts Lowercase to Uppercase, for when the client inputs lowercase and the internal character handles it as uppercase.
- Hiragana to Katakana
Japanese has two types of characters, Hiragana and Katakana, which correspond one-to-one. Hiragana input is converted to Katakana.
- Full-width to Half-width
In Japanese, characters are generally handled as multi-byte full-width characters rather than single-byte half-width characters, and for this reason Japanese keyboards have a function to input characters in full-width. Some full-width characters have the same meaning in half-width, and these are converted from full-width to half-width.

<Primary />

Expand Down
41 changes: 1 addition & 40 deletions src/components/Pega_Extensions_JapaneseInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '@pega/cosmos-react-core';
import '../create-nonce';
import type { FieldValueVariant } from '@pega/cosmos-react-core/lib/components/FieldValueList/FieldValueList';
import { convertHiraganaToKatakana, fullWidthToHalfWidth } from './utils';

enum DisplayMode {
DisplayOnly = 'DISPLAY_ONLY',
Expand Down Expand Up @@ -132,46 +133,6 @@ export const PegaExtensionsJapaneseInput: FC<PegaExtensionsJapaneseInputProps> =
hasValueChange.current = false;
}

const convertHiraganaToKatakana = (str: string) => {
return str.replace(/[\u3041-\u3096]/g, match => {
return String.fromCharCode(match.charCodeAt(0) + 0x60);
});
};

const fullWidthToHalfWidth = (str: string): string => {
str = str.replace(/[---]/g, match => {
return String.fromCharCode(match.charCodeAt(0) - 0xfee0);
});
// prettier-ignore
const kanaMap: Record<string, string> = {
: 'ガ', : 'ギ', : 'グ', : 'ゲ', : 'ゴ',
: 'ザ', : 'ジ', : 'ズ', : 'ゼ', : 'ゾ',
: 'ダ', : 'ヂ', : 'ヅ', : 'デ', : 'ド',
: 'バ', : 'ビ', : 'ブ', : 'ベ', : 'ボ',
: 'パ', : 'ピ', : 'プ', : 'ペ', : 'ポ',
: 'ヴ', : 'ヷ', : 'ヺ',
: 'ア', : 'イ', : 'ウ', : 'エ', : 'オ',
: 'カ', : 'キ', : 'ク', : 'ケ', : 'コ',
: 'サ', : 'シ', : 'ス', : 'セ', : 'ソ',
: 'タ', : 'チ', : 'ツ', : 'テ', : 'ト',
: 'ナ', : 'ニ', : 'ヌ', : 'ネ', : 'ノ',
: 'ハ', : 'ヒ', : 'フ', : 'ヘ', : 'ホ',
: 'マ', : 'ミ', : 'ム', : 'メ', : 'モ',
: 'ヤ', : 'ユ', : 'ヨ',
: 'ラ', : 'リ', : 'ル', : 'レ', : 'ロ',
: 'ワ', : 'ヲ', : 'ン',
: 'ァ', : 'ィ', : 'ゥ', : 'ェ', : 'ォ',
: 'ッ', : 'ャ', : 'ュ', : 'ョ',
'。': '。', '、': '、', : 'ー', '「': '「', '」': '」', '・': '・'
};
const reg = new RegExp(`(${Object.keys(kanaMap).join('|')})`, 'g');
return str
.replace(reg, match => {
return kanaMap[match];
})
.replace(//g, '゙')
.replace(//g, '゚');
};
let newValue = event.target.value;
if (hiraganaToKatakana) {
newValue = convertHiraganaToKatakana(newValue);
Expand Down
40 changes: 40 additions & 0 deletions src/components/Pega_Extensions_JapaneseInput/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export const convertHiraganaToKatakana = (str: string) => {
return str.replace(/[\u3041-\u3096]/g, match => {
return String.fromCharCode(match.charCodeAt(0) + 0x60);
});
};

export const fullWidthToHalfWidth = (str: string): string => {
str = str.replace(/[---]/g, match => {
return String.fromCharCode(match.charCodeAt(0) - 0xfee0);
});
// prettier-ignore
const kanaMap: Record<string, string> = {
: 'ガ', : 'ギ', : 'グ', : 'ゲ', : 'ゴ',
: 'ザ', : 'ジ', : 'ズ', : 'ゼ', : 'ゾ',
: 'ダ', : 'ヂ', : 'ヅ', : 'デ', : 'ド',
: 'バ', : 'ビ', : 'ブ', : 'ベ', : 'ボ',
: 'パ', : 'ピ', : 'プ', : 'ペ', : 'ポ',
: 'ヴ', : 'ヷ', : 'ヺ',
: 'ア', : 'イ', : 'ウ', : 'エ', : 'オ',
: 'カ', : 'キ', : 'ク', : 'ケ', : 'コ',
: 'サ', : 'シ', : 'ス', : 'セ', : 'ソ',
: 'タ', : 'チ', : 'ツ', : 'テ', : 'ト',
: 'ナ', : 'ニ', : 'ヌ', : 'ネ', : 'ノ',
: 'ハ', : 'ヒ', : 'フ', : 'ヘ', : 'ホ',
: 'マ', : 'ミ', : 'ム', : 'メ', : 'モ',
: 'ヤ', : 'ユ', : 'ヨ',
: 'ラ', : 'リ', : 'ル', : 'レ', : 'ロ',
: 'ワ', : 'ヲ', : 'ン',
: 'ァ', : 'ィ', : 'ゥ', : 'ェ', : 'ォ',
: 'ッ', : 'ャ', : 'ュ', : 'ョ',
'。': '。', '、': '、', : 'ー', '「': '「', '」': '」', '・': '・'
};
const reg = new RegExp(`(${Object.keys(kanaMap).join('|')})`, 'g');
return str
.replace(reg, match => {
return kanaMap[match];
})
.replace(//g, '゙')
.replace(//g, '゚');
};

0 comments on commit ff8645c

Please sign in to comment.