UNPKG

one

Version:

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

200 lines (199 loc) • 6.39 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var textInputShared_exports = {}; __export(textInputShared_exports, { androidImeAction: () => androidImeAction, androidKeyboardType: () => androidKeyboardType, applyMaxLength: () => applyMaxLength, autoCompleteToTextContentType: () => autoCompleteToTextContentType, enterKeyHintToReturnKeyType: () => enterKeyHintToReturnKeyType, inputModeToKeyboardType: () => inputModeToKeyboardType, iosKeyboardType: () => iosKeyboardType, iosSubmitLabel: () => iosSubmitLabel, resolveEditable: () => resolveEditable, useTextInputFocus: () => useTextInputFocus }); module.exports = __toCommonJS(textInputShared_exports); var import_react = require("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] = (0, import_react.useState)(autoFocus ?? false); const [focusRevision, setFocusRevision] = (0, import_react.useState)(0); const isFocusedRef = (0, import_react.useRef)(false); const latest = (0, import_react.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); }; (0, import_react.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); } }; }