|
| 1 | +/** |
| 2 | + * Copyright (c) HashiCorp, Inc. |
| 3 | + * SPDX-License-Identifier: MPL-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import Component from '@glimmer/component'; |
| 7 | +import { assert } from '@ember/debug'; |
| 8 | +import { guidFor } from '@ember/object/internals'; |
| 9 | +import { action } from '@ember/object'; |
| 10 | + |
| 11 | +import { |
| 12 | + HdsStepperStatusesValues, |
| 13 | + HdsStepperTitleTagValues, |
| 14 | +} from '../types.ts'; |
| 15 | +import { |
| 16 | + type HdsStepperStatuses, |
| 17 | + type HdsStepperTitleTags, |
| 18 | + type HdsStepperListStepIds, |
| 19 | + HdsStepperStatusToSrOnlyText, |
| 20 | +} from '../types.ts'; |
| 21 | + |
| 22 | +export const DEFAULT_STATUS = HdsStepperStatusesValues.Incomplete; |
| 23 | +export const STATUSES: string[] = Object.values(HdsStepperStatusesValues); |
| 24 | +export const MAPPING_STATUS_TO_SR_ONLY_TEXT = HdsStepperStatusToSrOnlyText; |
| 25 | + |
| 26 | +export interface HdsStepperListStepSignature { |
| 27 | + Args: { |
| 28 | + status: HdsStepperStatusesValues; |
| 29 | + currentStep: number; |
| 30 | + stepNumber?: number; |
| 31 | + titleTag?: HdsStepperTitleTags; |
| 32 | + stepIds: HdsStepperListStepIds; |
| 33 | + didInsertNode?: (element: HTMLElement) => void; |
| 34 | + }; |
| 35 | + Blocks: { |
| 36 | + title: []; |
| 37 | + description?: []; |
| 38 | + content?: []; |
| 39 | + }; |
| 40 | + Element: HTMLElement; |
| 41 | +} |
| 42 | + |
| 43 | +export default class HdsStepperListStep extends Component<HdsStepperListStepSignature> { |
| 44 | + /** |
| 45 | + * Generate a unique ID for the Step |
| 46 | + * @return {string} |
| 47 | + * @param _stepId |
| 48 | + */ |
| 49 | + private _stepId = 'step-' + guidFor(this); |
| 50 | + |
| 51 | + /** |
| 52 | + * Get the index of the step from the _stepIds list |
| 53 | + * @param nodeIndex |
| 54 | + * @type {number} |
| 55 | + */ |
| 56 | + get nodeIndex(): number | undefined { |
| 57 | + return this.args.stepIds?.indexOf(this._stepId); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Get the step number that should be displayed |
| 62 | + * @param stepNumber |
| 63 | + * @type {number} |
| 64 | + */ |
| 65 | + get stepNumber(): number | undefined { |
| 66 | + return this.args.stepNumber ?? this.args.stepIds?.indexOf(this._stepId) + 1; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @param status |
| 71 | + * @type {HdsStepperStatuses} |
| 72 | + * @default "incomplete" |
| 73 | + */ |
| 74 | + get status(): HdsStepperStatuses { |
| 75 | + const { status = DEFAULT_STATUS } = this.args; |
| 76 | + |
| 77 | + assert( |
| 78 | + `@status for "Hds::Stepper::Step::Indicator" must be one of the following: ${STATUSES.join( |
| 79 | + ', ' |
| 80 | + )}; received: ${status}`, |
| 81 | + STATUSES.includes(status) |
| 82 | + ); |
| 83 | + |
| 84 | + return status; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Get the screen reader only text that should be added based on the step status |
| 89 | + * @param statusSrOnlyText |
| 90 | + * @type {string} |
| 91 | + * @default '' |
| 92 | + */ |
| 93 | + get statusSrOnlyText(): string { |
| 94 | + return MAPPING_STATUS_TO_SR_ONLY_TEXT[this.status]; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Get the DOM tag that should be used for the title |
| 99 | + * @param titleTag |
| 100 | + * @type {HdsStepperTitleTags} |
| 101 | + * @default 'div' |
| 102 | + */ |
| 103 | + get titleTag(): HdsStepperTitleTags { |
| 104 | + return this.args.titleTag ?? HdsStepperTitleTagValues.Div; |
| 105 | + } |
| 106 | + |
| 107 | + @action |
| 108 | + didInsertNode(element: HTMLElement): void { |
| 109 | + const { didInsertNode } = this.args; |
| 110 | + |
| 111 | + if (typeof didInsertNode === 'function' && this._stepId != undefined) { |
| 112 | + didInsertNode(element); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Get the class names to apply to the component. |
| 118 | + * @method classNames |
| 119 | + * @return {string} The "class" attribute to apply to the component. |
| 120 | + */ |
| 121 | + get classNames(): string { |
| 122 | + const classes = ['hds-stepper-list__step']; |
| 123 | + |
| 124 | + return classes.join(' '); |
| 125 | + } |
| 126 | +} |
0 commit comments