text.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import Field from './_field';
  2. import { Input } from 'app/design/controls'
  3. import { useFormField } from 'app/lib/form/use-form-field';
  4. function getTextFieldInputMode(props) {
  5. const caption = String(props.caption || '');
  6. const name = String(props.name || '');
  7. const attrType = String(props.attrs?.type || props.type || '');
  8. const looksEmail =
  9. attrType === 'email' ||
  10. /e-?mail/i.test(caption) ||
  11. /e-?mail/i.test(name);
  12. const looksUsername =
  13. name === 'ID' ||
  14. name === 'login' ||
  15. name === 'username' ||
  16. /user\s*name/i.test(caption);
  17. if (looksEmail || looksUsername) {
  18. const useEmail = looksEmail && !looksUsername;
  19. return {
  20. keyboardType: 'email-address',
  21. autoComplete: useEmail ? 'email' : 'username',
  22. textContentType: useEmail ? 'emailAddress' : 'username',
  23. };
  24. }
  25. return {
  26. keyboardType: 'default',
  27. autoComplete: props.attrs?.autocomplete || 'off',
  28. textContentType: 'none',
  29. };
  30. }
  31. export default function FormFieldText(props) {
  32. const {
  33. name,
  34. field,
  35. placeholder,
  36. readOnly,
  37. placeholderTextColor,
  38. focused,
  39. onFocus,
  40. onBlur,
  41. inputRef,
  42. returnKeyProps,
  43. isAdaptiveLabel,
  44. inputId,
  45. errorId,
  46. invalid,
  47. } = useFormField(props);
  48. const inputMode = getTextFieldInputMode(props);
  49. return (
  50. <Field {...props} value={field.value} focused={focused} isAdaptiveLabel={isAdaptiveLabel}>
  51. <Input
  52. ref={inputRef}
  53. id={inputId}
  54. nativeID={inputId}
  55. textContentType={inputMode.textContentType}
  56. autoComplete={inputMode.autoComplete}
  57. autoCorrect={false}
  58. spellCheck={false}
  59. secureTextEntry={false}
  60. keyboardType={inputMode.keyboardType}
  61. autoFocus={props.auto_focus}
  62. name={name}
  63. readOnly={readOnly}
  64. placeholder={placeholder}
  65. placeholderTextColor={placeholderTextColor}
  66. onChangeText={field.onChange}
  67. onFocus={onFocus}
  68. onBlur={onBlur}
  69. value={String(field.value)}
  70. aria-label={props.caption}
  71. aria-invalid={invalid || undefined}
  72. aria-describedby={errorId}
  73. {...(props.checker?.params?.max ? { maxLength: props.checker.params.max } : {})}
  74. {...returnKeyProps}
  75. />
  76. </Field>
  77. );
  78. }