|
25 | 25 |
|
26 | 26 | import startCase from 'lodash/startCase'; |
27 | 27 |
|
28 | | -import type { ControlElement, JsonSchema, LabelDescription } from '../models'; |
| 28 | +import { |
| 29 | + ControlElement, |
| 30 | + JsonSchema, |
| 31 | + LabelDescription, |
| 32 | + UISchemaElement, |
| 33 | +} from '../models'; |
29 | 34 | import { decode } from './path'; |
| 35 | +import { getI18nKeyPrefix, Translator } from '../i18n'; |
| 36 | +import { Resolve } from './util'; |
| 37 | +import { |
| 38 | + getFirstPrimitiveProp, |
| 39 | + isEnumSchema, |
| 40 | + isOneOfEnumSchema, |
| 41 | +} from './schema'; |
| 42 | +import get from 'lodash/get'; |
| 43 | +import { findUiControl, getPropPath } from './uischema'; |
| 44 | +import { |
| 45 | + EnumOption, |
| 46 | + enumToEnumOptionMapper, |
| 47 | + oneOfToEnumOptionMapper, |
| 48 | +} from './renderer'; |
| 49 | +import isEqual from 'lodash/isEqual'; |
30 | 50 |
|
31 | 51 | const deriveLabel = ( |
32 | 52 | controlElement: ControlElement, |
@@ -81,3 +101,75 @@ const labelDescription = (text: string, show: boolean): LabelDescription => ({ |
81 | 101 | text: text, |
82 | 102 | show: show, |
83 | 103 | }); |
| 104 | + |
| 105 | +/** |
| 106 | + * Compute the child label title for array based controls |
| 107 | + * @param data the data of the control |
| 108 | + * @param childPath the child path |
| 109 | + * @param childLabelProp the dotted path to the value used as child label |
| 110 | + * @param {JsonSchema} schema the json schema for this control |
| 111 | + * @param {JsonSchema} rootSchema the root json schema |
| 112 | + * @param {Translator} translateFct the translator fonction |
| 113 | + * @param {UISchemaElement} uiSchema the uiSchema of the control |
| 114 | + */ |
| 115 | +export const computeChildLabel = ( |
| 116 | + data: any, |
| 117 | + childPath: string, |
| 118 | + childLabelProp: string, |
| 119 | + schema: JsonSchema, |
| 120 | + rootSchema: JsonSchema, |
| 121 | + translateFct: Translator, |
| 122 | + uiSchema: UISchemaElement |
| 123 | +): string => { |
| 124 | + const childData = Resolve.data(data, childPath); |
| 125 | + |
| 126 | + if (!childLabelProp) { |
| 127 | + childLabelProp = getFirstPrimitiveProp(schema); |
| 128 | + } |
| 129 | + |
| 130 | + // return early in case there is no prop we can query |
| 131 | + if (!childLabelProp) { |
| 132 | + return ''; |
| 133 | + } |
| 134 | + |
| 135 | + const currentValue = get(childData, childLabelProp, ''); |
| 136 | + |
| 137 | + // check whether the value is part of a oneOf or enum and needs to be translated |
| 138 | + const childSchema = Resolve.schema( |
| 139 | + schema, |
| 140 | + '#' + getPropPath(childLabelProp), |
| 141 | + rootSchema |
| 142 | + ); |
| 143 | + |
| 144 | + let enumOption: EnumOption = undefined; |
| 145 | + if (isEnumSchema(childSchema)) { |
| 146 | + enumOption = enumToEnumOptionMapper( |
| 147 | + currentValue, |
| 148 | + translateFct, |
| 149 | + getI18nKeyPrefix( |
| 150 | + childSchema, |
| 151 | + findUiControl(uiSchema, childLabelProp), |
| 152 | + childPath + '.' + childLabelProp |
| 153 | + ) |
| 154 | + ); |
| 155 | + } else if (isOneOfEnumSchema(childSchema)) { |
| 156 | + const oneOfArray = childSchema.oneOf as JsonSchema[]; |
| 157 | + const oneOfSchema = oneOfArray.find((e: JsonSchema) => |
| 158 | + isEqual(e.const, currentValue) |
| 159 | + ); |
| 160 | + |
| 161 | + if (oneOfSchema) { |
| 162 | + enumOption = oneOfToEnumOptionMapper( |
| 163 | + oneOfSchema, |
| 164 | + translateFct, |
| 165 | + getI18nKeyPrefix( |
| 166 | + oneOfSchema, |
| 167 | + undefined, |
| 168 | + childPath + '.' + childLabelProp |
| 169 | + ) |
| 170 | + ); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return enumOption ? enumOption.label : currentValue; |
| 175 | +}; |
0 commit comments