UNPKG

@rjsf/utils

Version:
73 lines 3.07 kB
import isString from 'lodash-es/isString.js'; import { ID_KEY } from './constants.js'; /** Generates a consistent `id` pattern for a given `id` and a `suffix` * * @param id - Either simple string id or an IdSchema from which to extract it * @param suffix - The suffix to append to the id */ function idGenerator(id, suffix) { const theId = isString(id) ? id : id[ID_KEY]; return `${theId}__${suffix}`; } /** Return a consistent `id` for the field description element * * @param id - Either simple string id or an IdSchema from which to extract it * @returns - The consistent id for the field description element from the given `id` */ export function descriptionId(id) { return idGenerator(id, 'description'); } /** Return a consistent `id` for the field error element * * @param id - Either simple string id or an IdSchema from which to extract it * @returns - The consistent id for the field error element from the given `id` */ export function errorId(id) { return idGenerator(id, 'error'); } /** Return a consistent `id` for the field examples element * * @param id - Either simple string id or an IdSchema from which to extract it * @returns - The consistent id for the field examples element from the given `id` */ export function examplesId(id) { return idGenerator(id, 'examples'); } /** Return a consistent `id` for the field help element * * @param id - Either simple string id or an IdSchema from which to extract it * @returns - The consistent id for the field help element from the given `id` */ export function helpId(id) { return idGenerator(id, 'help'); } /** Return a consistent `id` for the field title element * * @param id - Either simple string id or an IdSchema from which to extract it * @returns - The consistent id for the field title element from the given `id` */ export function titleId(id) { return idGenerator(id, 'title'); } /** Return a list of element ids that contain additional information about the field that can be used to as the aria * description of the field. This is correctly omitting `titleId` which would be "labeling" rather than "describing" the * element. * * @param id - Either simple string id or an IdSchema from which to extract it * @param [includeExamples=false] - Optional flag, if true, will add the `examplesId` into the list * @returns - The string containing the list of ids for use in an `aria-describedBy` attribute */ export function ariaDescribedByIds(id, includeExamples = false) { const examples = includeExamples ? ` ${examplesId(id)}` : ''; return `${errorId(id)} ${descriptionId(id)} ${helpId(id)}${examples}`; } /** Return a consistent `id` for the `optionIndex`s of a `Radio` or `Checkboxes` widget * * @param id - The id of the parent component for the option * @param optionIndex - The index of the option for which the id is desired * @returns - An id for the option index based on the parent `id` */ export function optionId(id, optionIndex) { return `${id}-${optionIndex}`; } //# sourceMappingURL=idGenerators.js.map