Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Customizing Element Plus Date Picker Behavior in Vue 3

Tech 1

Display Format vs. Internal Value

Use format to control how the date appears to users, and value-format to define the string format passed to the model:

<el-date-picker
  v-model="company.establishmentDate"
  type="date"
  placeholder="Select a date"
  format="YYYY/MM/DD"
  value-format="YYYY-MM-DD"
  suffix-icon="Calendar"
/>

Right-Aligned Calendar Icon with Conditional Clear Button

To avoid icon overlap and dynamical switch between calendar and clear icons:

<el-date-picker
  v-model="company.establishmentDate"
  type="date"
  placeholder="Select a date"
  format="YYYY-MM-DD"
  value-format="YYYY-MM-DD"
  :prefix-icon="company.establishmentDate ? 'Calendar' : ''"
  style="width: 500px"
/>

Apply scoped styles to reposition the prefix icon to the right and adjust input padding:

:deep(.el-date-editor) {
  .el-input__prefix {
    position: absolute;
    right: 0;
    top: 0;
  }
  input {
    padding-left: 15px;
  }
}

:deep(.el-date-editor input) {
  padding-left: 0;
}

:deep(.el-icon-calendar) {
  position: absolute;
  right: 25px;
}

Handling Null Values on Clear Action

Clicking the × button sets the bound model to null. Ensure your logic accounts to this, especially when validating or serializing form data.

Disabling Past Dates

Restrict selection to today and future dates using disabled-date:

<el-date-picker
  v-model="selectedDate"
  type="date"
  placeholder="Choose a date"
  :disabled-date="disablePastDays"
/>
const disablePastDays = (date: Date) => {
  const cutoff = new Date();
  cutoff.setHours(0, 0, 0, 0);
  return date.getTime() < cutoff.getTime();
};

DateTime Picker with Default Time and Past-Date Restriction

For datetime inputs that default to current time and disable past dates:

<el-date-picker
  v-model="task.expiryTime"
  type="datetime"
  format="YYYY-MM-DD HH:mm"
  value-format="YYYY-MM-DD HH:mm:ss"
  :default-time="initialTime"
  :disabled-date="disablePastDays"
  placeholder="Select reminder time"
/>
const task = reactive({
  expiryTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
  // ... other fields
});

const initialTime = ref(dayjs().format('YYYY-MM-DD HH:mm:ss'));

Restricting Selection to Days Before Yesterday

Allow only dates strictly earlier than the day before today:

<el-date-picker
  v-model="chosenDate"
  type="date"
  placeholder="Pick a date"
  :disabled-date="disableFromYesterday"
/>
const disableFromYesterday = (date: Date) => {
  const now = new Date();
  const cutoff = new Date(now);
  cutoff.setDate(now.getDate() - 2);
  cutoff.setHours(0, 0, 0, 0);
  return date.getTime() >= cutoff.getTime();
};

Controlling Popover Placement

Prevent dropdown clipping by forcing bottom-aligned positioning and disabling teleportation:

<el-date-picker
  v-model="dateRange"
  type="daterange"
  range-separator="–"
  start-placeholder="Start date"
  end-placeholder="End date"
  format="YYYY/MM/DD"
  value-format="YYYY-MM-DD"
  placement="bottom-end"
  :teleported="false"
/>

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.