mirror of
https://github.com/twbs/bootstrap.git
synced 2025-08-08 22:56:46 +02:00
Manipulator: Add JSON parse support (#35077)
Support parsing JSON from each component's main element using the `data-bs-config` attribute. The `bs-config` attribute will be reserved and omitted during `getDataAttributes` parsing. With this commit, every component, will create its config object, using: * defaults * data-bs-config * the rest of data attributes * configuration object given during instance initialization Co-authored-by: XhmikosR <xhmikosr@gmail.com> Co-authored-by: Mark Otto <markd.otto@gmail.com> Co-authored-by: Mark Otto <markdotto@gmail.com>
This commit is contained in:
@@ -22,7 +22,15 @@ function normalizeData(value) {
|
||||
return null
|
||||
}
|
||||
|
||||
return value
|
||||
if (typeof value !== 'string') {
|
||||
return value
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(decodeURIComponent(value))
|
||||
} catch {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeDataKey(key) {
|
||||
@@ -44,7 +52,7 @@ const Manipulator = {
|
||||
}
|
||||
|
||||
const attributes = {}
|
||||
const bsKeys = Object.keys(element.dataset).filter(key => key.startsWith('bs'))
|
||||
const bsKeys = Object.keys(element.dataset).filter(key => key.startsWith('bs') && !key.startsWith('bsConfig'))
|
||||
|
||||
for (const key of bsKeys) {
|
||||
let pureKey = key.replace(/^bs/, '')
|
||||
|
@@ -38,8 +38,11 @@ class Config {
|
||||
}
|
||||
|
||||
_mergeConfigObj(config, element) {
|
||||
const jsonConfig = isElement(element) ? Manipulator.getDataAttribute(element, 'config') : {} // try to parse
|
||||
|
||||
return {
|
||||
...this.constructor.Default,
|
||||
...(typeof jsonConfig === 'object' ? jsonConfig : {}),
|
||||
...(isElement(element) ? Manipulator.getDataAttributes(element) : {}),
|
||||
...(typeof config === 'object' ? config : {})
|
||||
}
|
||||
|
Reference in New Issue
Block a user