From 57da2b640cedb222394088f229de65a3a0cb17c4 Mon Sep 17 00:00:00 2001 From: Manish Kushare Date: Tue, 24 May 2022 03:45:03 +0530 Subject: [PATCH] added default value in the toggle widget (#3059) * added default value in the toggle widget * make sure existing apps do not get crashed if they don't have default value prop * removed unwanted code * changing defaultValue prop display name to Default Status * added value key in exposedVariables prop --- frontend/src/Editor/Components/Toggle.jsx | 12 +++++++----- frontend/src/Editor/Components/components.js | 2 ++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/frontend/src/Editor/Components/Toggle.jsx b/frontend/src/Editor/Components/Toggle.jsx index b20d306bd4..013458d3b5 100644 --- a/frontend/src/Editor/Components/Toggle.jsx +++ b/frontend/src/Editor/Components/Toggle.jsx @@ -1,5 +1,4 @@ import React, { useEffect } from 'react'; - class Switch extends React.Component { render() { const { on, onClick, onChange, disabledState, color } = this.props; @@ -24,7 +23,9 @@ class Switch extends React.Component { } export const ToggleSwitch = ({ height, properties, styles, fireEvent, setExposedVariable }) => { - const [on, setOn] = React.useState(false); + // definition props + const defaultValue = properties.defaultValue ?? false; + const [on, setOn] = React.useState(defaultValue); const label = properties.label; const { visibility, disabledState, toggleSwitchColor, textColor } = styles; @@ -37,10 +38,11 @@ export const ToggleSwitch = ({ height, properties, styles, fireEvent, setExposed // Exposing the initially set false value once on load useEffect(() => { - console.log('shashi'); - setExposedVariable('value', false); + setExposedVariable('value', defaultValue); // eslint-disable-next-line react-hooks/exhaustive-deps - }, []); + setOn(defaultValue); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [defaultValue]); const toggle = () => setOn(!on); diff --git a/frontend/src/Editor/Components/components.js b/frontend/src/Editor/Components/components.js index aa6b4171cd..a002c0cc89 100644 --- a/frontend/src/Editor/Components/components.js +++ b/frontend/src/Editor/Components/components.js @@ -618,6 +618,7 @@ export const componentTypes = [ }, properties: { label: { type: 'code', displayName: 'Label' }, + defaultValue: { type: 'toggle', displayName: 'Default Status' }, }, events: { onChange: { displayName: 'On change' }, @@ -638,6 +639,7 @@ export const componentTypes = [ }, properties: { label: { value: 'Toggle label' }, + defaultValue: { value: '{{false}}' }, }, events: [], styles: {