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

refactor(checkbox): component and utils #507

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 2 additions & 14 deletions packages/vue/src/checkbox/Checkbox.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script lang="ts">
import { createScope } from '@oku-ui/provide'
import type { PrimitiveProps } from '@oku-ui/primitive'

export interface ScopeCheckbox {
Expand All @@ -8,12 +7,6 @@ export interface ScopeCheckbox {

export type CheckedState = boolean | 'indeterminate'

export type CheckboxProvide = {
_names: 'OkuCheckbox'
state: Ref<CheckedState>
disabled?: Ref<boolean | undefined>
}

export interface CheckboxProps extends PrimitiveProps {
scopeOkuCheckbox?: any
checked?: CheckedState
Expand All @@ -31,11 +24,6 @@ export type CheckboxEmits = {
'click': [event: MouseEvent]
}

export const { composeProviderScopes, createProvide } = createScope<CheckboxProvide['_names']>('OkuCheckbox')

export const { useInject, useProvider }
= createProvide<Omit<CheckboxProvide, '_names'>>('OkuCheckbox')

</script>

<script setup lang="ts">
Expand All @@ -45,7 +33,7 @@ import { useComponentRef, useVModel } from '@oku-ui/use-composable'
import { Primitive } from '@oku-ui/primitive'
import { composeEventHandlers } from '@oku-ui/utils'
import OkuBubbleInput from './BubbleInput.vue'
import { getState, isIndeterminate } from './utils'
import { getState, isIndeterminate, useCheckboxProvide } from './utils'

defineOptions({
name: 'OkuCheckbox',
Expand Down Expand Up @@ -97,7 +85,7 @@ watchEffect((onInvalidate) => {
}
})

useProvider({
useCheckboxProvide({
scope: props.scopeOkuCheckbox,
state: checked,
disabled: toRef(props, 'disabled'),
Expand Down
6 changes: 2 additions & 4 deletions packages/vue/src/checkbox/CheckboxIndicator.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<script lang="ts">
productdevbook marked this conversation as resolved.
Show resolved Hide resolved
// import type { CheckedState, ScopeCheckbox } from './Checkbox.vue'
import { Primitive } from '@oku-ui/primitive'
import type { PrimitiveProps } from '@oku-ui/primitive'
import { useInject } from './Checkbox.vue'

export interface CheckboxIndicatorProps extends PrimitiveProps {
scopeOkuCheckbox?: any
Expand All @@ -14,7 +12,7 @@ export interface CheckboxIndicatorProps extends PrimitiveProps {
<script setup lang="ts">
import { defineOptions, withDefaults } from 'vue'
import { useComponentRef } from '@oku-ui/use-composable'
import { getState, isIndeterminate } from './utils'
import { getState, isIndeterminate, useCheckboxInject } from './utils'
import { OkuPresence } from '@oku-ui/presence'

defineOptions({
Expand All @@ -30,7 +28,7 @@ const props = withDefaults(defineProps<CheckboxIndicatorProps>(), {

const { componentRef, currentElement } = useComponentRef<HTMLInputElement | null>()

const inject = useInject('OkuCheckbox', props.scopeOkuCheckbox)
const inject = useCheckboxInject('OkuCheckbox', props.scopeOkuCheckbox)

defineExpose({
$el: currentElement,
Expand Down
4 changes: 4 additions & 0 deletions packages/vue/src/checkbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ export {
default as OkuCheckboxIndicator,
type CheckboxIndicatorProps,
} from './CheckboxIndicator.vue'

export {
createCheckboxScope,
} from './utils'
14 changes: 14 additions & 0 deletions packages/vue/src/checkbox/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createScope } from '@oku-ui/provide'
import type { Ref } from 'vue'
import type { CheckedState } from './Checkbox.vue'

export function isIndeterminate(checked?: CheckedState): checked is 'indeterminate' {
Expand All @@ -7,3 +9,15 @@ export function isIndeterminate(checked?: CheckedState): checked is 'indetermina
export function getState(checked: CheckedState) {
return isIndeterminate(checked) ? 'indeterminate' : checked ? 'checked' : 'unchecked'
}

export type CheckboxContext = {
state: Ref<CheckedState>
disabled?: Ref<boolean | undefined>
}

export const
[createCheckboxProvide, createCheckboxScope]
= createScope<'OkuCheckbox'>('OkuCheckbox')

export const [useCheckboxProvide, useCheckboxInject]
= createCheckboxProvide<CheckboxContext>('OkuCheckbox')
7 changes: 6 additions & 1 deletion packages/vue/src/provide/createProvide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ function createScope<T extends string>(scopeName: T, createScopeDeps: CreateScop
throw new Error(`\`${consumerName}\` must be used within \`${rootComponentName}\``)
}

// return [Provider, useInject] as const
/**
* [useNameProvide, useNameInject]
*/
return [
useProvider,
useInject,
Expand Down Expand Up @@ -99,6 +101,9 @@ function createScope<T extends string>(scopeName: T, createScopeDeps: CreateScop
createScope.scopeName = scopeName
// return [createProvide, composeProvderScopes(createScope, ...createScopeDeps)] as const

/**
* [createNameProvide, createNameScope]
*/
return [
createProvide,
composeScopes(createScope, ...createScopeDeps),
Expand Down
Loading