|
| 1 | +# Component Design |
| 2 | + |
| 3 | +## Field Rendering |
| 4 | + |
| 5 | +In field rendering, Django-Formify use components to render the HTML: |
| 6 | + |
| 7 | +``` |
| 8 | +Field Wrapper formify.tw.field_wrapper |
| 9 | + Label formify.tw.field_label |
| 10 | + Field |
| 11 | + Error And Help Text formify.tw.field_error_help_text |
| 12 | + Error formify.tw.field_error |
| 13 | + Help Text formify.tw.field_help_text |
| 14 | +``` |
| 15 | + |
| 16 | +Notes: |
| 17 | + |
| 18 | +1. Except `Field`, other parts are rendered using components, and each has its own logic. |
| 19 | +2. During the rendering, components can read variable from the `formify_helper` to control the rendering behavior. |
| 20 | + |
| 21 | +For example: |
| 22 | + |
| 23 | +```python |
| 24 | +form.formify_helper = FormifyHelper() |
| 25 | +form.formify_helper.field_wrapper_class = "md:flex md:items-center mb-6" |
| 26 | +form.formify_helper.label_container_class = "md:w-1/3" |
| 27 | +form.formify_helper.field_container_class = "md:w-2/3" |
| 28 | +``` |
| 29 | + |
| 30 | +After we set the above class, `formify.tw.field_wrapper` will use the above class to render something like this: |
| 31 | + |
| 32 | +```html |
| 33 | +<div class="md:flex md:items-center mb-6"> |
| 34 | + <div class="md:w-1/3"> |
| 35 | + <label for="id_email">Email</label> |
| 36 | + </div> |
| 37 | + <div class="md:w-2/3"> |
| 38 | + <input type="text" name="email" id="id_email"> |
| 39 | + </div> |
| 40 | +</div> |
| 41 | +``` |
| 42 | + |
| 43 | +We just easily changed the form layout to horizontal form, in clean way. |
| 44 | + |
| 45 | +## Customize |
| 46 | + |
| 47 | +All the field rending components are build using [django-viewcomponent](https://github.com/rails-inspire-django/django-viewcomponent), you can easily build your own components to fit your needs. |
| 48 | + |
| 49 | +For example, after you build your own `field wrapper` component, just override `field_wrapper_component` of the `formify_helper`, then it should work. |
| 50 | + |
| 51 | +## Layout |
| 52 | + |
| 53 | +The components in `django_formify.tailwind.layout` are all also built using `django-viewcomponent`. |
| 54 | + |
| 55 | +You can also build custom components to fit your needs, for example, if you want to use `Accordion` to generate complex form, you can build `Accordion` and `AccordionSection` components using `django-viewcomponent`. |
| 56 | + |
| 57 | +And then you can use them like this: |
| 58 | + |
| 59 | +```python |
| 60 | +class ExampleForm(forms.Form): |
| 61 | + |
| 62 | + def __init__(self, *args, **kwargs): |
| 63 | + super().__init__(*args, **kwargs) |
| 64 | + self.formify_helper = FormifyHelper() |
| 65 | + self.formify_helper.layout = Layout( |
| 66 | + Accordion( |
| 67 | + AccordionSection( |
| 68 | + ... |
| 69 | + ), |
| 70 | + AccordionSection( |
| 71 | + ... |
| 72 | + ), |
| 73 | + AccordionSection( |
| 74 | + ... |
| 75 | + ), |
| 76 | + dom_id="accordion-1" |
| 77 | + ), |
| 78 | + ) |
| 79 | +``` |
| 80 | + |
| 81 | +What is more, after creating `Accordion` component, you can also use them in normal web pages as well (not just in form rendering), which is convenient. |
| 82 | + |
| 83 | +## Django-ViewComponent |
| 84 | + |
| 85 | +`django-viewcomponen` provides solution for developer to build reusable components in Django, the biggest advantage in this case is that: **developers can use it to create components, which are used in both Django Templates and Python Code** |
| 86 | + |
| 87 | +In field rendering, we can use component in the django template: |
| 88 | + |
| 89 | +```html |
| 90 | +{% load viewcomponent_tags %} |
| 91 | + |
| 92 | +{% component formify_helper.field_wrapper_component as field_component %} |
| 93 | + |
| 94 | + {% call field_component.label %} |
| 95 | + {% component formify_helper.label_component field=field formify_helper=formify_helper %}{% endcomponent %} |
| 96 | + {% endcall %} |
| 97 | + |
| 98 | + {% call field_component.input %} |
| 99 | + {{ field_html }} |
| 100 | + {% endcall %} |
| 101 | + |
| 102 | + {% call field_component.field_helper_text_and_errors %} |
| 103 | + {% component formify_helper.field_error_help_text_component field=field formify_helper=formify_helper %}{% endcomponent %} |
| 104 | + {% endcall %} |
| 105 | + |
| 106 | +{% endcomponent %} |
| 107 | +``` |
| 108 | + |
| 109 | +In form layout, we can use component in the python code: |
| 110 | + |
| 111 | +```python |
| 112 | +self.formify_helper.layout = Layout( |
| 113 | + Div( |
| 114 | + Div(Field("email"), css_class="col-span-12 md:col-span-6"), |
| 115 | + Div(Field("password"), css_class="col-span-12 md:col-span-6"), |
| 116 | + Div(Field("address"), css_class="col-span-12"), |
| 117 | + Div(Field("address2"), css_class="col-span-12"), |
| 118 | + Div(Field("city"), css_class="col-span-12 md:col-span-6"), |
| 119 | + Div(Field("state"), css_class="col-span-12 md:col-span-4"), |
| 120 | + Div(Field("zip_code"), css_class="col-span-12 md:col-span-2"), |
| 121 | + Div(Field("check_box"), css_class="col-span-12"), |
| 122 | + Div(Submit(text="Sign in"), css_class="col-span-12"), |
| 123 | + css_class="grid grid-cols-12 gap-3", |
| 124 | + ), |
| 125 | +) |
| 126 | +``` |
| 127 | + |
| 128 | +Since `django-viewcomponent` solve problem very well, `django-formify` use it instead of creating another component solution. |
0 commit comments