Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SuperSelect components (HDS-3221) #2022

Merged
merged 147 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
147 commits
Select commit Hold shift + click to select a range
ff909ee
HDS-3221 Stub out files for SuperSelect components
KristinLBradley Mar 25, 2024
cdfb8e4
HDS-3221 Add PowerSelect components and forward arguments, add basic …
KristinLBradley Mar 27, 2024
bdc7608
HDS-3221 Add basic examples to Showcase, add basic styles with paddin…
KristinLBradley Mar 27, 2024
507df5a
HDS-3221 Add preliminary Search and Dropdown styles
KristinLBradley Mar 28, 2024
cd4cd73
HDS-3221 Tweak spacing, padding, and selected item styles to better m…
KristinLBradley Mar 28, 2024
72a1cb0
HDS-3221 Additional styling for active and hovered items, style clean…
KristinLBradley Mar 28, 2024
39d3ca8
HDS-3221 Style default tags for multi-select, adjust padding and spacing
KristinLBradley Mar 28, 2024
e74fe38
HDS-3221 Fix value for @matcher
KristinLBradley Mar 29, 2024
88a23a8
HDS-3221 Update Showcase to add new sections and reorganize, separate…
KristinLBradley Mar 29, 2024
c9bc8bd
HDS-3221 Move and rename style files in wrong places
KristinLBradley Mar 29, 2024
cce1123
HDS-3221 Clean up CSS code, add more styling for various option states
KristinLBradley Apr 2, 2024
ed10993
HDS-3221 Add encoded SVG backgrounds for multiple select checkboxes
KristinLBradley Apr 2, 2024
7d55214
HDS-3221 Change :disabled reference to aria-disabled since list item …
KristinLBradley Apr 2, 2024
ae1cb1f
HDS-3221 Style SuperSelect tags to follow HDS Tag, add flex layout to…
KristinLBradley Apr 3, 2024
292fc89
HDS-3221 Reorganize list related options in Showcase, add section for…
KristinLBradley Apr 3, 2024
efb80ed
HDS-3221 Update Showcase layout
KristinLBradley Apr 3, 2024
542f453
HDS-3221 Move SuperSelect to be under Form components, move all relat…
KristinLBradley Apr 3, 2024
2c02178
HDS-3221 Move back Tabs showcase file which was accidentally moved
KristinLBradley Apr 3, 2024
73ec1a9
HDS-3221 Add example of Right & Left positioning with matchTriggerWid…
KristinLBradley Apr 4, 2024
0eeeb84
HDS-3221 Add center position example for options
KristinLBradley Apr 4, 2024
5ee121c
HDS-3221 Replace background images for checkboxes with styled pseudo …
KristinLBradley Apr 4, 2024
c95ed33
HDS-3221 Add temporary section to Showcase for form layout experiment…
KristinLBradley Apr 4, 2024
e6bedef
HDS-3221 Improve layout of examples
KristinLBradley Apr 4, 2024
41cc290
HDS-3211 clean up CSS to fix linting error
KristinLBradley Apr 4, 2024
8a61bfb
HDS-3221 Add example of Grouped options with temp styles
KristinLBradley Apr 4, 2024
efa59e4
HDS-3221 Add invalid styles
alex-ju Apr 8, 2024
bceb1f1
Consolidate SuperSelect styles
alex-ju Apr 8, 2024
59736ea
Bring SuperSelect showcase in line with form components
alex-ju Apr 8, 2024
c0a7977
Update vertical list position showcase
alex-ju Apr 8, 2024
2fc8a7b
Rename `hds-super-select` β†’ `hds-form-super-select`
alex-ju Apr 9, 2024
4caa24f
Rename index to base
alex-ju Apr 9, 2024
2eb6a1a
Add field components
alex-ju Apr 9, 2024
a9fa96b
Update showcase after Base/Field changes
alex-ju Apr 9, 2024
8edf385
Update tests after Base/Field changes
alex-ju Apr 9, 2024
fb2ce79
Add `ember-power-select` as dependency
alex-ju Apr 9, 2024
90fdbca
Upgrade `ember-power-select` to `8.1.0`
alex-ju Apr 9, 2024
d152104
Enable selection on most examples
alex-ju Apr 10, 2024
74ed9d1
Add after-options to SuperSelect::Multiple
alex-ju Apr 10, 2024
1a04fa0
Add 'Show All' action
alex-ju Apr 10, 2024
40e0205
Add <BasicDropdownWormhole />
alex-ju Apr 10, 2024
2bf9e29
HDS-3221 Refactor naming of options data for showcase to be more cons…
KristinLBradley Apr 11, 2024
57936c7
HDS-3221 Add example of Multiple varian with rich content in options …
KristinLBradley Apr 11, 2024
29d3b38
Remove `@searchEnabled` from rich content example
alex-ju Apr 12, 2024
98512b8
Move afterOptions component in a shared space
alex-ju Apr 12, 2024
1774539
Remove option-like style from `no-matches-message`
alex-ju Apr 12, 2024
20ef690
Fix style for current items
alex-ju Apr 12, 2024
b495feb
Manage no option selected message
alex-ju Apr 12, 2024
0b0cee3
Add option-group component and make default, style groups and refacto…
KristinLBradley Apr 13, 2024
e997ec6
Add `@showAfterOptions ` (default `true`)
alex-ju Apr 15, 2024
880537d
Forward `showAfterOptions` and `afterOptionsContent` to Field components
alex-ju Apr 15, 2024
14ad3c0
HDS-3221 Adjust positioning of single hovered option border and check…
KristinLBradley Apr 15, 2024
3204549
HDS-3221 Align checkmark to top instead of vertical center, adjust sp…
KristinLBradley Apr 15, 2024
b697015
HDS-3221 Make width of SuperSelect control 100%, update Showcase
KristinLBradley Apr 15, 2024
f12eba5
Set an `id` for `Label` and expose it to `Field::Control`
alex-ju Apr 16, 2024
4b683b3
Ensure `Field` variants are labeled by default
alex-ju Apr 16, 2024
ba09663
Add `@ariaLabel` to `Base` variants
alex-ju Apr 16, 2024
8e2e97d
HDS-3221 Make search input height match mocks and other fields and it…
KristinLBradley Apr 16, 2024
06050f2
Fix getters description
alex-ju Apr 17, 2024
12cea8e
Add tests for `SuperSelect::Single`
alex-ju Apr 17, 2024
fa59ccd
HDS-3221 Change showAfterOptions to false by default Single SuperSelect
KristinLBradley Apr 17, 2024
63e723a
HDS-3221 Make long unbroken string example longer to purposely trigge…
KristinLBradley Apr 17, 2024
9280c0b
HDS-3221 Fix afterOptionsComponent so it renders if afterOptionsConte…
KristinLBradley Apr 17, 2024
034f43a
Remove unused actions from showcase controller
alex-ju Apr 18, 2024
d6d69ac
Allow custom content for Field via `<F.Options>`
alex-ju Apr 18, 2024
62b0b0e
Add dropdownMawWidth property
KristinLBradley Apr 18, 2024
c7aafb4
HDS-3222 Update style for active and hovered options in Multiple Supe…
KristinLBradley Apr 19, 2024
2a7bf5b
HDS-3221 Remove old example from Showcase
KristinLBradley Apr 19, 2024
ae69381
HDS-3221 Update SuperSelect Multiple components to support rich conte…
KristinLBradley Apr 19, 2024
932c57b
Adjust heading size for 'Required and optional'
alex-ju Apr 22, 2024
47e1111
Remove redundant `@ariaLabel` from Field examples
alex-ju Apr 22, 2024
9b7019c
Fix multiselect complex content examples
alex-ju Apr 22, 2024
3ce03ab
HDS-3221 Fix test for dropdownMaxWidth, add test for matchTriggerWidt…
KristinLBradley Apr 22, 2024
94ce78a
Auto calculate position using `hds-anchored-position`
alex-ju Apr 23, 2024
45dc9b3
Replicate cancel search style for multiselect
alex-ju Apr 23, 2024
81ff5d6
HDS-3221 Fix accessibility issues with Groups and placeholder in Show…
KristinLBradley Apr 23, 2024
3642663
HDS-3221 set renderInPlace to true for all components
KristinLBradley Apr 23, 2024
4d6f32f
HDS-3221 Fix linting error
KristinLBradley Apr 23, 2024
2f1760d
Improve VRT snapshots
alex-ju Apr 24, 2024
6d3eae2
Show 'Clear Selected' only when there are selected items
alex-ju Apr 25, 2024
f87e3f7
Fix default after options example
alex-ju Apr 25, 2024
e7e9829
Add tests for `SuperSelect::Multiple`
alex-ju Apr 25, 2024
68693e5
HDS-3221 Add example of custom selectedItem component for Single, add…
KristinLBradley Apr 25, 2024
876d687
HDS-3221 Rename group-header to group-title to match Figma, fix Showc…
KristinLBradley Apr 26, 2024
ad71378
Make PowerSelect API available in Multiple variant
alex-ju Apr 26, 2024
71d680d
Make PowerSelect API available in Single variant
alex-ju Apr 29, 2024
6ac601a
Guard against default option border
alex-ju Apr 30, 2024
7abdf94
Use shared `ID_PREFIX` from label
alex-ju May 3, 2024
c122dd9
Compute `ariaLabelledBy` in SuperSelect
alex-ju May 3, 2024
fe0306e
Apply suggestions from code review
KristinLBradley May 6, 2024
67ac327
HDS-3221 Fix text capitalization in tests
KristinLBradley May 7, 2024
a574b8a
HDS-3221 Refactor dropdownMaxWIdth to pass an object instead of setti…
KristinLBradley May 8, 2024
9469325
HDS-3221 Add comments as reminder to add same arguments both in base …
KristinLBradley May 8, 2024
ba4677a
HDS-3221 Refactor how typography styles are applied to various child …
KristinLBradley May 8, 2024
7770eec
HDS-3221 Refactor to remove extra div
KristinLBradley May 8, 2024
84e9e82
HDS-3221 Change span to div
KristinLBradley May 8, 2024
8467a8e
HDS-3221 Updated id prefix for group title
KristinLBradley May 8, 2024
3a59b14
HDS-3221 update selected options to follow saem approach
KristinLBradley May 8, 2024
b29a376
HDS-3221 Simplify structure and styles for rich content examples
KristinLBradley May 8, 2024
8ca5c65
HDS-3221 Add missing closeOnSelect option forwarding to Single compon…
KristinLBradley May 9, 2024
9ef7865
HDS-3221 Remove unneeded ariaLabels from tests
KristinLBradley May 10, 2024
c2718ee
HDS-3221 Clean up code repetition and delete unneeded IDs
KristinLBradley May 10, 2024
9b6769a
HDS-3221 Delete old TODOs
KristinLBradley May 10, 2024
1d4a4d1
HDS-3221 Set matchTriggerWidth to false if dropdownMaxWidth is set
KristinLBradley May 10, 2024
dc89a6e
Document `setPowerSelectAPI`
alex-ju May 10, 2024
7c4d62e
Rename `options` to `option`
alex-ju May 10, 2024
8689a05
Fix subcomponent paths in `showcase`
alex-ju May 10, 2024
927b30c
Add tests for `SuperSelect::Multiple::Field`
alex-ju May 10, 2024
d2c913c
Apply showcase-related suggestions from code review
alex-ju May 10, 2024
1143ca9
Revert "Rename `options` to `option`"
alex-ju May 10, 2024
e3f5077
Reorder before/after options examples
alex-ju May 10, 2024
5d04c8b
HDS-3221 Restructure layout of width examples in Showcase, refactor s…
KristinLBradley May 10, 2024
1153f6b
Refactor padding declarations
alex-ju May 13, 2024
d998223
Refactor `ember-basic-dropdown-content`'s `max-width`
alex-ju May 13, 2024
bd4bceb
Add comment around indicator's positioning
alex-ju May 13, 2024
da42bdf
Comments updates
alex-ju May 13, 2024
0d1dfa5
Add basic style to `@afterOptionsContent`
alex-ju May 13, 2024
b2692b4
Fix font on search input
alex-ju May 13, 2024
86ed357
Fix hover indicator on message
alex-ju May 13, 2024
927dd78
HDS-3221 Fix dropdownMaxWidth and matchTriggerWidth
KristinLBradley May 13, 2024
cb27e14
HDS-3221 Move mock components for Showcase into new directory
KristinLBradley May 13, 2024
0c39b63
HDS-3221 Modify options data to include disabled option
KristinLBradley May 13, 2024
7e0379d
HDS-3221 Clean up CSS
KristinLBradley May 13, 2024
c38df4e
HDS-3221 Clean up comments
KristinLBradley May 13, 2024
068c6af
HDS-3221 Refactor how some typograpghy and color styles are applied, …
KristinLBradley May 13, 2024
134268c
Upgrade `ember-power-select` and fix dependencies
alex-ju May 14, 2024
30a1531
Add changelog entry
alex-ju May 14, 2024
101f08b
Add missing Sass path in `website`
alex-ju May 14, 2024
4969b1e
Fix examples
alex-ju May 14, 2024
d0b88c6
Fix a11y issues on power-select override
alex-ju May 14, 2024
3b8983d
Add SuperSelect base elements to showcase
alex-ju May 14, 2024
d524ff7
HDS-3221 Fix styles for checked disabled options, modify data used in…
KristinLBradley May 14, 2024
697842b
Merge branch 'hds-3221-super-select-component' of https://github.com/…
KristinLBradley May 14, 2024
ac8eb9d
Apply suggestions from code review
KristinLBradley May 14, 2024
3f027bf
HDS-3221 Fix comment breaking test
KristinLBradley May 14, 2024
a9c3192
HDS-3221 Update classname
KristinLBradley May 14, 2024
b77b60f
HDS-3221 Pass down closeOnSelect to field component
KristinLBradley May 14, 2024
5a9e1d5
HDS-3221 Add explicit size and tag to text
KristinLBradley May 14, 2024
23f0b6c
HDS-3221 Define reusable optionsData for tests
KristinLBradley May 14, 2024
0691393
HDS-3221 import label id prefix to use in aria-labelledby
KristinLBradley May 15, 2024
0a3221b
Fix mocked states
alex-ju May 15, 2024
8ced493
Expand changelog entry
alex-ju May 15, 2024
87680a9
Fixed a11y tests and add known exceptions
alex-ju May 15, 2024
40d6980
Remove typography-related styles
alex-ju May 15, 2024
7af7981
Add `ember-power-select` dep to `showcase`
alex-ju May 15, 2024
d32eacb
`SuperSelect` web docs (HDS-3222) (#2026)
KristinLBradley May 16, 2024
5686cc8
Adjust docs to update links and correct version
alex-ju May 17, 2024
f54c713
Add and document the need for `<BasicDropdownWormhole />`
alex-ju May 17, 2024
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
5 changes: 5 additions & 0 deletions .changeset/hot-grapes-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hashicorp/design-system-components": minor
---

`SuperSelect` - added components for single and multiple selection based on [PowerSelect](https://ember-power-select.com/)
12 changes: 12 additions & 0 deletions packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"ember-focus-trap": "^1.1.0",
"ember-keyboard": "^8.2.1",
"ember-modifier": "^4.1.0",
"ember-power-select": "^8.2.0",
"ember-stargate": "^0.4.3",
"ember-style-modifier": "^3.0.1",
"ember-truth-helpers": "^4.0.3",
Expand All @@ -60,8 +61,10 @@
"@babel/core": "^7.24.5",
"@babel/plugin-transform-typescript": "^7.23.6",
"@babel/runtime": "^7.24.5",
"@ember/test-helpers": "^3.3.0",
"@embroider/addon-dev": "^4.3.1",
"@glimmer/component": "^1.1.2",
"@glimmer/tracking": "^1.1.2",
"@glint/core": "^1.4.0",
"@glint/environment-ember-loose": "^1.4.0",
"@glint/template": "^1.4.0",
Expand All @@ -75,6 +78,8 @@
"@typescript-eslint/parser": "^6.14.0",
"babel-plugin-ember-template-compilation": "^2.2.4",
"concurrently": "^8.2.2",
"ember-basic-dropdown": "^8.1.0",
"ember-concurrency": "^4.0.2",
"ember-template-lint": "^4.18.2",
"ember-template-lint-plugin-prettier": "^4.2.0",
"eslint": "^8.52.0",
Expand Down Expand Up @@ -188,6 +193,13 @@
"./components/hds/form/radio/group.js": "./dist/_app_/components/hds/form/radio/group.js",
"./components/hds/form/select/base.js": "./dist/_app_/components/hds/form/select/base.js",
"./components/hds/form/select/field.js": "./dist/_app_/components/hds/form/select/field.js",
"./components/hds/form/super-select/after-options.js": "./dist/_app_/components/hds/form/super-select/after-options.js",
"./components/hds/form/super-select/multiple/base.js": "./dist/_app_/components/hds/form/super-select/multiple/base.js",
"./components/hds/form/super-select/multiple/field.js": "./dist/_app_/components/hds/form/super-select/multiple/field.js",
"./components/hds/form/super-select/option-group.js": "./dist/_app_/components/hds/form/super-select/option-group.js",
"./components/hds/form/super-select/placeholder.js": "./dist/_app_/components/hds/form/super-select/placeholder.js",
"./components/hds/form/super-select/single/base.js": "./dist/_app_/components/hds/form/super-select/single/base.js",
"./components/hds/form/super-select/single/field.js": "./dist/_app_/components/hds/form/super-select/single/field.js",
"./components/hds/form/text-input/base.js": "./dist/_app_/components/hds/form/text-input/base.js",
"./components/hds/form/text-input/field.js": "./dist/_app_/components/hds/form/text-input/field.js",
"./components/hds/form/textarea/base.js": "./dist/_app_/components/hds/form/textarea/base.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
}}
<label class={{this.classNames}} for={{@controlId}} ...attributes>
<label class={{this.classNames}} for={{@controlId}} id={{this.id}} ...attributes>
didoo marked this conversation as resolved.
Show resolved Hide resolved
{{yield}}
<Hds::Form::Indicator @isRequired={{@isRequired}} @isOptional={{@isOptional}} />
</label>
14 changes: 14 additions & 0 deletions packages/components/src/components/hds/form/label/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,22 @@
*/

