File tree Expand file tree Collapse file tree 5 files changed +56
-15
lines changed Expand file tree Collapse file tree 5 files changed +56
-15
lines changed Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " @devtron-labs/devtron-fe-common-lib" ,
3
- "version" : " 0.2.28 " ,
3
+ "version" : " 0.2.31 " ,
4
4
"description" : " Supporting common component library" ,
5
5
"type" : " module" ,
6
6
"main" : " dist/index.js" ,
Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ import { FieldRowWithLabel } from '../common/FieldRow'
21
21
import { TitleField } from './TitleField'
22
22
import { AddButton } from './ButtonTemplates'
23
23
import { RJSFFormSchema } from '../types'
24
+ import { parseSchemaHiddenType } from '../utils'
24
25
25
26
const Field = ( {
26
27
disabled,
@@ -56,19 +57,19 @@ const Field = ({
56
57
return true
57
58
}
58
59
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' )
61
63
}
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' )
65
66
}
66
67
// NOTE: formContext is the formData passed to RJSFForm
67
68
const value = JSONPath ( {
68
- path,
69
+ path : convertJSONPointerToJSONPath ( hiddenSchema . path ) ,
69
70
json : formContext ,
70
71
} ) ?. [ 0 ]
71
- const isHidden = value === undefined || hiddenSchemaProp . value === value
72
+ const isHidden = value === undefined || hiddenSchema . value === value
72
73
return ! isHidden
73
74
} catch {
74
75
return true
Original file line number Diff line number Diff line change @@ -20,14 +20,20 @@ import { StrictRJSFSchema } from '@rjsf/utils'
20
20
21
21
export type FormProps = Omit < ComponentProps < typeof RJSFForm > , 'validator' >
22
22
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
27
33
28
34
export interface RJSFFormSchema extends StrictRJSFSchema {
29
35
properties : {
30
36
[ key : string ] : RJSFFormSchema
31
37
}
32
- hidden : Hidden
38
+ hidden : HiddenType
33
39
}
Original file line number Diff line number Diff line change 15
15
*/
16
16
17
17
import { TranslatableString , englishStringTranslator } from '@rjsf/utils'
18
+ import { HiddenType } from './types'
18
19
19
20
/**
20
21
* Override for the TranslatableString from RJSF
@@ -135,3 +136,36 @@ export const getInferredTypeFromValueType = (value) => {
135
136
return 'null'
136
137
}
137
138
}
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
+ }
You can’t perform that action at this time.
0 commit comments