Skip to content

Commit e19f60d

Browse files
committed
feat(presets): add the option to show presets only
1 parent f09896e commit e19f60d

File tree

6 files changed

+215
-4
lines changed

6 files changed

+215
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Try it in the [Svelte REPL](https://svelte.dev/repl/cae0ce6e92634878b6e1a587146d
7272
| isMultipane | If true, two calendar months will be shown side-by-side instead of one. | `boolean` (default: `false`)
7373
| isOpen | If true, the picker will be shown without user interaction. | `boolean` (default: `false`)
7474
| showPresets | If true, the picker will show the preset ranges for selection. | `boolean` (default: `false`)
75+
| showPresetsOnly | If true, the picker will show only preset ranges. | `boolean` (default: `false`)
7576
| showYearControls | If true, the picker will hide the year navigation controls. | `boolean` (default: `false`)
7677
| showTimePicker | If true, the picker will show the time picker. | `boolean` (default: `false`)
7778
| enableFutureDates | If true, the picker will allow the user to select future dates. | `boolean` (default: `false`)

docs/src/App.svelte

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script>
22
import { DatePicker } from '@svelte-plugins/datepicker';
33
4-
import { RangePicker, SinglePicker, ThemePicker } from './examples';
4+
import { PresetsOnlyPicker, RangePicker, SinglePicker, ThemePicker } from './examples';
55
66
const today = new Date();
77
@@ -103,6 +103,12 @@
103103
<td>If <code>true</code>, the picker will show the preset ranges for selection.</td>
104104
<td><code>false</code></td>
105105
</tr>
106+
<tr>
107+
<td>showPresetsOnly</td>
108+
<td><code>boolean</code></td>
109+
<td>If <code>true</code>, the preset ranges will only show. Requires range and showPreset to be set.</td>
110+
<td><code>false</code></td>
111+
</tr>
106112
<tr>
107113
<td>showYearControls</td>
108114
<td><code>boolean</code></td>
@@ -351,6 +357,12 @@
351357
<RangePicker showPresets days={6} />
352358
</div>
353359

360+
<h3>Presets Only</h3>
361+
362+
<div class="example">
363+
<PresetsOnlyPicker />
364+
</div>
365+
354366
<h3>Time Picker</h3>
355367

356368
<div class="example">

docs/src/examples/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { SinglePicker } from './single';
2+
export { PresetsOnlyPicker } from './presets';
23
export { RangePicker } from './range';
34
export { ThemePicker } from './theme';

docs/src/examples/presets/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as PresetsOnlyPicker } from './presets.svelte';
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<script>
2+
import { DatePicker } from '@svelte-plugins/datepicker';
3+
import { format } from 'date-fns';
4+
import Prism from 'svelte-prismjs';
5+
6+
export let days = 29;
7+
8+
const today = new Date();
9+
10+
const MILLISECONDS_IN_DAY = 24 * 60 * 60 * 1000;
11+
12+
const getDateFromToday = (days) => {
13+
return Date.now() - days * MILLISECONDS_IN_DAY;
14+
};
15+
16+
let startDate = getDateFromToday(days);
17+
let endDate = today;
18+
let dateFormat = 'MMM d, yyyy';
19+
let isOpen = false;
20+
21+
let formattedStartDate = '';
22+
23+
const onClearDates = () => {
24+
startDate = '';
25+
endDate = '';
26+
};
27+
28+
const onNavigationChange = (e) => {
29+
console.log(e, 'onNavigationChange');
30+
};
31+
32+
const onDateChange = (args) => {
33+
console.log(args, 'onDateChange');
34+
};
35+
36+
const toggleDatePicker = () => (isOpen = !isOpen);
37+
const formatDate = (dateString) => dateString && format(new Date(dateString), dateFormat) || '';
38+
39+
$: formattedStartDate = formatDate(startDate);
40+
$: formattedEndDate = formatDate(endDate);
41+
</script>
42+
43+
<div class="date-filter">
44+
<DatePicker
45+
bind:isOpen
46+
bind:startDate
47+
bind:endDate
48+
{onNavigationChange}
49+
{onDateChange}
50+
isRange
51+
showPresets
52+
showPresetsOnly
53+
{...$$restProps}
54+
>
55+
<div class="date-field" on:click={toggleDatePicker} role="button" tabindex="0" class:open={isOpen}>
56+
<i class="icon-calendar" />
57+
<div class="date">
58+
{#if startDate}
59+
{formattedStartDate} - {formattedEndDate}
60+
{:else}
61+
Pick a date
62+
{/if}
63+
</div>
64+
{#if startDate}
65+
<span role="button" tabindex="0" on:click={onClearDates}>
66+
<i class="os-icon-x" />
67+
</span>
68+
{/if}
69+
</div>
70+
</DatePicker>
71+
</div>
72+
73+
<Prism showLineNumbers={true} code={`
74+
<script>
75+
import { DatePicker } from '@svelte-plugins/datepicker';
76+
import { format } from 'date-fns';
77+
78+
const today = new Date();
79+
80+
const MILLISECONDS_IN_DAY = 24 * 60 * 60 * 1000;
81+
82+
const getDateFromToday = (days) => {
83+
return Date.now() - days * MILLISECONDS_IN_DAY;
84+
};
85+
86+
let startDate = getDateFromToday(29);
87+
let endDate = today;
88+
let dateFormat = 'MMM d, yyyy';
89+
let isOpen = false;
90+
91+
let formattedStartDate = '';
92+
93+
const onClearDates = () => {
94+
startDate = '';
95+
endDate = '';
96+
};
97+
98+
const onDateChange = (args) => {
99+
console.log(args, 'onDateChange');
100+
};
101+
102+
const toggleDatePicker = () => (isOpen = !isOpen);
103+
const formatDate = (dateString) => dateString && format(new Date(dateString), dateFormat) || '';
104+
105+
$: formattedStartDate = formatDate(startDate);
106+
$: formattedEndDate = formatDate(endDate);
107+
</script>
108+
109+
<div class="date-filter">
110+
<DatePicker bind:isOpen bind:startDate bind:endDate isRange showPresets showPresetsOnly {onDateChange}>
111+
<div class="date-field" on:click={toggleDatePicker} class:open={isOpen}>
112+
<i class="icon-calendar" />
113+
<div class="date">
114+
{#if startDate}
115+
{formattedStartDate} - {formattedEndDate}
116+
{:else}
117+
Pick a date
118+
{/if}
119+
</div>
120+
{#if startDate}
121+
<span on:click={onClearDates}>
122+
<i class="os-icon-x" />
123+
</span>
124+
{/if}
125+
</div>
126+
</DatePicker>
127+
</div>
128+
129+
<style>
130+
.date-field {
131+
align-items: center;
132+
background-color: #fff;
133+
border-bottom: 1px solid #e8e9ea;
134+
display: inline-flex;
135+
gap: 8px;
136+
min-width: 100px;
137+
padding: 16px;
138+
}
139+
140+
.date-field.open {
141+
border-bottom: 1px solid #0087ff;
142+
}
143+
144+
.date-field .icon-calendar {
145+
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAACXBIWXMAABYlAAAWJQFJUiTwAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAEmSURBVHgB7ZcPzcIwEMUfXz4BSCgKwAGgACRMAg6YBBxsOMABOAAHFAXgAK5Z2Y6lHbfQ8SfpL3lZaY/1rb01N+BHUKSMNBfEJjZWISA56Uo6C2KvVpkgFn9oRx9vICFtUT1JKO3tvRtZdjBxXQs+YY+1FenIfuesPUGVVLzfRWKvmrSzbbN19wS+kAb2+sCEuUxrYzkbe4YvCVM2Vr5NPAkVa+van7Wn38U95uTpN5TJ/A8ZKemAakmbmJJGpI0gVmwA0huieFItjG19DgTHtwIZhCfZq3ztCuzQYh+FKBSvusjAGs8PnLYkLgMf34JoIBqIBqKBaIAb0Kw9RlhMCTbzzPWAqYq7LsuPaGDUsYmznaOk5zChUJTNQ4TFVMkrOL4HPsoNn26PxROHCggAAAAASUVORK5CYII=) no-repeat center center;
146+
background-size: 14px 14px;
147+
height: 14px;
148+
width: 14px;
149+
}
150+
</style>
151+
`} />
152+
153+
<style>
154+
.date-field {
155+
align-items: center;
156+
background-color: #fff;
157+
border-bottom: 1px solid #e8e9ea;
158+
display: inline-flex;
159+
gap: 8px;
160+
min-width: 100px;
161+
padding: 16px;
162+
}
163+
164+
.date-field.open {
165+
border-bottom: 1px solid #0087ff;
166+
}
167+
168+
.date-field .icon-calendar {
169+
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAACXBIWXMAABYlAAAWJQFJUiTwAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAEmSURBVHgB7ZcPzcIwEMUfXz4BSCgKwAGgACRMAg6YBBxsOMABOAAHFAXgAK5Z2Y6lHbfQ8SfpL3lZaY/1rb01N+BHUKSMNBfEJjZWISA56Uo6C2KvVpkgFn9oRx9vICFtUT1JKO3tvRtZdjBxXQs+YY+1FenIfuesPUGVVLzfRWKvmrSzbbN19wS+kAb2+sCEuUxrYzkbe4YvCVM2Vr5NPAkVa+van7Wn38U95uTpN5TJ/A8ZKemAakmbmJJGpI0gVmwA0huieFItjG19DgTHtwIZhCfZq3ztCuzQYh+FKBSvusjAGs8PnLYkLgMf34JoIBqIBqKBaIAb0Kw9RlhMCTbzzPWAqYq7LsuPaGDUsYmznaOk5zChUJTNQ4TFVMkrOL4HPsoNn26PxROHCggAAAAASUVORK5CYII=) no-repeat center center;
170+
background-size: 14px 14px;
171+
height: 14px;
172+
width: 14px;
173+
}
174+
</style>

src/datepicker.svelte

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@
130130
*/
131131
export let showPresets = false;
132132
133+
/**
134+
* Indicates whether preset date ranges should only be displayed
135+
* @type {boolean}
136+
* @default false
137+
*/
138+
export let showPresetsOnly = false;
139+
133140
/**
134141
* Indicates whether the time picker is shown in the date picker.
135142
* @type {boolean}
@@ -929,7 +936,7 @@
929936
class:show={isOpen}
930937
>
931938
{#if isRange && showPresets}
932-
<div class="calendar-presets">
939+
<div class="calendar-presets" class:presets-only={showPresetsOnly}>
933940
{#each presetRanges as option}
934941
<button
935942
type="button"
@@ -942,7 +949,7 @@
942949
{/each}
943950
</div>
944951
{/if}
945-
<div class="calendar">
952+
<div class="calendar" class:presets-only={isRange && showPresetsOnly}>
946953
<header class:timepicker={showTimePicker}>
947954
<button type="button" on:click|preventDefault={toPrev}>
948955
<div class="icon-previous-month" aria-label="Previous month"></div>
@@ -1021,7 +1028,7 @@
10211028
</div>
10221029
10231030
{#if isRange && isMultipane}
1024-
<div class="calendar">
1031+
<div class="calendar" class:presets-only={showPresetsOnly}>
10251032
<header class:timepicker={showTimePicker}>
10261033
<button type="button" on:click|preventDefault={toPrev} class:hide={!(!isRange || (isRange && !isMultipane))}>
10271034
<div class="icon-previous-month" aria-label="Previous month"></div>
@@ -1499,6 +1506,11 @@
14991506
padding: var(--datepicker-presets-padding);
15001507
}
15011508
1509+
.datepicker .calendars-container.presets .calendar-presets.presets-only {
1510+
border-right: 0;
1511+
min-width: 100%;
1512+
}
1513+
15021514
.datepicker .calendars-container .calendar-presets button {
15031515
appearance: none;
15041516
background-color: var(--datepicker-presets-button-background);
@@ -1539,6 +1551,10 @@
15391551
width: var(--datepicker-calendar-width);
15401552
}
15411553
1554+
.datepicker .calendars-container .calendar.presets-only {
1555+
display: none;
1556+
}
1557+
15421558
.datepicker .calendars-container .calendar + .calendar {
15431559
border-left: var(--datepicker-calendar-split-border);
15441560
}
@@ -1903,4 +1919,10 @@
19031919
opacity: 1;
19041920
}
19051921
}
1922+
1923+
@media only screen and (max-width: 600px) {
1924+
.datepicker .calendars-container.presets .calendar-presets:not(.presets-only) {
1925+
display: none;
1926+
}
1927+
}
19061928
</style>

0 commit comments

Comments
 (0)