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: 优化组件 provide/inject 使用 #3027

Merged
merged 1 commit into from
Apr 11, 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
4 changes: 2 additions & 2 deletions src/packages/__VUE/audio/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ import { toRefs, ref, onMounted, reactive, watch, provide, type PropType } from
import { createComponent } from '@/packages/utils/create'
import { Service } from '@nutui/icons-vue'
import NutRange from '../range/index.vue'
import type { AudioType } from './types'
import { AUDIO_KEY, type AudioType } from './types'
const { create } = createComponent('audio')

export default create({
Expand Down Expand Up @@ -257,7 +257,7 @@ export default create({
}
)

provide('audioParent', {
provide(AUDIO_KEY, {
children: [],
props,
audioData,
Expand Down
2 changes: 2 additions & 0 deletions src/packages/__VUE/audio/types.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export type AudioType = 'progress' | 'icon' | 'controls' | 'none'
export type AudioOperateType = 'play' | 'back' | 'forward' | 'mute'
export const AUDIO_KEY = Symbol('nut-audio')
4 changes: 2 additions & 2 deletions src/packages/__VUE/audiooperate/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { toRefs, ref, useSlots, reactive, inject, type PropType } from 'vue'
import { createComponent } from '@/packages/utils/create'
import NutButton from '../button'
import { useLocale } from '@/packages/utils/useLocale'
import type { AudioOperateType } from './types'
import { type AudioOperateType, AUDIO_KEY } from '../audio/types'

const { create } = createComponent('audio-operate')
const cN = 'NutAudioOperate'
Expand All @@ -52,7 +52,7 @@ export default create({

setup(props) {
const translate = useLocale(cN)
const audio: any = inject('audioParent')
const audio: any = inject(AUDIO_KEY)
const parent: {
children: []
audioData: any
Expand Down
1 change: 0 additions & 1 deletion src/packages/__VUE/audiooperate/types.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/packages/__VUE/form/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ComputedRef, Ref } from 'vue'

export const FORM_KEY = Symbol('nut-form')
export const FORM_DISABLED_KEY = Symbol('nut-form-disabled')
export const FORM_TIP_KEY = Symbol('nut-form-tip')

export const useFormDisabled = (disabled: Ref<boolean>): ComputedRef<boolean> => {
const { parent } = useParent(FORM_DISABLED_KEY)
Expand Down
4 changes: 2 additions & 2 deletions src/packages/__VUE/form/form.taro.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { reactive, computed, provide, watch } from 'vue'
import { useChildren, getPropByPath, isPromise } from '@/packages/utils'
import NutCellGroup from '../cellgroup/index.taro'
import type { FormErrorMessage, FormLabelPosition, FormRule, FormRules, FormStarPosition, FormItemRule } from './types'
import { FORM_KEY, FORM_DISABLED_KEY } from './common'
import { FORM_KEY, FORM_DISABLED_KEY, FORM_TIP_KEY } from './common'

defineOptions({
name: 'NutForm'
Expand Down Expand Up @@ -42,7 +42,7 @@ linkChildren2({ props })

const formErrorTip = computed(() => reactive<any>({}))

provide('formErrorTip', formErrorTip)
provide(FORM_TIP_KEY, formErrorTip)
const clearErrorTips = () => {
Object.keys(formErrorTip.value).forEach((item) => {
formErrorTip.value[item] = ''
Expand Down
4 changes: 2 additions & 2 deletions src/packages/__VUE/form/form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { reactive, computed, provide, watch } from 'vue'
import { useChildren, getPropByPath, isPromise } from '@/packages/utils'
import NutCellGroup from '../cellgroup'
import type { FormErrorMessage, FormLabelPosition, FormRule, FormRules, FormStarPosition, FormItemRule } from './types'
import { FORM_KEY, FORM_DISABLED_KEY } from './common'
import { FORM_KEY, FORM_DISABLED_KEY, FORM_TIP_KEY } from './common'

defineOptions({
name: 'NutForm'
Expand Down Expand Up @@ -42,7 +42,7 @@ linkChildren2({ props })

const formErrorTip = computed(() => reactive<any>({}))

provide('formErrorTip', formErrorTip)
provide(FORM_TIP_KEY, formErrorTip)
const clearErrorTips = () => {
Object.keys(formErrorTip.value).forEach((item) => {
formErrorTip.value[item] = ''
Expand Down
4 changes: 2 additions & 2 deletions src/packages/__VUE/formitem/form-item.taro.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { pxCheck } from '@/packages/utils/pxCheck'
import { computed, inject, CSSProperties } from 'vue'
import type { FormItemRule, FormLabelPosition, FormStarPosition } from '../form/types'
import NutCell from '../cell/index.taro'
import { FORM_KEY } from '../form/common'
import { FORM_KEY, FORM_TIP_KEY } from '../form/common'
import { useParent } from '@/packages/utils'

defineOptions({
Expand Down Expand Up @@ -84,7 +84,7 @@ const starPositionClass = computed(() => {
return position !== 'left' ? `nut-form-item__star-${position}` : ''
})

const parent = inject('formErrorTip') as any
const parent = inject(FORM_TIP_KEY) as any

const labelStyle = computed(() => {
return {
Expand Down
4 changes: 2 additions & 2 deletions src/packages/__VUE/formitem/form-item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { pxCheck } from '@/packages/utils/pxCheck'
import { computed, inject, CSSProperties } from 'vue'
import type { FormItemRule, FormLabelPosition, FormStarPosition } from '../form/types'
import NutCell from '../cell'
import { FORM_KEY } from '../form/common'
import { FORM_KEY, FORM_TIP_KEY } from '../form/common'
import { useParent } from '@/packages/utils'

defineOptions({
Expand Down Expand Up @@ -84,7 +84,7 @@ const starPositionClass = computed(() => {
return position !== 'left' ? `nut-form-item__star-${position}` : ''
})

const parent = inject('formErrorTip') as any
const parent = inject(FORM_TIP_KEY) as any

const labelStyle = computed(() => {
return {
Expand Down
3 changes: 2 additions & 1 deletion src/packages/__VUE/tabpane/index.taro.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<script lang="ts">
import { computed, CSSProperties, inject } from 'vue'
import { createComponent } from '@/packages/utils/create'
import { TABS_KEY } from '../tabs/types'
const { create } = createComponent('tab-pane')

export default create({
Expand All @@ -25,7 +26,7 @@ export default create({
},
emits: ['click'],
setup(props) {
const parentOption = inject('tabsOpiton') as any
const parentOption = inject(TABS_KEY) as any
const paneStyle = computed(() => {
return {
display:
Expand Down
3 changes: 2 additions & 1 deletion src/packages/__VUE/tabpane/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<script lang="ts">
import { computed, CSSProperties, inject } from 'vue'
import { createComponent } from '@/packages/utils/create'
import { TABS_KEY } from '../tabs/types'
const { create } = createComponent('tab-pane')

export default create({
Expand All @@ -25,7 +26,7 @@ export default create({
},
emits: ['click'],
setup(props) {
const parentOption = inject('tabsOpiton') as any
const parentOption = inject(TABS_KEY) as any
const paneStyle = computed(() => {
return {
display:
Expand Down
4 changes: 2 additions & 2 deletions src/packages/__VUE/tabs/index.taro.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import { TypeOfFun } from '@/packages/utils/util'
import raf from '@/packages/utils/raf'
import { useTabContentTouch } from './hooks'
import { useTaroRect } from '@/packages/utils/useTaroRect'
import type { RectItem, TabsDirection, TabsSize, TabsType, TabsAlign } from './types'
import { type RectItem, type TabsDirection, type TabsSize, type TabsType, type TabsAlign, TABS_KEY } from './types'

export class Title {
title = ''
Expand Down Expand Up @@ -158,7 +158,7 @@ export default create({
setup(props, { emit, slots }: any) {
const refRandomId = Math.random().toString(36).slice(-8)
const container = ref(null)
provide('tabsOpiton', {
provide(TABS_KEY, {
activeKey: computed(() => props.modelValue || '0'),
autoHeight: computed(() => props.autoHeight),
animatedTime: computed(() => props.animatedTime)
Expand Down
4 changes: 2 additions & 2 deletions src/packages/__VUE/tabs/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ import { TypeOfFun } from '@/packages/utils/util'
import { useRect } from '@/packages/utils/useRect'
import raf from '@/packages/utils/raf'
import { useTabContentTouch } from './hooks'
import type { TabsDirection, TabsSize, TabsType, TabsAlign } from './types'
import { type TabsDirection, type TabsSize, type TabsType, type TabsAlign, TABS_KEY } from './types'

export class Title {
title = ''
Expand Down Expand Up @@ -184,7 +184,7 @@ export default create({
setup(props, { emit, slots }: any) {
const container = ref<any>(null)
let stickyFixed: boolean
provide('tabsOpiton', {
provide(TABS_KEY, {
activeKey: computed(() => props.modelValue || '0'),
autoHeight: computed(() => props.autoHeight),
animatedTime: computed(() => props.animatedTime)
Expand Down
1 change: 1 addition & 0 deletions src/packages/__VUE/tabs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export type TabsDirection = 'horizontal' | 'vertical'
export type TabsSize = 'large' | 'normal' | 'small'
export type TabsType = 'line' | 'smile'
export type TabsAlign = 'left' | 'center'
export const TABS_KEY = Symbol('nut-tab')