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

Optgroup feature for select and multipleSelect Widgets #145

Closed
wants to merge 2 commits into from

Conversation

tolleiv
Copy link

@tolleiv tolleiv commented Oct 22, 2014

Hi,

thanks for this great form framework. I missed an option to have "optgroups" within the select widgets and therefore added an option to have them.

For now I've kept the functions to render the groups and options separate per widget, but that could be refactored later on to have a single rendering function for both.

I've also added basic tests, which also show the configuration syntax.

Would be great to get your feedback.
Cheers,
Tolleiv

@ljharb
Copy link
Collaborator

ljharb commented Oct 22, 2014

Instead of:

choices: {
  foo: 'bar',
  group1: {
    label: 'Hamster Dance',
    choices: {
      1: 'one',
      2: 'two'
    }
  }
}

as a data format to create an optgroup, what would you think about this format:

choices: {
  foo: 'bar',
  'Hamster Dance': {
    1: 'group1:one',
    2: 'group1:two'
  }
}

That might be more compact, and also would remove the implicit value munging that happens with "group1", and might also make the implementation a bit cleaner?

@tolleiv
Copy link
Author

tolleiv commented Oct 22, 2014

Your suggestion looks like my first attempt (which I tried locally) - but that made it harder to have just one function to render the options and made the code quite ugly. I also found it nice to have an option to specify the label explizit.

Regarding the value munging - you're right, I also don't like it, but it would still be needed in the case were the options are passed as an array:

choices: {
  foo: 'bar',
  'Hamster Dance': [
    'group1:one',
    'group1:two'
  ]
}

But I'm fine with your suggestion - that would mean that arrays can't be used in these cases.
I can try to come up with a changed pull request tomorrow. Just let me know what you think about the "array" issue?

Cheers

@ljharb
Copy link
Collaborator

ljharb commented Oct 22, 2014

I don't think the options should ever be passed as an array - imo the value and text should always be explicitly specified, which requires an object (since string parsing is an antipattern).

@tolleiv
Copy link
Author

tolleiv commented Oct 22, 2014

Ok ... then I'll go with your suggestion. But the new patch has to wait until tomorrow

@tolleiv
Copy link
Author

tolleiv commented Oct 23, 2014

I've adjusted the code and extended the tests a bit.

}
function choicesHTML(choices, value) {
return Object.keys(choices).reduce(function (html, k) {
if (typeof choices[k] == 'object') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please use is.object(choices[k]) here - typeof null is "object", and please always avoid == in any circumstances.

@ljharb
Copy link
Collaborator

ljharb commented Oct 23, 2014

Other than these two comments, this looks great. Thanks!

@ljharb ljharb self-assigned this Oct 23, 2014
@ljharb
Copy link
Collaborator

ljharb commented May 23, 2015

@tolleiv Not sure how this fell through the cracks - can you freshly rebase it on top of latest master if you're still interested in landing it?

@tolleiv tolleiv closed this Aug 30, 2015
@tolleiv tolleiv deleted the optgroup_feature branch August 30, 2015 13:54
@tolleiv tolleiv mentioned this pull request Aug 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants