@@ -14,6 +14,16 @@ import { comboboxStyles as styles } from './combobox.styles';
1414 * @public
1515 */
1616export class Combobox extends FoundationCombobox {
17+ /**
18+ * Whether the combobox has a compact layout or not.
19+ *
20+ * @public
21+ * @remarks
22+ * HTML Attribute: minimal
23+ */
24+ @attr ( { attribute : 'autowidth' , mode : 'boolean' } )
25+ public autoWidth : boolean ;
26+
1727 /**
1828 * Whether the combobox has a compact layout or not.
1929 *
@@ -23,6 +33,68 @@ export class Combobox extends FoundationCombobox {
2333 */
2434 @attr ( { attribute : 'minimal' , mode : 'boolean' } )
2535 public minimal : boolean ;
36+
37+ /**
38+ * The connected callback for this FASTElement.
39+ *
40+ * @override
41+ *
42+ * @internal
43+ */
44+ connectedCallback ( ) : void {
45+ super . connectedCallback ( ) ;
46+ this . setAutoWidth ( ) ;
47+ }
48+
49+ /**
50+ * Synchronize the form-associated proxy and updates the value property of the element.
51+ *
52+ * @param prev - the previous collection of slotted option elements
53+ * @param next - the next collection of slotted option elements
54+ *
55+ * @internal
56+ */
57+ slottedOptionsChanged ( prev : Element [ ] | undefined , next : Element [ ] ) : void {
58+ super . slottedOptionsChanged ( prev , next ) ;
59+ this . setAutoWidth ( ) ;
60+ }
61+
62+ /**
63+ * (Un-)set the width when the autoWidth property changes.
64+ *
65+ * @param prev - the previous autoWidth value
66+ * @param next - the current autoWidth value
67+ */
68+ protected autoWidthChanged ( prev : boolean | undefined , next : boolean ) : void {
69+ if ( next ) {
70+ this . setAutoWidth ( ) ;
71+ } else {
72+ this . style . removeProperty ( 'width' ) ;
73+ }
74+ }
75+
76+ /**
77+ * Compute the listbox width to set the one of the input.
78+ */
79+ protected setAutoWidth ( ) : void {
80+ if ( ! this . autoWidth || ! this . isConnected ) {
81+ return ;
82+ }
83+
84+ let listWidth = this . listbox . getBoundingClientRect ( ) . width ;
85+ // If the list has not been displayed yet trick to get its size
86+ if ( listWidth === 0 && this . listbox . hidden ) {
87+ Object . assign ( this . listbox . style , { visibility : 'hidden' } ) ;
88+ this . listbox . removeAttribute ( 'hidden' ) ;
89+ listWidth = this . listbox . getBoundingClientRect ( ) . width ;
90+ this . listbox . setAttribute ( 'hidden' , '' ) ;
91+ this . listbox . style . removeProperty ( 'visibility' ) ;
92+ }
93+
94+ if ( listWidth > 0 ) {
95+ Object . assign ( this . style , { width : `${ listWidth } px` } ) ;
96+ }
97+ }
2698}
2799
28100/**
0 commit comments