Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions ui/lib/core/addon/components/toggle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @module Toggle
* Toggle components are used to indicate boolean values which can be toggled on or off.
* They are a stylistic alternative to checkboxes, but still use the input[type="checkbox"] under the hood.
*
* @example
* ```js
* <Toggle @requiredParam={requiredParam} @optionalParam={optionalParam} @param1={{param1}}/>
* ```
* @param {function} onChange - onChange is triggered on checkbox change (select, deselect). Must manually mutate checked value
* @param {string} name - name is passed along to the form field, as well as to generate the ID of the input & "for" value of the label
* @param {boolean} [checked=false] - checked status of the input, and must be passed in and mutated from the parent
* @param {boolean} [disabled=false] - disabled makes the switch unclickable
* @param {string} [size='medium'] - Sizing can be small or medium
* @param {string} [status='normal'] - Status can be normal or success, which makes the switch have a blue background when checked=true
*/

import Component from '@ember/component';
import { computed } from '@ember/object';
import layout from '../templates/components/toggle';

export default Component.extend({
layout,
tagName: '',
checked: false,
disabled: false,
name: '',
size: 'normal',
status: 'normal',
safeId: computed('name', function() {
return `toggle-${this.name.replace(/\W/g, '')}`;
}),
inputClasses: computed('size', 'status', function() {
const sizeClass = `is-${this.size}`;
const statusClass = `is-${this.status}`;
return `toggle ${statusClass} ${sizeClass}`;
}),
actions: {
handleChange(value) {
this.onChange(value);
},
},
});
17 changes: 17 additions & 0 deletions ui/lib/core/addon/templates/components/toggle.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{{input
id=safeId
name=name
type="checkbox"
checked=checked
change=(action "handleChange" value='target.checked')
class=inputClasses
disabled=disabled
data-test-toggle-input=name
}}
<label data-test-toggle-label={{name}} for={{safeId}}>
{{#if (has-block)}}
{{yield}}
{{else}}
{{name}}
{{/if}}
</label>
1 change: 1 addition & 0 deletions ui/lib/core/app/components/toggle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'core/components/toggle';