Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added choice radio button handling #1798

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/controls/dynamicForm/DynamicForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import "@pnp/sp/content-types";
import "@pnp/sp/folders";
import "@pnp/sp/items";
import { IFolder } from "@pnp/sp/folders";
import { IInstalledLanguageInfo, IItemUpdateResult, IList, ITermInfo } from "@pnp/sp/presets/all";
import { IInstalledLanguageInfo, IItemUpdateResult, IList, ITermInfo, ChoiceFieldFormatType } from "@pnp/sp/presets/all";
import { cloneDeep, isEqual } from "lodash";
import { ICustomFormatting, ICustomFormattingBodySection, ICustomFormattingNode } from "../../common/utilities/ICustomFormatting";
import SPservice from "../../services/SPService";
Expand Down Expand Up @@ -1148,6 +1148,7 @@ export class DynamicFormBase extends React.Component<
let showAsPercentage: boolean | undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const selectedTags: any = [];
let choiceType: ChoiceFieldFormatType | undefined;

let fieldName = field.InternalName;
if (fieldName.startsWith('_x') || fieldName.startsWith('_')) {
Expand Down Expand Up @@ -1461,6 +1462,7 @@ export class DynamicFormBase extends React.Component<
showAsPercentage: showAsPercentage,
customIcon: customIcons ? customIcons[field.InternalName] : undefined,
useModernTaxonomyPickerControl: useModernTaxonomyPicker,
choiceType: choiceType
});

// This may not be necessary now using RenderListDataAsStream
Expand Down
48 changes: 38 additions & 10 deletions src/controls/dynamicForm/dynamicField/DynamicField.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '@pnp/sp/folders';
import { sp } from '@pnp/sp/presets/all';
import { ChoiceFieldFormatType, sp } from '@pnp/sp/presets/all';
import '@pnp/sp/webs';
import * as strings from 'ControlStrings';
import { ActionButton } from '@fluentui/react/lib/Button';
Expand All @@ -23,11 +23,10 @@ import { IDynamicFieldProps, IDynamicFieldStyleProps, IDynamicFieldStyles } from
import { IDynamicFieldState } from './IDynamicFieldState';
import CurrencyMap from "../CurrencyMap";
import { ModernTaxonomyPicker } from '../../modernTaxonomyPicker';
import { classNamesFunction, IProcessedStyleSet, styled } from '@fluentui/react';
import { classNamesFunction, IProcessedStyleSet, styled, ChoiceGroup, IChoiceGroupOption } from '@fluentui/react';
import { getFluentUIThemeOrDefault } from '../../../common/utilities/ThemeUtility';
import { getFieldStyles } from './DynamicField.styles';


const getClassNames = classNamesFunction<IDynamicFieldStyleProps, IDynamicFieldStyles>();

export class DynamicFieldBase extends React.Component<IDynamicFieldProps, IDynamicFieldState> {
Expand Down Expand Up @@ -95,6 +94,7 @@ export class DynamicFieldBase extends React.Component<IDynamicFieldProps, IDynam
itemsQueryCountLimit,
customIcon,
orderBy,
choiceType,
useModernTaxonomyPickerControl
} = this.props;

Expand Down Expand Up @@ -191,18 +191,46 @@ export class DynamicFieldBase extends React.Component<IDynamicFieldProps, IDynam
}

case 'Choice':
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
case 'Choice':
case 'Choice': {

return <div className={styles.fieldContainer}>
<div className={`${styles.labelContainer} ${styles.titleContainer}`}>
<Icon className={styles.fieldIcon} iconName={customIcon ?? "CheckMark"} />
{labelEl}
</div>
<Dropdown
let choiceControl: any = undefined;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let choiceControl: any = undefined;
let choiceControl: JSX.Element = undefined;


// If the choiceType is dropdown
if (choiceType === ChoiceFieldFormatType.Dropdown) {
choiceControl = <Dropdown
{...dropdownOptions}
defaultSelectedKey={valueToDisplay ? undefined : defaultValue}
selectedKey={typeof valueToDisplay === "object" ? valueToDisplay?.key : valueToDisplay}
onChange={(e, option) => { this.onChange(option, true); }}
onBlur={this.onBlur}
errorMessage={errorText} />
errorMessage={errorText} />;
}
// If the choiceType is radio buttons
else if (choiceType === ChoiceFieldFormatType.RadioButtons) {
// Parse options into radio buttons
const optionsGroup: IChoiceGroupOption[] =
options.map((option) => {
return {
key: option.key.toString(),
text: option.text,
checked: option.key.toString() === valueToDisplay
};
});

// Create radio group
choiceControl = <ChoiceGroup
defaultSelectedKey={valueToDisplay ? undefined : defaultValue}
selectedKey={typeof valueToDisplay === "object" ? valueToDisplay?.key : valueToDisplay}
options={optionsGroup}
onChange={(e, option) => { this.onChange(option, true); }}
disabled={disabled}
/>;
}

return <div className={styles.fieldContainer}>
<div className={`${styles.labelContainer} ${styles.titleContainer}`}>
<Icon className={styles.fieldIcon} iconName={customIcon ?? "CheckMark"} />
{labelEl}
</div>
{choiceControl}
{descriptionEl}
</div>;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}

Expand Down
2 changes: 2 additions & 0 deletions src/controls/dynamicForm/dynamicField/IDynamicFieldProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BaseComponentContext } from '@microsoft/sp-component-base';
import { IDropdownOption } from "@fluentui/react/lib/Dropdown";
import { IStyle, IStyleFunctionOrObject, Theme } from '@fluentui/react';
import { IFilePickerResult } from '../../filePicker';
import { ChoiceFieldFormatType } from '@pnp/sp/fields';

export type DateFormat = 'DateTime' | 'DateOnly';
export type FieldChangeAdditionalData = IFilePickerResult;
Expand Down Expand Up @@ -96,6 +97,7 @@ export interface IDynamicFieldProps {
itemsQueryCountLimit?: number;
customIcon?: string;
orderBy?: string;
choiceType?: ChoiceFieldFormatType;
/** Used for customize component styling */
styles?:IStyleFunctionOrObject<IDynamicFieldStyleProps, IDynamicFieldStyles>;
}
Expand Down