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 non-restrictive palette choices option #73

Conversation

rniem379349
Copy link
Contributor

addresses Issue #68
This PR proposes a new field option which works similarly to django's choices kwarg, the difference being that the field's values are not restricted to the given choices. This way, one can use both the color palette and the color picker. Until now, with choices defined, the color picker is effectively useless, since we cannot choose values outside of the palette.

The supplied screenshot shows this logic in action in a sample django project. It illustrates the ability to save a color value outside the predefined choices. I realise screenshots are not necessarily always accurate - there is always a possibility that I doctored/edited it - so I have also added a test that cleans a model instance with palette_choices set and a supplied value outside those choices. Feel free to run the code yourself as well and test it all out.

I have replaced the choices option with the new option here. Please let me know if we should keep the original choices as well. Perhaps adding some logic to make the two options mutually exclusive would be a good idea.

image

@fabiocaccamo fabiocaccamo self-assigned this Dec 4, 2021
Copy link
Owner

@fabiocaccamo fabiocaccamo left a comment

Choose a reason for hiding this comment

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

Please, could you rename the option to samples as discussed in #68 ?

Then I will test your PR as soon as possible, thanks in advance!

@rniem379349 rniem379349 force-pushed the feature/add-non-restrictive-palette-choices branch 2 times, most recently from 118a0a2 to 32d64be Compare December 5, 2021 08:56
@rniem379349
Copy link
Contributor Author

Done :) thanks for the quick response

Copy link
Owner

@fabiocaccamo fabiocaccamo left a comment

Choose a reason for hiding this comment

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

This is out of scope for this PR... but as plus, when using choices the color-widget spectrum should be hidden, allowing the user to choose only from the choices, but this is hard to do even by dirty-patching the widget via css.
If you can try to do it it would be very appreciated.

if self.choices:
choices = self.get_choices(include_blank=False)
palette = [choice[0] for choice in choices]
if self.samples:
Copy link
Owner

Choose a reason for hiding this comment

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

The choices options has been removed, this is not backward-compatible at all.
We should keep both options: choices (restrictive) and samples (not restrictive).

kwargs['widget'] = ColorWidget(attrs={
'default': self.get_default(),
'format': self.format,
'palette': palette,
})
return super(ColorField, self).formfield(**kwargs)

def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
Copy link
Owner

Choose a reason for hiding this comment

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

Please pass the class and self to the super call for backward-compatibility.

@rniem379349 rniem379349 force-pushed the feature/add-non-restrictive-palette-choices branch from 32d64be to 9382e24 Compare December 6, 2021 17:40
@@ -53,12 +55,23 @@ def __init__(self, *args, **kwargs):

def formfield(self, **kwargs):
palette = []
if self.choices and self.samples:
raise ImproperlyConfigured("You can only set one of 'choices' and 'samples' for ColorFields.")
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe is it more correct to raise this exception in the init method?

@rniem379349 rniem379349 force-pushed the feature/add-non-restrictive-palette-choices branch from 9382e24 to 4f9ff26 Compare December 6, 2021 18:30
@fabiocaccamo fabiocaccamo merged commit f1a9ed5 into fabiocaccamo:master Dec 6, 2021
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.

None yet

2 participants