winduum 3.0.0-next.6 → 3.0.0-next.7

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "winduum",
3
- "version": "3.0.0-next.6",
3
+ "version": "3.0.0-next.7",
4
4
  "type": "module",
5
5
  "types": "types/index.d.ts",
6
6
  "main": "plugin/index.cjs",
@@ -7,14 +7,14 @@ export interface ValidateFormOptions {
7
7
  }
8
8
 
9
9
  export interface ValidateFieldOptions {
10
- validate?: boolean
11
10
  validationMessage?: string
12
11
  selector?: string
13
12
  validitySelector?: string
14
13
  infoContent?: string
15
- endParentSelector?: string
16
- endSelector?: string
17
- endContent?: string
14
+ iconParentSelector?: string
15
+ iconSelector?: string
16
+ iconContent?: string
17
+ validIcon?: string | null
18
18
  invalidIcon?: string
19
19
  }
20
20
 
@@ -6,26 +6,22 @@
6
6
  export const validateForm = (event, options = {}) => {
7
7
  options = {
8
8
  validateSelector: '.x-field',
9
- validateOptions: {
10
- validate: true,
11
- },
9
+ validateOptions: {},
12
10
  validateField,
13
11
  submitterLoadingAttribute: 'data-loading',
14
12
  scrollOptions: { behavior: 'smooth', block: 'center' },
15
13
  ...options,
16
14
  }
17
15
 
18
- if (options.validateOptions.validate) {
19
- if (!event.target.checkValidity()) {
20
- event.preventDefault()
21
- event.stopImmediatePropagation()
16
+ if (!event.target.checkValidity()) {
17
+ event.preventDefault()
18
+ event.stopImmediatePropagation()
22
19
 
23
- event.target.querySelector(':invalid').scrollIntoView(options.scrollOptions)
24
- event.target.querySelector(':invalid').focus()
25
- }
26
- else if (options.submitterLoadingAttribute) {
27
- event?.submitter?.setAttribute(options.submitterLoadingAttribute, '')
28
- }
20
+ event.target.querySelector(':invalid').scrollIntoView(options.scrollOptions)
21
+ event.target.querySelector(':invalid').focus()
22
+ }
23
+ else if (options.submitterLoadingAttribute) {
24
+ event?.submitter?.setAttribute(options.submitterLoadingAttribute, '')
29
25
  }
30
26
 
31
27
  event.target.querySelectorAll(options.validateSelector).forEach((element) => {
@@ -40,39 +36,40 @@ export const validateForm = (event, options = {}) => {
40
36
  */
41
37
  export const validateField = (element, options = {}) => {
42
38
  options = {
43
- validate: true,
44
39
  selector: ':is(input:not([type="hidden"]), textarea, select):not([readonly], [data-novalidate])',
45
40
  validitySelector: '[data-validity]',
46
41
  infoContent: '<div class="x-info text-error" data-validity></div>',
47
- endParentSelector: '.x-control',
48
- endSelector: '.ms-auto',
49
- endContent: '<div class="ms-auto"></div>',
50
- invalidIcon: '<svg class="text-error" data-validity aria-hidden="true"><use href="#icon-exclamation-circle"></use></svg>',
42
+ iconParentSelector: '.x-control',
43
+ iconSelector: '.ms-auto',
44
+ iconContent: '<div class="ms-auto"></div>',
45
+ validIcon: null,
46
+ invalidIcon: '<svg class="text-error" data-validity aria-hidden="true"><use href="#heroicons-outline/exclamation-circle"></use></svg>',
51
47
  ...options,
52
48
  }
53
49
 
54
- const validationElements = /** @type {HTMLInputElement[]} */ ([...element.querySelectorAll(options.selector)])
50
+ const validationElements = [...element.querySelectorAll(options.selector)]
55
51
 
56
- if (!validationElements.length || !options.validate) return
52
+ if (!validationElements.length) return
57
53
 
58
54
  element.querySelectorAll(options.validitySelector).forEach(el => el.remove())
59
55
 
60
56
  const invalidElements = validationElements.filter(validationElement => !validationElement.checkValidity())
61
57
 
62
- if (!invalidElements.length) return
58
+ validationElements.forEach((validationElement) => {
59
+ const icon = invalidElements.includes(validationElement) ? options.invalidIcon : options.validIcon
60
+ const iconParentElement = validationElement.closest(options.iconParentSelector)
63
61
 
64
- invalidElements.forEach((validationElement) => {
65
- const endParentElement = validationElement.closest(options.endParentSelector)
62
+ if (!iconParentElement || !icon) return
66
63
 
67
- if (!endParentElement || !options.invalidIcon) return
68
-
69
- if (!endParentElement.querySelector(options.endSelector)) {
70
- endParentElement.insertAdjacentHTML('beforeend', options.endContent)
64
+ if (!iconParentElement.querySelector(options.iconSelector)) {
65
+ iconParentElement.insertAdjacentHTML('beforeend', options.iconContent)
71
66
  }
72
67
 
73
- endParentElement.querySelector(options.endSelector).insertAdjacentHTML('afterbegin', options.invalidIcon)
68
+ iconParentElement.querySelector(options.iconSelector).insertAdjacentHTML('afterbegin', icon)
74
69
  })
75
70
 
71
+ if (!invalidElements.length) return
72
+
76
73
  element.insertAdjacentHTML('beforeend', options.infoContent)
77
74
  element.lastElementChild.textContent = options.validationMessage ?? invalidElements[0].dataset.validationMessage ?? invalidElements[0].validationMessage
78
75
  }
package/types/index.d.ts CHANGED
@@ -129,14 +129,14 @@ declare module 'winduum/src/components/form' {
129
129
  }
130
130
 
131
131
  export interface ValidateFieldOptions {
132
- validate?: boolean
133
132
  validationMessage?: string
134
133
  selector?: string
135
134
  validitySelector?: string
136
135
  infoContent?: string
137
- endParentSelector?: string
138
- endSelector?: string
139
- endContent?: string
136
+ iconParentSelector?: string
137
+ iconSelector?: string
138
+ iconContent?: string
139
+ validIcon?: string | null
140
140
  invalidIcon?: string
141
141
  }
142
142