-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: focus a new declaration prop automatically (#18)
* refactor: split style declaration component * chore: add ability to auto focus on style value component * feat: focus a new declaration prop automatically * test: add style declaration component test
- Loading branch information
Showing
5 changed files
with
206 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<template> | ||
<div class="style-declaration"> | ||
<span class="style-declaration-prop"><StyleValue | ||
class="style-declaration-prop-text" | ||
:auto-focus="autoFocusProp" | ||
:value="prop" | ||
@input="inputProp" | ||
@input-end="finishInputProp" | ||
/></span> | ||
<span class="style-declaration-value"><StyleValue | ||
:value="value" | ||
@input="inputValue" | ||
@input-end="finishInputValue" | ||
/></span> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import Vue from 'vue' | ||
import StyleValue from './StyleValue.vue' | ||
export default Vue.extend({ | ||
name: 'StyleDeclaration', | ||
components: { | ||
StyleValue | ||
}, | ||
props: { | ||
prop: { | ||
type: String, | ||
required: true | ||
}, | ||
value: { | ||
type: String, | ||
required: true | ||
}, | ||
autoFocusProp: Boolean | ||
}, | ||
methods: { | ||
inputProp(prop: string): void { | ||
this.$emit('update:prop', prop) | ||
}, | ||
inputValue(value: string): void { | ||
this.$emit('update:value', value) | ||
}, | ||
finishInputProp(rawProp: string): void { | ||
const prop = rawProp.trim() | ||
if (!prop) { | ||
this.$emit('remove') | ||
} else { | ||
this.inputProp(prop) | ||
} | ||
}, | ||
finishInputValue(rawValue: string): void { | ||
const value = rawValue.trim() | ||
if (!value) { | ||
this.$emit('remove') | ||
} else { | ||
this.inputValue(value) | ||
} | ||
} | ||
} | ||
}) | ||
</script> | ||
|
||
|
||
<style lang="scss" scoped> | ||
.style-declaration-prop::after { | ||
content: ':'; | ||
} | ||
.style-declaration-value::after { | ||
content: ';'; | ||
} | ||
.style-declaration-prop-text { | ||
color: #24b600; | ||
} | ||
</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
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,71 @@ | ||
import { mount } from '@vue/test-utils' | ||
import StyleDeclaration from '@/view/components/StyleDeclaration.vue' | ||
import StyleValue from '@/view/components/StyleValue.vue' | ||
|
||
describe('StyleDeclaration', () => { | ||
it('should notify prop update', () => { | ||
const wrapper = mount(StyleDeclaration, { | ||
propsData: { | ||
prop: 'color', | ||
value: 'red' | ||
} | ||
}) | ||
|
||
const prop = wrapper.find(StyleValue) | ||
prop.vm.$emit('input', 'background-color') | ||
prop.vm.$emit('input-end', 'background-color') | ||
|
||
expect(wrapper.emitted('update:prop')[0]).toEqual(['background-color']) | ||
expect(wrapper.emitted('remove')).toBe(undefined) | ||
}) | ||
|
||
it('should notify value update', () => { | ||
const wrapper = mount(StyleDeclaration, { | ||
propsData: { | ||
prop: 'color', | ||
value: 'red' | ||
} | ||
}) | ||
|
||
const value = wrapper.findAll(StyleValue).at(1) | ||
value.vm.$emit('input', 'blue') | ||
value.vm.$emit('input-end', 'background-color') | ||
|
||
expect(wrapper.emitted('update:value')[0]).toEqual(['blue']) | ||
expect(wrapper.emitted('remove')).toBe(undefined) | ||
}) | ||
|
||
it('should request removing when prop is empty', () => { | ||
const wrapper = mount(StyleDeclaration, { | ||
propsData: { | ||
prop: 'color', | ||
value: 'red' | ||
} | ||
}) | ||
|
||
const prop = wrapper.find(StyleValue) | ||
prop.vm.$emit('input', '') | ||
prop.vm.$emit('input-end', '') | ||
|
||
expect(wrapper.emitted('update:prop').length).toBe(1) | ||
expect(wrapper.emitted('update:prop')[0]).toEqual(['']) | ||
expect(wrapper.emitted('remove')).not.toBe(undefined) | ||
}) | ||
|
||
it('should request removing when value is empty', () => { | ||
const wrapper = mount(StyleDeclaration, { | ||
propsData: { | ||
prop: 'color', | ||
value: 'red' | ||
} | ||
}) | ||
|
||
const value = wrapper.findAll(StyleValue).at(1) | ||
value.vm.$emit('input', '') | ||
value.vm.$emit('input-end', '') | ||
|
||
expect(wrapper.emitted('update:value').length).toBe(1) | ||
expect(wrapper.emitted('update:value')[0]).toEqual(['']) | ||
expect(wrapper.emitted('remove')).not.toBe(undefined) | ||
}) | ||
}) |
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