Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Adds defaultRichTextElements prop #119

Merged
merged 4 commits into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .storybook/stories/01-Message.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ const meta = {
title: 'Example/01 Message'
};

const messagesRichText = {
en: {
message:
'Something <strong>important</strong>.<br></br>Something <underline>underlined</underline>.'
},
es: {
message:
'Algo <strong>importante</strong>.<br></br>Algo <underline>subrayado</underline>.'
}
};

const getMessages = (locale) => messagesRichText[locale];

export default meta;

export const Default = () => {
Expand All @@ -18,3 +31,18 @@ PerStory.parameters = {
locales: [...defaultLocales, 'es']
}
};

export const RichTextElements = Default.bind(null);
RichTextElements.parameters = {
intl: {
locales: ['en', 'es'],
getMessages,
defaultRichTextElements: {
br: () => <br />,
strong: (text) => <strong>{text}</strong>,
underline: (text) => (
<span style={{ textDecoration: 'underline' }}>{text}</span>
)
}
}
};
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,22 @@ Fallback locale.

Type: `(locale: string) => object`

Getter function that takes the active locale as arguments and expects an object of messages as return value.
Getter function that takes the active locale as arguments and expects an `object` of messages as a return value.

<small>(See `messages` in [`IntlProvider` docs](https://formatjs.io/docs/react-intl/components#intlprovider) of react-intl)</small>

### `getFormats`

Type: `(locale: string) => object`

Getter function that takes the active locale as arguments and expects an formats objects as return value.
Getter function that takes the active locale as arguments and expects an `object` of formats as a return value.

<small>(See `formats` in [`IntlProvider` docs](https://formatjs.io/docs/react-intl/components#intlprovider) of react-intl)</small>

### `defaultRichTextElements`

Type: `object`

Object of rich text elements.

<small>(See `defaultRichTextElements` in [`IntlProvider` docs](https://formatjs.io/docs/react-intl/components#intlprovider) of react-intl)</small>
3 changes: 2 additions & 1 deletion src/preview/decorators/withIntl.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function withIntl(StoryFn, context) {
return null;
}

const { getMessages, getFormats } = intlConfig;
const { getMessages, getFormats, defaultRichTextElements } = intlConfig;

const messages = getMessages(activeLocale);
const formats =
Expand All @@ -36,6 +36,7 @@ export function withIntl(StoryFn, context) {
locale={activeLocale}
messages={messages}
formats={formats}
defaultRichTextElements={defaultRichTextElements}
>
{StoryFn()}
</IntlProvider>
Expand Down
6 changes: 5 additions & 1 deletion src/utils/validateConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ export function validateConfig(config) {
return 'invalid-get-messages';
} else if (!!config.getFormats && typeof config.getFormats !== 'function') {
return 'invalid-get-formats';
} else if (
!!config.defaultRichTextElements &&
typeof config.defaultRichTextElements !== 'object'
) {
return 'invalid-rich-text-elements';
}

return null;
}