Skip to content

Commit

Permalink
idk
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickpatrickpatrick committed Oct 21, 2024
1 parent 7825248 commit cc9f48d
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 81 deletions.
115 changes: 35 additions & 80 deletions packages/govuk-frontend/src/govuk/common/config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GOVUKFrontendComponent } from '../govuk-frontend-component.mjs'
import { isObject } from './index.mjs'
// import { normaliseString } from "./normalise-string.mjs"

/**
* Config
Expand All @@ -8,11 +8,26 @@ import { isObject } from './index.mjs'
*/
class Config {
/**
* Config Object
*
* @type {ConfigurationType}
* Schema for configuration
*
* @type {Schema}
*/
static schema


/**
* Defaults for configuration
*
* @type {ConfigObject}
*/
configObject
static defaults

/**
* Dataset overrides
*
* @type {ConfigObject}
*/
static overrides

/**
* Config flattening function
Expand All @@ -22,9 +37,9 @@ class Config {
* greatest priority on the LAST item passed in.
*
* @param {...ConfigObject} configObjects - configuration objects
* @returns {ConfigurationType} - merged configuration object
* @returns {import('./index.mjs').ObjectNested} - merged configuration object
*/
mergeConfigs(...configObjects) {
static mergeConfigs(...configObjects) {
// Start with an empty object as our base
/** @type {{ [key: string]: unknown }} */
const formattedConfigObject = {}
Expand All @@ -48,25 +63,20 @@ class Config {
}
}

return /** @type {ConfigurationType} */ (formattedConfigObject)
return /** @type {import('./index.mjs').ObjectNested} */ (formattedConfigObject)
}

/**
* Instance of component
*
* @type {ComponentInstance}
*/
// component
configObject = {}

// * @param {ComponentInstance} component - instance of component

/**
* @param {...ConfigurationType} configObjects - configuration objects
*/
constructor(...configObjects) {
this.configObject = this.mergeConfigs(...configObjects)
* @param {import('../govuk-frontend-component.mjs').ChildClassConstructorWithConfig} component - component instance
* @param {...ConfigObject} configObjects - configuration objects
*/
constructor(component, ...configObjects) {
// const configObject = Config.mergeConfigs(...configObjects)

const configObject = this.configObject
this.configObject = configObject

return new Proxy(this, {
get(target, name, receiver) {
Expand All @@ -75,67 +85,8 @@ class Config {
}
return Reflect.get(target, name, receiver)
}
})
})
}

/**
* Extracts keys starting with a particular namespace from dataset ('data-*')
* object, removing the namespace in the process, normalising all values
*
* @param {string} namespace - The namespace to filter keys with
* @returns {ObjectNested | undefined} Nested object with dot-separated key namespace removed
*/
// extractConfigByNamespace (namespace) {
// const componentClass = this.component.constructor;
// const dataset = this.component.$root.dataset;

// // const property = Component.schema.properties[namespace]
// const property = componentClass.schema.properties[namespace]

// // Only extract configs for object schema properties
// if (property.type !== 'object') {
// return
// }

// // Add default empty config
// const newObject = {
// [namespace]: /** @type {ObjectNested} */ ({})
// }

// for (const [key, value] of Object.entries(dataset)) {
// /** @type {ObjectNested | ObjectNested[NestedKey]} */
// let current = newObject

// // Split the key into parts, using . as our namespace separator
// const keyParts = key.split('.')

// /**
// * Create new level per part
// *
// * e.g. 'i18n.textareaDescription.other' becomes
// * `{ i18n: { textareaDescription: { other } } }`
// */
// for (const [index, name] of keyParts.entries()) {
// if (typeof current === 'object') {
// // Drop down to nested object until the last part
// if (index < keyParts.length - 1) {
// // New nested object (optionally) replaces existing value
// if (!isObject(current[name])) {
// current[name] = {}
// }

// // Drop down into new or existing nested object
// current = current[name]
// } else if (key !== namespace) {
// // Normalised value (optionally) replaces existing value
// current[name] = normaliseString(value)
// }
// }
// }
// }

// return newObject[namespace]
// }
}

/* eslint-disable jsdoc/valid-types --
Expand Down Expand Up @@ -164,4 +115,8 @@ class Config {
* @typedef {{ [key: string]: string | boolean | number | ObjectNested | undefined }} ObjectNested
*/

/**
* @typedef {import('./index.mjs').Schema} Schema
*/

export default Config
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// import { mergeConfigs } from '../../common/index.mjs'
// import { normaliseDataset } from '../../common/normalise-dataset.mjs'
import { normaliseDataset } from '../../common/normalise-dataset.mjs'
import { ElementError } from '../../errors/index.mjs'
import { GOVUKFrontendComponentConfigurable } from '../../govuk-frontend-component.mjs'
import Config from '../../common/config.mjs'
import { I18n } from '../../i18n.mjs'

/**
Expand Down Expand Up @@ -607,6 +608,57 @@ export class Accordion extends GOVUKFrontendComponentConfigurable {
})
}


/**
* Accordion Config Class
*
* @augments Config<AccordionConfig>
*/
class AccordionConfigClass extends Config {
/**
* Accordion default config
*
* @see {@link AccordionConfig}
* @constant
* @type {AccordionConfig}
*/
static defaults = Object.freeze({
i18n: {
hideAllSections: 'Hide all sections',
hideSection: 'Hide',
hideSectionAriaLabel: 'Hide this section',
showAllSections: 'Show all sections',
showSection: 'Show',
showSectionAriaLabel: 'Show this section'
},
rememberExpanded: true
})

/**
* Accordion config schema
*
* @constant
* @satisfies {Schema}
*/
static schema = Object.freeze({
properties: {
i18n: { type: 'object' },
rememberExpanded: { type: 'boolean' }
}
})

/**
* @param {...AccordionConfig} configs - objects to set config
*/
constructor(Component, ...configs) {
super(AccordionConfigClass.defaults, ...configs, normaliseDataset(

));

console.log('hello')
}
}

/**
* Accordion config
*
Expand Down

0 comments on commit cc9f48d

Please sign in to comment.