|
| 1 | +/** |
| 2 | + * Copyright (c) HashiCorp, Inc. |
| 3 | + * SPDX-License-Identifier: MPL-2.0 |
| 4 | + */ |
| 5 | +import Component from '@glimmer/component'; |
| 6 | +import { fn, hash } from '@ember/helper'; |
| 7 | +import { tracked } from '@glimmer/tracking'; |
| 8 | +import { eq } from 'ember-truth-helpers'; |
| 9 | +import type Owner from '@ember/owner'; |
| 10 | + |
| 11 | +import CodeFragmentWithSelectedComponent from 'showcase/components/page-components/form/super-select/code-fragments/with-selected-component'; |
| 12 | + |
| 13 | +import { HdsFormSuperSelectMultipleField } from '@hashicorp/design-system-components/components'; |
| 14 | + |
| 15 | +import type { HdsFormSuperSelectMultipleFieldSignature } from '@hashicorp/design-system-components/components/hds/form/super-select/multiple/field'; |
| 16 | + |
| 17 | +interface GroupedOption { |
| 18 | + groupName: string; |
| 19 | + options: (string | GroupedOption)[]; |
| 20 | +} |
| 21 | + |
| 22 | +const OPTIONS = ['Option 1', 'Option 2', 'Option 3']; |
| 23 | + |
| 24 | +const CLUSTER_SIZE_OPTIONS = [ |
| 25 | + { |
| 26 | + size: 'Extra Small', |
| 27 | + description: '2 vCPU | 1 GiB RAM', |
| 28 | + price: '$0.02', |
| 29 | + }, |
| 30 | + { |
| 31 | + size: 'Small', |
| 32 | + description: '2 vCPU | 2 GiB RAM', |
| 33 | + price: '$0.04', |
| 34 | + disabled: true, |
| 35 | + }, |
| 36 | + { |
| 37 | + size: 'Medium', |
| 38 | + description: '4 vCPU | 4 GiB RAM', |
| 39 | + price: '$0.08', |
| 40 | + disabled: true, |
| 41 | + }, |
| 42 | + { size: 'Large', description: '8 vCPU | 8 GiB RAM', price: '$0.16' }, |
| 43 | + { |
| 44 | + size: 'Extra Large', |
| 45 | + description: '16 vCPU | 16 GiB RAM', |
| 46 | + price: '$0.32', |
| 47 | + }, |
| 48 | +]; |
| 49 | + |
| 50 | +const GROUPED_OPTIONS = [ |
| 51 | + { groupName: 'Group', options: ['Option 1', 'Option 2'] }, |
| 52 | + { groupName: 'Group', options: ['Option 3', 'Option 4'] }, |
| 53 | + { |
| 54 | + groupName: 'Group', |
| 55 | + options: [ |
| 56 | + { groupName: 'Subgroup', options: ['Option 5', 'Option 6'] }, |
| 57 | + { groupName: 'Subgroup', options: ['Option 7', 'Option 8'] }, |
| 58 | + ], |
| 59 | + }, |
| 60 | + { |
| 61 | + groupName: 'Group', |
| 62 | + options: [ |
| 63 | + { groupName: 'Subgroup', options: ['Option 10', 'Option 11'] }, |
| 64 | + { groupName: 'Subgroup', options: ['Option 12', 'Option 13'] }, |
| 65 | + ], |
| 66 | + }, |
| 67 | +]; |
| 68 | + |
| 69 | +export interface CodeFragmentWithMultipleFieldElementSignature { |
| 70 | + Args: { |
| 71 | + options?: 'basic' | 'cluster-size' | 'grouped'; |
| 72 | + isSelected?: boolean; |
| 73 | + hasSelectedItemComponent?: boolean; |
| 74 | + hasRichContent?: boolean; |
| 75 | + isInvalid?: HdsFormSuperSelectMultipleFieldSignature['Args']['isInvalid']; |
| 76 | + isRequired?: HdsFormSuperSelectMultipleFieldSignature['Args']['isRequired']; |
| 77 | + isOptional?: HdsFormSuperSelectMultipleFieldSignature['Args']['isOptional']; |
| 78 | + disabled?: HdsFormSuperSelectMultipleFieldSignature['Args']['disabled']; |
| 79 | + initiallyOpened?: HdsFormSuperSelectMultipleFieldSignature['Args']['initiallyOpened']; |
| 80 | + verticalPosition?: HdsFormSuperSelectMultipleFieldSignature['Args']['verticalPosition']; |
| 81 | + horizontalPosition?: HdsFormSuperSelectMultipleFieldSignature['Args']['horizontalPosition']; |
| 82 | + }; |
| 83 | + Blocks: { |
| 84 | + default: [ |
| 85 | + { |
| 86 | + Label?: HdsFormSuperSelectMultipleFieldSignature['Blocks']['default'][0]['Label']; |
| 87 | + HelperText?: HdsFormSuperSelectMultipleFieldSignature['Blocks']['default'][0]['HelperText']; |
| 88 | + Error?: HdsFormSuperSelectMultipleFieldSignature['Blocks']['default'][0]['Error']; |
| 89 | + Options?: HdsFormSuperSelectMultipleFieldSignature['Blocks']['default'][0]['Options']; |
| 90 | + options?: unknown; |
| 91 | + }, |
| 92 | + ]; |
| 93 | + }; |
| 94 | +} |
| 95 | + |
| 96 | +export default class CodeFragmentWithMultipleFieldElement extends Component<CodeFragmentWithMultipleFieldElementSignature> { |
| 97 | + @tracked selectedOptions; |
| 98 | + |
| 99 | + constructor( |
| 100 | + owner: Owner, |
| 101 | + args: CodeFragmentWithMultipleFieldElementSignature['Args'], |
| 102 | + ) { |
| 103 | + super(owner, args); |
| 104 | + if (args.isSelected) { |
| 105 | + if (args.options === 'grouped') { |
| 106 | + this.selectedOptions = [ |
| 107 | + (this.options as GroupedOption[])[0]?.options[0] as |
| 108 | + | GroupedOption |
| 109 | + | string, |
| 110 | + (this.options as GroupedOption[])[1]?.options[0] as |
| 111 | + | GroupedOption |
| 112 | + | string, |
| 113 | + ]; |
| 114 | + } else { |
| 115 | + this.selectedOptions = [this.options[0], this.options[1]]; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + get options() { |
| 121 | + const { options } = this.args; |
| 122 | + |
| 123 | + if (options === 'cluster-size') { |
| 124 | + return CLUSTER_SIZE_OPTIONS; |
| 125 | + } else if (options === 'grouped') { |
| 126 | + return GROUPED_OPTIONS; |
| 127 | + } else { |
| 128 | + return OPTIONS; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + <template> |
| 133 | + <HdsFormSuperSelectMultipleField |
| 134 | + @onChange={{fn (mut this.selectedOptions)}} |
| 135 | + @options={{this.options}} |
| 136 | + @selected={{this.selectedOptions}} |
| 137 | + @selectedItemComponent={{if |
| 138 | + @hasSelectedItemComponent |
| 139 | + CodeFragmentWithSelectedComponent |
| 140 | + }} |
| 141 | + @isInvalid={{@isInvalid}} |
| 142 | + @isRequired={{@isRequired}} |
| 143 | + @isOptional={{@isOptional}} |
| 144 | + @disabled={{@disabled}} |
| 145 | + @initiallyOpened={{@initiallyOpened}} |
| 146 | + @verticalPosition={{@verticalPosition}} |
| 147 | + @horizontalPosition={{@horizontalPosition}} |
| 148 | + as |F| |
| 149 | + > |
| 150 | + {{#if (eq @options "grouped")}} |
| 151 | + {{yield |
| 152 | + (hash |
| 153 | + Label=F.Label |
| 154 | + HelperText=F.HelperText |
| 155 | + Error=F.Error |
| 156 | + Options=F.Options |
| 157 | + ) |
| 158 | + }} |
| 159 | + {{else}} |
| 160 | + {{yield |
| 161 | + (hash |
| 162 | + Label=F.Label |
| 163 | + HelperText=F.HelperText |
| 164 | + Error=F.Error |
| 165 | + Options=F.Options |
| 166 | + options=F.options |
| 167 | + ) |
| 168 | + }} |
| 169 | + {{/if}} |
| 170 | + {{#unless @hasRichContent}} |
| 171 | + {{! @glint-expect-error - https://hashicorp.atlassian.net/browse/HDS-5090 }} |
| 172 | + {{F.options}} |
| 173 | + {{/unless}} |
| 174 | + </HdsFormSuperSelectMultipleField> |
| 175 | + </template> |
| 176 | +} |
0 commit comments