All Downloads are FREE. Search and download functionalities are using the official Maven repository.

components.widgets.Form.fields.utils.ts Maven / Gradle / Ivy

The newest version!
import { FieldDependency } from '../../../../ducks/form/types'
import { replaceIndex as replaceIndexKey } from '../../../../core/datasource/ArrayField/replaceIndex'

const replaceWord = (str: string, from: string, to: string) => {
    // \b и \$ кофликтуют, когда стоят рядом
    const prefix = from.startsWith('$') ? '' : '\\b'
    const suffix = from.endsWith('$') ? '' : '\\b'
    // Экранируем, чтобы регулярка не ломалась на спецсимволе
    const word = from.replace('$', '\\$')
    const reg = new RegExp(`${prefix}${word}${suffix}`, 'g')

    return str.replaceAll(reg, to)
}

export const modifyDependencies = (
    dependencies: FieldDependency[] | void,
    ctx: Record,
) => dependencies?.map((dep) => {
    const newDep = { ...dep }

    if (newDep.on) {
        newDep.on = newDep.on.map(key => replaceIndexKey(key, ctx))
    }

    return newDep
})

export const replaceIndex = (
    obj: object,
    ctx: Record,
) => {
    let jsonString = JSON.stringify(obj)

    for (const [key, index] of Object.entries(ctx)) {
        jsonString = replaceWord(jsonString, key, `${index}`)
    }

    return JSON.parse(jsonString)
}

export const resolveControlIndexes = (
    control: { dataProvider?: object },
    ctx: Record,
) => {
    if (control.dataProvider) {
        const dataProvider = replaceIndex(control.dataProvider, ctx)

        return {
            ...control,
            dataProvider,
        }
    }

    return control
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy