{% from "components/error/_macro.njk" import onsError %}

{% macro onsField(params) %}
    {% if params is not defined %}
        {% set params = {} %}
    {% endif %}

    {% set field %}
        {% if params.dontWrap %}
            {{- caller() if caller -}}
        {% else %}
            <div
                {% if params.id %}id="{{ params.id }}"{% endif %}
                class="ons-field{{ ' ons-field--inline' if params.inline }}{{ ' ' + params.classes if params.classes else '' }}"
                {% if params.attributes %}{% for attribute, value in (params.attributes.items() if params.attributes is mapping and params.attributes.items else params.attributes) %}{{ ' ' }}{{ attribute }}{% if value %}="{{ value }}"{% endif %}{% endfor %}{% endif %}
            >
                {{- caller() if caller -}}
            </div>
        {% endif %}
    {% endset %}

    {% if params.error %}
        {% call onsError(params.error) %}
            {{ field | safe }}
        {% endcall %}
    {% else %}
        {{ field | safe }}
    {% endif %}
{% endmacro %}
