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(progress): ts to sfc #506

Merged
merged 5 commits into from
Jan 18, 2024
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
110 changes: 110 additions & 0 deletions packages/vue/src/progress/Progress.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<script lang="ts">
import type { PrimitiveProps } from '@oku-ui/primitive'
import { Primitive } from '@oku-ui/primitive'
import type { Scope } from '@oku-ui/provide'
import type { Ref } from 'vue'
import { computed } from 'vue'
import { useComponentRef, useVModel } from '@oku-ui/use-composable'
import {
defaultGetValueLabel,
getInvalidMaxError,
getInvalidValueError,
getProgressState,
isNumber,
isValidMaxNumber,
isValidValueNumber,
useProgressProvide,
} from './utils'

const DEFAULT_MAX = 100

export type ProgressElement = HTMLDivElement

export interface ProgressProps extends PrimitiveProps {
scopeOkuProgress?: Scope
value?: number | null | undefined
max?: number
getValueLabel?(value: number, max: number): string
scopeProgress?: Scope
valueChange?(value: number | null | undefined): void
}

export type ProgressEmits = {
'update:value': [value: number | null | undefined]
'valueChange': [value: number | null | undefined]
}

</script>

<script setup lang="ts">

defineOptions({
name: 'OkuProgress',
})

const props = withDefaults(defineProps<ProgressProps>(), {
value: undefined,
max: DEFAULT_MAX,
getValueLabel: defaultGetValueLabel,
})
const emits = defineEmits<ProgressEmits>()

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

const value = useVModel(props, 'value', emits, {
passive: (props.value === undefined) as false,
shouldEmit(v: any) {
emits('valueChange', v)
return true
},
}) as Ref<number | null | undefined>

// propstype check
if (props.max && !isValidMaxNumber(props.max))
console.error(getInvalidMaxError(props.max))

if (props.value != null && !isValidValueNumber(props.value, props.max))
console.error(getInvalidValueError(props.value))
Comment on lines +63 to +67
Copy link
Collaborator

@Cr0zy07 Cr0zy07 Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to be on the props validator?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to use it with the interface.


const maxProp = computed(() =>
isValidMaxNumber(props.max) ? props.max : DEFAULT_MAX,
)

const valueProp = computed(() =>
isValidValueNumber(props.value, maxProp.value) ? props.value : null,
)

const valueLabel = computed(() =>
isNumber(valueProp.value)
? props.getValueLabel(valueProp.value, maxProp.value)
: undefined,
)

useProgressProvide({
scope: props.scopeOkuProgress,
value,
max: maxProp,
})

defineExpose({
$el: currentElement,
})
</script>

<template>
<Primitive
:is="props.is"
ref="componentRef"
:as-child="props.asChild"
:aria-valuemax="props.max"
:aria-valuemin="0"
:aria-valuenow="isNumber(props.value) ? props.value : undefined"
:aria-valuetext="valueLabel"
role="progressbar"
:data-state="getProgressState(maxProp, props.value)"
:data-value="props.value ?? undefined"
:data-max="maxProp"
>
<slot />
</Primitive>
</template>
43 changes: 43 additions & 0 deletions packages/vue/src/progress/ProgressIndicator.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<script lang="ts">
import { Primitive } from '@oku-ui/primitive'
import type { PrimitiveProps } from '@oku-ui/primitive'
import type { Scope } from '@oku-ui/provide'

export interface ProgressIndicatorProps extends PrimitiveProps {
scopeOkuProgress?: Scope
}
</script>

<script setup lang="ts">
import { useComponentRef } from '@oku-ui/use-composable'
import { getProgressState, useProgressInject } from './utils'

defineOptions({
name: 'OkuProgressIndicator',
})

const props = defineProps<ProgressIndicatorProps>()

const { componentRef } = useComponentRef<HTMLButtonElement | null>()
const inject = useProgressInject('OkuProgress', props.scopeOkuProgress)

defineExpose({
$el: componentRef,
})
</script>

<template>
<Primitive
:is="props.is"
ref="componentRef"
:as-child="props.asChild"
:data-state="getProgressState(
inject.max.value,
inject.value.value,
)"
:data-value="inject.value.value ?? undefined"
:data-max="inject.max.value"
>
<slot />
</Primitive>
</template>
3 changes: 0 additions & 3 deletions packages/vue/src/progress/constants.ts

This file was deleted.

26 changes: 10 additions & 16 deletions packages/vue/src/progress/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
export {
OkuProgress,
createProgressScope,
progressProps,
} from './progress'

export type {
ProgressProps,
ProgressElement,
} from './progress'
default as OkuProgress,
type ProgressProps,
type ProgressEmits,
} from './Progress.vue'

export {
OkuProgressIndicator,
progressIndicatorProps,
} from './progressIndicator'
default as OkuProgressIndicator,
type ProgressIndicatorProps,
} from './ProgressIndicator.vue'

export type {
ProgressIndicatorElement,
ProgressIndicatorProps,
} from './progressIndicator'
export {
createProgressScope,
} from './utils'
155 changes: 0 additions & 155 deletions packages/vue/src/progress/progress.ts

This file was deleted.

Loading
Loading