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

chore(slider): fix story tests #4464

Merged
merged 2 commits into from
Jan 31, 2025
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
4 changes: 2 additions & 2 deletions packages/ui/src/components/va-slider/VaSlider.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import { VaSlider } from './'

function getSlider () {
return {
slider: getStoryId('slider'),
slider: getStoryId('slider').querySelector('[role="slider"]') as HTMLElement,
sliderTrack: getStorySelector('.va-slider__track'),
sliderThumb: getStorySelector('.va-slider__handler'),
}
}

function getSliderAll () {
return {
slider: getStoryIdAll('slider'),
slider: [...getStoryIdAll('slider')].map((el) => el.querySelector('[role="slider"]') as HTMLElement),
sliderTrack: getStorySelectorAll('.va-slider__track:not([class*=" "])'),
sliderThumb: getStorySelectorAll('.va-slider__handler'),
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-slider/VaSlider.vue
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ const sliderAriaAttributes = (value: number, order: 0 | 1) => {
role: 'slider',
'aria-valuemin': minComputed.value,
'aria-valuemax': maxComputed.value,
'aria-valuenow:': value,
'aria-valuenow': value,
'aria-valuetext': String(value),
'aria-label': props.range
? order === 0
Expand Down
23 changes: 15 additions & 8 deletions packages/ui/src/utils/watch-setter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { ComputedRef, Ref } from 'vue'

const isComputedRef = <T>(value: Ref<T>): value is ComputedRef<any> & { _setter: (v: T) => void} => {
return typeof value === 'object' && '_setter' in value
}
import { Ref } from 'vue'
import { warn } from './console'

// TODO: Maybe it is better to tweak useStateful
/**
Expand All @@ -12,10 +9,20 @@ const isComputedRef = <T>(value: Ref<T>): value is ComputedRef<any> & { _setter:
* @notice you likely want to watch when value is changed, not setter is called.
*/
export const watchSetter = <T>(ref: Ref<T>, cb: (newValue: T) => void) => {
if (!isComputedRef(ref)) { return }
const originalSetter = ref._setter
let key: 'setter' | '_setter'

if ('_setter' in ref) {
key = '_setter' as const
} else if ('setter' in ref) {
key = 'setter' as const
} else {
warn('watchSetter', 'Ref does not have setter', ref)
return
}

const originalSetter = (ref as any)[key]

ref._setter = (newValue: T) => {
;(ref as any)[key] = (newValue: T) => {
cb(newValue)
originalSetter(newValue)
}
Expand Down
Loading