The Wayback Machine - https://web.archive.org/web/20201109171724/https://github.com/downshift-js/downshift/pull/264
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

Add warning when user use Object for selectedItem #264

Merged

Conversation

@philipyoungg
Copy link
Contributor

@philipyoungg philipyoungg commented Nov 27, 2017

What: Add console.warn when user passed Object to selectedItem

Why: To improve DX—to minimize a case when user get [object Object] as an input value. Almost refactored my whole codebase. I thought I can't use Object as a selected value. Thankfully I found itemToString API that accept a function with current selectedItem passed on the argument.

How: Two things:

  1. Adding simple isPlainObject function that check if the selectedItem is Object literal, and
  2. use that function inside itemToString's defaultProps

Checklist:

  • [N/A] Documentation
  • Tests
  • Ready to be merged
  • Added myself to contributors table
Copy link
Member

@kentcdodds kentcdodds left a comment

Thanks! A few requests.

@@ -67,7 +68,12 @@ class Downshift extends Component {
defaultIsOpen: false,
getA11yStatusMessage,
id: generateId('downshift'),
itemToString: i => (i == null ? '' : String(i)),
itemToString: i => {
if (isPlainObject(i)) {

This comment has been minimized.

@kentcdodds

kentcdodds Nov 27, 2017
Member

I'd prefer this to only happen in dev. I think the build is wired up to handle process.env.NODE_ENV properly. Could you make this: if (process.env.NODE_ENV !== 'production' && isPlainObject(i)) {

This comment has been minimized.

@philipyoungg

philipyoungg Nov 27, 2017
Author Contributor

Would process.env.NODE_ENV === 'development' more prefereable in this instance?

itemToString: i => (i == null ? '' : String(i)),
itemToString: i => {
if (isPlainObject(i)) {
console.warn('you passed an Object as the value of `selectedItem`. Please refer to `itemToString` API documentation on Downshift\'s repository.')

This comment has been minimized.

@kentcdodds

kentcdodds Nov 27, 2017
Member

I think prettier should have reformatted this automatically when you committed. Did you npm install?

@@ -67,7 +68,12 @@ class Downshift extends Component {
defaultIsOpen: false,
getA11yStatusMessage,
id: generateId('downshift'),
itemToString: i => (i == null ? '' : String(i)),
itemToString: i => {

This comment has been minimized.

@kentcdodds

kentcdodds Nov 27, 2017
Member

I'd like to have a test for this. Could you add one in downshift.misc.js? Just do jest.spyOn(console, 'warn') on the first line of the test, then console.warn.mockRestore() on the last line to clean up. You could even use a snapshot if you want: expect(console.warn).toHaveBeenCalledTimes(1) and then expect(console.warn.mock.calls).toMatchSnapshot()

This comment has been minimized.

@philipyoungg

philipyoungg Nov 27, 2017
Author Contributor

Sure!

@philipyoungg
Copy link
Contributor Author

@philipyoungg philipyoungg commented Nov 27, 2017

update

  • added test
  • run function only when it's on development
@codecov-io
Copy link

@codecov-io codecov-io commented Nov 27, 2017

Codecov Report

Merging #264 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #264   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           4      4           
  Lines         299    304    +5     
  Branches       72     73    +1     
=====================================
+ Hits          299    304    +5
Impacted Files Coverage Δ
src/downshift.js 100% <100%> (ø) ⬆️
src/utils.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a45294a...0209811. Read the comment docs.

kentcdodds added 2 commits Nov 29, 2017
Copy link
Member

@kentcdodds kentcdodds left a comment

I made a few updates. This is a great addition. Thank you @philipyoungg

@kentcdodds kentcdodds merged commit 124885a into downshift-js:master Nov 29, 2017
1 of 2 checks passed
1 of 2 checks passed
continuous-integration/travis-ci/pr The Travis CI build is in progress
Details
deploy/netlify Deploy preview ready!
Details
Rendez added a commit to Rendez/downshift that referenced this pull request Sep 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants
You can’t perform that action at this time.