one
Version:
One is a React framework for web, iOS, and Android apps.
180 lines (177 loc) • 5.84 kB
JavaScript
import { useEffect, useRef, useState } from "react";
function resolveEditable(editable, readOnly) {
if (readOnly) return false;
return editable !== null && editable !== void 0 ? editable : true;
}
function inputModeToKeyboardType(inputMode) {
switch (inputMode) {
case "decimal": return "decimal-pad";
case "email": return "email-address";
case "none": return "default";
case "numeric": return "numeric";
case "search": return "web-search";
case "tel": return "phone-pad";
case "text": return "default";
case "url": return "url";
default: return void 0;
}
}
function enterKeyHintToReturnKeyType(enterKeyHint) {
switch (enterKeyHint) {
case "enter": return "default";
case "done": return "done";
case "go": return "go";
case "next": return "next";
case "previous": return "previous";
case "search": return "search";
case "send": return "send";
default: return void 0;
}
}
var autoCompleteMap = {
"additional-name": "middleName",
"address-line1": "streetAddressLine1",
"address-line2": "streetAddressLine2",
bday: "birthdate",
"bday-day": "birthdateDay",
"bday-month": "birthdateMonth",
"bday-year": "birthdateYear",
"cc-additional-name": "creditCardName",
"cc-csc": "creditCardSecurityCode",
"cc-exp": "creditCardExpiration",
"cc-exp-month": "creditCardExpirationMonth",
"cc-exp-year": "creditCardExpirationYear",
"cc-family-name": "creditCardFamilyName",
"cc-given-name": "creditCardGivenName",
"cc-name": "creditCardName",
"cc-number": "creditCardNumber",
"cc-type": "creditCardType",
country: "countryName",
"country-name": "countryName",
"current-password": "password",
email: "emailAddress",
"family-name": "familyName",
"given-name": "givenName",
honorific: "namePrefix",
"honorific-prefix": "namePrefix",
"honorific-suffix": "nameSuffix",
name: "name",
"new-password": "newPassword",
nickname: "nickname",
off: "none",
"one-time-code": "oneTimeCode",
organization: "organizationName",
"organization-title": "jobTitle",
password: "password",
"postal-code": "postalCode",
tel: "telephoneNumber",
"tel-area-code": "telephoneNumber",
"tel-country-code": "telephoneNumber",
"tel-extension": "telephoneNumber",
"tel-local": "telephoneNumber",
"tel-local-prefix": "telephoneNumber",
"tel-local-suffix": "telephoneNumber",
"tel-national": "telephoneNumber",
url: "URL",
username: "username"
};
function autoCompleteToTextContentType(autoComplete) {
if (!autoComplete) return void 0;
return autoCompleteMap[autoComplete];
}
function iosKeyboardType(value) {
switch (value) {
case "ascii-capable": return "asciiCapable";
case "numbers-and-punctuation": return "numbersAndPunctuation";
case "url": return "url";
case "number-pad": return "numberPad";
case "phone-pad": return "phonePad";
case "name-phone-pad": return "namePhonePad";
case "email-address": return "emailAddress";
case "decimal-pad": return "decimalPad";
case "twitter": return "twitter";
case "web-search": return "webSearch";
case "numeric": return "decimalPad";
default: return "default";
}
}
function iosSubmitLabel(value) {
if (value === "google" || value === "yahoo") return "search";
if (value === "default" || value === "none" || value === "previous" || value === "emergency-call") {
return "return";
}
return value;
}
function androidKeyboardType(value) {
switch (value) {
case "email-address": return "email";
case "numeric":
case "decimal-pad": return "decimal";
case "number-pad": return "number";
case "phone-pad": return "phone";
case "url": return "url";
default: return "default";
}
}
function androidImeAction(value) {
if (value === "google" || value === "yahoo") return "search";
if (value === "join" || value === "route" || value === "emergency-call") return "default";
return value;
}
function applyMaxLength(text, maxLength) {
if (maxLength == null || maxLength <= 0) return text;
return text.length > maxLength ? text.slice(0, maxLength) : text;
}
function useTextInputFocus(param) {
var { autoFocus, editable, onFocus, onBlur } = param;
var [focused, setFocused] = useState(autoFocus !== null && autoFocus !== void 0 ? autoFocus : false);
var [focusRevision, setFocusRevision] = useState(0);
var isFocusedRef = useRef(false);
var latest = useRef({
editable,
onFocus,
onBlur
});
latest.current = {
editable,
onFocus,
onBlur
};
var report = function report2(next) {
var _latest_current_onFocus, _latest_current, _latest_current_onBlur, _latest_current1;
if (isFocusedRef.current === next) return;
isFocusedRef.current = next;
if (next) (_latest_current_onFocus = (_latest_current = latest.current).onFocus) === null || _latest_current_onFocus === void 0 ? void 0 : _latest_current_onFocus.call(_latest_current);
else (_latest_current_onBlur = (_latest_current1 = latest.current).onBlur) === null || _latest_current_onBlur === void 0 ? void 0 : _latest_current_onBlur.call(_latest_current1);
};
var request = function request2(next) {
if (next && !latest.current.editable) return;
setFocused(next);
setFocusRevision(function(revision) {
return revision + 1;
});
report(next);
};
useEffect(function() {
if (autoFocus && latest.current.editable) report(true);
}, []);
return {
focused,
focusRevision,
focus: function focus() {
return request(true);
},
blur: function blur() {
return request(false);
},
isFocused: function isFocused() {
return isFocusedRef.current;
},
handleFocusChange: function handleFocusChange(next) {
setFocused(next);
report(next);
}
};
}
export { androidImeAction, androidKeyboardType, applyMaxLength, autoCompleteToTextContentType, enterKeyHintToReturnKeyType, inputModeToKeyboardType, iosKeyboardType, iosSubmitLabel, resolveEditable, useTextInputFocus };
//# sourceMappingURL=textInputShared.native.js.map