({ option }: { option: SettingsOption })
| 515 | } |
| 516 | |
| 517 | function EditOptionNumberField({ option }: { option: SettingsOption }) { |
| 518 | const inputRef = useRef<HTMLInputElement | null>(null) |
| 519 | const { value: rawValue, setValue, error } = useSettingsField(option.name) |
| 520 | const value = textFieldValue(rawValue?.value, option) |
| 521 | |
| 522 | if (option.kind.kind !== "float" |
| 523 | && option.kind.kind !== "integer" |
| 524 | && option.kind.kind !== "integer64") { |
| 525 | throw new Error("not a number field") |
| 526 | } |
| 527 | |
| 528 | const isInteger = option.kind.kind === "integer64" |
| 529 | || option.kind.kind === "integer" |
| 530 | |
| 531 | useEffect(() => { |
| 532 | if (!inputRef.current) { |
| 533 | return |
| 534 | } |
| 535 | |
| 536 | if ((value?.toString() ?? "") !== inputRef.current.value) { |
| 537 | inputRef.current.value = value?.toString() ?? "" |
| 538 | } |
| 539 | }, [value, inputRef]) |
| 540 | |
| 541 | return <Box mt={3}> |
| 542 | <TextField |
| 543 | id={"script-option-" + option.name} |
| 544 | label={option.label} |
| 545 | // defaultValue="Default Value" |
| 546 | type={"number"} |
| 547 | inputProps={{ |
| 548 | step: isInteger ? 1 : undefined, |
| 549 | min: option.kind.min, |
| 550 | max: option.kind.max, |
| 551 | }} |
| 552 | inputRef={inputRef} |
| 553 | helperText={<FieldHelpText description={option.description} error={error} />} |
| 554 | onBlur={(evt) => { |
| 555 | const newValue = evt.target.value |
| 556 | if (evt.target.value === "") { |
| 557 | setValue(null) |
| 558 | evt.target.value = "" |
| 559 | } else { |
| 560 | if (option.kind.kind === "float") { |
| 561 | const parsed = parseFloat(newValue) |
| 562 | const realNewValue = isNaN(parsed) ? 0 : parsed |
| 563 | setValue(realNewValue) |
| 564 | } else if (option.kind.kind === "integer") { |
| 565 | const parsed = parseFloat(newValue.replaceAll(".", "")) |
| 566 | const realNewValue = isNaN(parsed) ? 0 : parsed |
| 567 | setValue(realNewValue) |
| 568 | } else { |
| 569 | setValue(newValue) |
| 570 | } |
| 571 | |
| 572 | |
| 573 | } |
| 574 | }} |
nothing calls this directly
no test coverage detected