Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slider onchange event improvement #4921

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 18 additions & 21 deletions app/packages/core/src/plugins/SchemaIO/components/SliderView.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React, { useEffect, useMemo, useRef, useState } from "react";
import {
Box,
FormControl,
Expand All @@ -9,10 +8,11 @@ import {
TextField,
Typography,
} from "@mui/material";
import FieldWrapper from "./FieldWrapper";
import { autoFocus, getComponentProps } from "../utils";
import { useKey } from "../hooks";
import { isNumber } from "lodash";
import React, { useEffect, useMemo, useRef, useState } from "react";
import { useKey } from "../hooks";
import { autoFocus, getComponentProps } from "../utils";
import FieldWrapper from "./FieldWrapper";

type ValueFormat = "flt" | "%";

Expand Down Expand Up @@ -156,6 +156,18 @@ export default function SliderView(props) {
}
};

// Update the UI immediately during sliding
const handleSliderChange = (_, value: number) => {
setMinText(valueLabelFormat(value[0], min, max, unit, valuePrecision));
setMaxText(valueLabelFormat(value[1], min, max, unit, valuePrecision));
};

// Trigger actual onChange when the slider is released
const handleSliderCommit = (_, value: number) => {
onChange(path, value, schema);
setUserChanged();
};

const UnitSelection = useMemo(
() => (
<FormControl variant="outlined">
Expand Down Expand Up @@ -224,23 +236,8 @@ export default function SliderView(props) {
valueLabelFormat={(value) =>
valueLabelFormat(value, min, max, unit, valuePrecision, false)
}
onChange={(_, value: number) => {
onChange(
path,
type === "number" ? parseFloat(value) : value,
schema
);
setUserChanged();

if (variant === "withInputs") {
setMinText(
valueLabelFormat(value[0], min, max, unit, valuePrecision)
);
setMaxText(
valueLabelFormat(value[1], min, max, unit, valuePrecision)
);
}
}}
onChange={handleSliderChange}
onChangeCommitted={handleSliderCommit}
ref={sliderRef}
{...getComponentProps(props, "slider")}
/>
Expand Down
Loading