Skip to content

Commit

Permalink
Fix numeric value detection again
Browse files Browse the repository at this point in the history
  • Loading branch information
WithoutPants committed Nov 29, 2024
1 parent f0f448f commit 1e6ad46
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions ui/v2.5/src/components/Shared/CustomFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,16 @@ export const CustomFields: React.FC<ICustomFields> = ({ values }) => {
};

function isNumeric(v: string) {
return /^-?[0-9]+(?:\.[0-9]+)?$/.test(v);
return /^-?(?:0|(?:[1-9][0-9]*))(?:\.[0-9]+)?$/.test(v);
}

function convertCustomValue(v: string) {
// if the value is numeric, convert it to a number
if (isNumeric(v)) {
return Number(v);
} else {
return v;
}
}

const CustomFieldInput: React.FC<{
Expand All @@ -84,33 +93,24 @@ const CustomFieldInput: React.FC<{
}> = ({ field, value, onChange, isNew = false, error }) => {
const intl = useIntl();
const [currentField, setCurrentField] = useState(field);
const [currentValue, setCurrentValue] = useState(value);
const [currentValue, setCurrentValue] = useState(value as string);

const fieldRef = useRef<HTMLInputElement>(null);
const valueRef = useRef<HTMLInputElement>(null);

useEffect(() => {
setCurrentField(field);
setCurrentValue(value);
setCurrentValue(value as string);
}, [field, value]);

function onBlur() {
onChange(currentField, currentValue);
onChange(currentField, convertCustomValue(currentValue));
}

function onDelete() {
onChange("", "");
}

function onValueChanged(v: string) {
// if the value is numeric, convert it to a number
if (isNumeric(v)) {
setCurrentValue(Number(v));
} else {
setCurrentValue(v);
}
}

return (
<FormGroup>
<Row className={cx("custom-fields-row", { "custom-fields-new": isNew })}>
Expand All @@ -121,7 +121,7 @@ const CustomFieldInput: React.FC<{
ref={fieldRef}
className="input-control"
type="text"
value={(currentField as string) ?? ""}
value={currentField ?? ""}
placeholder={intl.formatMessage({ id: "custom_fields.field" })}
onChange={(event) => setCurrentField(event.currentTarget.value)}
onBlur={onBlur}
Expand All @@ -139,7 +139,7 @@ const CustomFieldInput: React.FC<{
type="text"
value={(currentValue as string) ?? ""}
placeholder={currentField}
onChange={(event) => onValueChanged(event.currentTarget.value)}
onChange={(event) => setCurrentValue(event.currentTarget.value)}
onBlur={onBlur}
/>
<InputGroup.Append>
Expand Down

0 comments on commit 1e6ad46

Please sign in to comment.