-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5174 from manuelmeister/fix/null-colorpicker
Create new color picker & color field
- Loading branch information
Showing
22 changed files
with
938 additions
and
280 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,44 @@ | ||
<!-- | ||
Displays a field as a e-color-field + write access via API wrapper | ||
--> | ||
|
||
<template> | ||
<api-wrapper v-slot="wrapper" v-bind="$props" separate-buttons v-on="$listeners"> | ||
<e-color-field | ||
:value="wrapper.localValue" | ||
v-bind="$attrs" | ||
:path="path" | ||
:readonly="wrapper.readonly" | ||
:disabled="disabled" | ||
:error-messages="wrapper.errorMessages" | ||
:loading="wrapper.isSaving || wrapper.isLoading ? 'secondary' : false" | ||
:outlined="outlined" | ||
:filled="filled" | ||
:dense="dense" | ||
@input="wrapper.on.input" | ||
@blur="wrapper.on.blur" | ||
> | ||
<template #append> | ||
<api-wrapper-append :wrapper="wrapper" /> | ||
</template> | ||
</e-color-field> | ||
</api-wrapper> | ||
</template> | ||
|
||
<script> | ||
import { apiPropsMixin } from '@/mixins/apiPropsMixin.js' | ||
import ApiWrapper from './ApiWrapper.vue' | ||
import ApiWrapperAppend from './ApiWrapperAppend.vue' | ||
export default { | ||
name: 'ApiColorField', | ||
components: { ApiWrapper, ApiWrapperAppend }, | ||
mixins: [apiPropsMixin], | ||
data() { | ||
return {} | ||
}, | ||
} | ||
</script> | ||
|
||
<style scoped></style> |
92 changes: 92 additions & 0 deletions
92
frontend/src/components/form/api/__tests__/ApiColorField.spec.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,92 @@ | ||
import ApiColorField from '../ApiColorField.vue' | ||
import { fireEvent, screen, waitFor } from '@testing-library/vue' | ||
import { render } from '@/test/renderWithVuetify.js' | ||
import user from '@testing-library/user-event' | ||
import { ApiMock } from '@/components/form/api/__tests__/ApiMock' | ||
import { extend } from 'vee-validate' | ||
import { regex } from 'vee-validate/dist/rules' | ||
import { ColorSpace, sRGB } from 'colorjs.io/fn' | ||
|
||
extend('regex', regex) | ||
|
||
ColorSpace.register(sRGB) | ||
describe('An ApiColorField', () => { | ||
let apiMock | ||
|
||
const FIELD_PATH = 'test-field/123' | ||
const FIELD_LABEL = 'Test field' | ||
const COLOR_1 = '#FF0000' | ||
const COLOR_2 = '#FAFFAF' | ||
|
||
beforeEach(() => { | ||
apiMock = ApiMock.create() | ||
}) | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks() | ||
}) | ||
|
||
test('triggers api.patch and status update if input changes', async () => { | ||
// given | ||
apiMock.get().thenReturn(ApiMock.success(COLOR_1).forPath(FIELD_PATH)) | ||
apiMock.patch().thenReturn(ApiMock.success(COLOR_2)) | ||
render(ApiColorField, { | ||
props: { | ||
autoSave: false, | ||
path: FIELD_PATH, | ||
uri: 'test-field/123', | ||
label: FIELD_LABEL, | ||
required: true, | ||
}, | ||
mocks: { | ||
api: apiMock.getMocks(), | ||
}, | ||
}) | ||
|
||
// when | ||
const inputField = await screen.findByLabelText(FIELD_LABEL) | ||
inputField.value = COLOR_2 | ||
await fireEvent.input(inputField) | ||
// click the button to open the picker | ||
// click the save button | ||
await waitFor(async () => { | ||
await user.click(screen.getByLabelText('Speichern')) | ||
}) | ||
|
||
// then | ||
await waitFor(async () => { | ||
const inputField = await screen.findByLabelText(FIELD_LABEL) | ||
expect(inputField.value).toBe(COLOR_2) | ||
expect(apiMock.getMocks().patch).toBeCalledTimes(1) | ||
}) | ||
}) | ||
|
||
test('updates state if value in store is refreshed and has new value', async () => { | ||
// given | ||
apiMock.get().thenReturn(ApiMock.networkError().forPath(FIELD_PATH)) | ||
render(ApiColorField, { | ||
props: { | ||
autoSave: false, | ||
path: FIELD_PATH, | ||
uri: 'test-field/123', | ||
label: FIELD_LABEL, | ||
required: true, | ||
}, | ||
mocks: { | ||
api: apiMock.getMocks(), | ||
}, | ||
}) | ||
await screen.findByText('A network error occurred.') | ||
expect((await screen.findByLabelText(FIELD_LABEL)).value).not.toBe(COLOR_1) | ||
const retryButton = await screen.findByText('Erneut versuchen') | ||
apiMock.get().thenReturn(ApiMock.success(COLOR_1).forPath(FIELD_PATH)) | ||
|
||
// when | ||
await user.click(retryButton) | ||
|
||
// then | ||
await waitFor(async () => { | ||
expect((await screen.findByLabelText(FIELD_LABEL)).value).toBe(COLOR_1) | ||
}) | ||
}) | ||
}) |
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,107 @@ | ||
<template> | ||
<EParseField | ||
ref="input" | ||
:value="value" | ||
:format="format" | ||
:parse="parse" | ||
:serialize="serialize" | ||
:deserialize="deserialize" | ||
:required="required" | ||
:vee-id="veeId" | ||
:vee-rules="veeRules" | ||
reset-on-blur | ||
v-bind="$attrs" | ||
v-on="$listeners" | ||
@input="$emit('input', $event)" | ||
> | ||
<!-- passing through all slots --> | ||
<slot v-for="(_, name) in $slots" :slot="name" :name="name" /> | ||
<template v-for="(_, name) in $scopedSlots" :slot="name" slot-scope="slotData"> | ||
<slot v-if="name !== 'prepend'" :name="name" v-bind="slotData" /> | ||
</template> | ||
<template #prepend="props"> | ||
<slot name="prepend" v-bind="props"> | ||
<ColorSwatch | ||
class="mt-n1" | ||
:color="props.serializedValue" | ||
tag="div" | ||
:aria-label="props.serializedValue" | ||
/> | ||
</slot> | ||
</template> | ||
</EParseField> | ||
</template> | ||
|
||
<script> | ||
import { reactive } from 'vue' | ||
import { formComponentMixin } from '@/mixins/formComponentMixin.js' | ||
import { parse, serialize } from 'colorjs.io/fn' | ||
import ColorSwatch from '@/components/form/base/ColorPicker/ColorSwatch.vue' | ||
export default { | ||
name: 'EColorField', | ||
components: { ColorSwatch }, | ||
mixins: [formComponentMixin], | ||
props: { | ||
value: { type: String, required: false, default: null }, | ||
}, | ||
emits: ['input'], | ||
methods: { | ||
format(value) { | ||
if (typeof value === 'string') { | ||
return value | ||
} | ||
return !value | ||
? '' | ||
: serialize(value, { | ||
space: 'srgb', | ||
format: 'hex', | ||
collapse: false, | ||
}).toUpperCase() | ||
}, | ||
/** | ||
* @param {string} value | ||
*/ | ||
parse(value) { | ||
if (value === '') { | ||
return null | ||
} | ||
try { | ||
const color = parse(value) | ||
color.alpha = 1 | ||
return reactive(color) | ||
} catch (e) { | ||
if (e instanceof TypeError) { | ||
throw new Error(this.$tc('components.form.base.eColorField.parseError')) | ||
} else { | ||
throw e | ||
} | ||
} | ||
}, | ||
/** | ||
* @param {string|null} value | ||
*/ | ||
serialize(value) { | ||
try { | ||
return serialize(value, { format: 'hex', collapse: false }).toUpperCase() | ||
} catch (e) { | ||
return null | ||
} | ||
}, | ||
/** | ||
* @param value {null|string} | ||
* @return {null|Color} | ||
*/ | ||
deserialize(value) { | ||
try { | ||
return !value ? null : reactive(parse(value, { space: 'srgb', format: 'hex' })) | ||
} catch (e) { | ||
return null | ||
} | ||
}, | ||
focus() { | ||
this.$refs.input.focus() | ||
}, | ||
}, | ||
} | ||
</script> |
Oops, something went wrong.