UNPKG

@rjsf/core

Version:

A simple React component capable of building HTML forms out of a JSON schema.

24 lines (23 loc) 1.05 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { examplesId } from '@rjsf/utils'; /** Renders a `<datalist>` element containing options from schema examples and default value. * Normalizes types to prevent duplicate keys when examples and default have different types. * For example, if examples are strings ["5432"] and default is number 5432, the default * will not be added as a duplicate option. * * @param props - The `SchemaExamplesProps` for this component */ export default function SchemaExamples(props) { const { id, schema } = props; const { examples, default: schemaDefault } = schema; if (!Array.isArray(examples)) { return null; } return (_jsx("datalist", { id: examplesId(id), children: examples .concat(schemaDefault !== undefined && !examples.map(String).includes(String(schemaDefault)) ? [schemaDefault] : []) .map((example) => { return _jsx("option", { value: example }, String(example)); }) }, `datalist_${id}`)); }