Skip to content

Commit

Permalink
feat: refactor all components to glimmer and use native classes
Browse files Browse the repository at this point in the history
BREAKING CHANGE: While the public API won't change, there is a huge
chance that this will break implementations if someone's extending the
components of this addon. Components that do need to be refactored to
glimmer.
  • Loading branch information
anehx committed Oct 8, 2021
1 parent c0e7aff commit cee7373
Show file tree
Hide file tree
Showing 115 changed files with 1,027 additions and 955 deletions.
7 changes: 7 additions & 0 deletions addon/components/validated-button.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{#let (component this.buttonComponent onClick=@action loading=@loading disabled=@disabled label=@label type=@type) as |Button|}}
{{#if (has-block)}}
<Button ...attributes>{{yield}}</Button>
{{else}}
<Button ...attributes />
{{/if}}
{{/let}}
12 changes: 4 additions & 8 deletions addon/components/validated-button.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import Component from "@ember/component";
import Component from "@glimmer/component";

import themedComponent from "../-private/themed-component";
import layout from "../templates/components/validated-button";

export default Component.extend({
layout,
type: "button",
tagName: "",
buttonComponent: themedComponent("validated-button/button"),
});
export default class ValidatedButtonComponent extends Component {
@themedComponent("validated-button/button") buttonComponent;
}
16 changes: 7 additions & 9 deletions addon/components/validated-button/-themes/bootstrap/button.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { computed } from "@ember/object";

import Component from "../../button";

export default Component.extend({
classNames: ["btn"],
classNameBindings: ["style"],
export default class BootstrapButton extends Component {
get class() {
const style = this.args.type === "submit" ? "btn-primary" : "btn-default";
const loading = this.args.loading ? "loading" : "";

style: computed("type", function () {
return this.type === "submit" ? "btn-primary" : "btn-default";
}),
});
return `btn ${style} ${loading}`;
}
}
16 changes: 7 additions & 9 deletions addon/components/validated-button/-themes/uikit/button.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { computed } from "@ember/object";

import Component from "../../button";

export default Component.extend({
classNames: ["uk-button"],
classNameBindings: ["style"],
export default class UikitButton extends Component {
get class() {
const style =
this.args.type === "submit" ? "uk-button-primary" : "uk-button-default";

style: computed("type", function () {
return this.type === "submit" ? "uk-button-primary" : "uk-button-default";
}),
});
return `uk-button ${style}`;
}
}
9 changes: 9 additions & 0 deletions addon/components/validated-button/button.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<button
class={{this.class}}
type={{@type}}
disabled={{@disabled}}
{{on "click" @onClick}}
...attributes
>
{{#if (has-block)}}{{~yield~}}{{else}}{{~@label~}}{{/if}}
</button>
10 changes: 2 additions & 8 deletions addon/components/validated-button/button.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import Component from "@ember/component";
import Component from "@glimmer/component";

import layout from "../../templates/components/validated-button/button";

export default Component.extend({
layout,
tagName: "button",
attributeBindings: ["disabled", "type", "action:onclick"],
});
export default class ButtonComponent extends Component {}
20 changes: 20 additions & 0 deletions addon/components/validated-form.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<form
autocomplete={{@autocomplete}}
class={{if this.submitted "submitted"}}
>
{{yield (hash
model=@model
loading=this.loading
input=(component "validated-input"
model=@model
submitted=this.submitted
validateBeforeSubmit=@validateBeforeSubmit
)
submit=(component "validated-button"
type="submit"
loading=this.loading
label="Save"
action=this.submit
)
)}}
</form>
78 changes: 32 additions & 46 deletions addon/components/validated-form.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,56 @@
import Component from "@ember/component";
import { action } from "@ember/object";
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { resolve } from "rsvp";

import layout from "../templates/components/validated-form";

const PROP_ON_SUBMIT = "on-submit";
const PROP_ON_INVALID_SUBMIT = "on-invalid-submit";

export default Component.extend({
tagName: "form",

classNameBindings: ["submitted"],
attributeBindings: ["autocomplete"],

loading: false,

submitted: false,

layout,
export default class ValidatedFormComponent extends Component {
@tracked loading = false;
@tracked submitted = false;
@tracked validateBeforeSubmit = true;

validateBeforeSubmit: true,
constructor(...args) {
super(...args);

init(...args) {
this._super(...args);
if (this.model && this.model.validate) {
this.model.validate();
if (this.args.model && this.args.model.validate) {
this.args.model.validate();
}
},
}

submit() {
this.set("submitted", true);
const model = this.model;
@action
async submit(event) {
event.preventDefault();

this.submitted = true;
const model = this.args.model;

if (!model || !model.validate) {
this.runCallback(PROP_ON_SUBMIT);
return false;
}

model.validate().then(() => {
if (!this.element) {
// We were removed from the DOM while validating
return;
}
await model.validate();

if (model.get("isInvalid")) {
this.runCallback(PROP_ON_INVALID_SUBMIT);
} else {
this.runCallback(PROP_ON_SUBMIT);
}

if (model.get("isInvalid")) {
this.runCallback(PROP_ON_INVALID_SUBMIT);
} else {
this.runCallback(PROP_ON_SUBMIT);
}
});
return false;
},
}

runCallback(callbackProp) {
const callback = this.get(callbackProp);
const callback = this.args[callbackProp];
if (typeof callback !== "function") {
return;
}
const model = this.model;

this.set("loading", true);
resolve(callback(model)).finally(() => {
if (!this.element) {
// We were removed from the DOM while running on-submit()
return;
}
this.set("loading", false);
this.loading = true;
resolve(callback(this.args.model)).finally(() => {
this.loading = false;
});
},
});
}
}
File renamed without changes.
6 changes: 1 addition & 5 deletions addon/components/validated-input.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { setComponentTemplate } from "@ember/component";
import { action, set, get } from "@ember/object";
import { guidFor } from "@ember/object/internals";
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";

import themedComponent from "../-private/themed-component";
import template from "../templates/components/validated-input";

/**
* This component wraps form inputs.
Expand All @@ -20,7 +18,7 @@ import template from "../templates/components/validated-input";
* @class validated-input
* @export default
*/
export class ValidatedInput extends Component {
export default class ValidatedInputComponent extends Component {
inputId = guidFor(this);

@tracked dirty;
Expand Down Expand Up @@ -97,5 +95,3 @@ export class ValidatedInput extends Component {
}
}
}

export default setComponentTemplate(template, ValidatedInput);
6 changes: 3 additions & 3 deletions addon/components/validated-input/-themes/bootstrap/error.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Component from "../../error";

export default Component.extend({
classNames: ["invalid-feedback"],
});
export default class BootstrapErrorComponent extends Component {
class = "invalid-feedback";
}
6 changes: 3 additions & 3 deletions addon/components/validated-input/-themes/bootstrap/hint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Component from "../../hint";

export default Component.extend({
classNames: ["form-text", "text-muted"],
});
export default class BootstrapHintComponent extends Component {
class = "form-text text-muted";
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import Component from "../../label";

export default Component.extend({});
export default class BootstrapLabelComponent extends Component {}
6 changes: 3 additions & 3 deletions addon/components/validated-input/-themes/bootstrap/render.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Component from "../../render";

export default Component.extend({
classNames: ["form-group"],
});
export default class BootstrapRenderComponent extends Component {
class = "form-group";
}
1 change: 1 addition & 0 deletions addon/components/validated-input/-themes/uikit/error.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<small class="uk-text-danger" ...attributes>{{yield}}{{this.errorString}}</small>
5 changes: 1 addition & 4 deletions addon/components/validated-input/-themes/uikit/error.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import Component from "../../error";

export default Component.extend({
tagName: "small",
classNames: ["uk-text-danger"],
});
export default class UikitErrorComponent extends Component {}
6 changes: 3 additions & 3 deletions addon/components/validated-input/-themes/uikit/hint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Component from "../../hint";

export default Component.extend({
classNames: ["uk-text-muted"],
});
export default class UikitHintComponent extends Component {
class = "uk-text-muted";
}
6 changes: 3 additions & 3 deletions addon/components/validated-input/-themes/uikit/label.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Component from "../../label";

export default Component.extend({
classNames: ["uk-form-label"],
});
export default class UikitLabelComponent extends Component {
class = "uk-form-label";
}
Loading

0 comments on commit cee7373

Please sign in to comment.