sveltacular 0.0.55 → 0.0.56

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,8 @@ export let type = "button";
7
7
  export let block = false;
8
8
  export let flex = false;
9
9
  export let disabled = false;
10
+ export let noMargin = false;
11
+ export let collapse = false;
10
12
  const dispatch = createEventDispatcher();
11
13
  const click = (e) => {
12
14
  if (disabled) {
@@ -21,7 +23,7 @@ const click = (e) => {
21
23
  };
22
24
  </script>
23
25
 
24
- <button {type} on:click={click} class="{size} {style} {flex ? 'flex' : ''}" class:block {disabled}>
26
+ <button {type} on:click={click} class="{size} {style} {flex ? 'flex' : ''}" class:block class:noMargin class:collapse {disabled}>
25
27
  <slot />
26
28
  </button>
27
29
 
@@ -53,6 +55,13 @@ button[disabled] {
53
55
  opacity: 0.5;
54
56
  cursor: not-allowed;
55
57
  }
58
+ button.noMargin {
59
+ margin: 0;
60
+ }
61
+ button.collapse {
62
+ min-width: auto;
63
+ padding: 0.5rem;
64
+ }
56
65
  button.flex {
57
66
  flex-grow: 1;
58
67
  }
@@ -9,6 +9,8 @@ declare const __propDef: {
9
9
  block?: boolean | undefined;
10
10
  flex?: boolean | undefined;
11
11
  disabled?: boolean | undefined;
12
+ noMargin?: boolean | undefined;
13
+ collapse?: boolean | undefined;
12
14
  };
13
15
  events: {
14
16
  click: CustomEvent<void>;
@@ -1,7 +1,9 @@
1
- <script>import { currentDateTime, isDate, isDateOrDateTime, isDateTime } from "../../helpers/date.js";
1
+ <script>import { addUnits, currentDateTime, isDateString, isDateOrDateTimeString, isDateTimeString } from "../../helpers/date.js";
2
2
  import { uniqueId } from "../../helpers/unique-id.js";
3
3
  import FormField from "../form-field.svelte";
4
4
  import FormLabel from "../form-label.svelte";
5
+ import Button from "../button/button.svelte";
6
+ import { createEventDispatcher } from "svelte";
5
7
  const id = uniqueId();
6
8
  export let value = void 0;
7
9
  export let defaultValue = void 0;
@@ -11,17 +13,31 @@ export let nullable = false;
11
13
  export let enabled = true;
12
14
  export let type = "date";
13
15
  export let required = false;
16
+ export let steps = [];
17
+ const dipatch = createEventDispatcher();
14
18
  const _defaultValue = defaultValue || value || currentDateTime();
15
19
  const getDefaultValue = () => {
16
20
  if (type === "date") {
17
- return isDate(String(_defaultValue)) ? _defaultValue : currentDateTime().substring(0, 10);
21
+ return isDateString(_defaultValue) ? _defaultValue : currentDateTime().substring(0, 10);
18
22
  }
19
- return isDateTime(String(_defaultValue)) ? _defaultValue : currentDateTime();
23
+ return isDateTimeString(_defaultValue) ? _defaultValue : currentDateTime();
20
24
  };
21
25
  const checkChanged = () => {
22
26
  if (nullable) {
23
27
  value = enabled ? getDefaultValue() : "";
24
28
  }
29
+ dipatch("checkChanged", enabled);
30
+ onInput();
31
+ };
32
+ const incrementValue = (step) => {
33
+ if (isDateOrDateTimeString(value)) {
34
+ const newDate = addUnits(step.value, step.unit, value);
35
+ newDate.setUTCDate(newDate.getUTCDate() + step.value);
36
+ value = newDate.toISOString().substring(0, type === "date" ? 10 : 19);
37
+ }
38
+ };
39
+ const onInput = () => {
40
+ dipatch("value", enabled ? value : null);
25
41
  };
26
42
  if (!value) {
27
43
  if (nullable)
@@ -39,13 +55,20 @@ $:
39
55
  {/if}
40
56
  <div class:nullable class:disabled>
41
57
  <span class="input">
42
- <input {...{ type }} {id} {placeholder} {disabled} bind:value {required} />
58
+ <input {...{ type }} {id} {placeholder} {disabled} bind:value {required} on:input={onInput} />
43
59
  </span>
44
60
  {#if nullable}
45
61
  <span class="toggle">
46
62
  <input type="checkbox" bind:checked={enabled} on:change={checkChanged} />
47
63
  </span>
48
64
  {/if}
65
+ {#if steps.length > 0}
66
+ <span class="steps">
67
+ {#each steps as step}
68
+ <Button noMargin={true} collapse={true} on:click={() => incrementValue(step)}>{step.label}</Button>
69
+ {/each}
70
+ </span>
71
+ {/if}
49
72
  </div>
50
73
  </FormField>
51
74
 
@@ -68,8 +91,16 @@ input::placeholder {
68
91
  }
69
92
 
70
93
  div {
71
- display: block;
94
+ display: flex;
72
95
  position: relative;
96
+ gap: 0.5rem;
97
+ }
98
+ div .input {
99
+ flex-grow: 1;
100
+ }
101
+ div .steps {
102
+ display: flex;
103
+ gap: 0.25rem;
73
104
  }
74
105
  div.nullable input {
75
106
  padding-left: 2.5rem;
@@ -1,5 +1,5 @@
1
1
  import { SvelteComponent } from "svelte";
2
- import type { FormFieldSizeOptions } from '../../index.js';
2
+ import type { DateUnit, FormFieldSizeOptions } from '../../index.js';
3
3
  declare const __propDef: {
4
4
  props: {
5
5
  value?: string | undefined | null;
@@ -10,8 +10,16 @@ declare const __propDef: {
10
10
  enabled?: boolean | undefined;
11
11
  type?: "date" | "datetime-local" | undefined;
12
12
  required?: boolean | undefined;
13
+ steps?: {
14
+ label: string;
15
+ value: number;
16
+ unit: DateUnit;
17
+ }[] | undefined;
13
18
  };
14
19
  events: {
20
+ value: CustomEvent<string | null>;
21
+ checkChanged: CustomEvent<boolean>;
22
+ } & {
15
23
  [evt: string]: CustomEvent<any>;
16
24
  };
17
25
  slots: {
@@ -1,17 +1,17 @@
1
+ export type DateUnit = 'day' | 'month' | 'year' | 'minute' | 'hour' | 'second';
1
2
  export declare const currentDate: () => string;
2
3
  export declare const currentDateTime: () => string;
3
- /**
4
- * Get the current date plus x days in YYYY-MM-DD format
5
- *
6
- * @param days
7
- * @param fromDate
8
- * @returns YYYY-MM-DD
9
- */
10
- export declare const addDays: (days: number, fromDate?: string) => string;
4
+ export declare const addDays: (days: number, fromDate?: string | Date) => Date;
5
+ export declare const addMonths: (months: number, fromDate?: string | Date) => Date;
6
+ export declare const addYears: (years: number, fromDate?: string | Date) => Date;
7
+ export declare const addMinutes: (minutes: number, fromDateTime?: string | Date) => Date;
8
+ export declare const addHours: (hours: number, fromDateTime?: string | Date) => Date;
9
+ export declare const addSeconds: (seconds: number, fromDateTime?: string | Date) => Date;
10
+ export declare const addUnits: (value: number, units: DateUnit, fromDateTime?: string | Date) => Date;
11
11
  export declare const dateTimeFromSupabase: (dateTime: string | null) => string;
12
12
  export declare const dateTimeToSupabase: (dateTime: string) => string | null;
13
13
  export declare const dateTimeToHuman: (dateTime: string) => string;
14
14
  export declare const dateTimeToInput: (dateTime?: Date) => string;
15
- export declare const isDate: (date: string) => boolean;
16
- export declare const isDateTime: (dateTime: string) => boolean;
17
- export declare const isDateOrDateTime: (dateOrDateTime: string) => boolean;
15
+ export declare const isDateString: (date: string) => date is string;
16
+ export declare const isDateTimeString: (dateTime: string) => dateTime is string;
17
+ export declare const isDateOrDateTimeString: (dateOrDateTime: unknown) => dateOrDateTime is string;
@@ -12,17 +12,51 @@ export const currentDateTime = () => {
12
12
  date.setMinutes(date.getMinutes() - offset);
13
13
  return date.toISOString().split('.')[0].slice(0, -3);
14
14
  };
15
- /**
16
- * Get the current date plus x days in YYYY-MM-DD format
17
- *
18
- * @param days
19
- * @param fromDate
20
- * @returns YYYY-MM-DD
21
- */
22
15
  export const addDays = (days, fromDate) => {
23
16
  const date = fromDate ? new Date(fromDate) : new Date();
24
17
  date.setDate(date.getDate() + days);
25
- return date.toISOString().split('T')[0];
18
+ return date;
19
+ };
20
+ export const addMonths = (months, fromDate) => {
21
+ const date = fromDate ? new Date(fromDate) : new Date();
22
+ date.setMonth(date.getMonth() + months);
23
+ return date;
24
+ };
25
+ export const addYears = (years, fromDate) => {
26
+ const date = fromDate ? new Date(fromDate) : new Date();
27
+ date.setFullYear(date.getFullYear() + years);
28
+ return date;
29
+ };
30
+ export const addMinutes = (minutes, fromDateTime) => {
31
+ const date = fromDateTime ? new Date(fromDateTime) : new Date();
32
+ date.setMinutes(date.getMinutes() + minutes);
33
+ return date;
34
+ };
35
+ export const addHours = (hours, fromDateTime) => {
36
+ const date = fromDateTime ? new Date(fromDateTime) : new Date();
37
+ date.setHours(date.getHours() + hours);
38
+ return date;
39
+ };
40
+ export const addSeconds = (seconds, fromDateTime) => {
41
+ const date = fromDateTime ? new Date(fromDateTime) : new Date();
42
+ date.setSeconds(date.getSeconds() + seconds);
43
+ return date;
44
+ };
45
+ export const addUnits = (value, units, fromDateTime) => {
46
+ const date = fromDateTime ? new Date(fromDateTime) : new Date();
47
+ if (units === 'day')
48
+ date.setDate(date.getDate() + value);
49
+ if (units === 'month')
50
+ date.setMonth(date.getMonth() + value);
51
+ if (units === 'year')
52
+ date.setFullYear(date.getFullYear() + value);
53
+ if (units === 'minute')
54
+ date.setMinutes(date.getMinutes() + value);
55
+ if (units === 'hour')
56
+ date.setHours(date.getHours() + value);
57
+ if (units === 'second')
58
+ date.setSeconds(date.getSeconds() + value);
59
+ return date;
26
60
  };
27
61
  // Convert datetime from YYYY-MM-DDTHH:MM+TZ format to YYYY-MM-DD HH:MM format
28
62
  export const dateTimeFromSupabase = (dateTime) => {
@@ -67,13 +101,15 @@ export const dateTimeToInput = (dateTime) => {
67
101
  return dateTime.toISOString().split('.')[0].slice(0, -3);
68
102
  };
69
103
  // Is this date formatted in YYYY-MM-DD format?
70
- export const isDate = (date) => {
104
+ export const isDateString = (date) => {
71
105
  return /^\d{4}-\d{2}-\d{2}$/.test(date);
72
106
  };
73
107
  // Is this datetime formatted in YYYY-MM-DD HH:MM format?
74
- export const isDateTime = (dateTime) => {
108
+ export const isDateTimeString = (dateTime) => {
75
109
  return /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}(:\d{2})?$/.test(dateTime);
76
110
  };
77
- export const isDateOrDateTime = (dateOrDateTime) => {
78
- return isDate(dateOrDateTime) || isDateTime(dateOrDateTime);
111
+ export const isDateOrDateTimeString = (dateOrDateTime) => {
112
+ if (typeof dateOrDateTime !== 'string')
113
+ return false;
114
+ return isDateString(dateOrDateTime) || isDateTimeString(dateOrDateTime);
79
115
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sveltacular",
3
- "version": "0.0.55",
3
+ "version": "0.0.56",
4
4
  "description": "A Svelte component library",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",