Skip to content

Commit 9f91e24

Browse files
Added choice radio button handling (#1798)
* Added choice radio button handling * Removed duplicate label * Update src/controls/dynamicForm/dynamicField/DynamicField.tsx Co-authored-by: Michaël Maillot <[email protected]> * Update src/controls/dynamicForm/dynamicField/DynamicField.tsx Co-authored-by: Michaël Maillot <[email protected]> * Update src/controls/dynamicForm/dynamicField/DynamicField.tsx Co-authored-by: Michaël Maillot <[email protected]> * Updated choice field initialization * Update src/controls/dynamicForm/dynamicField/DynamicField.tsx Co-authored-by: Michaël Maillot <[email protected]> * Updated condition --------- Co-authored-by: Michaël Maillot <[email protected]>
1 parent 9cb498f commit 9f91e24

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

src/controls/dynamicForm/DynamicForm.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import "@pnp/sp/content-types";
3333
import "@pnp/sp/folders";
3434
import "@pnp/sp/items";
3535
import { IFolder } from "@pnp/sp/folders";
36-
import { IInstalledLanguageInfo, IItemUpdateResult, IList, ITermInfo } from "@pnp/sp/presets/all";
36+
import { IInstalledLanguageInfo, IItemUpdateResult, IList, ITermInfo, ChoiceFieldFormatType } from "@pnp/sp/presets/all";
3737
import { cloneDeep, isEqual } from "lodash";
3838
import { ICustomFormatting, ICustomFormattingBodySection, ICustomFormattingNode } from "../../common/utilities/ICustomFormatting";
3939
import SPservice from "../../services/SPService";
@@ -1148,6 +1148,7 @@ export class DynamicFormBase extends React.Component<
11481148
let showAsPercentage: boolean | undefined;
11491149
// eslint-disable-next-line @typescript-eslint/no-explicit-any
11501150
const selectedTags: any = [];
1151+
let choiceType: ChoiceFieldFormatType | undefined;
11511152

11521153
let fieldName = field.InternalName;
11531154
if (fieldName.startsWith('_x') || fieldName.startsWith('_')) {
@@ -1167,11 +1168,20 @@ export class DynamicFormBase extends React.Component<
11671168
field.Choices.forEach((element) => {
11681169
choices.push({ key: element, text: element });
11691170
});
1171+
1172+
if (field.FormatType === 1) {
1173+
choiceType = ChoiceFieldFormatType.RadioButtons;
1174+
}
1175+
else {
1176+
choiceType = ChoiceFieldFormatType.Dropdown;
1177+
}
11701178
}
11711179
if (field.FieldType === "MultiChoice") {
11721180
field.MultiChoices.forEach((element) => {
11731181
choices.push({ key: element, text: element });
11741182
});
1183+
1184+
choiceType = ChoiceFieldFormatType.Dropdown;
11751185
}
11761186

11771187
// Setup Note, Number and Currency fields
@@ -1461,6 +1471,7 @@ export class DynamicFormBase extends React.Component<
14611471
showAsPercentage: showAsPercentage,
14621472
customIcon: customIcons ? customIcons[field.InternalName] : undefined,
14631473
useModernTaxonomyPickerControl: useModernTaxonomyPicker,
1474+
choiceType: choiceType
14641475
});
14651476

14661477
// This may not be necessary now using RenderListDataAsStream

src/controls/dynamicForm/dynamicField/DynamicField.tsx

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import '@pnp/sp/folders';
2-
import { sp } from '@pnp/sp/presets/all';
2+
import { ChoiceFieldFormatType, sp } from '@pnp/sp/presets/all';
33
import '@pnp/sp/webs';
44
import * as strings from 'ControlStrings';
55
import { ActionButton } from '@fluentui/react/lib/Button';
@@ -23,11 +23,10 @@ import { IDynamicFieldProps, IDynamicFieldStyleProps, IDynamicFieldStyles } from
2323
import { IDynamicFieldState } from './IDynamicFieldState';
2424
import CurrencyMap from "../CurrencyMap";
2525
import { ModernTaxonomyPicker } from '../../modernTaxonomyPicker';
26-
import { classNamesFunction, IProcessedStyleSet, styled } from '@fluentui/react';
26+
import { classNamesFunction, IProcessedStyleSet, styled, ChoiceGroup, IChoiceGroupOption } from '@fluentui/react';
2727
import { getFluentUIThemeOrDefault } from '../../../common/utilities/ThemeUtility';
2828
import { getFieldStyles } from './DynamicField.styles';
2929

