Commit 2953bc02 authored by Marvin Karegyeya's avatar Marvin Karegyeya Committed by Kushal Pandya

Migrate Bootstrap dropdown to GitLab UI GlDropdown in pikaday.vue

parent 88f935cb
<script> <script>
import Pikaday from 'pikaday'; import { GlDatepicker } from '@gitlab/ui';
import { GlIcon } from '@gitlab/ui'; import { pikadayToString } from '~/lib/utils/datetime_utility';
import { parsePikadayDate, pikadayToString } from '~/lib/utils/datetime_utility';
import { __ } from '~/locale';
export default { export default {
name: 'DatePicker', name: 'DatePicker',
components: { components: {
GlIcon, GlDatepicker,
}, },
props: { props: {
label: {
type: String,
required: false,
default: __('Date picker'),
},
selectedDate: { selectedDate: {
type: Date, type: Date,
required: false, required: false,
...@@ -31,32 +24,9 @@ export default { ...@@ -31,32 +24,9 @@ export default {
default: null, default: null,
}, },
}, },
mounted() {
this.calendar = new Pikaday({
field: this.$el.querySelector('.dropdown-menu-toggle'),
theme: 'gitlab-theme animate-picker',
format: 'yyyy-mm-dd',
container: this.$el,
defaultDate: this.selectedDate,
setDefaultDate: Boolean(this.selectedDate),
minDate: this.minDate,
maxDate: this.maxDate,
parse: dateString => parsePikadayDate(dateString),
toString: date => pikadayToString(date),
onSelect: this.selected.bind(this),
onClose: this.toggled.bind(this),
firstDay: gon.first_day_of_week,
});
this.$el.append(this.calendar.el);
this.calendar.show();
},
beforeDestroy() {
this.calendar.destroy();
},
methods: { methods: {
selected(dateText) { selected(date) {
this.$emit('newDateSelected', this.calendar.toString(dateText)); this.$emit('newDateSelected', pikadayToString(date));
}, },
toggled() { toggled() {
this.$emit('hidePicker'); this.$emit('hidePicker');
...@@ -66,12 +36,13 @@ export default { ...@@ -66,12 +36,13 @@ export default {
</script> </script>
<template> <template>
<div class="pikaday-container"> <gl-datepicker
<div class="dropdown open"> :value="selectedDate"
<button type="button" class="dropdown-menu-toggle" data-toggle="dropdown" @click="toggled"> :min-date="minDate"
<span class="dropdown-toggle-text"> {{ label }} </span> :max-date="maxDate"
<gl-icon name="chevron-down" class="gl-absolute gl-right-3 gl-top-3 gl-text-gray-500" /> start-opened
</button> @close="toggled"
</div> @click="toggled"
</div> @input="selected"
/>
</template> </template>
---
title: Migrate-Bootstrap-dropdown-to-GitLab-UI-GlDropdown-in-app/assets/javascripts/vue_shared/components/pikaday.vue
merge_request: 41458
author: nuwe1
type: other
...@@ -149,8 +149,8 @@ RSpec.describe 'Update Epic', :js do ...@@ -149,8 +149,8 @@ RSpec.describe 'Update Epic', :js do
it 'opens datepicker when clicking Edit button' do it 'opens datepicker when clicking Edit button' do
page.within('.issuable-sidebar .block.start-date') do page.within('.issuable-sidebar .block.start-date') do
click_button('Edit') click_button('Edit')
expect(find('.value-type-fixed')).to have_selector('.pikaday-container') expect(find('.value-type-fixed')).to have_selector('.gl-datepicker')
expect(find('.value-type-fixed')).to have_selector('.pikaday-container .pika-single.is-bound') expect(find('.value-type-fixed')).to have_selector('.gl-datepicker .pika-single.is-bound')
end end
end end
end end
......
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import { GlDatepicker } from '@gitlab/ui';
import datePicker from '~/vue_shared/components/pikaday.vue'; import datePicker from '~/vue_shared/components/pikaday.vue';
describe('datePicker', () => { describe('datePicker', () => {
let wrapper; let wrapper;
beforeEach(() => {
const buildWrapper = (propsData = {}) => {
wrapper = shallowMount(datePicker, { wrapper = shallowMount(datePicker, {
propsData: { propsData,
label: 'label',
},
attachToDocument: true,
}); });
}); };
afterEach(() => { afterEach(() => {
wrapper.destroy(); wrapper.destroy();
wrapper = null; wrapper = null;
}); });
it('should emit newDateSelected when GlDatePicker emits the input event', () => {
const minDate = new Date();
const maxDate = new Date();
const selectedDate = new Date();
const theDate = selectedDate.toISOString().slice(0, 10);
it('should render label text', () => { buildWrapper({ minDate, maxDate, selectedDate });
expect(
wrapper
.find('.dropdown-toggle-text')
.text()
.trim(),
).toEqual('label');
});
it('should show calendar', () => { expect(wrapper.find(GlDatepicker).props()).toMatchObject({
expect(wrapper.find('.pika-single').element).toBeDefined(); minDate,
maxDate,
value: selectedDate,
});
wrapper.find(GlDatepicker).vm.$emit('input', selectedDate);
expect(wrapper.emitted('newDateSelected')[0][0]).toBe(theDate);
}); });
it('should emit the hidePicker event when GlDatePicker emits the close event', () => {
buildWrapper();
it('should emit hidePicker event when dropdown is clicked', () => { wrapper.find(GlDatepicker).vm.$emit('close');
// Removing the bootstrap data-toggle property,
// because it interfers with our click event
delete wrapper.find('.dropdown-menu-toggle').element.dataset.toggle;
wrapper.find('.dropdown-menu-toggle').trigger('click');
expect(wrapper.emitted('hidePicker')).toEqual([[]]); expect(wrapper.emitted('hidePicker')).toHaveLength(1);
}); });
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment