Skip to content

Commit

Permalink
feat(form-items): add slug and fix stack
Browse files Browse the repository at this point in the history
  • Loading branch information
ariellalgilmore committed Nov 30, 2023
1 parent f7fd7e7 commit 01e53b1
Show file tree
Hide file tree
Showing 27 changed files with 870 additions and 63 deletions.
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/carbon-web-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
],
"dependencies": {
"@babel/runtime": "^7.16.3",
"@carbon/styles": "^1.42.0",
"@carbon/styles": "1.44.0-rc.0",
"flatpickr": "4.6.1",
"lit": "^2.7.6",
"lodash-es": "^4.17.21"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,28 @@ $css--plex: true !default;
padding-bottom: rem(15px);
}
}

:host(#{$prefix}-combo-box[slug]) {
@extend .#{$prefix}--list-box__wrapper--slug;

.#{$prefix}--list-box__field {
padding: 0;
}

::slotted(#{$prefix}-slug) {
position: absolute;
inset-block-start: 50%;
inset-inline-end: $spacing-08;
}

::slotted(#{$prefix}-slug:not([revert-active])) {
transform: translateY(-50%);
}
}

:host(#{$prefix}-combo-box[warn]),
:host(#{$prefix}-combo-box[invalid]) {
::slotted(#{$prefix}-slug) {
inset-inline-end: $spacing-10;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ class CDSComboBox extends CDSDropdown {
}

updated() {
super.updated();
const { _listBoxNode: listBoxNode } = this;
if (listBoxNode) {
listBoxNode.classList.add(`${prefix}--combo-box`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@ export { DATE_PICKER_INPUT_COLOR_SCHEME, DATE_PICKER_INPUT_KIND };
*/
@customElement(`${prefix}-date-picker-input`)
class CDSDatePickerInput extends FocusMixin(LitElement) {
/**
* `true` if there is a slug.
*/
private _hasSlug = false;

/**
* Handles `slotchange` event.
*/
protected _handleSlugSlotChange({ target }: Event) {
const hasContent = (target as HTMLSlotElement)
.assignedNodes()
.filter((elem) =>
(elem as HTMLElement).matches !== undefined
? (elem as HTMLElement).matches(
(this.constructor as typeof CDSDatePickerInput).slugItem
)
: false
);

this._hasSlug = Boolean(hasContent);
(hasContent[0] as HTMLElement).setAttribute('size', 'mini');
this.requestUpdate();
}

/**
* The calendar icon.
*/
Expand Down Expand Up @@ -218,6 +242,7 @@ class CDSDatePickerInput extends FocusMixin(LitElement) {
warnText,
_handleClickWrapper: handleClickWrapper,
_handleInput: handleInput,
_hasSlug: hasSlug,
} = this;

const invalidIcon = WarningFilled16({
Expand Down Expand Up @@ -264,6 +289,7 @@ class CDSDatePickerInput extends FocusMixin(LitElement) {
[`${prefix}--date-picker-input__wrapper--invalid`]:
normalizedProps.invalid,
[`${prefix}--date-picker-input__wrapper--warn`]: normalizedProps.warn,
[`${prefix}--date-picker-input__wrapper--slug`]: hasSlug,
});

const helperTextClasses = classMap({
Expand All @@ -276,18 +302,21 @@ class CDSDatePickerInput extends FocusMixin(LitElement) {
<slot name="label-text">${labelText}</slot>
</label>
<div class="${inputWrapperClasses}" @click="${handleClickWrapper}">
<input
id="input"
type="${type}"
class="${inputClasses}"
?disabled="${disabled}"
pattern="${pattern}"
placeholder="${ifDefined(placeholder)}"
.value="${ifDefined(value)}"
?data-invalid="${invalid}"
@input="${handleInput}"
?readonly="${readonly}" />
${normalizedProps.icon || this._renderIcon()}
<span>
<input
id="input"
type="${type}"
class="${inputClasses}"
?disabled="${disabled}"
pattern="${pattern}"
placeholder="${ifDefined(placeholder)}"
.value="${ifDefined(value)}"
?data-invalid="${invalid}"
@input="${handleInput}"
?readonly="${readonly}" />
${normalizedProps.icon || this._renderIcon()}
<slot name="slug" @slotchange="${this._handleSlugSlotChange}"></slot>
</span>
</div>
<div
class="${prefix}--form-requirement"
Expand Down Expand Up @@ -319,6 +348,13 @@ class CDSDatePickerInput extends FocusMixin(LitElement) {
return `${prefix}-date-picker`;
}

/**
* A selector that will return the slug item.
*/
static get slugItem() {
return `${prefix}-slug`;
}

static shadowRootOptions = {
...LitElement.shadowRootOptions,
delegatesFocus: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ $css--plex: true !default;
@use '@carbon/styles/scss/theme' as *;
@use '@carbon/styles/scss/utilities' as *;
@use '@carbon/styles/scss/colors' as *;
@use '@carbon/styles/scss/spacing' as *;
@use '@carbon/styles/scss/components/form';
@use '@carbon/styles/scss/components/date-picker/date-picker' as *;

Expand Down Expand Up @@ -151,3 +152,15 @@ $css--plex: true !default;
height: rem(14px);
}
}

:host(#{$prefix}-date-picker-input) {
::slotted(#{$prefix}-slug) {
position: absolute;
inset-block-start: 50%;
inset-inline-end: $spacing-08;
}

::slotted(#{$prefix}-slug:not([revert-active])) {
transform: translateY(-50%);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@ $css--plex: true !default;
@use '@carbon/styles/scss/spacing' as *;
@use '@carbon/styles/scss/theme' as *;
@use '@carbon/styles/scss/utilities' as *;
@use '@carbon/styles/scss/components/list-box/list-box' as *;
@use '@carbon/styles/scss/components/list-box/list-box';
@use '@carbon/styles/scss/components/dropdown' as *;
@use '@carbon/styles/scss/components/form';
@use '@carbon/styles/scss/components/checkbox';
@use '@carbon/styles/scss/components/tag';

// https://github.com/carbon-design-system/carbon/issues/11408
@include list-box;

:host(#{$prefix}-dropdown) {
outline: none;

Expand Down Expand Up @@ -152,3 +149,30 @@ $css--plex: true !default;
@include skeleton;
}
}

:host(#{$prefix}-dropdown[slug]) {
@extend .#{$prefix}--list-box__wrapper--slug;

::slotted(#{$prefix}-slug) {
position: absolute;
inset-block-start: 50%;
inset-inline-end: $spacing-08;
}

::slotted(#{$prefix}-slug:not([revert-active])) {
transform: translateY(-50%);
}
}

:host(#{$prefix}-dropdown[slug][warn]),
:host(#{$prefix}-dropdown[slug][invalid]) {
.#{$prefix}--list-box {
.#{$prefix}--list-box__field {
padding-inline-end: $spacing-12;
}
}

::slotted(#{$prefix}-slug) {
inset-inline-end: $spacing-10;
}
}
39 changes: 39 additions & 0 deletions packages/carbon-web-components/src/components/dropdown/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ export {
class CDSDropdown extends ValidityMixin(
HostListenerMixin(FormMixin(FocusMixin(LitElement)))
) {
/**
* `true` if there is a slug.
*/
protected _hasSlug = false;

/**
* The latest status of this dropdown, for screen reader to accounce.
*/
Expand Down Expand Up @@ -246,6 +251,25 @@ class CDSDropdown extends ValidityMixin(
this.requestUpdate();
}

/**
* Handles `slotchange` event.
*/
protected _handleSlugSlotChange({ target }: Event) {
const hasContent = (target as HTMLSlotElement)
.assignedNodes()
.filter((elem) =>
(elem as HTMLElement).matches !== undefined
? (elem as HTMLElement).matches(
(this.constructor as typeof CDSDropdown).slugItem
)
: false
);

this._hasSlug = Boolean(hasContent);
(hasContent[0] as HTMLElement).setAttribute('size', 'mini');
this.requestUpdate();
}

/**
* Handles user-initiated selection of a dropdown item.
*
Expand Down Expand Up @@ -647,6 +671,12 @@ class CDSDropdown extends ValidityMixin(
return true;
}

updated() {
this._hasSlug
? this.setAttribute('slug', '')
: this.removeAttribute('slug');
}

/**
* The CSS class list for dropdown listbox
*/
Expand Down Expand Up @@ -692,6 +722,7 @@ class CDSDropdown extends ValidityMixin(
_handleKeydownInner: handleKeydownInner,
_handleKeypressInner: handleKeypressInner,
_handleSlotchangeHelperText: handleSlotchangeHelperText,
_handleSlugSlotChange: handleSlugSlotChange,
_slotHelperTextNode: slotHelperTextNode,
} = this;
const inline = type === DROPDOWN_TYPE.INLINE;
Expand Down Expand Up @@ -760,6 +791,7 @@ class CDSDropdown extends ValidityMixin(
${ChevronDown16({ 'aria-label': toggleLabel })}
</div>
</div>
<slot name="slug" @slotchange=${handleSlugSlotChange}></slot>
${menuBody}
</div>
<div
Expand Down Expand Up @@ -836,6 +868,13 @@ class CDSDropdown extends ValidityMixin(
return `${prefix}-dropdown-toggled`;
}

/**
* A selector that will return the slug item.
*/
static get slugItem() {
return `${prefix}-slug`;
}

static shadowRootOptions = {
...LitElement.shadowRootOptions,
delegatesFocus: true,
Expand Down
Loading

0 comments on commit 01e53b1

Please sign in to comment.