import * as React from "react"; import BaseWidget, { WidgetProps, WidgetState } from "widgets/BaseWidget"; import { TAILWIND_COLORS } from "constants/ThemeConstants"; import { EventType } from "constants/AppsmithActionConstants/ActionConstants"; import SliderComponent, { SliderComponentProps } from "../component/Slider"; import contentConfig from "./propertyConfig/contentConfig"; import styleConfig from "./propertyConfig/styleConfig"; export interface NumberSliderWidgetProps extends WidgetProps, SliderComponentProps { /** Color from theme.colors */ accentColor?: string; /** defaultValue for the slider */ defaultValue?: number; /** isDirty meta property */ isDirty: boolean; /** value meta property */ value: number; /** onChangeEnd action selector */ onChange: string; } class NumberSliderWidget extends BaseWidget< NumberSliderWidgetProps, WidgetState > { static getPropertyPaneContentConfig() { return contentConfig; } static getPropertyPaneStyleConfig() { return styleConfig; } componentDidUpdate(prevProps: NumberSliderWidgetProps) { /** * If you change the defaultValue from the propertyPane * or say an input widget you are basically resetting the widget * therefore we reset the isDirty. */ if ( this.props.defaultValue !== prevProps.defaultValue && this.props.isDirty ) { this.props.updateWidgetMetaProperty("isDirty", false); } } static getDefaultPropertiesMap(): Record { return { value: "defaultValue", }; } static getMetaPropertiesMap(): Record { return { value: 0, isDirty: false, }; } onChangeEnd = (value: number) => { if (this.props.value !== value) { this.props.updateWidgetMetaProperty("value", value, { triggerPropertyName: "onChange", dynamicString: this.props.onChange, event: { type: EventType.ON_OPTION_CHANGE, }, }); } // Set isDirty to true when we change slider value if (!this.props.isDirty) { this.props.updateWidgetMetaProperty("isDirty", true); } }; getSliderTooltip = (sliderValue: number) => { /** * Check if the step is in decimal if yes fix * the slider tooltip to only one place decimal */ return this.props.step % 1 !== 0 ? sliderValue.toFixed(1).toString() : sliderValue.toString(); }; getPageView() { return ( ); } static getWidgetType() { return "NUMBER_SLIDER_WIDGET"; } } export default NumberSliderWidget;