-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: checkbox separate each component (#229)
- Loading branch information
1 parent
8e68694
commit 261f4f1
Showing
5 changed files
with
188 additions
and
141 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import type { PropType, Ref } from 'vue' | ||
import { defineComponent, h, ref, toRefs, watchEffect } from 'vue' | ||
|
||
import { usePrevious, useSize } from '@oku-ui/use-composable' | ||
|
||
import type { ElementType, PrimitiveProps } from '@oku-ui/primitive' | ||
|
||
import { type CheckedState, isIndeterminate } from './utils' | ||
|
||
type BubbleInputElement = ElementType<'input'> | ||
|
||
interface BubbleInputProps extends PrimitiveProps { | ||
checked: Ref<CheckedState> | ||
control: HTMLElement | null | ||
bubbles: boolean | ||
} | ||
|
||
const OkuBubbleInput = defineComponent({ | ||
name: 'OkuBubbleInput', | ||
inheritAttrs: false, | ||
props: { | ||
checked: { | ||
type: [Boolean, String, Number] as PropType< | ||
boolean | string | number | undefined | 'indeterminate' | ||
>, | ||
default: false, | ||
}, | ||
control: { | ||
type: Object as PropType<Ref<HTMLElement | null>>, | ||
default: null, | ||
}, | ||
bubbles: { | ||
type: Boolean, | ||
default: true, | ||
}, | ||
}, | ||
setup(props, { attrs }) { | ||
const { ...inputAttrs } = attrs as BubbleInputElement | ||
const { checked, bubbles, control } = toRefs(props) | ||
const _ref = ref<HTMLInputElement>() | ||
|
||
const prevChecked = usePrevious(checked) | ||
const controlSize = useSize(control) | ||
|
||
watchEffect(() => { | ||
const input = _ref.value! | ||
const inputProto = window.HTMLInputElement.prototype | ||
const descriptor = Object.getOwnPropertyDescriptor(inputProto, 'checked') as PropertyDescriptor | ||
const setChecked = descriptor.set | ||
|
||
if (prevChecked !== checked.value && setChecked) { | ||
const event = new Event('click', { bubbles: bubbles.value }) | ||
input.indeterminate = isIndeterminate(checked.value) | ||
setChecked.call(input, isIndeterminate(checked.value) ? false : checked) | ||
input.dispatchEvent(event) | ||
} | ||
}) | ||
|
||
return () => | ||
h('input', { | ||
'type': 'checkbox', | ||
'aria-hidden': true, | ||
'defaultChecked': isIndeterminate(checked.value) ? false : checked, | ||
...inputAttrs, | ||
'tabIndex': -1, | ||
'ref': _ref, | ||
'style': { | ||
...inputAttrs.style as any, | ||
...controlSize, | ||
position: 'absolute', | ||
pointerEvents: 'none', | ||
opacity: 0, | ||
margin: 0, | ||
}, | ||
}) | ||
}, | ||
}) | ||
|
||
export type { | ||
BubbleInputProps, | ||
} | ||
export { | ||
OkuBubbleInput, | ||
} |
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 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,78 @@ | ||
import type { PropType, Ref } from 'vue' | ||
import { Transition, defineComponent, h, toRefs } from 'vue' | ||
|
||
import { useRef } from '@oku-ui/use-composable' | ||
import { Primitive } from '@oku-ui/primitive' | ||
|
||
import type { ElementType, MergeProps, PrimitiveProps, RefElement } from '@oku-ui/primitive' | ||
|
||
import type { Scope } from '@oku-ui/provide' | ||
import { getState, isIndeterminate } from './utils' | ||
import { useCheckboxInject } from './checkbox' | ||
|
||
type CheckboxIndicatorElement = ElementType<'span'> | ||
|
||
interface CheckboxIndicatorProps extends PrimitiveProps { | ||
forceMount?: true | ||
} | ||
|
||
const INDICATOR_NAME = 'OkuCheckboxIndicator' | ||
|
||
const CheckboxIndicator = defineComponent({ | ||
name: INDICATOR_NAME, | ||
components: { Transition }, | ||
props: { | ||
scopeCheckbox: { | ||
type: Object as unknown as PropType<Scope>, | ||
required: false, | ||
}, | ||
forceMount: Boolean, | ||
}, | ||
setup(props, { attrs, expose, slots }) { | ||
const { scopeCheckbox, forceMount } = toRefs(props) | ||
const { ...indicatorProps } = attrs as CheckboxIndicatorElement | ||
const { $el, newRef } = useRef<CheckboxIndicatorElement>() | ||
expose({ | ||
innerRef: $el, | ||
}) | ||
|
||
const context = useCheckboxInject(INDICATOR_NAME, scopeCheckbox.value) | ||
|
||
const originalReturn = () => h(Transition, {}, { | ||
default: () => (forceMount.value || isIndeterminate(context.value.state.value) || context.value.state.value === true) | ||
? h(Primitive.span, { | ||
'ref': newRef, | ||
'data-state': getState(context.value.state.value), | ||
'data-disabled': context.value.disabled ? '' : undefined, | ||
...indicatorProps, | ||
'style': { pointerEvents: 'none', ...attrs.style as any }, | ||
}, | ||
{ | ||
default: () => slots.default?.(), | ||
}, | ||
) | ||
: null, | ||
}) | ||
|
||
return originalReturn as unknown as { | ||
innerRef: Ref<HTMLButtonElement> | ||
} | ||
}, | ||
}) | ||
|
||
// TODO: https://github.com/vuejs/core/pull/7444 after delete | ||
type _OkuCheckboxIndicatorProps = MergeProps<CheckboxIndicatorProps, CheckboxIndicatorElement> | ||
|
||
type CheckboxIndicatorRef = RefElement<typeof OkuCheckboxIndicator> | ||
|
||
const OkuCheckboxIndicator = CheckboxIndicator as typeof CheckboxIndicator & (new () => { $props: _OkuCheckboxIndicatorProps }) | ||
|
||
export { | ||
OkuCheckboxIndicator, | ||
} | ||
|
||
export type { | ||
CheckboxIndicatorProps, | ||
CheckboxIndicatorElement, | ||
CheckboxIndicatorRef, | ||
} |
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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
export { OkuCheckbox, OkuCheckboxIndicator } from './checkbox' | ||
export { OkuCheckbox } from './checkbox' | ||
|
||
export type { | ||
CheckboxProps, | ||
CheckboxIndicatorProps, | ||
CheckboxElement, | ||
CheckboxIndicatorElement, | ||
CheckboxRef, | ||
CheckboxIndicatorRef, | ||
} from './checkbox' | ||
|
||
export { OkuCheckboxIndicator } from './checkboxIndicator' | ||
export type { CheckboxIndicatorRef } from './checkboxIndicator' |
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,14 @@ | ||
export type CheckedState = boolean | string | number | undefined | 'indeterminate' | ||
|
||
function isIndeterminate(checked?: CheckedState): checked is 'indeterminate' { | ||
return checked === 'indeterminate' | ||
} | ||
|
||
function getState(checked: CheckedState) { | ||
return isIndeterminate(checked) ? 'indeterminate' : checked ? 'checked' : 'unchecked' | ||
} | ||
|
||
export { | ||
isIndeterminate, | ||
getState, | ||
} |