Skip to content

Commit

Permalink
Merge pull request #55 from storyblok/hotfix/datepicker
Browse files Browse the repository at this point in the history
Fix component: datepicker
  • Loading branch information
gustavomelki authored Dec 17, 2020
2 parents 831bfa9 + 511df15 commit 57e68aa
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/components/Datepicker/Datepicker.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ const Template = (args) => ({
props: Object.keys(args),

data: () => ({
internalDatetimeValue: '',
internalTimeValue: '',
internalDatetimeValue: '2017-09-09 00:00',
}),

template: `
Expand All @@ -19,6 +18,7 @@ const Template = (args) => ({
:tz-tooltip="tzTooltip"
:type="type"
v-model="internalDatetimeValue"
style="width: 29.4rem"
/>
`,
})
Expand Down
19 changes: 18 additions & 1 deletion src/components/Datepicker/Datepicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ export default {
value: {
type: String,
default: '',
required: true,
},
},
Expand Down Expand Up @@ -210,6 +209,13 @@ export default {
},
},
watch: {
value: {
handler: 'syncInternalValue',
immediate: true,
},
},
mounted() {
this.$nextTick(() => {
this.inputElement = this.$refs.input && this.$refs.input.$el
Expand All @@ -219,6 +225,7 @@ export default {
methods: {
handleCancelAction() {
this.closeOverlay()
this.syncInternalValue(this.value)
},
handleDoneAction() {
Expand Down Expand Up @@ -251,6 +258,7 @@ export default {
if (this.type === 'date') {
this.closeOverlay()
this.handleDoneAction()
return
}
Expand All @@ -268,6 +276,10 @@ export default {
},
handleInputClick() {
if (this.disabled) {
return
}
this.isOverlayVisible = true
this.internalVisualization =
this.type === 'time' ? INTERNAL_VIEWS.TIME : INTERNAL_VIEWS.CALENDAR
Expand All @@ -283,6 +295,11 @@ export default {
this.isOverlayVisible = false
},
syncInternalValue(value) {
this.internalValue = value
this.internalDate = value || dayjs().format()
},
$_wrapClose(e) {
if (!this.$el.contains(e.target)) {
this.closeOverlay()
Expand Down
1 change: 1 addition & 0 deletions src/components/Datepicker/__tests__/Datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ describe('SbDatepicker component', () => {
const wrapper = factory({
...SbDatepickerData.args,
...WithTzOffset.args,
value: '2017-09-09',
})

it('Should exist the SbTooltip component with correct label', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/Datepicker/__tests__/DatepickerYears.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ describe('SbDatepickerYears component', () => {

describe('Test component method to handle the MONTHS', () => {
it('Should emit date for input on click', async () => {
const value = dayjs().format()
await wrapper.setProps({ value })
const internalDate = dayjs().format()
await wrapper.setProps({ internalDate })
await wrapper.find('.sb-datepicker-years__item').trigger('click')
expect(wrapper.emitted().input[0].length).toBe(1)
})
Expand Down
4 changes: 2 additions & 2 deletions src/components/Datepicker/components/DatepickerMonths.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default {
monthsList() {
return this.months.map((month) => {
return {
checked: dayjs(this.value).format('MMM') === month,
checked: dayjs(this.internalDate).format('MMM') === month,
label: month,
}
})
Expand All @@ -65,7 +65,7 @@ export default {
handleMonthClick($event, month) {
$event.stopPropagation()
const monthIndex = this.months.indexOf(month)
const value = dayjs(this.value).month(monthIndex).format()
const value = dayjs(this.internalDate).month(monthIndex).format()
this.$emit('input', value)
},
},
Expand Down
9 changes: 6 additions & 3 deletions src/components/Datepicker/components/DatepickerTime.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default {
name: 'SbDatepickerTime',
props: {
value: {
internalDate: {
type: String,
default: null,
},
Expand Down Expand Up @@ -76,7 +76,7 @@ export default {
},
watch: {
value: {
internalDate: {
handler: '$_syncValue',
immediate: true,
},
Expand All @@ -96,7 +96,10 @@ export default {
$_processInput() {
const hours = this.internalHour || 0
const minutes = this.internalMinutes || 0
const value = dayjs(this.value).hour(hours).minute(minutes).format()
const value = dayjs(this.internalDate)
.hour(hours)
.minute(minutes)
.format()
this.$emit('input', value)
},
Expand Down
6 changes: 3 additions & 3 deletions src/components/Datepicker/components/DatepickerYears.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default {
type: Number,
default: 30,
},
value: {
internalDate: {
type: String,
default: null,
},
Expand All @@ -48,7 +48,7 @@ export default {
},
currentYear() {
return dayjs(this.value).year()
return dayjs(this.internalDate).year()
},
},
Expand All @@ -60,7 +60,7 @@ export default {
*/
handleYearClick($event, year) {
$event.stopPropagation()
const value = dayjs(this.value).year(year).format()
const value = dayjs(this.internalDate).year(year).format()
this.$emit('input', value)
},
Expand Down
6 changes: 5 additions & 1 deletion src/components/Datepicker/datepicker.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

.sb-datepicker {
position: relative;
width: 29.4rem;

&__input {
position: relative;
Expand All @@ -39,13 +38,16 @@

&__overlay {
@include popoverComponent();
max-width: 29.4rem;
width: 100%;
box-sizing: border-box;
z-index: 1;
}

&__actions {
display: flex;
justify-content: space-between;
background-color: $white;
}

&__action-button {
Expand Down Expand Up @@ -130,6 +132,7 @@
display: grid;
grid-template-columns: repeat(7, 1fr);
grid-template-rows: repeat(6, 1fr);
background-color: $white;

&__item {
@include flexCentering();
Expand All @@ -152,6 +155,7 @@

&__numbers-container {
display: flex;
background-color: $white;
}

&__number-column {
Expand Down

0 comments on commit 57e68aa

Please sign in to comment.