-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(VDatePicker): extract ok/cancel into separate component (#18575)
closes #2945
- Loading branch information
Showing
50 changed files
with
372 additions
and
197 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
117 changes: 117 additions & 0 deletions
117
packages/vuetify/src/labs/VConfirmEdit/VConfirmEdit.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,117 @@ | ||
// Components | ||
import { VBtn } from '@/components/VBtn' | ||
|
||
// Composables | ||
import { useLocale } from '@/composables' | ||
import { useProxiedModel } from '@/composables/proxiedModel' | ||
|
||
// Utilities | ||
import { computed, ref, toRaw, watchEffect } from 'vue' | ||
import { deepEqual, genericComponent, propsFactory, useRender } from '@/util' | ||
|
||
// Types | ||
import type { Ref, VNode } from 'vue' | ||
import type { GenericProps } from '@/util' | ||
|
||
export type VConfirmEditSlots<T> = { | ||
default: { | ||
model: Ref<T> | ||
get actions (): VNode | ||
} | ||
} | ||
|
||
export const makeVConfirmEditProps = propsFactory({ | ||
modelValue: null, | ||
color: String, | ||
cancelText: { | ||
type: String, | ||
default: '$vuetify.confirmEdit.cancel', | ||
}, | ||
okText: { | ||
type: String, | ||
default: '$vuetify.confirmEdit.ok', | ||
}, | ||
}, 'VConfirmEdit') | ||
|
||
export const VConfirmEdit = genericComponent<new <T> ( | ||
props: { | ||
modelValue?: T | ||
'onUpdate:modelValue'?: (value: T) => void | ||
'onSave'?: (value: T) => void | ||
}, | ||
slots: VConfirmEditSlots<T> | ||
) => GenericProps<typeof props, typeof slots>>()({ | ||
name: 'VConfirmEdit', | ||
|
||
props: makeVConfirmEditProps(), | ||
|
||
emits: { | ||
cancel: () => true, | ||
save: (value: any) => true, | ||
'update:modelValue': (value: any) => true, | ||
}, | ||
|
||
setup (props, { emit, slots }) { | ||
const model = useProxiedModel(props, 'modelValue') | ||
const internalModel = ref() | ||
watchEffect(() => { | ||
internalModel.value = structuredClone(toRaw(model.value)) | ||
}) | ||
|
||
const { t } = useLocale() | ||
|
||
const isPristine = computed(() => { | ||
return deepEqual(model.value, internalModel.value) | ||
}) | ||
|
||
function save () { | ||
model.value = internalModel.value | ||
emit('save', internalModel.value) | ||
} | ||
|
||
function cancel () { | ||
internalModel.value = structuredClone(toRaw(model.value)) | ||
emit('cancel') | ||
} | ||
|
||
let actionsUsed = false | ||
useRender(() => { | ||
const actions = ( | ||
<> | ||
<VBtn | ||
disabled={ isPristine.value } | ||
variant="text" | ||
color={ props.color } | ||
onClick={ cancel } | ||
text={ t(props.cancelText) } | ||
/> | ||
|
||
<VBtn | ||
disabled={ isPristine.value } | ||
variant="text" | ||
color={ props.color } | ||
onClick={ save } | ||
text={ t(props.okText) } | ||
/> | ||
</> | ||
) | ||
return ( | ||
<> | ||
{ | ||
slots.default?.({ | ||
model: internalModel, | ||
get actions () { | ||
actionsUsed = true | ||
return actions | ||
}, | ||
}) | ||
} | ||
|
||
{ !actionsUsed && actions } | ||
</> | ||
) | ||
}) | ||
}, | ||
}) | ||
|
||
export type VConfirmEdit = InstanceType<typeof VConfirmEdit> |
81 changes: 81 additions & 0 deletions
81
packages/vuetify/src/labs/VConfirmEdit/__test__/VConfirmEdit.spec.cy.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,81 @@ | ||
/// <reference types="../../../../types/cypress" /> | ||
|
||
import { VConfirmEdit } from '..' | ||
|
||
// Utilities | ||
import { ref } from 'vue' | ||
|
||
// Tests | ||
describe('VConfirmEdit', () => { | ||
it('mirrors external updates', () => { | ||
const externalModel = ref('foo') | ||
|
||
cy.mount(() => ( | ||
<VConfirmEdit modelValue={ externalModel.value }> | ||
{ ({ model }) => ( | ||
<p>{ model.value }</p> | ||
)} | ||
</VConfirmEdit> | ||
)).get('p') | ||
.should('have.text', 'foo') | ||
.then(() => { | ||
externalModel.value = 'bar' | ||
}) | ||
.get('p') | ||
.should('have.text', 'bar') | ||
}) | ||
it.only(`doesn't mutate the original value`, () => { | ||
const externalModel = ref(['foo']) | ||
|
||
cy.mount( | ||
<VConfirmEdit v-model={ externalModel.value } modelValue={ externalModel.value }> | ||
{ ({ model }) => ( | ||
<> | ||
<p>{ model.value.join(',') }</p> | ||
<button data-test="push" onClick={ () => model.value.push('bar') }>Push</button> | ||
</> | ||
)} | ||
</VConfirmEdit> | ||
).get('p') | ||
.should('have.text', 'foo') | ||
.get('[data-test="push"]').click() | ||
.get('p') | ||
.should('have.text', 'foo,bar') | ||
.then(() => { | ||
expect(externalModel.value).to.deep.equal(['foo']) | ||
}) | ||
cy.contains('.v-btn', 'OK').click() | ||
cy.get('p') | ||
.should('have.text', 'foo,bar') | ||
.then(() => { | ||
expect(externalModel.value).to.deep.equal(['foo', 'bar']) | ||
}) | ||
}) | ||
it('hides actions if used from the slot', () => { | ||
cy.mount( | ||
<VConfirmEdit></VConfirmEdit> | ||
).get('.v-btn').should('have.length', 2) | ||
|
||
cy.mount( | ||
<VConfirmEdit> | ||
{ ({ model }) => { | ||
void model | ||
}} | ||
</VConfirmEdit> | ||
).get('.v-btn').should('have.length', 2) | ||
|
||
cy.mount( | ||
<VConfirmEdit> | ||
{ ({ actions }) => { | ||
void actions | ||
}} | ||
</VConfirmEdit> | ||
).get('.v-btn').should('have.length', 0) | ||
|
||
cy.mount( | ||
<VConfirmEdit> | ||
{ ({ actions }) => actions } | ||
</VConfirmEdit> | ||
).get('.v-btn').should('have.length', 2) | ||
}) | ||
}) |
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 { VConfirmEdit } from './VConfirmEdit' |
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
53 changes: 0 additions & 53 deletions
53
packages/vuetify/src/labs/VDatePicker/__tests__/VDatePicker.spec.cy.tsx
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.