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

feat: add formatList & FormattedList #1494

Merged
merged 1 commit into from
Nov 1, 2019
Merged

feat: add formatList & FormattedList #1494

merged 1 commit into from
Nov 1, 2019

Conversation

longlho
Copy link
Member

@longlho longlho commented Sep 27, 2019

List Formatting APIs

This is currently stage 3 so polyfill would be required.

formatList

type ListFormatOptions = {
  type?: 'disjunction' | 'conjunction' | 'unit';
  style?: 'long' | 'short' | 'narrow';
};

function formatPlural(
  elements: (string | React.ReactNode)[],
  options?: Intl.ListFormatOptions
): string | React.ReactNode[];

This function allows you to join list of things together in an i18n-safe way. For example:

formatList(['Me', 'myself', 'I'], {type: 'conjunction'}); // Me, myself and I
formatList(['5 hours', '3 minues'], {type: 'unit'}); // 5 hours, 3 minutes

List Formatting Components

FormattedList

This component uses formatList API and Intl.ListFormat. Its props corresponds to Intl.ListFormatOptions.

Props:

props: ListFormatOptions &
  {
    children: (chunksOrString: string | React.ReactElement[]) => ReactElement,
  };

Example:

<FormattedList type="conjunction" value={['Me', 'myself', 'I']} />
Me, myself and I
<FormattedList type="conjunction" value={['Me', <b>myself</b>, 'I']} />
Me, <b>myself</b> and I

fix #1328

Things to do:

  • FormattedList
  • Unit tests
  • Docs

@longlho longlho changed the title [WIP] feat: add formatList & ListFormat feat: add formatList & FormattedList Oct 24, 2019
@longlho longlho force-pushed the list branch 2 times, most recently from 243af6f to 2f81566 Compare October 24, 2019 02:59
@longlho longlho requested a review from redonkulus October 24, 2019 03:27
@longlho longlho merged commit f5eacbf into master Nov 1, 2019
@longlho longlho deleted the list branch November 1, 2019 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add <FormattedList> & formatList
1 participant