editor.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import Field, { getValidationRules } from './_field';
  2. import { useFormContext } from 'react-hook-form';
  3. import { InputMulti } from 'app/design/controls'
  4. import { useState } from 'react';
  5. import RftText from './editor-rft-text';
  6. import { useFormField } from 'app/lib/form/use-form-field';
  7. export default function FormFieldText(props) {
  8. const formContext = useFormContext();
  9. return (
  10. <Field {...props} error2={formContext.formState.errors[props.name]}>
  11. {props.html == 1 || props.html == 2 || props.html == 3 ? <>
  12. <RftText {...props} />
  13. </> : <PlainText {...props} />}
  14. </Field>
  15. );
  16. }
  17. function PlainText(props) {
  18. const {
  19. name,
  20. field,
  21. placeholder,
  22. readOnly,
  23. placeholderTextColor,
  24. onFocus,
  25. onBlur,
  26. } = useFormField(props, {
  27. rules: getValidationRules(props),
  28. returnKey: false,
  29. });
  30. // Use smaller initial height for comments forms
  31. const isCommentsForm = props.container_class === 'comments';
  32. const initialHeight = isCommentsForm ? 24 : null;
  33. let h = props.height ? props.height : initialHeight;
  34. const [height, setHeight] = useState(h);
  35. const accessibility = props.caption.length > 0 ? props.caption : 'text';
  36. const isAutoHeight = true;
  37. const minHeightValue = isCommentsForm ? 24 : 100;
  38. const maxHeightValue = 200;
  39. const clampedHeight = Math.min(Math.max(height ?? minHeightValue, minHeightValue), maxHeightValue);
  40. return (
  41. <InputMulti
  42. multiline
  43. name={name}
  44. placeholder={placeholder}
  45. placeholderTextColor={placeholderTextColor}
  46. onChangeText={field.onChange}
  47. onFocus={onFocus}
  48. onBlur={onBlur}
  49. autoFocus={props.autofocus}
  50. value={String(field.value ?? '')}
  51. readOnly={readOnly}
  52. editable={!readOnly}
  53. aria-label={accessibility}
  54. onContentSizeChange={e => {
  55. if (isAutoHeight) {
  56. const next = Math.min(
  57. Math.max(e.nativeEvent.contentSize.height, minHeightValue),
  58. maxHeightValue
  59. );
  60. setHeight(prev => (prev === next ? prev : next));
  61. }
  62. }}
  63. style={isAutoHeight ? { height: clampedHeight, minHeight: minHeightValue, maxHeight: maxHeightValue } : {}}
  64. />
  65. );
  66. }