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

How to show rating value on full/rating symbol. #31

Open
MakarovAV opened this issue Nov 23, 2016 · 6 comments
Open

How to show rating value on full/rating symbol. #31

MakarovAV opened this issue Nov 23, 2016 · 6 comments

Comments

@MakarovAV
Copy link

MakarovAV commented Nov 23, 2016

I want to create a rating labelled with numbers of corresponding rate.
Something like this: [1][2][3][4]
"empty" and "full" elements are custom React components, so I am using component this way:

<Rating 
    start = {0}
    stop = {4}
    onChange = {rate => this.applyRate(rate, index)}
    empty = {<EmptyRating/>}
    full  = {<FullRating/>}
 />

But I cant figure out how to access corresponding rate number from my "EmptyRating" and "FullRating" components.
Is there any way to achieve it?

@dreyescat
Copy link
Owner

I am not sure to understand what you mean... Are your <EmtpyRating/> and <FullRating/> also Rating components?

Do you mean having a rating where the symbols correspond to the rate number they represent? For example, a rating of 4 would be something like this:

[1] [2] [3] [4]

Then, for example, selecting 3 rate would appear like this:

[*] [*] [*] [4]

Or just changing the color of the rate to another color.

@MakarovAV
Copy link
Author

MakarovAV commented Nov 23, 2016

Are your and also Rating components?

No, just my custom components

Do you mean having a rating where the symbols correspond to the rate number they represent?

yes

At the moment I already checked the source and unfortunately did not found this feature "from the box".

As quick and very dirty workaround I created an array of my components

var emptyArr =  [<EmptyRating index={1}/>, <EmptyRating index={2}>]
<Rating 
    empty = {<EmptyRating/>}
 />

It is working, but now I have console warning.

Warning: Failed prop type: Invalid prop empty supplied to Rating

@dreyescat
Copy link
Owner

You can pass an array of symbols to empty and full properties. It is OK. It is the way to customize each and every one of the symbols at your will.

I suppose you meant:

var emptyArr =  [<EmptyRating index={1}/>, <EmptyRating index={2}>]
<Rating 
    empty = {emptyArr}
 />

Mind the empty property. Right?

BTW:
A quick example with numerical react span components as symbols.

<Rating start={0} stop={4}
  empty={[1,2,3,4].map(n => <span className="icon-text">{n}</span>)}
  full={[1,2,3,4].map(n => <span className="icon-text icon-selected">{n}</span>)}
  />

Check the jsfiddle

@dreyescat
Copy link
Owner

dreyescat commented Nov 23, 2016

Ah! And regarding the warning:

Warning: Failed prop type: Invalid prop empty supplied to Rating

This is probably because of how propTypes are defined for empty and full properties. Right now they only consider having an array of strings or objects. And maybe a React element is not considered an object.

Should not be an issue because it is only a propTypes complain. I will look into it later. If you want you can play around with React.PropTypes. The part where types are defined is here.

@MakarovAV
Copy link
Author

MakarovAV commented Nov 23, 2016

I suppose you meant:

Yeah, misstyped)

It is OK.

It is ok cause it works, but It seems kinda clumsy for me. I expected corresponding rate to be passed into react component.

A quick example with numerical react span components as symbols.

It is a bit more complicated in my case, but thanks for help)

And maybe a React element is not considered an object.

Yes, there is React.PropTypes.element

dreyescat added a commit that referenced this issue Nov 26, 2016
This warning was revealed in issue #31.
@dreyescat
Copy link
Owner

It is ok cause it works, but It seems kinda clumsy for me. I expected corresponding rate to be passed into react component.

Yep, it works because this is the way it was designed to work. For your use case it might seem clumsy because you are using exactly the same symbol with different rates. But, you could use a completely different symbol for each rate, regardless the rate. I mean a star, a heart, a circle, and a thumb up. All them in the same rating.

Another thing is that we could add a way to make it easier to solve this use case, that might be more frequent than having a mix of different ones. Or even better. We could move this decission to the user through a function. I mean, passing a function to the empty, full, and placeholder that will allow the caller to customize the symbol. Sort of symbol builder. This function could pass the rate and/or index as parameter allowing you to decide what symbol to show in each case. This is just a brainstorm but I will look into this option. I feel that it could increase the flexibility a lot.

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

No branches or pull requests

2 participants