-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add VDefaultsProvider component (#13743)
- Loading branch information
Showing
13 changed files
with
154 additions
and
25 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
packages/api-generator/src/locale/en/v-defaults-provider.json
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,5 @@ | ||
{ | ||
"props": { | ||
"defaults": "Specify new default prop values for components. Keep in mind that this will be merged with previously defined values" | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/api-generator/src/maps/components/v-defaults-provider.js
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,10 @@ | ||
module.exports = { | ||
'v-defaults-provider': { | ||
props: [ | ||
{ | ||
name: 'defaults', | ||
type: 'object', | ||
}, | ||
], | ||
}, | ||
} |
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
23 changes: 23 additions & 0 deletions
23
packages/docs/src/examples/v-defaults-provider/prop-defaults.vue
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,23 @@ | ||
<template> | ||
<div> | ||
<v-card title="Title" subtitle="Subtitle" class="ma-10"></v-card> | ||
<v-defaults-provider :defaults="defaults"> | ||
<v-card title="Title" subtitle="Subtitle" class="ma-10"></v-card> | ||
</v-defaults-provider> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
data: () => ({ | ||
defaults: { | ||
global: { | ||
elevation: 10, | ||
}, | ||
VCard: { | ||
color: 'secondary', | ||
}, | ||
}, | ||
}) | ||
} | ||
</script> |
24 changes: 24 additions & 0 deletions
24
packages/docs/src/pages/en/components/defaults-providers.md
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,24 @@ | ||
--- | ||
meta: | ||
title: Defaults provider component | ||
description: The defaults provider allows you to provide specific default prop values to components in a section of your application | ||
keywords: defaults provider, vuetify defaults provider component, vue defaults provider component | ||
related: | ||
- /features/global-config/ | ||
--- | ||
|
||
# Defaults providers | ||
|
||
The defaults provider allows you to provide specific default prop values to components in a section of your application | ||
|
||
## API | ||
|
||
- [v-defaults-provider](/api/v-defaults-provider) | ||
|
||
## Examples | ||
|
||
### Defaults | ||
|
||
The `v-defaults-provider` expects a prop **defaults** which looks the same as the **defaults** object that you can pass to `createVuetify` when creating your application. | ||
|
||
<example file="v-defaults-provider/prop-defaults" /> |
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
17 changes: 17 additions & 0 deletions
17
packages/vuetify/src/components/VDefaultsProvider/VDefaultsProvider.tsx
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,17 @@ | ||
import { provideDefaults } from '@/composables/defaults' | ||
import { defineComponent } from 'vue' | ||
|
||
import type { PropType } from 'vue' | ||
import type { DefaultsOptions } from '@/composables/defaults' | ||
|
||
export default defineComponent({ | ||
props: { | ||
defaults: Object as PropType<DefaultsOptions>, | ||
}, | ||
|
||
setup (props, { slots }) { | ||
provideDefaults(props) | ||
|
||
return () => slots.default?.() | ||
}, | ||
}) |
17 changes: 17 additions & 0 deletions
17
packages/vuetify/src/components/VDefaultsProvider/__tests__/VDefaultsProvider.cy.spec.tsx
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,17 @@ | ||
/// <reference types="../../../../types/cypress" /> | ||
|
||
import { CenteredGrid } from '@/../cypress/templates' | ||
import { VCard } from '@/components' | ||
import VDefaultsProvider from '../VDefaultsProvider' | ||
|
||
describe('VDefaultsProvider', () => { | ||
it('should apply new defaults', () => { | ||
cy.mount(() => ( | ||
<CenteredGrid width="200px"> | ||
<VDefaultsProvider defaults={{ global: { elevation: 10 }, VCard: { color: 'primary' } }}> | ||
<VCard title="foo" subtitle="bar"></VCard> | ||
</VDefaultsProvider> | ||
</CenteredGrid> | ||
)).get('.v-card').should('have.class', 'elevation-10').should('have.class', 'bg-primary') | ||
}) | ||
}) |
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 @@ | ||
export { default as VDefaultsProvider } from './VDefaultsProvider' |
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,35 @@ | ||
import { computed, inject, provide, ref } from 'vue' | ||
import { mergeDeep } from '@/util' | ||
|
||
import type { InjectionKey, Ref } from 'vue' | ||
|
||
interface DefaultsInstance { | ||
[key: string]: undefined | Record<string, unknown> | ||
global?: Record<string, unknown> | ||
} | ||
|
||
export type DefaultsOptions = Partial<DefaultsInstance> | ||
|
||
export const VuetifyDefaultsSymbol: InjectionKey<Ref<DefaultsInstance>> = Symbol.for('vuetify:defaults') | ||
|
||
export function createDefaults (options?: DefaultsInstance): Ref<DefaultsInstance> { | ||
return ref(options ?? {}) | ||
} | ||
|
||
export function useDefaults () { | ||
const defaults = inject(VuetifyDefaultsSymbol) | ||
|
||
if (!defaults) throw new Error('[Vuetify] Could not find defaults instance') | ||
|
||
return defaults | ||
} | ||
|
||
export function provideDefaults (props?: { defaults?: DefaultsInstance }) { | ||
const defaults = useDefaults() | ||
|
||
const newDefaults = computed(() => mergeDeep(defaults.value, props?.defaults) as any as DefaultsInstance) | ||
|
||
provide(VuetifyDefaultsSymbol, newDefaults) | ||
|
||
return newDefaults | ||
} |
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