forked from oku-ui/primitives
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(switch): ts to sfc (oku-ui#504)
* refactor: switch ts to vue * remove: useControllable * chore: expose currentElement * fix: aria-checked not present * fix: data-disabled always appears * chore: update snapshots * Refactor switch component and add switch v-model tests
- Loading branch information
Showing
19 changed files
with
851 additions
and
705 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<script lang="ts"> | ||
export interface BubbleInputProps { | ||
checked: boolean | ||
control: HTMLElement | null | ||
bubbles: boolean | ||
} | ||
</script> | ||
|
||
<script setup lang="ts"> | ||
import { computed, toRefs, watchEffect } from 'vue' | ||
import { useComponentRef, usePrevious, useSize } from '@oku-ui/use-composable' | ||
defineOptions({ | ||
name: 'OkuBubbleInput', | ||
}) | ||
const props = defineProps<BubbleInputProps>() | ||
const { checked, control } = toRefs(props) | ||
const { componentRef, currentElement } = useComponentRef<HTMLInputElement | null>() | ||
const prevChecked = usePrevious(checked) | ||
const controlSize = computed(() => useSize(control)) | ||
// Bubble checked change to parents (e.g form change event) | ||
watchEffect(() => { | ||
const input = currentElement.value! | ||
const inputProto = window.HTMLInputElement.prototype | ||
const descriptor = Object.getOwnPropertyDescriptor(inputProto, 'checked') as PropertyDescriptor | ||
const setChecked = descriptor.set | ||
if (prevChecked.value !== props.checked && setChecked) { | ||
const event = new Event('click', { bubbles: props.bubbles }) | ||
setChecked.call(input, props.checked) | ||
input.dispatchEvent(event) | ||
} | ||
}) | ||
</script> | ||
|
||
<template> | ||
<input | ||
ref="componentRef" | ||
type="checkbox" | ||
:aria-hidden="true" | ||
:defaultChecked="checked" | ||
:tabIndex="-1" | ||
:style="{ | ||
...$attrs.style as any, | ||
...controlSize, | ||
position: 'absolute', | ||
pointerEvents: 'none', | ||
opacity: 0, | ||
margin: '0px', | ||
}" | ||
> | ||
</template> |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.