@rjsf/utils
Version:
Utility functions for @rjsf/core
26 lines • 1.88 kB
JavaScript
import { ID_KEY } from './constants.js';
/** Constructs the `FieldPathId` for `fieldPath`. If `parentPathId` is provided, the `fieldPath` is appended to the end
* of the parent path. Then the `ID_KEY` of the resulting `FieldPathId` is constructed from the `idPrefix` and
* `idSeparator` contained within the `globalFormOptions`. If `fieldPath` is passed as an empty string, it will simply
* generate the path from the `parentPath` (if provided) and the `idPrefix` and `idSeparator`. If a `nameGenerator`
* is provided in `globalFormOptions`, it will also generate the HTML `name` attribute.
*
* @param fieldPath - The property name or array index of the current field element
* @param globalFormOptions - The `GlobalFormOptions` used to get the `idPrefix` and `idSeparator`
* @param [parentPath] - The optional `FieldPathId` or `FieldPathList` of the parent element for this field element
* @param [isMultiValue] - Optional flag indicating this field accepts multiple values
* @returns - The `FieldPathId` for the given `fieldPath` and the optional `parentPathId`
*/
export default function toFieldPathId(fieldPath, globalFormOptions, parentPath, isMultiValue) {
const basePath = Array.isArray(parentPath) ? parentPath : parentPath === null || parentPath === void 0 ? void 0 : parentPath.path;
const childPath = fieldPath === '' ? [] : [fieldPath];
const path = basePath ? basePath.concat(...childPath) : childPath;
const id = [globalFormOptions.idPrefix, ...path].join(globalFormOptions.idSeparator);
// Generate name attribute if nameGenerator is provided
let name;
if (globalFormOptions.nameGenerator && path.length > 0) {
name = globalFormOptions.nameGenerator(path, globalFormOptions.idPrefix, isMultiValue);
}
return { path, [ID_KEY]: id, ...(name !== undefined && { name }) };
}
//# sourceMappingURL=toFieldPathId.js.map