Skip to content

Commit

Permalink
fix: formData not correctly parsed
Browse files Browse the repository at this point in the history
formData not parsed when provided by render method
  • Loading branch information
kevinchappell committed Nov 26, 2024
1 parent 376568d commit cec4935
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/demo/js/options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import external from './external'
import controls from './controls'
import config from './config'

const editorContainer = document.querySelector('.build-form')
const editorContainer = '.build-form'
const renderContainer = '.render-form'

export const editorOptions = {
Expand Down
21 changes: 21 additions & 0 deletions src/lib/js/common/utils/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,24 @@ export function identity(value) {
export function noop() {
// Do nothing
}

/**
* Parses the provided data argument. If the argument is a string, it attempts to parse it as JSON.
* If the parsing fails, it logs an error and returns an empty object.
* If the argument is not a string, it returns the argument as is.
*
* @param {string|Object} dataArg - The data to be parsed. Can be a JSON string or an object.
* @returns {Object} - The parsed object or the original object if the input was not a string.
*/
export function parseData(data = Object.create(null)) {
if (typeof data === 'string') {
try {
return JSON.parse(data)
} catch (e) {
console.error('Invalid JSON string provided:', e)
return Object.create(null)
}
}

return data
}
4 changes: 2 additions & 2 deletions src/lib/js/components/component-data.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Data from './data.js'
import { uuid, clone, merge } from '../common/utils/index.mjs'
import { uuid, clone, merge, parseData } from '../common/utils/index.mjs'
import { get } from '../common/utils/object.mjs'

export default class ComponentData extends Data {
load = dataArg => {
const data = this.parseformData(dataArg)
const data = parseData(dataArg)
this.empty()
for (const [key, val] of Object.entries(data)) {
this.add(key, val)
Expand Down
21 changes: 0 additions & 21 deletions src/lib/js/components/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,4 @@ export default class Data {
}
setCallbacks = {}
configVal = Object.create(null)

/**
* Parses the provided data argument. If the argument is a string, it attempts to parse it as JSON.
* If the parsing fails, it logs an error and returns an empty object.
* If the argument is not a string, it returns the argument as is.
*
* @param {string|Object} dataArg - The data to be parsed. Can be a JSON string or an object.
* @returns {Object} - The parsed object or the original object if the input was not a string.
*/
parseformData = (dataArg = Object.create(null)) => {
if (typeof dataArg === 'string') {
try {
return JSON.parse(dataArg)
} catch (e) {
console.error('Invalid JSON string provided:', e)
return Object.create(null)
}
}

return dataArg
}
}
3 changes: 2 additions & 1 deletion src/lib/js/components/fields/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ComponentData from '../component-data.js'
import Field from './field.js'
import Controls from '../controls/index.js'
import { get, set } from '../../common/utils/object.mjs'
import { parseData } from '../../common/utils/index.mjs'

const DEFAULT_CONFIG = {
actionButtons: {
Expand Down Expand Up @@ -62,7 +63,7 @@ export class Fields extends ComponentData {
}

load = (dataArg = Object.create(null)) => {
const allFieldData = this.parseformData(dataArg)
const allFieldData = parseData(dataArg)
this.empty()

for (const [key, val] of Object.entries(allFieldData)) {
Expand Down
9 changes: 5 additions & 4 deletions src/lib/js/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import dom from './common/dom'
import { uuid, isAddress, isExternalAddress, merge } from './common/utils'
import { STAGE_CLASSNAME, UUID_REGEXP } from './constants'
import { fetchDependencies } from './common/loaders'
import { parseData } from './common/utils/index.mjs'

const RENDER_PREFIX = 'f-'

Expand Down Expand Up @@ -37,10 +38,10 @@ const createRemoveButton = () =>
)

export default class FormeoRenderer {
constructor(opts, formData = {}) {
const { renderContainer, external, elements } = processOptions(opts)
constructor(opts, formDataArg) {
const { renderContainer, external, elements, formData = {} } = processOptions(opts)
this.container = renderContainer
this.form = formData
this.form = parseData(formDataArg || formData)
this.external = external
this.dom = dom
this.components = Object.create(null)
Expand All @@ -52,7 +53,7 @@ export default class FormeoRenderer {
* @param {Object} formData
*/
render = (formData = this.form) => {
this.form = formData
this.form = parseData(formData)

const renderCount = document.getElementsByClassName('formeo-render').length
const config = {
Expand Down

0 comments on commit cec4935

Please sign in to comment.