Re: Proposal: Add utility class "ClassList"

2022-09-30 Thread Jacob Rief
Hey Carlton,
in my opinion, the main use case would be to improve type safety, 
readability and to add a
utility class for 3rd party packages. Someday, Django may add type hints 
and then this would
be really beneficial.

In Django itself, the mentioned method css_classes 

 does 
not offer any type safety;
the passed in argument extra_classes can be None, a string containing a 
single
CSS class or list containing those classes.

By adding such a utility class, we can rewrite the above method to

css_classes(extra_classes: ClassList):
# optionally: extra_classes = ClassList(extra_classes)
extra_classes.toggle(getattr(self.form, "error_css_class", None), 
self.errors)
extra_classes.toggle(getattr(self.form, "required_css_class", None), 
self.field.required)
return extra_classes

which is much easier to read.

In my project django-formset , I 
was able to remove a lot of boilerplate, by introducing that class.

Naming that Python class ClassSet or CSSClassSet or CSSClasses might be a 
better option.
I used ClassList because that's the name in JavaScript.

– Jacob  


On Thursday, September 29, 2022 at 9:35:02 AM UTC+1 Carlton Gibson wrote:

> Hey Jacob. Thanks for this. 
>
> Can I ask you to give a few examples of potential usages in Django, and 
> showing the gain over use a set in these cases? 
>
> I'm trying to imagine exactly what you have in mind, but I'm not entirely 
> clear. 
>
> Thanks again. 
> Carlton
>
> On Friday, 23 September 2022 at 11:14:27 UTC+2 Jacob Rief wrote:
>
>> In JavaScript each HTMLElement has a property named classList 
>> . 
>> This actually is a set allowing to *add* a single CSS class string, 
>> *remove* it
>> and/or *toggle* it.
>>
>> If we would reimplement this as a Python class, methods such as
>> css_classes 
>> 
>>  
>> could be implemented as a one-liner. This would also be beneficial
>> for future uses of similar methods in Django and 3rd party libraries, 
>> because it is a quite
>> common use case that one has to change the list of CSS classes as an 
>> element
>> attribute.
>>
>> – Jacob
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/6d01cf69-23eb-4e8a-a13e-b29f26deab7an%40googlegroups.com.


Re: Proposal: Add utility class "ClassList"

2022-09-29 Thread Carlton Gibson
Hey Jacob. Thanks for this. 

Can I ask you to give a few examples of potential usages in Django, and 
showing the gain over use a set in these cases? 

I'm trying to imagine exactly what you have in mind, but I'm not entirely 
clear. 

Thanks again. 
Carlton

On Friday, 23 September 2022 at 11:14:27 UTC+2 Jacob Rief wrote:

> In JavaScript each HTMLElement has a property named classList 
> . 
> This actually is a set allowing to *add* a single CSS class string, 
> *remove* it
> and/or *toggle* it.
>
> If we would reimplement this as a Python class, methods such as
> css_classes 
> 
>  
> could be implemented as a one-liner. This would also be beneficial
> for future uses of similar methods in Django and 3rd party libraries, 
> because it is a quite
> common use case that one has to change the list of CSS classes as an 
> element
> attribute.
>
> – Jacob
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/d63a27a1-6a67-403e-9ecd-f03b19e8660dn%40googlegroups.com.


Proposal: Add utility class "ClassList"

2022-09-23 Thread Jacob Rief
In JavaScript each HTMLElement has a property named classList 
. 
This actually is a set allowing to *add* a single CSS class string, *remove* 
it
and/or *toggle* it.

If we would reimplement this as a Python class, methods such as
css_classes 

 
could be implemented as a one-liner. This would also be beneficial
for future uses of similar methods in Django and 3rd party libraries, 
because it is a quite
common use case that one has to change the list of CSS classes as an element
attribute.

– Jacob

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/eecbea4e-84f8-4aef-8998-26ff96a82bc0n%40googlegroups.com.