@ministryofjustice/frontend 5.1.0 → 5.1.1

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.
@@ -312,7 +312,7 @@ function createAll(Component, config, createAllOptions) {
312
312
  *
313
313
  * {@link https://github.com/ministryofjustice/moj-frontend/releases}
314
314
  */
315
- const version = '5.1.0';
315
+ const version = '5.1.1';
316
316
 
317
317
  class AddAnother extends Component {
318
318
  /**
@@ -3097,8 +3097,21 @@ class SortableTable extends ConfigurableComponent {
3097
3097
  }
3098
3098
  this.$head = $head;
3099
3099
  this.$body = $body;
3100
+ this.$caption = this.$root.querySelector('caption');
3101
+ 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">
3102
+ <path d="M6.5625 15.5L11 6.63125L15.4375 15.5H6.5625Z" fill="currentColor"/>
3103
+ </svg>`;
3104
+ 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">
3105
+ <path d="M15.4375 7L11 15.8687L6.5625 7L15.4375 7Z" fill="currentColor"/>
3106
+ </svg>`;
3107
+ 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">
3108
+ <path d="M8.1875 9.5L10.9609 3.95703L13.7344 9.5H8.1875Z" fill="currentColor"/>
3109
+ <path d="M13.7344 12.0781L10.9609 17.6211L8.1875 12.0781H13.7344Z" fill="currentColor"/>
3110
+ </svg>`;
3100
3111
  this.$headings = this.$head ? Array.from(this.$head.querySelectorAll('th')) : [];
3101
3112
  this.createHeadingButtons();
3113
+ this.updateCaption();
3114
+ this.updateDirectionIndicators();
3102
3115
  this.createStatusBox();
3103
3116
  this.initialiseSortedColumn();
3104
3117
  this.$head.addEventListener('click', this.onSortButtonClick.bind(this));
@@ -3134,7 +3147,7 @@ class SortableTable extends ConfigurableComponent {
3134
3147
  initialiseSortedColumn() {
3135
3148
  var _$sortButton$getAttri;
3136
3149
  const $rows = this.getTableRowsArray();
3137
- const $heading = this.$root.querySelector('th[aria-sort]');
3150
+ const $heading = this.$root.querySelector('th[aria-sort="ascending"], th[aria-sort="descending"]');
3138
3151
  const $sortButton = $heading == null ? void 0 : $heading.querySelector('button');
3139
3152
  const sortDirection = $heading == null ? void 0 : $heading.getAttribute('aria-sort');
3140
3153
  const columnNumber = Number.parseInt((_$sortButton$getAttri = $sortButton == null ? void 0 : $sortButton.getAttribute('data-index')) != null ? _$sortButton$getAttri : '0', 10);
@@ -3150,7 +3163,8 @@ class SortableTable extends ConfigurableComponent {
3150
3163
  */
3151
3164
  onSortButtonClick(event) {
3152
3165
  var _$button$getAttribute;
3153
- const $button = event.target;
3166
+ const $target = /** @type {HTMLElement} */event.target;
3167
+ const $button = $target.closest('button');
3154
3168
  if (!$button || !($button instanceof HTMLButtonElement) || !$button.parentElement) {
3155
3169
  return;
3156
3170
  }
@@ -3163,6 +3177,20 @@ class SortableTable extends ConfigurableComponent {
3163
3177
  this.addRows($sortedRows);
3164
3178
  this.removeButtonStates();
3165
3179
  this.updateButtonState($button, newSortDirection);
3180
+ this.updateDirectionIndicators();
3181
+ }
3182
+ updateCaption() {
3183
+ if (!this.$caption) {
3184
+ return;
3185
+ }
3186
+ let assistiveText = this.$caption.querySelector('.govuk-visually-hidden');
3187
+ if (assistiveText) {
3188
+ return;
3189
+ }
3190
+ assistiveText = document.createElement('span');
3191
+ assistiveText.classList.add('govuk-visually-hidden');
3192
+ assistiveText.textContent = ' (column headers with buttons are sortable).';
3193
+ this.$caption.appendChild(assistiveText);
3166
3194
  }
3167
3195
 
3168
3196
  /**
@@ -3179,6 +3207,27 @@ class SortableTable extends ConfigurableComponent {
3179
3207
  message = message.replace(/%direction%/, this.config[`${direction}Text`]);
3180
3208
  this.$status.textContent = message;
3181
3209
  }
3210
+ updateDirectionIndicators() {
3211
+ for (const $heading of this.$headings) {
3212
+ const $button = /** @type {HTMLButtonElement} */
3213
+ $heading.querySelector('button');
3214
+ if ($heading.hasAttribute('aria-sort') && $button) {
3215
+ var _$button$querySelecto;
3216
+ const direction = $heading.getAttribute('aria-sort');
3217
+ (_$button$querySelecto = $button.querySelector('svg')) == null || _$button$querySelecto.remove();
3218
+ switch (direction) {
3219
+ case 'ascending':
3220
+ $button.insertAdjacentHTML('beforeend', this.$upArrow);
3221
+ break;
3222
+ case 'descending':
3223
+ $button.insertAdjacentHTML('beforeend', this.$downArrow);
3224
+ break;
3225
+ default:
3226
+ $button.insertAdjacentHTML('beforeend', this.$upDownArrow);
3227
+ }
3228
+ }
3229
+ }
3230
+ }
3182
3231
  removeButtonStates() {
3183
3232
  for (const $heading of this.$headings) {
3184
3233
  $heading.setAttribute('aria-sort', 'none');