prismicio / prismic-react

React components and hooks to fetch and present Prismic content
https://prismic.io/docs/technologies/homepage-reactjs
Apache License 2.0
154 stars 40 forks source link

Add `fallback` prop to `<PrismicText>` #105

Closed angeloashmore closed 2 years ago

angeloashmore commented 2 years ago

Is your feature request related to a problem? Please describe.

<PrismicText> does not have a way to fallback to a default text value if the given field is falsey (i.e. empty).

Describe the solution you'd like

A prop that renders if the text value of the given field is falsey. The prop could accept a string or any valid React.Element.

<PrismicText
  field={document.data.text_field}
  fallback="Learn more…"
/>

Describe alternatives you've considered

To do this today, one must use asText from @prismicio/helpers instead with a {} expression.

<div>
  {prismicH.asText(callToActionText) || "Learn more…"}
</div>

This isn't terrible, but switching between <PrismicText> and asText is awkward.

Additional context

N/A

angeloashmore commented 2 years ago

This is implemented in #126.