-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/MEDITOR-858-bulk-UI
- Loading branch information
Showing
27 changed files
with
521 additions
and
64 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
version: "3.3" | ||
|
||
services: | ||
notebookviewer: | ||
depends_on: | ||
|
This file contains 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 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
44 changes: 44 additions & 0 deletions
44
packages/app/components/jsonschemaform/widgets/AnchorWidget.tsx
This file contains 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 @@ | ||
import type { WidgetProps } from '@rjsf/utils' | ||
import { useEffect, useState } from 'react' | ||
import { findAndReplace, macros } from './utils' | ||
|
||
/** | ||
* Schema Layout: | ||
"MyProperty": { | ||
"ui:widget": "anchor", | ||
"ui:options": { | ||
"href": "http://localhost:8080/service-request-from-config?variable_entry_id=******", | ||
"text": "", | ||
"change": { | ||
"every": "***", | ||
"to": ["FIELD_LOOKUP:EntryID", "FIELD_LOOKUP:VariableEntryID"] // replace with no regex, not replaceAll, is used. This means you can stack "every" to replace per "to" field. | ||
} | ||
} | ||
}, | ||
*/ | ||
export default function AnchorWidget({ options }: WidgetProps) { | ||
const [href, setHref] = useState(options.href as string) | ||
|
||
useEffect(() => { | ||
// @ts-expect-error | ||
if (!!options.change.every) { | ||
setHref( | ||
findAndReplace( | ||
options.href as string, | ||
// @ts-expect-error | ||
options.change.every as string, | ||
// @ts-expect-error | ||
options.change.to as string[], | ||
macros | ||
) | ||
) | ||
} | ||
// @ts-expect-error | ||
}, [options.href, options.change.every, options.change.to, setHref]) | ||
|
||
return ( | ||
<a href={href} target="_blank" rel="noopener noreferrer"> | ||
{options.text || href} | ||
</a> | ||
) | ||
} |
This file contains 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
2 changes: 1 addition & 1 deletion
2
packages/app/components/jsonschemaform/widgets/ConcatenatedWidget.tsx
This file contains 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 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
97 changes: 97 additions & 0 deletions
97
packages/app/components/jsonschemaform/widgets/__tests__/anchor-widget.test.ts
This file contains 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,97 @@ | ||
import { findAndReplace } from '../utils' | ||
|
||
/** | ||
"href": "http://localhost:8080/service-request-from-config?variable_entry_id=******", | ||
"text": "", | ||
"change": { | ||
"every": "***", | ||
"to": ["FIELD_LOOKUP:EntryID", "FIELD_LOOKUP:VariableEntryID"] // replace with no regex, not replaceAll, is used. This means you can stack "every" to replace per "to" field. | ||
} | ||
*/ | ||
describe(`RJSF Widgets`, () => { | ||
describe(`findAndReplace`, () => { | ||
const sourceString = | ||
'http://localhost:8080/service-request-from-config?variable_entry_id=***' | ||
const every = '***' | ||
const to = ['a-variable-entry-id'] | ||
const macros = { | ||
FIELD_LOOKUP: () => 'this value was returned', | ||
} as const | ||
|
||
test(`replaces a single instance with a single value, no macro`, () => { | ||
const value = findAndReplace(sourceString, every, to, macros) | ||
|
||
expect(value).toMatchInlineSnapshot( | ||
`"http://localhost:8080/service-request-from-config?variable_entry_id=a-variable-entry-id"` | ||
) | ||
}) | ||
|
||
test(`can repeat multiple replacement for multiple matches`, () => { | ||
const sourceString = | ||
'http://localhost:***/service-request-from-config?variable_entry_id=******' | ||
const to = ['8080', 'foo', 'bar'] | ||
const value = findAndReplace(sourceString, every, to, macros) | ||
|
||
expect(value).toMatchInlineSnapshot( | ||
`"http://localhost:8080/service-request-from-config?variable_entry_id=foobar"` | ||
) | ||
}) | ||
|
||
test(`can use macros`, () => { | ||
// Our FIELD_LOOKUP macro does nothing but return a string, unlike the real macro. | ||
const to = ['FIELD_LOOKUP'] | ||
const value = findAndReplace(sourceString, every, to, macros) | ||
|
||
expect(value).toMatchInlineSnapshot( | ||
`"http://localhost:8080/service-request-from-config?variable_entry_id=this value was returned"` | ||
) | ||
}) | ||
|
||
test(`will throw if macro does not have a handler`, () => { | ||
const to = ['NOT_A_HANDLED_MACRO'] | ||
const macros = { | ||
NOT_A_HANDLED_MACRO: () => `this doesn't have a handler`, | ||
} as const | ||
|
||
expect(() => { | ||
findAndReplace(sourceString, every, to, macros) | ||
}).toThrowErrorMatchingInlineSnapshot( | ||
`"Handler does not yet have a case to handle this macro: NOT_A_HANDLED_MACRO"` | ||
) | ||
}) | ||
|
||
test(`will match per "to" entry found, but no further`, () => { | ||
const value = findAndReplace(sourceString, every, to, macros) | ||
|
||
expect(value).toMatchInlineSnapshot( | ||
`"http://localhost:8080/service-request-from-config?variable_entry_id=a-variable-entry-id"` | ||
) | ||
}) | ||
|
||
test(`will not replace on empty "to" arrays`, () => { | ||
const to = [] | ||
const value = findAndReplace(sourceString, every, to, macros) | ||
|
||
expect(value).toMatchInlineSnapshot( | ||
`"http://localhost:8080/service-request-from-config?variable_entry_id=***"` | ||
) | ||
}) | ||
|
||
test(`will not replace on empty "every" string`, () => { | ||
const every = '' | ||
const value = findAndReplace(sourceString, every, to, macros) | ||
|
||
expect(value).toMatchInlineSnapshot( | ||
`"a-variable-entry-idhttp://localhost:8080/service-request-from-config?variable_entry_id=***"` | ||
) | ||
}) | ||
|
||
test(`returns unmodified source string when no matches are found`, () => { | ||
const sourceString = | ||
'http://localhost:8080/service-request-from-config?variable_entry_id=^^^' | ||
const value = findAndReplace(sourceString, every, to, macros) | ||
|
||
expect(value).toEqual(sourceString) | ||
}) | ||
}) | ||
}) |
This file contains 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,3 @@ | ||
const ID_PREFIX = 'root_' | ||
|
||
export { ID_PREFIX } |
This file contains 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 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,47 @@ | ||
import { ID_PREFIX } from './constants' | ||
|
||
const macros = { | ||
FIELD_LOOKUP: (name: string) => { | ||
let el = document.getElementById(`${ID_PREFIX}${name}`) as HTMLInputElement | ||
// This may be run before the element has been rendered to the DOM, so don't return `undefined`. | ||
const value = el.value ?? '' | ||
|
||
return globalThis.encodeURIComponent(value) | ||
}, | ||
} as const | ||
|
||
function findAndReplace( | ||
sourceString: string = '', | ||
every: string = '', | ||
to: string[] = [], | ||
macros: Record<string, Function> = {} | ||
) { | ||
// Starting from the source string, iterate through every entry in "to", replacing with either a macro's return value or a value. | ||
return to.reduce((accumulator, current) => { | ||
// We may have a MACRO_NAME:FieldName combo, or we may have a value. | ||
const [macroNameOrValue, maybeFieldName] = current.split(':') | ||
const maybeMacro = macros[macroNameOrValue] | ||
|
||
if (maybeMacro) { | ||
// We know we have a macro now. | ||
const macroName = macroNameOrValue | ||
const fieldName = maybeFieldName | ||
|
||
switch (macroName) { | ||
case 'FIELD_LOOKUP': | ||
const replacementValue = macros[macroName](fieldName) | ||
|
||
return accumulator.replace(every, replacementValue) | ||
|
||
default: | ||
throw Error( | ||
`Handler does not yet have a case to handle this macro: ${macroName}` | ||
) | ||
} | ||
} else { | ||
return accumulator.replace(every, current) | ||
} | ||
}, sourceString) | ||
} | ||
|
||
export { findAndReplace, macros } |
This file contains 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
Oops, something went wrong.