Skip to content

Commit

Permalink
Breaking: Convert indicator to jsx, add outer container (fixes 230)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverfoster committed Jun 12, 2024
1 parent f764480 commit c480f90
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 40 deletions.
51 changes: 25 additions & 26 deletions js/PageLevelProgressIndicatorView.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import Adapt from 'core/js/adapt';
import React from 'react';
import ReactDOM from 'react-dom';
import { templates } from 'core/js/reactHelpers';

class PageLevelProgressIndicatorView extends Backbone.View {

Expand All @@ -16,25 +19,15 @@ class PageLevelProgressIndicatorView extends Backbone.View {
this.setUpEventListeners();
this.setPercentageComplete();
this.render();
this.refresh();
}

addClasses() {
this.$el.addClass([
'pagelevelprogress__indicator',
'pagelevelprogress__indicator-outer',
'is-' + this.type
].join(' '));
}

checkAria() {
if (!this.ariaLabel) {
this.$el.attr('aria-hidden', true);
return;
}
const data = this.getRenderData();
this.$('.js-indicator-aria-label').html(Handlebars.compile(this.ariaLabel)(data));
}

setUpEventListeners() {
if (this.parent) {
this.listenToOnce(this.parent, 'postRemove', this.remove);
Expand All @@ -47,6 +40,9 @@ class PageLevelProgressIndicatorView extends Backbone.View {
setPercentageComplete() {
const percentage = this.calculatePercentage();
this.model.set('percentageComplete', percentage);
this.$el.css({
'--adapt-pagelevelprogress-percentage': percentage + '%'
});
return percentage;
}

Expand All @@ -55,24 +51,15 @@ class PageLevelProgressIndicatorView extends Backbone.View {
}

render() {
this.checkCompletion();
this.checkAriaHidden();
const data = this.getRenderData();
const template = Handlebars.templates[this.constructor.template];
this.$el.html(template(data));
}

getRenderData() {
const data = this.model.toJSON();
data.ariaLabel = this.ariaLabel;
data.type = this.type;
return data;
const Component = templates.pageLevelProgressIndicator;
ReactDOM.render(<Component {...data} />, this.el);
}

refresh() {
this.checkCompletion();
this.checkAria();
this.$('.js-indicator-bar').css({
width: this.calculatePercentage() + '%'
});
this.render();
}

checkCompletion() {
Expand All @@ -90,8 +77,20 @@ class PageLevelProgressIndicatorView extends Backbone.View {
.toggleClass('is-incorrect', isIncorrect);
}

checkAriaHidden() {
if (this.ariaLabel) return;
this.$el.attr('aria-hidden', true);
}

getRenderData() {
const data = this.model.toJSON();
data.ariaLabel = this.ariaLabel;
data.type = this.type;
return data;
}

}

PageLevelProgressIndicatorView.template = 'pageLevelProgressIndicator';
PageLevelProgressIndicatorView.template = 'pageLevelProgressIndicator.jsx';

export default PageLevelProgressIndicatorView;
1 change: 1 addition & 0 deletions less/pageLevelProgressIndicator.less
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
height: inherit;
min-width: 15%;
background-color: @black;
width: var(--adapt-pagelevelprogress-percentage);
}

&__indicator .js-indicator-aria-label {
Expand Down
14 changes: 0 additions & 14 deletions templates/pageLevelProgressIndicator.hbs

This file was deleted.

24 changes: 24 additions & 0 deletions templates/pageLevelProgressIndicator.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react';
import { compile } from 'core/js/reactHelpers';

export default function PageLevelProgressIndicator (props) {
const {
ariaLabel
} = props;

return (
<span className='pagelevelprogress__indicator'>
<span className="pagelevelprogress__indicator-inner">

<span className="pagelevelprogress__indicator-bar"></span>

{ariaLabel &&
<span className="aria-label">
{compile(ariaLabel)}
</span>
}

</span>
</span>
);
};

0 comments on commit c480f90

Please sign in to comment.