forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Migrate AssertionsOptions menu to Vue. (cypress-io#24286)
* React to vue * Goodbye dom. * Use event modifier. * Remove leading underscore in names * Use modifier. * Add flip, shift options. * Use createPopper instead of floating-ui. * Remove unnecessary dependencies. * addAssertion to event. * setPopperElement to event. * Strong types. * clarify test. Co-authored-by: Blue F <[email protected]>
- Loading branch information
Showing
19 changed files
with
574 additions
and
543 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
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,96 @@ | ||
<template> | ||
<div | ||
ref="popper" | ||
class="assertion-options" | ||
> | ||
<div | ||
v-for="{ name, value } in options" | ||
:key="`${name}${value}`" | ||
class="assertion-option" | ||
@click.stop="() => onClick(name, value)" | ||
> | ||
<span | ||
v-if="name" | ||
class="assertion-option-name" | ||
> | ||
{{ truncate(name) }}:{{ ' ' }} | ||
</span> | ||
<span | ||
v-else | ||
class="assertion-option-value" | ||
> | ||
{{ typeof value === 'string' && truncate(value) }} | ||
</span> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { createPopper } from '@popperjs/core' | ||
import { onMounted, ref, nextTick, Ref } from 'vue' | ||
import type { AssertionOption } from './types' | ||
const props = defineProps<{ | ||
type: string | ||
options: AssertionOption[] | ||
}>() | ||
const emit = defineEmits<{ | ||
(eventName: 'addAssertion', value: { type: string, name: string, value: string }) | ||
(eventName: 'setPopperElement', value: HTMLElement) | ||
}>() | ||
const truncate = (str: string) => { | ||
if (str && str.length > 80) { | ||
return `${str.substr(0, 77)}...` | ||
} | ||
return str | ||
} | ||
const popper: Ref<HTMLElement | null> = ref(null) | ||
onMounted(() => { | ||
nextTick(() => { | ||
const popperEl = popper.value as HTMLElement | ||
const reference = popperEl.parentElement as HTMLElement | ||
createPopper(reference, popperEl, { | ||
placement: 'right-start', | ||
}) | ||
emit('setPopperElement', popperEl) | ||
}) | ||
}) | ||
const onClick = (name, value) => { | ||
emit('addAssertion', { type: props.type, name, value }) | ||
} | ||
</script> | ||
|
||
<style lang="scss"> | ||
@import './assertions-style.scss'; | ||
.assertion-options { | ||
@include menu-style; | ||
font-size: 14px; | ||
max-width: 150px; | ||
overflow: hidden; | ||
overflow-wrap: break-word; | ||
position: absolute; | ||
.assertion-option { | ||
cursor: pointer; | ||
padding: 0.4rem 0.6rem; | ||
&:hover { | ||
background-color: #e9ecef; | ||
} | ||
.assertion-option-value { | ||
font-weight: 600; | ||
} | ||
} | ||
} | ||
</style> |
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,123 @@ | ||
<template> | ||
<div | ||
:class="['assertion-type', { 'single-assertion': !hasOptions }]" | ||
@click.stop="onClick" | ||
@mouseover.stop="onOpen" | ||
@mouseout.stop="onClose" | ||
> | ||
<div class="assertion-type-text"> | ||
<span> | ||
{{ type.replace(/\./g, ' ') }} | ||
</span> | ||
<span | ||
v-if="hasOptions" | ||
class="dropdown-arrow" | ||
> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="12" | ||
height="12" | ||
fill="currentColor" | ||
viewBox="0 0 16 16" | ||
> | ||
<path | ||
fillRule="evenodd" | ||
d="M4.646 1.646a.5.5 0 0 1 .708 0l6 6a.5.5 0 0 1 0 .708l-6 6a.5.5 0 0 1-.708-.708L10.293 8 4.646 2.354a.5.5 0 0 1 0-.708z" | ||
/> | ||
</svg> | ||
</span> | ||
</div> | ||
<AssertionOptions | ||
v-if="hasOptions && isOpen" | ||
:type="type" | ||
:options="options" | ||
@set-popper-element="setPopperElement" | ||
@add-assertion="addAssertion" | ||
/> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { Ref, ref } from 'vue' | ||
import AssertionOptions from './AssertionOptions.ce.vue' | ||
const props = defineProps<{ | ||
type: string | ||
options: any | ||
}>() | ||
const emit = defineEmits<{ | ||
(eventName: 'addAssertion', value: { type: string, name?: string, value?: string }) | ||
}>() | ||
const isOpen = ref(false) | ||
const hasOptions = props.options && !!props.options.length | ||
const popperElement: Ref<HTMLElement | null> = ref(null) | ||
const onOpen = () => { | ||
isOpen.value = true | ||
} | ||
const onClose = (e: MouseEvent) => { | ||
if (e.relatedTarget instanceof Element && | ||
popperElement.value && popperElement.value.contains(e.relatedTarget)) { | ||
return | ||
} | ||
isOpen.value = false | ||
} | ||
const onClick = () => { | ||
if (!hasOptions) { | ||
emit('addAssertion', { type: props.type }) | ||
} | ||
} | ||
const setPopperElement = (el: HTMLElement) => { | ||
popperElement.value = el | ||
} | ||
const addAssertion = ({ type, name, value }) => { | ||
emit('addAssertion', { type, name, value }) | ||
} | ||
</script> | ||
|
||
<style lang="scss"> | ||
@import './assertions-style.scss'; | ||
.assertion-type { | ||
color: #202020; | ||
cursor: default; | ||
font-size: 14px; | ||
padding: 0.4rem 0.4rem 0.4rem 0.7rem; | ||
position: static; | ||
&:first-of-type { | ||
padding-top: 0.5rem; | ||
} | ||
&:last-of-type { | ||
border-bottom-left-radius: $border-radius; | ||
border-bottom-right-radius: $border-radius; | ||
padding-bottom: 0.5rem; | ||
} | ||
&:hover { | ||
background-color: #e9ecef; | ||
} | ||
&.single-assertion { | ||
cursor: pointer; | ||
font-weight: 600; | ||
} | ||
.assertion-type-text { | ||
align-items: center; | ||
display: flex; | ||
.dropdown-arrow { | ||
margin-left: auto; | ||
} | ||
} | ||
} | ||
</style> |
Oops, something went wrong.