Skip to content

Commit

Permalink
🐛 fix(segment): improve vertical rendering for the initial render (#1486
Browse files Browse the repository at this point in the history
)

* fix(segment): improve vertical rendering for the initial render

* fix(segment): triggers change when selecting with space or enter key

* fix(segement): get inital veritcal value
  • Loading branch information
hirsch88 authored Oct 22, 2024
1 parent 7e920eb commit 4b57650
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
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

0 comments on commit 4b57650

Please sign in to comment.