diff --git a/addon/components/el-switch.hbs b/addon/components/el-switch.hbs index 71b7317..e357e1a 100644 --- a/addon/components/el-switch.hbs +++ b/addon/components/el-switch.hbs @@ -1,11 +1,16 @@ {{! template-lint-disable }} - - {{input - type="checkbox" - checked=model - class="el-switch__input" - disabled=disabled - }} + + + + {{#if isInactive}} diff --git a/addon/components/el-switch.js b/addon/components/el-switch.js index 416dfdb..2b251d6 100644 --- a/addon/components/el-switch.js +++ b/addon/components/el-switch.js @@ -1,63 +1,110 @@ -import Component from '@ember/component'; -import layout from '../templates/components/el-switch'; -import {computed} from "@ember/object"; +import Component from '@glimmer/component'; +import {action, computed} from "@ember/object"; import {htmlSafe} from '@ember/template'; - -export default Component.extend({ - layout, - classNames: ['el-switch'], - tagName : 'label', - - id : '', - name : '', - disabled : false, - width : '', - activeIconClass : '', - inactiveIconClass: '', - activeText : '', - inactiveText : '', - activeValue : true, - inactiveValue : false, - validateEvent : true, - activeColor : '', - inactiveColor : '', - model : false, - - isChecked: computed('model', function () { - return !!this.get('model'); - }), - - notChecked: computed('model', function () { - return !this.get('model'); - }), - - isInactive: computed.or('inactiveIconClass', 'inactiveText'), - - notInactiveIconClass: computed('inactiveIconClass', function () { - return !this.get('inactiveIconClass'); - }), - - isInactiveText: computed.and('notInactiveIconClass', 'inactiveText'), - - - isActive: computed.or('activeIconClass', 'activeText'), - - notActiveIconClass: computed('isActive', function () { - return !this.get('activeIconClass'); - }), - - isActiveText: computed.and('notActiveIconClass', 'activeText'), - - - spanStyle: computed("isChecked", "inactiveColor", "activeColor", 'width', function () { - let color = this.get("model") ? this.get("activeColor") : this.get("inactiveColor"); - let width = this.get("width"); - - if (width === '') { - width = 40; - } - +import { tracked } from '@glimmer/tracking'; + +export default class ElSwitchComponent extends Component { + + @tracked model; + + constructor(owner, args) { + super(owner, args); + this.model = this.args.model; + } + + // disabled : false, + @computed ("args.disabled") + get isDisabled() { + return this.args.disabled || false; + } + // activeIconClass : '', + @computed ("args.activeIconClass") + get activeIconClass() { + return this.args.activeIconClass || ''; + } + // inactiveIconClass: '', + @computed ("args.inactiveIconClass") + get inactiveIconClass() { + return this.args.inactiveIconClass || ''; + } + // activeText : '', + @computed ("args.activeText") + get activeText() { + return this.args.activeText || ''; + } + // inactiveText : '', + @computed ("args.inactiveText") + get inactiveText() { + return this.args.inactiveText || ''; + } + // activeValue : true, + @computed ("args.activeValue") + get activeValue() { + return this.args.activeValue || true; + } + // inactiveValue : false, + @computed ("args.inactiveValue") + get inactiveValue() { + return this.args.inactiveValue || false; + } + // validateEvent : true, + @computed ("args.validateEvent") + get validateEvent() { + return this.args.validateEvent || true; + } + // activeColor : '', + @computed ("args.activeColor") + get activeColor() { + return this.args.activeColor || ''; + } + // inactiveColor : '', + @computed ("args.inactiveColor") + get inactiveColor() { + return this.args.inactiveColor || ''; + } + + get isChecked() { + return !!this.model; + } + + get notChecked() { + return !this.model; + } + + get isInactive() { + return this.inactiveIconClass || this.inactiveText; + } + + get notInactiveIconClass() { + return !this.inactiveIconClass; + } + + get isInactiveText() { + return this.notInactiveIconClass && this.inactiveText; + } + + get isActive() { + return this.activeIconClass && this.activeText; + } + + get notActiveIconClass() { + return !this.activeIconClass; + } + + get isActiveText() { + return this.notActiveIconClass && this.activeText; + } + + get spanStyle() { + const color = this.model ? this.activeColor : this.inactiveColor; + const width = this.width || 40; return htmlSafe(`background: ${color}; border-color: ${color}; width: ${width}px;`); - }), + } + + @action + handleAction () { + if(!this.isDisabled) + this.model = !this.model; + } -}); +} diff --git a/tests/dummy/app/templates/switch.hbs b/tests/dummy/app/templates/switch.hbs index f4eb383..27c8fe5 100644 --- a/tests/dummy/app/templates/switch.hbs +++ b/tests/dummy/app/templates/switch.hbs @@ -9,12 +9,12 @@ {{#card.body}} {{#el-card class="box-card" shadow="naver" as |card|}} {{#card.body}} - {{el-switch }} - {{#el-switch activeColor="#13ce66" inactiveColor="#ff4949" model=true}}{{/el-switch}} + + {{/card.body}} {{/el-card}} -
\{{el-switch }}
-\{{#el-switch  activeColor="#13ce66" inactiveColor="#ff4949" model=true}}\{{/el-switch}}
+
<ElSwitch />
+<ElSwitch @activeColor="#13ce66" @inactiveColor="#ff4949" @model=true></ElSwitch>
{{/card.body}} {{/el-card}} @@ -23,12 +23,12 @@ {{#card.body}} {{#el-card class="box-card" shadow="naver" as |card|}} {{#card.body}} - {{#el-switch activeText="Pay by month" inactiveText="Pay by year"}}{{/el-switch}}


- {{#el-switch model=true activeColor="#13ce66" inactiveColor="#ff4949" activeText="Pay by month" inactiveText="Pay by year"}}{{/el-switch}} +


+ {{/card.body}} {{/el-card}} -
\{{#el-switch  activeText="Pay by month" inactiveText="Pay by year"}}\{{/el-switch}}
-\{{#el-switch  model=true activeColor="#13ce66" inactiveColor="#ff4949" activeText="Pay by month" inactiveText="Pay by year"}}\{{/el-switch}}
+
<ElSwitch @activeText="Pay by month" @inactiveText="Pay by year"></ElSwitch>
+<ElSwitch @model=true @activeColor="#13ce66" @inactiveColor="#ff4949" @activeText="Pay by month" @inactiveText="Pay by year"></ElSwitch>
{{/card.body}} {{/el-card}} @@ -41,8 +41,8 @@ {{#el-switch model=false disabled=true}}{{/el-switch}} {{/card.body}} {{/el-card}} -
\{{#el-switch model=true disabled=true}}\{{/el-switch}}
-\{{#el-switch model=false disabled=true}}\{{/el-switch}}
+
<ElSwitch @model=true @disabled=true></ElSwitch>
+<ElSwitch @model=false @disabled=true></ElSwitch>
{{/card.body}} {{/el-card}} @@ -50,12 +50,12 @@

Attributes

{{#el-card class="box-card" shadow="hover" bodyStyle="padding:0px;" as |card|}} {{#card.body}} - {{#el-table data=tableAttributes as |t|}} + {{!-- {{#el-table data=tableAttributes as |t|}} {{t.column prop="attribute" label="Attribute" width=150}} {{t.column prop="desc" label="Description" width=300}} {{t.column prop="type" label="Type" width=120}} {{t.column prop="val" label="Accepted Values" width=130}} {{t.column prop="def" label="Default" width=75}} - {{/el-table}} + {{/el-table}} --}} {{/card.body}} {{/el-card}} \ No newline at end of file