Skip to content

Commit 3d0e2e6

Browse files
committed
feat: add support for other hidden types
1 parent 539077c commit 3d0e2e6

File tree

5 files changed

+56
-15
lines changed

5 files changed

+56
-15
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "0.2.28",
3+
"version": "0.2.31",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Common/RJSF/templates/ObjectFieldTemplate.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { FieldRowWithLabel } from '../common/FieldRow'
2121
import { TitleField } from './TitleField'
2222
import { AddButton } from './ButtonTemplates'
2323
import { RJSFFormSchema } from '../types'
24+
import { parseSchemaHiddenType } from '../utils'
2425

2526
const Field = ({
2627
disabled,
@@ -56,19 +57,19 @@ const Field = ({
5657
return true
5758
}
5859
try {
59-
if (!hiddenSchemaProp.path) {
60-
throw new Error('Empty path element')
60+
const hiddenSchema = parseSchemaHiddenType(hiddenSchemaProp)
61+
if (!hiddenSchema.path) {
62+
throw new Error('Empty path property of hidden descriptor field')
6163
}
62-
let path = ''
63-
if (hiddenSchemaProp.path.match(/^\w+(\/\w+)*$/g) && hiddenSchemaProp.path.charAt(0) !== '/') {
64-
path = convertJSONPointerToJSONPath(`/${hiddenSchemaProp.path}`)
64+
if (!hiddenSchema.path.match(/^\/\w+(\/\w+)*$/g)) {
65+
throw new Error('Provided path is not a valid JSON pointer')
6566
}
6667
// NOTE: formContext is the formData passed to RJSFForm
6768
const value = JSONPath({
68-
path,
69+
path: convertJSONPointerToJSONPath(hiddenSchema.path),
6970
json: formContext,
7071
})?.[0]
71-
const isHidden = value === undefined || hiddenSchemaProp.value === value
72+
const isHidden = value === undefined || hiddenSchema.value === value
7273
return !isHidden
7374
} catch {
7475
return true

src/Common/RJSF/types.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@ import { StrictRJSFSchema } from '@rjsf/utils'
2020

2121
export type FormProps = Omit<ComponentProps<typeof RJSFForm>, 'validator'>
2222

23-
interface Hidden {
24-
value: boolean
25-
path: string
26-
}
23+
export type HiddenType =
24+
| {
25+
value: any
26+
path: string
27+
}
28+
| {
29+
condition: any
30+
value: string
31+
}
32+
| string
2733

2834
export interface RJSFFormSchema extends StrictRJSFSchema {
2935
properties: {
3036
[key: string]: RJSFFormSchema
3137
}
32-
hidden: Hidden
38+
hidden: HiddenType
3339
}

src/Common/RJSF/utils.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import { TranslatableString, englishStringTranslator } from '@rjsf/utils'
18+
import { HiddenType } from './types'
1819

1920
/**
2021
* Override for the TranslatableString from RJSF
@@ -135,3 +136,36 @@ export const getInferredTypeFromValueType = (value) => {
135136
return 'null'
136137
}
137138
}
139+
140+
const conformPathToPointers = (path: string) => {
141+
const trimmedPath = path.trim()
142+
const isSlashSeparatedPathMissingBeginSlash = trimmedPath.match(/^\w+(\/\w+)*$/g)
143+
if (isSlashSeparatedPathMissingBeginSlash) {
144+
return `/${trimmedPath}`
145+
}
146+
const isDotSeparatedPath = trimmedPath.match(/^\w+(\.\w+)*$/g)
147+
if (isDotSeparatedPath) {
148+
// NOTE: replacing dots with forward slash (/)
149+
return `/${trimmedPath.replaceAll(/\./g, '/')}`
150+
}
151+
return trimmedPath
152+
}
153+
154+
export const parseSchemaHiddenType = (hiddenSchema: HiddenType) => {
155+
if (typeof hiddenSchema === 'string') {
156+
return {
157+
value: false,
158+
path: conformPathToPointers(hiddenSchema),
159+
}
160+
}
161+
if ('condition' in hiddenSchema) {
162+
return {
163+
value: hiddenSchema.condition,
164+
path: conformPathToPointers(hiddenSchema.value),
165+
}
166+
}
167+
return {
168+
...hiddenSchema,
169+
path: conformPathToPointers(hiddenSchema.path),
170+
}
171+
}

0 commit comments

Comments
 (0)