|
| 1 | +import Component from '@glimmer/component'; |
| 2 | +import { assert } from '@ember/debug'; |
| 3 | + |
| 4 | +export const TYPES = ['page', 'inline', 'compact']; |
| 5 | +export const DEFAULT_COLOR = 'neutral'; |
| 6 | +export const COLORS = [ |
| 7 | + 'neutral', |
| 8 | + 'highlight', |
| 9 | + 'success', |
| 10 | + 'warning', |
| 11 | + 'critical', |
| 12 | +]; |
| 13 | +export const MAPPING_COLORS_TO_ICONS = { |
| 14 | + neutral: 'info', |
| 15 | + highlight: 'info', |
| 16 | + success: 'check-circle', |
| 17 | + warning: 'alert-triangle', |
| 18 | + critical: 'alert-diamond', |
| 19 | +}; |
| 20 | + |
| 21 | +export default class HdsAlertIndexComponent extends Component { |
| 22 | + constructor() { |
| 23 | + super(...arguments); |
| 24 | + |
| 25 | + assert( |
| 26 | + `@type for "Hds::Alert" must be one of the following: ${TYPES.join( |
| 27 | + ', ' |
| 28 | + )}; received: ${this.args.type}`, |
| 29 | + TYPES.includes(this.args.type) |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @param color |
| 35 | + * @type {enum} |
| 36 | + * @default neutral |
| 37 | + * @description Determines the color scheme for the alert. |
| 38 | + */ |
| 39 | + get color() { |
| 40 | + let { color = DEFAULT_COLOR } = this.args; |
| 41 | + |
| 42 | + assert( |
| 43 | + `@color for "Hds::Alert" must be one of the following: ${COLORS.join( |
| 44 | + ', ' |
| 45 | + )}; received: ${color}`, |
| 46 | + COLORS.includes(color) |
| 47 | + ); |
| 48 | + |
| 49 | + return color; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @param icon |
| 54 | + * @type {string} |
| 55 | + * @default null |
| 56 | + * @description The name of the icon to be used. |
| 57 | + */ |
| 58 | + get icon() { |
| 59 | + let { icon } = this.args; |
| 60 | + |
| 61 | + // If `icon` isn't passed, use the pre-defined one from `color` |
| 62 | + if (icon === undefined) { |
| 63 | + if (this.args.type === 'compact') { |
| 64 | + // for the "compact" type by default we use filled icons |
| 65 | + return `${MAPPING_COLORS_TO_ICONS[this.color]}-fill`; |
| 66 | + } else { |
| 67 | + // for all the other types by default we use outlined icons |
| 68 | + return MAPPING_COLORS_TO_ICONS[this.color]; |
| 69 | + } |
| 70 | + // If `icon` is set explicitly to false, user doesn't want any icon in the alert |
| 71 | + } else if (icon === false) { |
| 72 | + assert( |
| 73 | + `@icon for "Hds::Alert" with @type "compact" is required`, |
| 74 | + this.args.type !== 'compact' |
| 75 | + ); |
| 76 | + |
| 77 | + return false; |
| 78 | + } else { |
| 79 | + // If a name for `icon` is passed, set FlightIcon to that name |
| 80 | + return icon; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @param onDismiss |
| 86 | + * @type {function} |
| 87 | + * @default () => {} |
| 88 | + */ |
| 89 | + get onDismiss() { |
| 90 | + let { onDismiss } = this.args; |
| 91 | + |
| 92 | + if (typeof onDismiss === 'function') { |
| 93 | + return onDismiss; |
| 94 | + } else { |
| 95 | + return false; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @param iconSize |
| 101 | + * @type {string} |
| 102 | + * @description ensures that the correct icon size is used. Automatically calculated. |
| 103 | + */ |
| 104 | + get iconSize() { |
| 105 | + if (this.args.type === 'compact') { |
| 106 | + return '16'; |
| 107 | + } else { |
| 108 | + return '24'; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Get the class names to apply to the component. |
| 114 | + * @method Alert#classNames |
| 115 | + * @return {string} The "class" attribute to apply to the component. |
| 116 | + */ |
| 117 | + get classNames() { |
| 118 | + let classes = ['hds-alert']; |
| 119 | + |
| 120 | + // Add a class based on the @type argument |
| 121 | + classes.push(`hds-alert--type-${this.args.type}`); |
| 122 | + |
| 123 | + // Add a class based on the @color argument |
| 124 | + classes.push(`hds-alert--color-${this.color}`); |
| 125 | + |
| 126 | + return classes.join(' '); |
| 127 | + } |
| 128 | +} |
0 commit comments