Skip to content

Commit

Permalink
feat: emit opened/closed event
Browse files Browse the repository at this point in the history
Closes #98

Co-Authored-by: Liumingxun <[email protected]>
  • Loading branch information
icehaunter and Liumingxun committed Oct 10, 2022
1 parent b0507d9 commit 99f3141
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ Full props documentation is available at https://icehaunter.github.io/vue3-datep
| `clearable` | `Boolean` | `false` | Allows clearing the selected date and setting the value to `null` |
| `allowOutsideInterval` | `Boolean` | `false` | Allows user to click dates outside of current interval |

### Events

- `opened`: Emitted every time the popup opens, including on field focus
- `closed`: Emitted every time the popup closes, including on field blur

## Compatibility

Package is transpiled and should be usable for everyone with ES6 and above, but the styling of the datepicker itself uses CSS Grid and CSS variables.
Expand Down
5 changes: 5 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ More in-depth documentation of the props, as well as examples, can be found in [
| `weekStartsOn` | `Number` | 1 | Day on which the week should start. Number from 0 to 6, where 0 is Sunday and 6 is Saturday. Week starts with a Monday (1) by default |
| `clearable` | `Boolean` | `false` | Allows clearing the selected date and setting the value to `null` |

### Events

- `opened`: Emitted every time the popup opens, including on field focus
- `closed`: Emitted every time the popup closes, including on field blur

## Styling

Styling is done via CSS variables, which control colors used in the popup. All variables, as well as styling example and playground can be found in [Configuration section](/config.html#styling-example-and-playground)
25 changes: 24 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<img alt="Vue logo" src="./assets/logo.png" />
<div>
<datepicker
@opened="log('opened')"
@closed="log('closed')"
class="picker"
v-model="selected"
:locale="locale"
Expand All @@ -17,6 +19,8 @@
</div>
<div>
<datepicker
@opened="log('opened')"
@closed="log('closed')"
class="picker"
v-model="time"
:locale="locale"
Expand All @@ -29,6 +33,8 @@
</div>
<div>
<datepicker
@opened="log('opened')"
@closed="log('closed')"
class="picker"
v-model="full"
:locale="locale"
Expand All @@ -40,6 +46,8 @@
</div>
<div>
<datepicker
@opened="log('opened')"
@closed="log('closed')"
class="picker"
v-model="selected"
:locale="locale"
Expand All @@ -62,10 +70,18 @@
/>
</div>
<div>
<datepicker class="picker" v-model="to" placeholder="to" />
<datepicker
@opened="log('opened')"
@closed="log('closed')"
class="picker"
v-model="to"
placeholder="to"
/>
</div>
<div>
<datepicker
@opened="log('opened')"
@closed="log('closed')"
class="picker"
v-model="to"
:locale="locale"
Expand All @@ -75,6 +91,8 @@
</div>
<div>
<datepicker
@opened="log('opened')"
@closed="log('closed')"
class="picker"
v-model="yearSelected"
:locale="locale"
Expand All @@ -84,6 +102,8 @@
</div>
<div>
<datepicker
@opened="log('opened')"
@closed="log('closed')"
class="picker"
v-model="monthSelected"
:locale="locale"
Expand Down Expand Up @@ -134,6 +154,9 @@ export default defineComponent({
const newDate = set(new Date(date.getTime()), { hours: 11, minutes: 0 })
return date < newDate
},
log(data) {
console.log(data)
},
},
})
</script>
Expand Down
37 changes: 29 additions & 8 deletions src/datepicker/Datepicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
:disabled="disabled"
:tabindex="disabled ? -1 : 0"
@keyup="keyUp"
@blur="renderView()"
@focus="renderView(initialView)"
@click="renderView(initialView)"
@blur="blur"
@focus="focus"
@click="click"
/>
<div class="v3dp__clearable" v-show="clearable && modelValue">
<slot name="clear" :onClear="clearModelValue">
Expand Down Expand Up @@ -247,11 +247,14 @@ export default defineComponent({
emits: {
'update:modelValue': (value: Date | null | undefined) =>
value === null || value === undefined || isValid(value),
opened: null,
closed: null,
},
setup(props, { emit, attrs }) {
const viewShown = ref('none' as 'year' | 'month' | 'day' | 'time' | 'none')
const pageDate = ref<Date>(new Date())
const inputRef = ref(null as HTMLInputElement | null)
const isFocused = ref(false)
const input = ref('')
watchEffect(() => {
Expand All @@ -277,16 +280,23 @@ export default defineComponent({
pageDate.value =
props.modelValue || boundedDate(props.lowerLimit, props.upperLimit)
viewShown.value = view
if (view !== 'none') {
emit('opened')
} else {
emit('closed')
}
}
}
watchEffect(() => {
if (props.disabled) viewShown.value = 'none'
})
const selectYear = (date: Date) => {
pageDate.value = date
if (props.minimumView === 'year') {
viewShown.value = 'none'
renderView('none')
emit('update:modelValue', date)
} else {
viewShown.value = 'month'
Expand All @@ -296,7 +306,7 @@ export default defineComponent({
pageDate.value = date
if (props.minimumView === 'month') {
viewShown.value = 'none'
renderView('none')
emit('update:modelValue', date)
} else {
viewShown.value = 'day'
Expand All @@ -306,17 +316,16 @@ export default defineComponent({
pageDate.value = date
if (props.minimumView === 'day') {
viewShown.value = 'none'
renderView('none')
emit('update:modelValue', date)
} else {
viewShown.value = 'time'
}
}
const selectTime = (date: Date) => {
renderView('none')
emit('update:modelValue', date)
viewShown.value = 'none'
}
const clearModelValue = () => {
Expand All @@ -325,6 +334,15 @@ export default defineComponent({
}
}
const click = () => (isFocused.value = true)
const focus = () => renderView(initialView.value)
const blur = () => {
isFocused.value = false
renderView()
}
const keyUp = (event: KeyboardEvent) => {
const code = event.keyCode ? event.keyCode : event.which
// close calendar if escape or enter are pressed
Expand Down Expand Up @@ -373,6 +391,9 @@ export default defineComponent({
: (viewShown.value = 'day')
return {
blur,
focus,
click,
input,
inputRef,
pageDate,
Expand Down

0 comments on commit 99f3141

Please sign in to comment.