@ministryofjustice/frontend 5.1.0 → 5.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/moj/all.bundle.js CHANGED
@@ -14,7 +14,7 @@
14
14
  *
15
15
  * {@link https://github.com/ministryofjustice/moj-frontend/releases}
16
16
  */
17
- const version = '5.1.0';
17
+ const version = '5.1.2';
18
18
 
19
19
  class AddAnother extends govukFrontend.Component {
20
20
  /**
@@ -90,7 +90,7 @@
90
90
  */
91
91
  updateAttributes($item, index) {
92
92
  $item.querySelectorAll('[data-name]').forEach($input => {
93
- if (!($input instanceof HTMLInputElement)) {
93
+ if (!this.isValidInputElement($input)) {
94
94
  return;
95
95
  }
96
96
  const name = $input.getAttribute('data-name') || '';
@@ -121,13 +121,23 @@
121
121
  */
122
122
  resetItem($item) {
123
123
  $item.querySelectorAll('[data-name], [data-id]').forEach($input => {
124
- if (!($input instanceof HTMLInputElement)) {
124
+ if (!this.isValidInputElement($input)) {
125
125
  return;
126
126
  }
127
- if ($input.type === 'checkbox' || $input.type === 'radio') {
128
- $input.checked = false;
129
- } else {
127
+ if ($input instanceof HTMLSelectElement) {
128
+ $input.selectedIndex = -1;
129
+ $input.value = '';
130
+ } else if ($input instanceof HTMLTextAreaElement) {
130
131
  $input.value = '';
132
+ } else {
133
+ switch ($input.type) {
134
+ case 'checkbox':
135
+ case 'radio':
136
+ $input.checked = false;
137
+ break;
138
+ default:
139
+ $input.value = '';
140
+ }
131
141
  }
132
142
  });
133
143
  }
@@ -157,6 +167,13 @@
157
167
  }
158
168
  }
159
169
 
170
+ /**
171
+ * @param {Element} $input - the input to validate
172
+ */
173
+ isValidInputElement($input) {
174
+ return $input instanceof HTMLInputElement || $input instanceof HTMLSelectElement || $input instanceof HTMLTextAreaElement;
175
+ }
176
+
160
177
  /**
161
178
  * Name for the component used when initialising using data-module attributes.
162
179
  */
@@ -2799,8 +2816,21 @@
2799
2816
  }
2800
2817
  this.$head = $head;
2801
2818
  this.$body = $body;
2819
+ this.$caption = this.$root.querySelector('caption');
2820
+ this.$upArrow = `<svg width="22" height="22" focusable="false" aria-hidden="true" role="img" viewBox="0 0 22 22" fill="none" xmlns="http://www.w3.org/2000/svg">
2821
+ <path d="M6.5625 15.5L11 6.63125L15.4375 15.5H6.5625Z" fill="currentColor"/>
2822
+ </svg>`;
2823
+ this.$downArrow = `<svg width="22" height="22" focusable="false" aria-hidden="true" role="img" vviewBox="0 0 22 22" fill="none" xmlns="http://www.w3.org/2000/svg">
2824
+ <path d="M15.4375 7L11 15.8687L6.5625 7L15.4375 7Z" fill="currentColor"/>
2825
+ </svg>`;
2826
+ this.$upDownArrow = `<svg width="22" height="22" focusable="false" aria-hidden="true" role="img" vviewBox="0 0 22 22" fill="none" xmlns="http://www.w3.org/2000/svg">
2827
+ <path d="M8.1875 9.5L10.9609 3.95703L13.7344 9.5H8.1875Z" fill="currentColor"/>
2828
+ <path d="M13.7344 12.0781L10.9609 17.6211L8.1875 12.0781H13.7344Z" fill="currentColor"/>
2829
+ </svg>`;
2802
2830
  this.$headings = this.$head ? Array.from(this.$head.querySelectorAll('th')) : [];
2803
2831
  this.createHeadingButtons();
2832
+ this.updateCaption();
2833
+ this.updateDirectionIndicators();
2804
2834
  this.createStatusBox();
2805
2835
  this.initialiseSortedColumn();
2806
2836
  this.$head.addEventListener('click', this.onSortButtonClick.bind(this));
@@ -2836,7 +2866,7 @@
2836
2866
  initialiseSortedColumn() {
2837
2867
  var _$sortButton$getAttri;
2838
2868
  const $rows = this.getTableRowsArray();
2839
- const $heading = this.$root.querySelector('th[aria-sort]');
2869
+ const $heading = this.$root.querySelector('th[aria-sort="ascending"], th[aria-sort="descending"]');
2840
2870
  const $sortButton = $heading == null ? void 0 : $heading.querySelector('button');
2841
2871
  const sortDirection = $heading == null ? void 0 : $heading.getAttribute('aria-sort');
2842
2872
  const columnNumber = Number.parseInt((_$sortButton$getAttri = $sortButton == null ? void 0 : $sortButton.getAttribute('data-index')) != null ? _$sortButton$getAttri : '0', 10);
@@ -2852,7 +2882,8 @@
2852
2882
  */
2853
2883
  onSortButtonClick(event) {
2854
2884
  var _$button$getAttribute;
2855
- const $button = event.target;
2885
+ const $target = /** @type {HTMLElement} */event.target;
2886
+ const $button = $target.closest('button');
2856
2887
  if (!$button || !($button instanceof HTMLButtonElement) || !$button.parentElement) {
2857
2888
  return;
2858
2889
  }
@@ -2865,6 +2896,20 @@
2865
2896
  this.addRows($sortedRows);
2866
2897
  this.removeButtonStates();
2867
2898
  this.updateButtonState($button, newSortDirection);
2899
+ this.updateDirectionIndicators();
2900
+ }
2901
+ updateCaption() {
2902
+ if (!this.$caption) {
2903
+ return;
2904
+ }
2905
+ let assistiveText = this.$caption.querySelector('.govuk-visually-hidden');
2906
+ if (assistiveText) {
2907
+ return;
2908
+ }
2909
+ assistiveText = document.createElement('span');
2910
+ assistiveText.classList.add('govuk-visually-hidden');
2911
+ assistiveText.textContent = ' (column headers with buttons are sortable).';
2912
+ this.$caption.appendChild(assistiveText);
2868
2913
  }
2869
2914
 
2870
2915
  /**
@@ -2881,6 +2926,27 @@
2881
2926
  message = message.replace(/%direction%/, this.config[`${direction}Text`]);
2882
2927
  this.$status.textContent = message;
2883
2928
  }
2929
+ updateDirectionIndicators() {
2930
+ for (const $heading of this.$headings) {
2931
+ const $button = /** @type {HTMLButtonElement} */
2932
+ $heading.querySelector('button');
2933
+ if ($heading.hasAttribute('aria-sort') && $button) {
2934
+ var _$button$querySelecto;
2935
+ const direction = $heading.getAttribute('aria-sort');
2936
+ (_$button$querySelecto = $button.querySelector('svg')) == null || _$button$querySelecto.remove();
2937
+ switch (direction) {
2938
+ case 'ascending':
2939
+ $button.insertAdjacentHTML('beforeend', this.$upArrow);
2940
+ break;
2941
+ case 'descending':
2942
+ $button.insertAdjacentHTML('beforeend', this.$downArrow);
2943
+ break;
2944
+ default:
2945
+ $button.insertAdjacentHTML('beforeend', this.$upDownArrow);
2946
+ }
2947
+ }
2948
+ }
2949
+ }
2884
2950
  removeButtonStates() {
2885
2951
  for (const $heading of this.$headings) {
2886
2952
  $heading.setAttribute('aria-sort', 'none');