hi-tm 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -42,11 +42,35 @@ const TextInput = ({ label, required, value, placeholderText, autoComplete, auto
|
|
|
42
42
|
const [inputValue, setInputValue] = (0, react_1.useState)('');
|
|
43
43
|
const inputRef = (0, react_1.useRef)(null);
|
|
44
44
|
// Function to detect keyboard language
|
|
45
|
-
const detectLanguage = (text) => {
|
|
46
|
-
|
|
45
|
+
const detectLanguage = (text, event) => {
|
|
46
|
+
// Try to detect from keyboard event first
|
|
47
|
+
if (event) {
|
|
48
|
+
// Check if the event has language information
|
|
49
|
+
const nativeEvent = event.nativeEvent;
|
|
50
|
+
const locale = nativeEvent.locale;
|
|
51
|
+
if (locale) {
|
|
52
|
+
if (locale.includes('fa') || locale.includes('persian'))
|
|
53
|
+
return 'fa';
|
|
54
|
+
if (locale.includes('en') || locale.includes('english'))
|
|
55
|
+
return 'en';
|
|
56
|
+
if (locale.includes('ar') || locale.includes('arabic'))
|
|
57
|
+
return 'ar';
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
// Fallback to character analysis
|
|
61
|
+
if (!text.trim()) {
|
|
62
|
+
// Check browser language as fallback
|
|
63
|
+
const browserLang = navigator.language || navigator.languages?.[0];
|
|
64
|
+
if (browserLang?.includes('fa'))
|
|
65
|
+
return 'fa';
|
|
66
|
+
if (browserLang?.includes('ar'))
|
|
67
|
+
return 'ar';
|
|
68
|
+
if (browserLang?.includes('en'))
|
|
69
|
+
return 'en';
|
|
47
70
|
return 'unknown';
|
|
48
|
-
|
|
49
|
-
|
|
71
|
+
}
|
|
72
|
+
// Persian/Farsi detection - improved regex
|
|
73
|
+
const persianRegex = /[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF\u2000-\u206F]/;
|
|
50
74
|
if (persianRegex.test(text)) {
|
|
51
75
|
return 'fa';
|
|
52
76
|
}
|
|
@@ -67,6 +91,49 @@ const TextInput = ({ label, required, value, placeholderText, autoComplete, auto
|
|
|
67
91
|
}
|
|
68
92
|
return 'unknown';
|
|
69
93
|
};
|
|
94
|
+
// Function to detect keyboard layout using multiple methods
|
|
95
|
+
const detectKeyboardLayout = () => {
|
|
96
|
+
try {
|
|
97
|
+
// Method 1: Try to get from navigator.languages (most reliable)
|
|
98
|
+
const languages = navigator.languages || [navigator.language];
|
|
99
|
+
for (const lang of languages) {
|
|
100
|
+
if (lang?.includes('fa') || lang?.includes('persian') || lang?.includes('ir'))
|
|
101
|
+
return 'fa';
|
|
102
|
+
if (lang?.includes('ar') || lang?.includes('arabic'))
|
|
103
|
+
return 'ar';
|
|
104
|
+
if (lang?.includes('en') || lang?.includes('english') || lang?.includes('us'))
|
|
105
|
+
return 'en';
|
|
106
|
+
}
|
|
107
|
+
// Method 2: Try to get from navigator.language
|
|
108
|
+
const browserLang = navigator.language;
|
|
109
|
+
if (browserLang?.includes('fa') ||
|
|
110
|
+
browserLang?.includes('persian') ||
|
|
111
|
+
browserLang?.includes('ir'))
|
|
112
|
+
return 'fa';
|
|
113
|
+
if (browserLang?.includes('ar') || browserLang?.includes('arabic'))
|
|
114
|
+
return 'ar';
|
|
115
|
+
if (browserLang?.includes('en') ||
|
|
116
|
+
browserLang?.includes('english') ||
|
|
117
|
+
browserLang?.includes('us'))
|
|
118
|
+
return 'en';
|
|
119
|
+
// Method 3: Try to get from user agent
|
|
120
|
+
const userAgent = navigator.userAgent.toLowerCase();
|
|
121
|
+
if (userAgent.includes('persian') ||
|
|
122
|
+
userAgent.includes('farsi') ||
|
|
123
|
+
userAgent.includes('iran'))
|
|
124
|
+
return 'fa';
|
|
125
|
+
if (userAgent.includes('arabic'))
|
|
126
|
+
return 'ar';
|
|
127
|
+
// Method 4: Try to get from timezone (Iran timezone might indicate Persian)
|
|
128
|
+
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
129
|
+
if (timezone?.includes('Tehran') || timezone?.includes('Iran'))
|
|
130
|
+
return 'fa';
|
|
131
|
+
return 'unknown';
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
return 'unknown';
|
|
135
|
+
}
|
|
136
|
+
};
|
|
70
137
|
const handleInputChange = (e) => {
|
|
71
138
|
let newValue = e.target.value;
|
|
72
139
|
// Apply captcha validation if type is 'captcha'
|
|
@@ -103,6 +170,13 @@ const TextInput = ({ label, required, value, placeholderText, autoComplete, auto
|
|
|
103
170
|
if (onFocusHandler) {
|
|
104
171
|
onFocusHandler(e);
|
|
105
172
|
}
|
|
173
|
+
// Detect keyboard language on focus
|
|
174
|
+
if (detectKeyboardLanguage && onKeyboardLanguageChange) {
|
|
175
|
+
const detectedLanguage = detectKeyboardLayout();
|
|
176
|
+
if (detectedLanguage !== 'unknown') {
|
|
177
|
+
onKeyboardLanguageChange(detectedLanguage);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
106
180
|
if (inputRef.current && type === 'text') {
|
|
107
181
|
setTimeout(() => {
|
|
108
182
|
if (inputRef.current) {
|
|
@@ -112,6 +186,34 @@ const TextInput = ({ label, required, value, placeholderText, autoComplete, auto
|
|
|
112
186
|
}, 0);
|
|
113
187
|
}
|
|
114
188
|
};
|
|
189
|
+
const handleKeyDown = (e) => {
|
|
190
|
+
// Detect keyboard language on key press
|
|
191
|
+
if (detectKeyboardLanguage && onKeyboardLanguageChange) {
|
|
192
|
+
// Try to detect from the key pressed
|
|
193
|
+
const key = e.key;
|
|
194
|
+
const code = e.code;
|
|
195
|
+
// Log for debugging (you can remove this later)
|
|
196
|
+
console.log('Key pressed:', key, 'Code:', code, 'Event:', e.nativeEvent);
|
|
197
|
+
// Check if the key is a Persian character
|
|
198
|
+
if (/[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF]/.test(key)) {
|
|
199
|
+
onKeyboardLanguageChange('fa');
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
// Check if the key is an English character
|
|
203
|
+
if (/[a-zA-Z]/.test(key)) {
|
|
204
|
+
onKeyboardLanguageChange('en');
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
// Fallback to layout detection
|
|
208
|
+
const detectedLanguage = detectLanguage('', e);
|
|
209
|
+
if (detectedLanguage !== 'unknown') {
|
|
210
|
+
onKeyboardLanguageChange(detectedLanguage);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (onKeyDownHandler) {
|
|
214
|
+
onKeyDownHandler(e);
|
|
215
|
+
}
|
|
216
|
+
};
|
|
115
217
|
const showError = () => {
|
|
116
218
|
return textError === '' || textError === null || textError.trim() === '';
|
|
117
219
|
};
|
|
@@ -129,7 +231,7 @@ const TextInput = ({ label, required, value, placeholderText, autoComplete, auto
|
|
|
129
231
|
react_1.default.createElement("span", { className: `text-red-primary ${required ? 'opacity-100' : 'opacity-0'} ` }, "*")),
|
|
130
232
|
react_1.default.createElement("div", { className: "relative flex items-center" },
|
|
131
233
|
startContent && (react_1.default.createElement("div", { className: "absolute left-2 z-10", style: { ...startContentStyle } }, startContent)),
|
|
132
|
-
react_1.default.createElement("input", { ref: inputRef, type: type === 'captcha' ? 'tel' : type, id: label, disabled: disabled, value: value || inputValue, required: false, onChange: handleInputChange, onBlur: onBlurHandler ? onBlurHandler : () => { }, onFocus: handleFocus, onKeyDown:
|
|
234
|
+
react_1.default.createElement("input", { ref: inputRef, type: type === 'captcha' ? 'tel' : type, id: label, disabled: disabled, value: value || inputValue, required: false, onChange: handleInputChange, onBlur: onBlurHandler ? onBlurHandler : () => { }, onFocus: handleFocus, onKeyDown: handleKeyDown, onKeyUp: onKeyUpHandler ? onKeyUpHandler : () => { }, placeholder: placeholderText, autoComplete: autoComplete ? 'on' : 'off', autoFocus: autoFocus, maxLength: type === 'captcha' ? 4 : maxLength, inputMode: type === 'captcha' ? 'numeric' : undefined, lang: detectKeyboardLanguage ? detectKeyboardLayout() : undefined, className: `input-style bg-inherit ${disabled ? 'cursor-not-allowed' : ''} text-gray-secondary ${textError === ''
|
|
133
235
|
? 'border-[2px] border-solid border-gray-tertiary'
|
|
134
236
|
: ' border-[2.5px] border-solid border-red-primary'} ${startContent ? 'pl-12' : ''} placeholder-rtl`, style: computedInputStyle })),
|
|
135
237
|
react_1.default.createElement("span", { className: `absolute bottom-0 text-sm mr-1 text-nowrap font-normal text-red-primary ${showError() ? 'opacity-0' : 'opacity-100'}` }, textError?.trim()))));
|