text-translatable.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import Field, { getValidationRules } from './_field';
  2. import { Input } from 'app/design/controls';
  3. import { View } from 'app/design/view';
  4. import { useFormField } from 'app/lib/form/use-form-field';
  5. import {
  6. getMaxLength,
  7. useTranslatableField,
  8. TranslatableFieldLayout,
  9. } from 'app/lib/form/form-fields-helpers';
  10. function LangInput({
  11. fieldName,
  12. defaultValue,
  13. rules,
  14. autoFocus,
  15. maxLength,
  16. lang,
  17. active,
  18. caption,
  19. placeholder: placeholderProp,
  20. use_caption_as_placeholder,
  21. attrs,
  22. handleSubmit,
  23. type,
  24. }) {
  25. const {
  26. field,
  27. placeholder,
  28. readOnly,
  29. placeholderTextColor,
  30. onFocus,
  31. onBlur,
  32. inputRef,
  33. returnKeyProps,
  34. } = useFormField(
  35. {
  36. name: fieldName,
  37. value: defaultValue,
  38. caption,
  39. placeholder: placeholderProp,
  40. use_caption_as_placeholder,
  41. attrs,
  42. handleSubmit,
  43. type,
  44. },
  45. {
  46. defaultValue: defaultValue ?? '',
  47. rules,
  48. syncValue: false,
  49. returnKey: !!active,
  50. }
  51. );
  52. return (
  53. <View
  54. className={active ? 'w-full' : 'hidden'}
  55. aria-hidden={!active}
  56. >
  57. <Input
  58. ref={inputRef}
  59. textContentType="none"
  60. autoComplete="off"
  61. autoCorrect={false}
  62. spellCheck={false}
  63. secureTextEntry={false}
  64. keyboardType="default"
  65. autoFocus={autoFocus}
  66. name={fieldName}
  67. readOnly={readOnly}
  68. placeholder={placeholder}
  69. placeholderTextColor={placeholderTextColor}
  70. onChangeText={field.onChange}
  71. onFocus={onFocus}
  72. onBlur={onBlur}
  73. value={String(field.value ?? '')}
  74. aria-label={lang ? `${caption} (${lang})` : caption}
  75. {...(maxLength ? { maxLength } : {})}
  76. {...returnKeyProps}
  77. />
  78. </View>
  79. );
  80. }
  81. export default function FormFieldTextTranslatable(props) {
  82. const { entries, currentFieldName, setActiveFieldName } =
  83. useTranslatableField(props);
  84. const rules = getValidationRules(props);
  85. const maxLength = getMaxLength(props.checker);
  86. if (!entries.length) return null;
  87. return (
  88. <Field {...props}>
  89. <TranslatableFieldLayout
  90. entries={entries}
  91. currentFieldName={currentFieldName}
  92. setActiveFieldName={setActiveFieldName}
  93. >
  94. {entries.map((entry) => (
  95. <LangInput
  96. key={entry.fieldName}
  97. fieldName={entry.fieldName}
  98. lang={entry.title || entry.lang}
  99. defaultValue={entry.defaultValue}
  100. rules={rules}
  101. autoFocus={
  102. entry.fieldName === currentFieldName
  103. ? props.auto_focus
  104. : false
  105. }
  106. maxLength={maxLength}
  107. active={entry.fieldName === currentFieldName}
  108. caption={props.caption}
  109. placeholder={props.placeholder}
  110. use_caption_as_placeholder={
  111. props.use_caption_as_placeholder
  112. }
  113. attrs={props.attrs}
  114. handleSubmit={props.handleSubmit}
  115. type={props.type}
  116. />
  117. ))}
  118. </TranslatableFieldLayout>
  119. </Field>
  120. );
  121. }