30-
3130
const getClassNames = classNamesFunction<IDynamicFieldStyleProps, IDynamicFieldStyles>();
3231

3332
export class DynamicFieldBase extends React.Component<IDynamicFieldProps, IDynamicFieldState> {
@@ -95,6 +94,7 @@ export class DynamicFieldBase extends React.Component<IDynamicFieldProps, IDynam
9594
itemsQueryCountLimit,
9695
customIcon,
9796
orderBy,
97+
choiceType,
9898
useModernTaxonomyPickerControl
9999
} = this.props;
100100

@@ -190,22 +190,50 @@ export class DynamicFieldBase extends React.Component<IDynamicFieldProps, IDynam
190190
</div>;
191191
}
192192

193-
case 'Choice':
194-
return <div className={styles.fieldContainer}>
195-
<div className={`${styles.labelContainer} ${styles.titleContainer}`}>
196-
<Icon className={styles.fieldIcon} iconName={customIcon ?? "CheckMark"} />
197-
{labelEl}
198-
</div>
199-
<Dropdown
193+
case 'Choice': {
194+
let choiceControl: JSX.Element = undefined;
195+
196+
// If the choiceType is dropdown
197+
if (choiceType === ChoiceFieldFormatType.Dropdown) {
198+
choiceControl = <Dropdown
200199
{...dropdownOptions}
201200
defaultSelectedKey={valueToDisplay ? undefined : defaultValue}
202201
selectedKey={typeof valueToDisplay === "object" ? valueToDisplay?.key : valueToDisplay}
203202
onChange={(e, option) => { this.onChange(option, true); }}
204203
onBlur={this.onBlur}
205-
errorMessage={errorText} />
204+
errorMessage={errorText} />;
205+
}
206+
// If the choiceType is radio buttons
207+
else {
208+
// Parse options into radio buttons
209+
const optionsGroup: IChoiceGroupOption[] =
210+
options.map((option) => {
211+
return {
212+
key: option.key.toString(),
213+
text: option.text,
214+
checked: option.key.toString() === valueToDisplay
215+
};
216+
});
217+
218+
// Create radio group
219+
choiceControl = <ChoiceGroup
220+
defaultSelectedKey={valueToDisplay ? undefined : defaultValue}
221+
selectedKey={typeof valueToDisplay === "object" ? valueToDisplay?.key : valueToDisplay}
222+
options={optionsGroup}
223+
onChange={(e, option) => { this.onChange(option, true); }}
224+
disabled={disabled}
225+
/>;
226+
}
227+
228+
return <div className={styles.fieldContainer}>
229+
<div className={`${styles.labelContainer} ${styles.titleContainer}`}>
230+
<Icon className={styles.fieldIcon} iconName={customIcon ?? "CheckMark"} />
231+
{labelEl}
232+
</div>
233+
{choiceControl}
206234
{descriptionEl}
207235
</div>;
208-
236+
}
209237
case 'MultiChoice':
210238
return <div className={styles.fieldContainer}>
211239
<div className={`${styles.labelContainer} ${styles.titleContainer}`}>

src/controls/dynamicForm/dynamicField/IDynamicFieldProps.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { BaseComponentContext } from '@microsoft/sp-component-base';
22
import { IDropdownOption } from "@fluentui/react/lib/Dropdown";
33
import { IStyle, IStyleFunctionOrObject, Theme } from '@fluentui/react';
44
import { IFilePickerResult } from '../../filePicker';
5+
import { ChoiceFieldFormatType } from '@pnp/sp/fields';
56

67
export type DateFormat = 'DateTime' | 'DateOnly';
78
export type FieldChangeAdditionalData = IFilePickerResult;
@@ -96,6 +97,7 @@ export interface IDynamicFieldProps {
9697
itemsQueryCountLimit?: number;
9798
customIcon?: string;
9899
orderBy?: string;
100+
choiceType?: ChoiceFieldFormatType;
99101
/** Used for customize component styling */
100102
styles?:IStyleFunctionOrObject<IDynamicFieldStyleProps, IDynamicFieldStyles>;
101103
}

0 commit comments

Comments
 (0)