Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

fix(localizecount): allow localized string for count in MenuSelect #657

Merged
merged 4 commits into from
Nov 27, 2017

Conversation

devwax
Copy link
Contributor

@devwax devwax commented Nov 27, 2017

Summary

MenuSelect prop-types only allow for type number for item.count. This makes it impossible to transformItems in the widget so that the count is localized, because .toLocaleString() returns a string which causes prop validation to fail.

Result
packages/react-instantsearch/src/components/MenuSelect.js
Changed MenuSelect items > item.count prop-type to accept string as well as number using PropTypes.oneOfType([...]).

Require-chaining w/ oneOfType keeps the prop mandatory regardless of type. See prop-types docs.

stories/MenuSelect.stories.js
Added with localized count MenuSelect story to Storybook to demonstrate concept.

packages/react-instantsearch/src/widgets/MenuSelect.js
Added localize count transformItems demonstration to MenuSelect @example docs.

@algobot
Copy link
Contributor

algobot commented Nov 27, 2017

Deploy preview ready!

Built with commit e1890a6

https://deploy-preview-657--react-instantsearch.netlify.com

@@ -27,6 +27,12 @@ import MenuSelectComponent from '../components/MenuSelect.js';
* >
* <MenuSelect
* attributeName="category"
* transformItems={items =>
* items.map(item => {
* item.count = (item.count + 1000).toLocaleString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this documentation is needed, it might be confusing why you're adding 1000

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with @Haroenv, the examples in the documentation try to be the most minimal possible. For more elaborate example we have Storybook. No need to demonstrate the usage of transform Items on the documentation page.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the input. I'll remove it and push the commit.

defaultRefinement={text('defaultSelectedItem', 'Bathroom')}
transformItems={items =>
items.map(item => {
item.count = (item.count + 1000).toLocaleString();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid to modify an object inside a map, try to always return a new object. Prefer something like:

items.map(({ count, ...item }) => ({
  ...item,
  count: count.toLocaleString()
}));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will update the PR. Thanks.

Copy link
Collaborator

@samouss samouss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution! 🎉

@samouss samouss merged commit 67ebd34 into algolia:master Nov 27, 2017
samouss added a commit that referenced this pull request Nov 27, 2017
<a name="4.3.0-beta.0"></a>
# [4.3.0-beta.0](v4.2.0...v4.3.0-beta.0) (2017-11-27)

### Bug Fixes

* **babelrc:** add a key for each env development, production, es ([#547](#547)) ([fa9528d](fa9528d))
* **localizecount:** allow localized string for count in MenuSelect ([#657](#657)) ([67ebd34](67ebd34))
* **react-router-example:** Properly update search query when using browser navigation ([#604](#604)) ([9ee6600](9ee6600))

### Features

* **refreshcache:** add prop refresh to InstantSearch instance ([#619](#619)) ([19f6de0](19f6de0))
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants