PromucFlow_constructor/app/client/src/widgets/NumberSliderWidget/utils.ts
Arsalan Yaldram 46b3627bac
feat: implemented slider widgets (#15712)
* feat: implemented slider widget from mantine ui

* fix: remove widget from widgetName

* fix: memoize marks, use primitives for rangeslider value

* fix: use theme accentColor, remove xs and xl size

* fix: renamed slider widget, added searchTags, parity properties

* feat: added labels to the slider widgets.

* fix: Rename SingleSlider components to Slider

* feat: added basic disabled state

* feat: added category slider widget, handled label callback function

* fix: Renamed slider widgets fixed imports and bindings.

* feat: widget redesign, added hover, focus, disabled states.

* feat: added isDirty meta property to the slider widgets.

* feat: Added onChange action triggers to the slider widgets.

* feat: added show marks label property for category widget.

* fix: fixed initial feedback on designs, issues.

* feat: added the new tabbed property pane improvements.

* feat: changes to the slider property panes.

* fix: removed show label on hover and add tooltip always on property.

* fix: slider sizes changed inline with the designs

* feat: added initial logos.

* fix: slider size subtext.

* feat: added show marks to number and range sliders.

* fix: Label will show one decimal point if step Size is in decimal.

* feat: added validations for the slider widgets.

* fix: remove mark dots when show marks is off, change icons.

* fix: addressed validations feedback.

* fix: added stepsize validation number widget & fixed top label aligning.

* fix: Long label styles.

* fix: wrapping removed for long labels, handled focus state.

* fix: remove overflow hidden.

* fix: added pointerEvents none to Marks.

* fix: added default value in the Number Slider widget.

* fix: refactor marks remove un-necessary function calls.

* fix: Review changes done.

* fix: remove transformstyles outside component.

* fix: remove old proprty pane config.

* fix: remove any type from widget files.

* fix: small code refactor validations category slider.

* fix: removed throttle, fixed slider setting to zero issue.

* fix: Added validation for stepSize RangeSlider. Added clamp for nextValue in getChangeValue.

* fix: number and category slider fire events only if value has changed.

* fix: added 0 as default for the min value, range and number sliders.

* fix: remove tooltip always on from category slider.

* fix: minRange validation accept only numbers.

* fix: added old property pane method.
2022-09-22 21:26:50 +05:30

138 lines
2.6 KiB
TypeScript

import { Colors } from "constants/Colors";
import { darkenColor } from "widgets/WidgetUtils";
type Position = {
value: number;
min: number;
max: number;
};
/**
*
* @returns the position of value to be used in the Track component
*/
export function getPosition({ max, min, value }: Position) {
const position = ((value - min) / (max - min)) * 100;
return Math.min(Math.max(position, 0), 100);
}
type ChangeValue = {
value: number;
min: number;
max: number;
step: number;
precision?: number;
/**
* container width is passed in case of RangeSlider
*/
containerWidth?: number;
};
export function getChangeValue({
containerWidth,
max,
min,
step,
value,
}: ChangeValue) {
const left = !containerWidth
? value
: Math.min(Math.max(value, 0), containerWidth) / containerWidth;
const dx = left * (max - min);
const nextValue = (dx !== 0 ? Math.round(dx / step) * step : 0) + min;
return Math.max(Math.min(nextValue, max), min);
}
export function getClientPosition(event: any) {
if ("TouchEvent" in window && event instanceof window.TouchEvent) {
const touch = event.touches[0];
return touch.clientX;
}
return event.clientX;
}
export function clamp(value: number, min: number, max: number) {
return Math.min(Math.max(value, min), max);
}
type MarkedFilled = {
mark: { value: number; label?: any };
offset?: number;
value: number;
};
export function isMarkedFilled({ mark, offset, value }: MarkedFilled) {
return typeof offset === "number"
? mark.value >= offset && mark.value <= value
: mark.value <= value;
}
export const thumbSizeMap = {
s: "12px",
m: "16px",
l: "20px",
};
export const sizeMap = {
s: 4,
m: 6,
l: 8,
};
export type SliderSizes = "s" | "m" | "l";
export const getSliderStyles = ({
color,
disabled,
dragging,
hovering,
}: {
disabled: boolean;
hovering: boolean;
dragging: boolean;
color: string;
}) => {
const darkColor = darkenColor(color);
if (disabled) {
return {
track: Colors.GREY_5,
bar: Colors.GREY_6,
thumb: Colors.GREY_6,
marks: {
filled: Colors.GREY_6,
notFilled: Colors.GREY_5,
label: Colors.DARK_GRAY,
},
};
}
if (hovering || dragging) {
return {
track: Colors.GRAY_400,
bar: darkColor,
thumb: darkColor,
marks: {
filled: darkColor,
notFilled: Colors.GRAY_400,
label: Colors.CHARCOAL,
},
};
}
return {
track: Colors.GREY_5,
bar: color,
thumb: color,
marks: {
filled: color,
notFilled: Colors.GREY_5,
label: Colors.CHARCOAL,
},
};
};