Merge pull request #678 from johanjk/main

respect inputOptionLabels
This commit is contained in:
Joseph Garrone 2024-10-02 23:36:23 +02:00 committed by GitHub
commit 616e834c90
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 51 additions and 19 deletions

View File

@ -434,9 +434,7 @@ function AddRemoveButtonsMultiValuedAttribute(props: {
} }
function InputTagSelects(props: InputFieldByTypeProps) { function InputTagSelects(props: InputFieldByTypeProps) {
const { attribute, dispatchFormAction, kcClsx, valueOrValues } = props; const { attribute, dispatchFormAction, kcClsx, i18n, valueOrValues } = props;
const { advancedMsg } = props.i18n;
const { classDiv, classInput, classLabel, inputType } = (() => { const { classDiv, classInput, classLabel, inputType } = (() => {
const { inputType } = attribute.annotations; const { inputType } = attribute.annotations;
@ -533,7 +531,7 @@ function InputTagSelects(props: InputFieldByTypeProps) {
htmlFor={`${attribute.name}-${option}`} htmlFor={`${attribute.name}-${option}`}
className={`${classLabel}${attribute.readOnly ? ` ${kcClsx("kcInputClassRadioCheckboxLabelDisabled")}` : ""}`} className={`${classLabel}${attribute.readOnly ? ` ${kcClsx("kcInputClassRadioCheckboxLabelDisabled")}` : ""}`}
> >
{advancedMsg(option)} {inputLabel(i18n, attribute, option)}
</label> </label>
</div> </div>
))} ))}
@ -580,8 +578,6 @@ function TextareaTag(props: InputFieldByTypeProps) {
function SelectTag(props: InputFieldByTypeProps) { function SelectTag(props: InputFieldByTypeProps) {
const { attribute, dispatchFormAction, kcClsx, displayableErrors, i18n, valueOrValues } = props; const { attribute, dispatchFormAction, kcClsx, displayableErrors, i18n, valueOrValues } = props;
const { advancedMsgStr } = i18n;
const isMultiple = attribute.annotations.inputType === "multiselect"; const isMultiple = attribute.annotations.inputType === "multiselect";
return ( return (
@ -645,22 +641,26 @@ function SelectTag(props: InputFieldByTypeProps) {
return options.map(option => ( return options.map(option => (
<option key={option} value={option}> <option key={option} value={option}>
{(() => { {inputLabel(i18n, attribute, option)}
if (attribute.annotations.inputOptionLabels !== undefined) {
const { inputOptionLabels } = attribute.annotations;
return advancedMsgStr(inputOptionLabels[option] ?? option);
}
if (attribute.annotations.inputOptionLabelsI18nPrefix !== undefined) {
return advancedMsgStr(`${attribute.annotations.inputOptionLabelsI18nPrefix}.${option}`);
}
return option;
})()}
</option> </option>
)); ));
})()} })()}
</select> </select>
); );
} }
function inputLabel(i18n: I18n, attribute: Attribute, option: string) {
const { advancedMsg } = i18n;
if (attribute.annotations.inputOptionLabels !== undefined) {
const { inputOptionLabels } = attribute.annotations;
return advancedMsg(inputOptionLabels[option] ?? option);
}
if (attribute.annotations.inputOptionLabelsI18nPrefix !== undefined) {
return advancedMsg(`${attribute.annotations.inputOptionLabelsI18nPrefix}.${option}`);
}
return option;
}

View File

@ -115,6 +115,38 @@ export const WithFavoritePet: Story = {
) )
}; };
export const WithNewsletter: Story = {
render: () => (
<KcPageStory
kcContext={{
profile: {
attributesByName: {
newsletter: {
name: "newsletter",
displayName: "Sign up to the newsletter",
validators: {
options: {
options: ["yes"]
}
},
annotations: {
inputOptionLabels: {
"yes": "I want my email inbox filled with spam"
},
inputType: "multiselect-checkboxes"
},
required: false,
readOnly: false
} satisfies Attribute
}
},
}}
/>
)
};
export const WithEmailAsUsername: Story = { export const WithEmailAsUsername: Story = {
render: () => ( render: () => (
<KcPageStory <KcPageStory