Skip to content

Commit

Permalink
feat(components): [date-picker] type add months params
Browse files Browse the repository at this point in the history
- closes: #17317

BREAKING CHANGE :
- ζ–°ε’žεŠŸθƒ½: `date-picker` ζ”―ζŒ 月份 ε€šι€‰ ( element-ui ζ›Ύζ”―ζŒζ­€εŠŸθƒ½ )

closed #17317
  • Loading branch information
Panzer-Jack committed Jun 28, 2024
1 parent 9e76e6f commit 9106477
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/en-US/component/date-picker.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Note, date time locale (month name, first day of the week ...) are also configur
| placeholder | placeholder in non-range mode | ^[string] | '' |
| start-placeholder | placeholder for the start date in range mode | ^[string] | β€” |
| end-placeholder | placeholder for the end date in range mode | ^[string] | β€” |
| type | type of the picker | ^[enum]`'year' \| 'years' \|'month' \| 'date' \| 'dates' \| 'datetime' \| 'week' \| 'datetimerange' \| 'daterange' \| 'monthrange'` | date |
| type | type of the picker | ^[enum]`'year' \| 'years' \| 'month' \| 'months' \| 'date' \| 'dates' \| 'datetime' \| 'week' \| 'datetimerange' \| 'daterange' \| 'monthrange'` | date |
| format | format of the displayed value in the input box | ^[string] see [date formats](/en-US/component/date-picker#date-formats) | YYYY-MM-DD |
| popper-class | custom class name for DatePicker's dropdown | ^[string] | β€” |
| popper-options | Customized popper option see more at [popper.js](https://popper.js.org/docs/v2/) | ^[object]`Partial<PopperOptions>` | {} |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,20 @@ const handleMonthTableClick = (event: MouseEvent | KeyboardEvent) => {
const row = (target.parentNode as HTMLTableRowElement).rowIndex
const month = row * 4 + column
const newDate = props.date.startOf('year').month(month)
if (props.selectionMode === 'range') {
if (props.selectionMode === 'months') {
if (event.type === 'keydown') {
emit('pick', castArray(props.parsedValue), false)
return
}
const newMonth = props.date.startOf('month').month(month)
const newValue = hasClass(target, 'current')
? castArray(props.parsedValue).filter(
(d) => Number(d) !== Number(newMonth)
)
: castArray(props.parsedValue).concat([dayjs(newMonth)])
emit('pick', newValue)
} else if (props.selectionMode === 'range') {
if (!props.rangeState.selecting) {
emit('pick', { minDate: newDate, maxDate: null })
emit('select', true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
<month-table
v-if="currentView === 'month'"
ref="currentViewRef"
:selection-mode="selectionMode"
:date="innerDate"
:parsed-value="parsedValue"
:disabled-date="disabledDate"
Expand All @@ -161,7 +162,11 @@
</div>
<div v-show="footerVisible" :class="ppNs.e('footer')">
<el-button
v-show="selectionMode !== 'dates' && selectionMode !== 'years'"
v-show="
selectionMode !== 'dates' &&
selectionMode !== 'months' &&
selectionMode !== 'years'
"
text
size="small"
:class="ppNs.e('link-btn')"
Expand Down Expand Up @@ -225,6 +230,7 @@ import type { PanelDatePickProps } from '../props/panel-date-pick'
import type {
DateTableEmits,
DatesPickerEmits,
MonthsPickerEmits,
WeekPickerEmits,
YearsPickerEmits,
} from '../props/basic-date-table'
Expand Down Expand Up @@ -393,7 +399,8 @@ const handleShortcutClick = (shortcut: Shortcut) => {
const selectionMode = computed<DatePickType>(() => {
const { type } = props
if (['week', 'month', 'year', 'years', 'dates'].includes(type)) return type
if (['week', 'month', 'months', 'year', 'years', 'dates'].includes(type))
return type
return 'date' as DatePickType
})
Expand All @@ -405,11 +412,17 @@ const keyboardMode = computed<string>(() => {
const hasShortcuts = computed(() => !!shortcuts.length)
const handleMonthPick = async (month: number) => {
innerDate.value = innerDate.value.startOf('month').month(month)
const handleMonthPick = async (
month: number | MonthsPickerEmits,
keepOpen?: boolean
) => {
if (selectionMode.value === 'month') {
innerDate.value = innerDate.value.startOf('month').month(month as number)
emit(innerDate.value, false)
} else if (selectionMode.value === 'months') {
emit(month as MonthsPickerEmits, keepOpen ?? true)
} else {
innerDate.value = innerDate.value.startOf('month').month(month as number)
currentView.value = 'date'
if (['month', 'year', 'date', 'week'].includes(selectionMode.value)) {
emit(innerDate.value, true)
Expand Down Expand Up @@ -454,9 +467,15 @@ const showTime = computed(
const footerVisible = computed(() => {
const showDateFooter = showTime.value || selectionMode.value === 'dates'
const showYearFooter = selectionMode.value === 'years'
const showMonthFooter = selectionMode.value === 'months'
const isDateView = currentView.value === 'date'
const isYearView = currentView.value === 'year'
return (showDateFooter && isDateView) || (showYearFooter && isYearView)
const isMonthView = currentView.value === 'month'
return (
(showDateFooter && isDateView) ||
(showYearFooter && isYearView) ||
(showMonthFooter && isMonthView)
)
})
const disabledConfirm = computed(() => {
Expand All @@ -468,7 +487,11 @@ const disabledConfirm = computed(() => {
return disabledDate(props.parsedValue.toDate())
})
const onConfirm = () => {
if (selectionMode.value === 'dates' || selectionMode.value === 'years') {
if (
selectionMode.value === 'dates' ||
selectionMode.value === 'months' ||
selectionMode.value === 'years'
) {
emit(props.parsedValue as Dayjs[])
} else {
// deal with the scenario where: user opens the date time picker, then confirm without doing anything
Expand Down Expand Up @@ -740,6 +763,9 @@ watch(
} else if (val === 'years') {
currentView.value = 'year'
return
} else if (val === 'months') {
currentView.value = 'month'
return
}
currentView.value = 'date'
},
Expand Down Expand Up @@ -767,7 +793,11 @@ watch(
() => props.parsedValue,
(val) => {
if (val) {
if (selectionMode.value === 'dates' || selectionMode.value === 'years')
if (
selectionMode.value === 'dates' ||
selectionMode.value === 'months' ||
selectionMode.value === 'years'
)
return
if (Array.isArray(val)) return
innerDate.value = val
Expand Down
1 change: 1 addition & 0 deletions packages/components/date-picker/src/date-picker.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export declare type IDatePickerType =
| 'year'
| 'years'
| 'month'
| 'months'
| 'date'
| 'dates'
| 'week'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type BasicDateTableEmits = typeof basicDateTableEmits
export type RangePickerEmits = { minDate: Dayjs; maxDate: null }
export type DatePickerEmits = Dayjs
export type DatesPickerEmits = Dayjs[]
export type MonthsPickerEmits = Dayjs[]
export type YearsPickerEmits = Dayjs[]
export type WeekPickerEmits = {
year: number
Expand Down
1 change: 1 addition & 0 deletions packages/components/date-picker/src/props/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const selectionModes = [
'year',
'years',
'month',
'months',
'week',
'range',
]
Expand Down
5 changes: 4 additions & 1 deletion packages/components/time-picker/src/common/picker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
!editable ||
readonly ||
isDatesPicker ||
isMonthsPicker ||
isYearsPicker ||
type === 'week'
"
Expand Down Expand Up @@ -483,7 +484,7 @@ const displayValue = computed<UserInput>(() => {
if (!isTimePicker.value && valueIsEmpty.value) return ''
if (!pickerVisible.value && valueIsEmpty.value) return ''
if (formattedValue) {
return isDatesPicker.value || isYearsPicker.value
return isDatesPicker.value || isMonthsPicker.value || isYearsPicker.value
? (formattedValue as Array<string>).join(', ')
: formattedValue
}
Expand All @@ -496,6 +497,8 @@ const isTimePicker = computed(() => props.type.startsWith('time'))
const isDatesPicker = computed(() => props.type === 'dates')
const isMonthsPicker = computed(() => props.type === 'months')
const isYearsPicker = computed(() => props.type === 'years')
const triggerIcon = computed(
Expand Down
1 change: 1 addition & 0 deletions packages/components/time-picker/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const DEFAULT_FORMATS_DATEPICKER = {
year: 'YYYY',
years: 'YYYY',
month: 'YYYY-MM',
months: 'YYYY-MM',
datetime: `${DEFAULT_FORMATS_DATE} ${DEFAULT_FORMATS_TIME}`,
monthrange: 'YYYY-MM',
daterange: DEFAULT_FORMATS_DATE,
Expand Down
1 change: 1 addition & 0 deletions packages/constants/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const datePickTypes = [
'year',
'years',
'month',
'months',
'date',
'dates',
'week',
Expand Down
1 change: 1 addition & 0 deletions packages/utils/__tests__/vue/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('validator', () => {
expect(isValidDatePickType('year')).toBe(true)
expect(isValidDatePickType('years')).toBe(true)
expect(isValidDatePickType('month')).toBe(true)
expect(isValidDatePickType('months')).toBe(true)
expect(isValidDatePickType('date')).toBe(true)
expect(isValidDatePickType('dates')).toBe(true)
expect(isValidDatePickType('week')).toBe(true)
Expand Down

0 comments on commit 9106477

Please sign in to comment.