1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
/**
* --------------------------------------------------------------------------
* Bootstrap (v5.1.0): forms/field.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
import { isElement, typeCheckConfig } from '../util/index'
import Messages from './messages'
import Manipulator from '../dom/manipulator'
import EventHandler from '../dom/event-handler'
import BaseComponent from '../base-component'
import SelectorEngine from '../dom/selector-engine'
const NAME = 'field'
const DATA_KEY = 'bs.field'
const EVENT_KEY = `.${DATA_KEY}`
const EVENT_INPUT = `input${EVENT_KEY}`
const CLASS_PREFIX_ERROR = 'invalid'
const CLASS_PREFIX_INFO = 'info'
const CLASS_PREFIX_SUCCESS = 'valid'
const CLASS_FIELD_ERROR = 'is-invalid'
const CLASS_FIELD_SUCCESS = 'is-valid'
const ARIA_DESCRIBED_BY = 'aria-describedby'
const Default = {
name: null,
type: 'feedback', // or tooltip
valid: '', // valid message to add
invalid: '' // invalid message to add
}
const DefaultType = {
name: 'string',
type: 'string',
valid: 'string',
invalid: 'string'
}
class Field extends BaseComponent {
constructor(element, config) {
super(element)
if (!isElement(this._element)) {
throw new TypeError(`field "${this._config.name}" not found`)
}
this._config = this._getConfig(config)
this._errorMessages = this._getNewMessagesCollection(CLASS_PREFIX_ERROR, CLASS_FIELD_ERROR)
this._helpMessages = this._getNewMessagesCollection(CLASS_PREFIX_INFO, '')
this._successMessages = this._getNewMessagesCollection(CLASS_PREFIX_SUCCESS, CLASS_FIELD_SUCCESS)
this._initializeMessageCollections()
EventHandler.on(this._element, EVENT_INPUT, () => {
this.clearAppended()
})
}
static get NAME() {
return NAME
}
getElement() {
return this._element
}
clearAppended() {
const appendedFeedback = SelectorEngine.findOne(`[class*=-${this._config.type}], ${this._getId()}`, this._element.parentNode)
if (!appendedFeedback) {
return
}
appendedFeedback.remove()
this._element.classList.remove(CLASS_FIELD_ERROR, CLASS_FIELD_SUCCESS)
const initialDescribedBy = this._initialDescribedBy()
if (initialDescribedBy) {
this._element.setAttribute(ARIA_DESCRIBED_BY, initialDescribedBy)
} else {
this._element.removeAttribute(ARIA_DESCRIBED_BY)
}
}
dispose() {
EventHandler.off(this._element, EVENT_KEY)
Object.getOwnPropertyNames(this).forEach(propertyName => {
this[propertyName] = null
})
}
errorMessages() {
return this._errorMessages
}
helpMessages() {
return this._helpMessages
}
successMessages() {
return this._successMessages
}
_getConfig(config) {
config = {
...Default,
...Manipulator.getDataAttributes(this._element),
...(typeof config === 'object' ? config : {})
}
typeCheckConfig(NAME, config, DefaultType)
return config
}
_appendFeedback(htmlElement, elementClass) {
if (!isElement(htmlElement)) {
return
}
this.clearAppended()
const feedbackElement = htmlElement
this._element.parentNode.append(feedbackElement)
feedbackElement.id = this._getId()
this._element.classList.add(elementClass)
const initialDescribedBy = this._initialDescribedBy()
const describedBy = initialDescribedBy ? `${initialDescribedBy} ` : ''
this._element.setAttribute(ARIA_DESCRIBED_BY, `${describedBy}${feedbackElement.id}`)
}
_getId() {
return `${this._config.name}-formTip`
}
_getNewMessagesCollection(classPrefix, elementClass) {
const config = {
appendFunction: html => this._appendFeedback(html, elementClass),
extraClass: `${classPrefix}-${this._config.type}`
}
return new Messages(config)
}
_initialDescribedBy() {
return (this._element.getAttribute(ARIA_DESCRIBED_BY) || '').replaceAll(this._getId(), '').trim()
}
_initializeMessageCollections() {
if (this._config.invalid) {
this.errorMessages().set('default', this._config.invalid)
}
if (this._config.valid) {
this.successMessages().set('default', this._config.valid)
}
}
}
export default Field
|