Skip to content

Commit 4c58d82

Browse files
fix(#246): support temporary datatypes on the script form
Also improve numeric field options to distinguish float vs int, integer vs non-neg integer
1 parent 4e36f68 commit 4c58d82

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

src/renderer/components/Fields/CustomField.tsx

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { useWindowStore } from 'renderer/store'
22
import { CustomFieldDocumentation } from './CustomFieldDocumentation'
33
import { ScriptItemBase, TypeChoice, ValueChoice } from 'shared/types'
4-
import { useEffect, useState } from 'react'
4+
import { useEffect, useState, useMemo } from 'react'
55
import { ControlledInput } from './ControlledInput'
66
import { MarkdownDescription } from './MarkdownDescription'
7+
import { fetchTemporaryDatatype } from 'renderer/utils/temp-datatype'
78

89
export function CustomField({
910
item,
@@ -21,6 +22,20 @@ export function CustomField({
2122
const { pipeline } = useWindowStore()
2223
const [value, setValue] = useState(initialValue)
2324
const [userInteracted, setUserInteracted] = useState(false) // false if the user started typing
25+
const [datatype, setDatatype] = useState(
26+
pipeline.datatypes.find((dt) => dt.id == item.type) ?? null
27+
)
28+
29+
useMemo(() => {
30+
const fetchData = async () => {
31+
let datatypeDetails = await fetchTemporaryDatatype(item.type)
32+
// @ts-ignore
33+
setDatatype({ ...datatypeDetails })
34+
}
35+
if (!datatype) {
36+
fetchData().catch()
37+
}
38+
}, [])
2439

2540
useEffect(() => {
2641
const elem = document.getElementById(controlId) as HTMLInputElement
@@ -38,16 +53,13 @@ export function CustomField({
3853
'aria-invalid': false,
3954
}
4055

41-
// find the datatype in the pipeline.datatypes store
42-
let datatype = pipeline.datatypes.find((dt) => dt.id == item.type)
43-
4456
let onChangeValue = (newValue) => {
4557
setUserInteracted(true)
4658
setValue(newValue)
4759
onChange(newValue)
4860
}
4961

50-
if (datatype) {
62+
if (datatype && datatype.choices) {
5163
// if there are value choices, make a dropdown select
5264
let valueChoices = datatype.choices.filter((item) =>
5365
item.hasOwnProperty('value')

src/renderer/components/Fields/FormField.tsx

+25
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,31 @@ export function FormField({
107107
)}
108108
</>
109109
)
110+
} else if (
111+
['nonNegativeInteger', 'float', 'number'].includes(inputType)
112+
) {
113+
return (
114+
<>
115+
<input
116+
type="number"
117+
min={inputType == 'nonNegativeInteger' ? 1 : 'any'}
118+
step={inputType == 'float' ? 0.01 : 1}
119+
required={item.required}
120+
onChange={(e) => onChange(e.target.value, item)}
121+
id={controlId}
122+
defaultValue={initialValue}
123+
></input>
124+
{error && (
125+
<span
126+
id={controlId + '-error'}
127+
className="field-errors"
128+
aria-live="polite"
129+
>
130+
{error}
131+
</span>
132+
)}
133+
</>
134+
)
110135
} else if (inputType == 'custom') {
111136
return (
112137
<CustomField

src/renderer/utils/utils.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function findValue(
2828
isStylesheetParameter: boolean
2929
) {
3030
if (!jobRequest) return ''
31-
let arr = null;
31+
let arr = null
3232
if (kind == 'input') {
3333
arr = jobRequest.inputs
3434
} else {
@@ -65,17 +65,13 @@ export function findInputType(type) {
6565
} else if (['xsd:string', 'xs:string', 'string'].includes(type)) {
6666
inputType = 'text'
6767
} else if (
68-
[
69-
'xsd:integer',
70-
'xsd:float',
71-
'xsd:double',
72-
'xsd:decimal',
73-
'xs:integer',
74-
'integer',
75-
'number',
76-
].includes(type)
68+
['xsd:integer', 'xs:integer', 'integer', 'number'].includes(type)
7769
) {
7870
inputType = 'number'
71+
} else if (type == 'nonNegativeInteger') {
72+
inputType = 'nonNegativeInteger'
73+
} else if (['xsd:float', 'xsd:double', 'xsd:decimal'].includes(type)) {
74+
inputType = 'float'
7975
} else if (type == '') {
8076
inputType = 'text'
8177
} else {

0 commit comments

Comments
 (0)