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

🐛 fix(segment): improve vertical rendering for the initial render #1486

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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/cool-buckets-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@baloise/ds-core': patch
---

**segment**: triggers change when selecting with space or enter key
5 changes: 5 additions & 0 deletions .changeset/rare-bags-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@baloise/ds-core': patch
---

**segment**: improve vertical rendering for the initial render
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ export class SegmentItem implements ComponentInterface {
}
}

connectedCallback(): void {
const segmentEl = (this.segmentEl = this.el.closest('bal-segment'))
if (segmentEl && segmentEl.vertical) {
this.isVertical = segmentEl.vertical
}
}

componentDidLoad() {
const segmentEl = (this.segmentEl = this.el.closest('bal-segment'))
if (segmentEl) {
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/components/bal-segment/bal-segment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
isEndKey,
isArrowLeftKey,
isArrowRightKey,
isEnterKey,
} from '@baloise/web-app-utils'
import { stopEventBubbling } from '../../utils/form-input'
import { FOCUS_KEYS } from '../../utils/focus-visible'
Expand Down Expand Up @@ -143,6 +144,7 @@ export class Segment implements ComponentInterface, BalWindowResizeObserver, Bal
this.el.addEventListener('touchstart', this.onPointerDown)
this.el.addEventListener('mousedown', this.onPointerDown)
this.disabledChanged()
this.isVertical = this.vertical
}

disconnectedCallback() {
Expand Down Expand Up @@ -204,12 +206,14 @@ export class Segment implements ComponentInterface, BalWindowResizeObserver, Bal
@Listen('keydown')
listenOnKeyDown(ev: KeyboardEvent) {
this.keyboardMode = FOCUS_KEYS.includes(ev.key)
let forceChange = false

let current: undefined | HTMLBalSegmentItemElement

if (isSpaceKey(ev)) {
if (isSpaceKey(ev) || isEnterKey(ev)) {
stopEventBubbling(ev)
current = this.getSegmentItem('current')
forceChange = this.value !== current.value
this.value = current.value
} else if (isArrowUpKey(ev) || isArrowLeftKey(ev)) {
stopEventBubbling(ev)
Expand All @@ -230,7 +234,7 @@ export class Segment implements ComponentInterface, BalWindowResizeObserver, Bal
}

const previous = this.checked
if (current !== previous) {
if (current !== previous || forceChange) {
this.checkButton(previous, current)
this.emitValueChange()
}
Expand Down