value.js 790 B

12345678910111213141516171819202122232425
  1. import Field from './_field';
  2. import Html from 'app/ui/atoms/html';
  3. import { useFormField } from 'app/lib/form/use-form-field';
  4. import { looksLikeHtml, stripTags } from 'app/lib/util';
  5. function normalize(s) {
  6. return stripTags(s)?.trim() || '';
  7. }
  8. export default function FormFieldValue(props) {
  9. const { field } = useFormField(props);
  10. const value = field.value;
  11. const isHtml = looksLikeHtml(value);
  12. const caption = normalize(props.caption);
  13. const text = isHtml ? String(value ?? '') : normalize(value);
  14. const label = caption ? `${caption}:` : '';
  15. if (!text) return null;
  16. return (
  17. <Field {...props} caption={isHtml ? label : [label, text].filter(Boolean).join(' ')}>
  18. {isHtml ? <Html data={value} /> : null}
  19. </Field>
  20. );
  21. }