-
Notifications
You must be signed in to change notification settings - Fork 403
New Dynamic Form Feature(s) - Custom Formatting and Validation, ControlsTestWebPart updates #1672
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5db10f8
Refactor DynamicForm to use RenderListDataAsStream
d42db42
Amended ControlsTest config propertyPane
d9281fa
Added form validation to Dynamic Form
f02dbb8
Implemented Custom Formatting for Dynamic Form body
d5b0f42
Refactor of DynamicForm, DynamicField, ControlsTestWebPart, added exp…
a9577b7
Added tests and fixes.
10095b9
Fixes to desired functionality
e4cf220
Applied recommended changes following review
8cbac7d
Merge branch 'dev' into dynamic-form-list-customization
e13cff8
Fixes and cleanup following merge from 'dev'
801bb06
Merge branch 'dev' into dynamic-form-list-customization
08ac2e8
Fixes and cleanup following merge with 'dev'
62e483e
Amendment of FormulaEvaluation and CustomFormatting classes to reduce…
ee7931d
Merge branch 'dev' into dynamic-form-list-customization
8d665e8
Merge branch 'dev' into dynamic-form-list-customization
e027b1a
Tweaks to code and test web part to accomodate changes made in #1625
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
/** | ||
* Populate and rename this file to launch.json to configure debugging | ||
*/ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Hosted workbench (chrome)", | ||
"type": "chrome", | ||
"request": "launch", | ||
"url": "https://enter-your-SharePoint-site.sharepoint.com/sites/mySite/_layouts/15/workbench.aspx", | ||
"webRoot": "${workspaceRoot}", | ||
"sourceMaps": true, | ||
"sourceMapPathOverrides": { | ||
"webpack:///.././src/*": "${webRoot}/src/*", | ||
"webpack:///../../../src/*": "${webRoot}/src/*", | ||
"webpack:///../../../../src/*": "${webRoot}/src/*", | ||
"webpack:///../../../../../src/*": "${webRoot}/src/*" | ||
}, | ||
"preLaunchTask": "npm: serve", | ||
"runtimeArgs": [ | ||
"--remote-debugging-port=9222", | ||
] | ||
}, | ||
{ | ||
"name": "Hosted workbench (edge)", | ||
"type": "edge", | ||
"request": "launch", | ||
"url": "https://enter-your-SharePoint-site.sharepoint.com/sites/mySite/_layouts/15/workbench.aspx", | ||
"webRoot": "${workspaceRoot}", | ||
"sourceMaps": true, | ||
"sourceMapPathOverrides": { | ||
"webpack:///.././src/*": "${webRoot}/src/*", | ||
"webpack:///../../../src/*": "${webRoot}/src/*", | ||
"webpack:///../../../../src/*": "${webRoot}/src/*", | ||
"webpack:///../../../../../src/*": "${webRoot}/src/*" | ||
}, | ||
"preLaunchTask": "npm: serve", | ||
"runtimeArgs": [ | ||
"--remote-debugging-port=9222", | ||
] | ||
}, | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
/** | ||
* Populate and rename this file to launch.json to configure debugging | ||
*/ | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"type": "npm", | ||
"script": "serve", | ||
"isBackground": true, | ||
"problemMatcher": { | ||
"owner": "custom", | ||
"pattern": { | ||
"regexp": "." | ||
}, | ||
"background": { | ||
"activeOnStart": true, | ||
"beginsPattern": "Starting 'bundle'", | ||
"endsPattern": "\\[\\sFinished\\s\\]" | ||
} | ||
}, | ||
"label": "npm: serve", | ||
"detail": "gulp bundle --custom-serve --max_old_space_size=4096 && fast-serve", | ||
"group": { | ||
"kind": "build", | ||
"isDefault": true | ||
} | ||
}, | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import * as React from "react"; | ||
import { Icon } from "@fluentui/react/lib/Icon"; | ||
import { FormulaEvaluation } from "./FormulaEvaluation"; | ||
import { ASTNode, Context } from "./FormulaEvaluation.types"; | ||
import { ICustomFormattingExpressionNode, ICustomFormattingNode } from "./ICustomFormatting"; | ||
|
||
type CustomFormatResult = string | number | boolean | JSX.Element | ICustomFormattingNode; | ||
|
||
/** | ||
* A class that provides helper methods for custom formatting | ||
* See: https://learn.microsoft.com/en-us/sharepoint/dev/declarative-customization/formatting-syntax-reference | ||
*/ | ||
export default class CustomFormattingHelper { | ||
|
||
private _formulaEvaluator: FormulaEvaluation; | ||
|
||
/** | ||
* Custom Formatting Helper / Renderer | ||
* @param formulaEvaluator An instance of FormulaEvaluation used for evaluating expressions in custom formatting | ||
*/ | ||
constructor(formulaEvaluator: FormulaEvaluation) { | ||
this._formulaEvaluator = formulaEvaluator; | ||
} | ||
|
||
/** | ||
* The Formula Evaluator expects an ASTNode to be passed to it for evaluation. This method converts expressions | ||
* described by the interface ICustomFormattingExpressionNode to ASTNodes. | ||
* @param node An ICustomFormattingExpressionNode to be converted to an ASTNode | ||
*/ | ||
private convertCustomFormatExpressionNodes = (node: ICustomFormattingExpressionNode | string | number | boolean): ASTNode => { | ||
if (typeof node !== "object") { | ||
switch (typeof node) { | ||
case "string": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can combine both |
||
return { type: "string", value: node }; | ||
case "number": | ||
return { type: "number", value: node }; | ||
case "boolean": | ||
return { type: "booelan", value: node ? 1 : 0 }; | ||
} | ||
} | ||
const operator = node.operator; | ||
const operands = node.operands.map(o => this.convertCustomFormatExpressionNodes(o)); | ||
return { type: "operator", value: operator, operands }; | ||
} | ||
|
||
/** | ||
* Given a single custom formatting expression, node or element, this method evaluates the expression and returns the result | ||
* @param content An object, expression or literal value to be evaluated | ||
* @param context A context object containing values / variables to be used in the evaluation | ||
* @returns | ||
*/ | ||
private evaluateCustomFormatContent = (content: ICustomFormattingExpressionNode | ICustomFormattingNode | string | number | boolean, context: Context): CustomFormatResult => { | ||
|
||
// If content is a string or number, it is a literal value and should be returned as-is | ||
if ((typeof content === "string" && content.charAt(0) !== "=") || typeof content === "number") return content; | ||
|
||
// If content is a string beginning with '=' it is a formula/expression, and should be evaluated | ||
if (typeof content === "string" && content.charAt(0) === "=") { | ||
const result = this._formulaEvaluator.evaluate(content.substring(1), context); | ||
return result as CustomFormatResult; | ||
} | ||
|
||
// If content is an object, it is either further custom formatting described by an ICustomFormattingNode, | ||
// or an expression to be evaluated - as described by an ICustomFormattingExpressionNode | ||
|
||
if (typeof content === "object") { | ||
|
||
if (Object.prototype.hasOwnProperty.call(content, "elmType")) { | ||
|
||
// Custom Formatting Content | ||
return this.renderCustomFormatContent(content as ICustomFormattingNode, context); | ||
|
||
} else if (Object.prototype.hasOwnProperty.call(content, "operator")) { | ||
|
||
// Expression to be evaluated | ||
const astNode = this.convertCustomFormatExpressionNodes(content as ICustomFormattingExpressionNode); | ||
const result = this._formulaEvaluator.evaluateASTNode(astNode, context); | ||
if (typeof result === "object" && Object.prototype.hasOwnProperty.call(result, "elmType")) { | ||
return this.renderCustomFormatContent(result as ICustomFormattingNode, context); | ||
} | ||
return result as CustomFormatResult; | ||
|
||
} | ||
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
public renderCustomFormatContent = (node: ICustomFormattingNode, context: Context, rootEl: boolean = false): JSX.Element | string | number => { | ||
|
||
// We don't want attempts to render custom format content to kill the component or web part, | ||
// so we wrap the entire method in a try/catch block, log errors and return null if an error occurs | ||
try { | ||
|
||
// If node is a string or number, it is a literal value and should be returned as-is | ||
if (typeof node === "string" || typeof node === "number") return node; | ||
|
||
// Custom formatting nodes / elements may have a txtContent property, which represents the inner | ||
// content of a HTML element. This can be a string literal, or another expression to be evaluated: | ||
let textContent: CustomFormatResult | undefined; | ||
if (node.txtContent) { | ||
textContent = this.evaluateCustomFormatContent(node.txtContent, context); | ||
} | ||
|
||
// Custom formatting nodes / elements may have a style property, which contains the style rules | ||
// to be applied to the resulting HTML element. Rule values can be string literals or another expression | ||
// to be evaluated: | ||
const styleProperties: React.CSSProperties = {}; | ||
if (node.style) { | ||
for (const styleAttribute in node.style) { | ||
if (node.style[styleAttribute]) { | ||
styleProperties[styleAttribute] = this.evaluateCustomFormatContent(node.style[styleAttribute], context) as string; | ||
} | ||
} | ||
} | ||
|
||
// Custom formatting nodes / elements may have an attributes property, which represents the HTML attributes | ||
// to be applied to the resulting HTML element. Attribute values can be string literals or another expression | ||
// to be evaluated: | ||
const attributes = {} as Record<string, string>; | ||
if (node.attributes) { | ||
for (const attribute in node.attributes) { | ||
if (node.attributes[attribute]) { | ||
let attributeName = attribute; | ||
|
||
// Because we're using React to render the HTML content, we need to rename the 'class' attribute | ||
if (attributeName === "class") attributeName = "className"; | ||
|
||
// Evaluation | ||
attributes[attributeName] = this.evaluateCustomFormatContent(node.attributes[attribute], context) as string; | ||
|
||
// Add the 'sp-field-customFormatter' class to the root element | ||
if (attributeName === "className" && rootEl) { | ||
attributes[attributeName] = `${attributes[attributeName]} sp-field-customFormatter`; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Custom formatting nodes / elements may have children. These are likely to be further custom formatting | ||
let children: (CustomFormatResult)[] = []; | ||
|
||
// If the node has an iconName property, we'll render an Icon component as the first child. | ||
// SharePoint uses CSS to apply the icon in a ::before rule, but we can't count on the global selector for iconName | ||
// being present on the page, so we'll add it as a child instead: | ||
if (attributes.iconName) { | ||
const icon = React.createElement(Icon, { iconName: attributes.iconName }); | ||
children.push(icon); | ||
} | ||
|
||
// Each child object is evaluated recursively and added to the children array | ||
if (node.children) { | ||
children = node.children.map(c => this.evaluateCustomFormatContent(c, context)); | ||
} | ||
|
||
// The resulting HTML element is returned to the callee using React.createElement | ||
const el = React.createElement(node.elmType, { style: styleProperties, ...attributes }, textContent, ...children); | ||
return el; | ||
} catch (error) { | ||
console.error('Unable to render custom formatted content', error); | ||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.