Skip to content

Commit

Permalink
Remove JS noise from address primary settings
Browse files Browse the repository at this point in the history
  • Loading branch information
nfourtythree committed Oct 13, 2023
1 parent 584456f commit 8ff5dd5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 33 deletions.
20 changes: 4 additions & 16 deletions example-templates/dist/shop/_private/address/fields.twig
Original file line number Diff line number Diff line change
Expand Up @@ -198,29 +198,17 @@ Outputs address form fields for editing an address.
{% if showPrimaryCheckboxes %}
<hr class="my-2">
<div class="my-2">
{{ input('hidden', 'isPrimaryBilling', address.isPrimarybilling ? 1 : 0) }}
<label>{{ input('checkbox', 'isPrimaryBillingBox', 1, { checked: address.isPrimaryBilling, 'data-primary-input': 'isPrimaryBilling' }) }} {{ 'Use as the primary billing address'|t('commerce') }}</label>
{{ hiddenInput('isPrimaryBilling', 0) }}
<label>{{ input('checkbox', 'isPrimaryBilling', 1, { checked: address.isPrimaryBilling }) }} {{ 'Use as the primary billing address'|t('commerce') }}</label>
</div>
<div class="my-2">
{{ input('hidden', 'isPrimaryShipping', address.isPrimaryShipping ? 1 : 0) }}
<label>{{ input('checkbox', 'isPrimaryShippingBox', 1, { checked: address.isPrimaryShipping, 'data-primary-input': 'isPrimaryShipping' }) }} {{ 'Use as the primary shipping address'|t('commerce') }}</label>
{{ hiddenInput('isPrimaryShipping', 0) }}
<label>{{ input('checkbox', 'isPrimaryShipping', 1, { checked: address.isPrimaryShipping }) }} {{ 'Use as the primary shipping address'|t('commerce') }}</label>
</div>
{% endif %}
</div>

{% js %}
{% if showPrimaryCheckboxes %}
document.querySelectorAll('input[type=checkbox][data-primary-input]').forEach(el => {
el.addEventListener('change', ev => {
let primaryInput = document.querySelector(`input[name="${ev.target.dataset.primaryInput}"]`);
if (ev.target.checked) {
primaryInput.value = 1;
} else {
primaryInput.value = 0;
}
});
});
{% endif %}

document.querySelector('select#{{ 'countryCode'|namespaceInputId(addressName) }}').addEventListener('change', ev => {
const countryCode = ev.target.value;
Expand Down
21 changes: 4 additions & 17 deletions example-templates/src/shop/_private/address/fields.twig
Original file line number Diff line number Diff line change
Expand Up @@ -198,30 +198,17 @@ Outputs address form fields for editing an address.
{% if showPrimaryCheckboxes %}
<hr class="my-2">
<div class="my-2">
{{ input('hidden', 'isPrimaryBilling', address.isPrimarybilling ? 1 : 0) }}
<label>{{ input('checkbox', 'isPrimaryBillingBox', 1, { checked: address.isPrimaryBilling, 'data-primary-input': 'isPrimaryBilling' }) }} {{ 'Use as the primary billing address'|t('commerce') }}</label>
{{ hiddenInput('isPrimaryBilling', 0) }}
<label>{{ input('checkbox', 'isPrimaryBilling', 1, { checked: address.isPrimaryBilling }) }} {{ 'Use as the primary billing address'|t('commerce') }}</label>
</div>
<div class="my-2">
{{ input('hidden', 'isPrimaryShipping', address.isPrimaryShipping ? 1 : 0) }}
<label>{{ input('checkbox', 'isPrimaryShippingBox', 1, { checked: address.isPrimaryShipping, 'data-primary-input': 'isPrimaryShipping' }) }} {{ 'Use as the primary shipping address'|t('commerce') }}</label>
{{ hiddenInput('isPrimaryShipping', 0) }}
<label>{{ input('checkbox', 'isPrimaryShipping', 1, { checked: address.isPrimaryShipping }) }} {{ 'Use as the primary shipping address'|t('commerce') }}</label>
</div>
{% endif %}
</div>

{% js %}
{% if showPrimaryCheckboxes %}
document.querySelectorAll('input[type=checkbox][data-primary-input]').forEach(el => {
el.addEventListener('change', ev => {
let primaryInput = document.querySelector(`input[name="${ev.target.dataset.primaryInput}"]`);
if (ev.target.checked) {
primaryInput.value = 1;
} else {
primaryInput.value = 0;
}
});
});
{% endif %}

document.querySelector('select#{{ 'countryCode'|namespaceInputId(addressName) }}').addEventListener('change', ev => {
const countryCode = ev.target.value;
const stateSelect = document.querySelector('select#{{ 'administrativeArea'|namespaceInputId(addressName) }}');
Expand Down

0 comments on commit 8ff5dd5

Please sign in to comment.