UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

170 lines (167 loc) • 5.19 kB
import { useEffect, useRef, useState } from "react"; function resolveEditable(editable, readOnly) { if (readOnly) return false; return 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; } } const 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({ autoFocus, editable, onFocus, onBlur }) { const [focused, setFocused] = useState(autoFocus ?? false); const [focusRevision, setFocusRevision] = useState(0); const isFocusedRef = useRef(false); const latest = useRef({ editable, onFocus, onBlur }); latest.current = { editable, onFocus, onBlur }; const report = (next) => { if (isFocusedRef.current === next) return; isFocusedRef.current = next; if (next) latest.current.onFocus?.(); else latest.current.onBlur?.(); }; const request = (next) => { if (next && !latest.current.editable) return; setFocused(next); setFocusRevision((revision) => revision + 1); report(next); }; useEffect(() => { if (autoFocus && latest.current.editable) report(true); }, []); return { focused, focusRevision, focus: () => request(true), blur: () => request(false), isFocused: () => isFocusedRef.current, handleFocusChange: (next) => { setFocused(next); report(next); } }; } export { androidImeAction, androidKeyboardType, applyMaxLength, autoCompleteToTextContentType, enterKeyHintToReturnKeyType, inputModeToKeyboardType, iosKeyboardType, iosSubmitLabel, resolveEditable, useTextInputFocus }; //# sourceMappingURL=textInputShared.mjs.map