import Component from '@glimmer/component';
export const ID_PREFIX = 'label-';

export default class HdsFormLabelIndexComponent extends Component {
/**
* Determines the unique ID to assign to the element
* @method id
* @return {(string|null)} The "id" attribute to apply to the element or null, if no controlId is provided
didoo marked this conversation as resolved.
Show resolved Hide resolved
*/
get id() {
let { controlId } = this.args;
if (controlId) {
return `${ID_PREFIX}${controlId}`;
}
return null;
}

/**
* Get the class names to apply to the component.
* @method classNames
Expand Down
didoo marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{{! @glint-nocheck: not typesafe yet }}
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
}}
{{#if @showNoSelectedMessage}}
didoo marked this conversation as resolved.
Show resolved Hide resolved
<Hds::Text::Body @tag="div" @size="200" class="hds-form-super-select__no-options-selected hds-foreground-strong">
No options selected
</Hds::Text::Body>
{{/if}}
<div class="hds-form-super-select__after-options">
{{#if @content}}
<Hds::Text::Body @tag="div" @size="100" class="hds-foreground-strong">
{{@content}}
</Hds::Text::Body>
{{else}}
{{#if (or @showAll @showSelected @clearSelected)}}
didoo marked this conversation as resolved.
Show resolved Hide resolved
{{#if @showOnlySelected}}
<Hds::Button @text="Show all" @size="small" @color="secondary" {{on "click" @showAll}} />
{{else}}
<Hds::Button @text="Show selected" @size="small" @color="secondary" {{on "click" @showSelected}} />
{{/if}}
{{#if (not-eq @selectedCount "0")}}
didoo marked this conversation as resolved.
Show resolved Hide resolved
<Hds::Button @text="Clear selected" @size="small" @color="secondary" {{on "click" @clearSelected}} />
{{/if}}
{{/if}}
<Hds::Text::Body @tag="div" @size="100" class="hds-form-super-select__result-count hds-foreground-strong">
{{@resultCountMessage}}
</Hds::Text::Body>
{{/if}}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: MPL-2.0
}}
{{! Important: if an argument is added in base.hbs, it must also be added/processed in the Base component used in field.hbs }}
<div class={{this.classNames}} {{style this.dropdownMaxWidthStyle}}>
<PowerSelectMultiple
@afterOptionsComponent={{if
this.showAfterOptions
(or
@afterOptionsComponent
(component
"hds/form/super-select/after-options"
content=@afterOptionsContent
resultCountMessage=this.resultCountMessage
showNoSelectedMessage=this.showNoSelectedMessage
showOnlySelected=this.showOnlySelected
showSelected=this.showSelected
showAll=this.showAll
clearSelected=this.clearSelected
selectedCount=this.selectedCount
)
)
}}
@ariaDescribedBy={{@ariaDescribedBy}}
KristinLBradley marked this conversation as resolved.
Show resolved Hide resolved
@ariaInvalid={{@ariaInvalid}}
@ariaLabel={{@ariaLabel}}
@ariaLabelledBy={{@ariaLabelledBy}}
@beforeOptionsComponent={{@beforeOptionsComponent}}
@calculatePosition={{if @verticalPosition undefined this.calculatePosition}}
@closeOnSelect={{false}}
didoo marked this conversation as resolved.
Show resolved Hide resolved
@disabled={{@disabled}}
@dropdownClass={{@dropdownClass}}
@extra={{@extra}}
@groupComponent={{component "hds/form/super-select/option-group"}}
@horizontalPosition={{@horizontalPosition}}
@initiallyOpened={{@initiallyOpened}}
didoo marked this conversation as resolved.
Show resolved Hide resolved
@labelText={{@labelText}}
@loadingMessage={{@loadingMessage}}
@matcher={{@matcher}}
@matchTriggerWidth={{if @dropdownMaxWidth false @matchTriggerWidth}}
@noMatchesMessage={{@noMatchesMessage}}
@onBlur={{@onBlur}}
@onChange={{@onChange}}
@onClose={{@onClose}}
@onFocus={{@onFocus}}
@onInput={{@onInput}}
@onKeydown={{@onKeydown}}
@onOpen={{@onOpen}}
@options={{@options}}
@optionsComponent={{@optionsComponent}}
@placeholder={{@placeholder}}
@placeholderComponent={{@placeholderComponent}}
@preventScroll={{@preventScroll}}
@registerAPI={{this.setPowerSelectAPI}}
@renderInPlace={{true}}
@resultCountMessage={{@resultCountMessage}}
@scrollTo={{@scrollTo}}
@search={{@search}}
@searchEnabled={{@searchEnabled}}
@searchField={{@searchField}}
@searchMessage={{@searchMessage}}
@searchPlaceholder={{this.searchPlaceholder}}
@selected={{@selected}}
@selectedItemComponent={{@selectedItemComponent}}
@tabindex={{@tabindex}}
@triggerClass={{@triggerClass}}
@triggerComponent={{@triggerComponent}}
@triggerId={{@triggerId}}
@triggerRole={{@triggerRole}}
@typeAheadMatcher={{@typeAheadMatcher}}
@verticalPosition={{@verticalPosition}}
...attributes
as |option select|
>
{{! even if technically what is yielded here are _a list_ of options, we've decided to keep the `option` name for consistency with the existing `PowerSelect` API }}
{{yield option select}}
KristinLBradley marked this conversation as resolved.
Show resolved Hide resolved
</PowerSelectMultiple>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/

import PowerSelectComponent from 'ember-power-select/components/power-select';
import anchoredPositionModifier from '../../../../../modifiers/hds-anchored-position';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';

const DEFAULT_HORIZONTAL_POSITION = 'bottom-start';
const HORIZONTAL_POSITION_MAPPING = {
left: 'bottom-start',
center: 'bottom',
right: 'bottom-end',
};

export default class HdsSuperSelectMultipleBaseComponent extends PowerSelectComponent {
@tracked powerSelectAPI;
@tracked showOnlySelected = false;
@tracked showNoSelectedMessage = false;

get selectedCount() {
return this.selected?.length || '0';
}

get optionsCount() {
return this.options?.length || '0';
}

get resultCountMessage() {
return `${this.selectedCount} selected of ${this.optionsCount} total`;
}

@action calculatePosition(trigger, content) {
didoo marked this conversation as resolved.
Show resolved Hide resolved
// use `hds-anchored-position` to calculate and set position
anchoredPositionModifier(content, [trigger], {
placement: this.args.horizontalPosition
? HORIZONTAL_POSITION_MAPPING[this.args.horizontalPosition]
: DEFAULT_HORIZONTAL_POSITION,
offsetOptions: 4,
enableCollisionDetection: true,
});
// prevent PowerSelect from setting position
return {};
}

/**
* This action sets the powerSelectAPI property and optionally calls a registerAPI function.
*
* @param {Object} powerSelectAPI - The API object for the PowerSelect component.
*
* If a `registerAPI` function is passed in through the component's arguments,
* this function will be called with the `powerSelectAPI` as its argument.
* This allows parent components or controllers to have access to the PowerSelect API.
*
* The `powerSelectAPI` is also stored on the component instance and used in `clearSelected`
*/
@action
setPowerSelectAPI(powerSelectAPI) {
didoo marked this conversation as resolved.
Show resolved Hide resolved
if (typeof this.args.registerAPI === 'function') {
this.args.registerAPI(powerSelectAPI);
}
this.powerSelectAPI = powerSelectAPI;
}

@action showSelected() {
this.showNoSelectedMessage = this.selectedCount === '0';
this.showOnlySelected = true;
}

@action showAll() {
this.showNoSelectedMessage = false;
this.showOnlySelected = false;
}

@action clearSelected() {
this.powerSelectAPI.actions.select(null);
// show all options after clearing all selection
this.showNoSelectedMessage = false;
this.showOnlySelected = false;
}

/**
* Determine if `@afterOptionsComponent` gets displayed
* @param showAfterOptions
* @type {boolean}
* @default true
*/
get showAfterOptions() {
return this.args.showAfterOptions ?? true;
}

// NOTE: The searchPlaceholder doesn't currently work for the multiple select
/**
* Get the search placeholder text
* @param searchPlaceholder
* @type {string}
* @default 'Search'
*/
get searchPlaceholder() {
return this.args.searchPlaceholder ?? 'Search';
}

/**
* Get the maxWidth to apply to the dropdown
* @param dropdownMaxWidth
* @type {string}
* @default 'none'
*/
get dropdownMaxWidthStyle() {
const maxWidthStyle = {};
if (this.args.dropdownMaxWidth) {
maxWidthStyle['--hds-form-super-select-dropdown-max-width'] =
this.args.dropdownMaxWidth;
}
return maxWidthStyle;
}

/**
* Get the class names to apply to the component.
* @method classNames
* @return {string} The "class" attribute to apply to the component.
*/
get classNames() {
let classes = ['hds-form-super-select', 'hds-form-super-select-multiple'];

// add a class based on the @matchTriggerWidth argument or whether dropdownMaxWidth is set
if (this.args.matchTriggerWidth === false || this.args.dropdownMaxWidth) {
classes.push('hds-form-super-select--dropdown-content-auto-width');
}

// add a class based on the @isInvalid argument
if (this.args.isInvalid) {
classes.push(`hds-form-super-select--is-invalid`);
}

// add a class based on the showOnlySelected
if (this.showOnlySelected) {
classes.push(`hds-form-super-select--show-only-selected`);
}

return classes.join(' ');
}
}
Loading