ToolJet/frontend/src/Editor/Components/ToggleV2.jsx
Johnson Cherian bc5580e055
Release: Appbuilder 1.7 (#9875)
* Remove date-fns depedency from table datepicker

* Revert Remove date-fns depedency from table datepicker

* Fix Built with Tooljet" watermark and app mode switcher appear briefly before the correct login page loads. (#9545)

* fix: headers and action button UI on table (#9626)

* fix: delete all children and nested children on component delete (#9182)

* fix: delete all children and nested children on component delete

* refactor: removed duplicate logics

* refactor: reuse functions and remove duplicate functions

* fix: update parentid of kanban modal child on version creation

* fix: add kanban modal children to delete list

* Fix duplicate text appearing at the bottom from the previous page in PDF widget (#9567)

* chore: fixed vulnerability issues on FE (#9561)

Co-authored-by: Johnson Cherian <johnsonc.dev@gmail.com>

* chore: Removed unused packages in FE (#9161)

* Removed unused packages

* Remove date-fns dependency drom table date picker (#9585)

---------

Co-authored-by: Johnson Cherian <johnsonc.dev@gmail.com>
Co-authored-by: Nakul Nagargade <133095394+nakulnagargade@users.noreply.github.com>

* Fix Pagination and order of rows malfunction if default selected Id is not on page 1 (#9651)

* Fix Pagination and order of rows malfunction if default selected Id is not on page 1

* remove console logs

* Add support to add Manual date entry in Datepicker table column (#9631)

* Removed unused packages

* Add Manual date entry in Datepicker table column

* Remove date-fns dependency drom table date picker (#9585)

* fix date input issue

* Remove consoles

---------

Co-authored-by: Kavin Venkatachalam <kavin.saratha@gmail.com>
Co-authored-by: Johnson Cherian <johnsonc.dev@gmail.com>

* Feature: pages sidebar collapsable and pin functionality in preview/release mode (#9650)

* add: pin and collapsable functionality to pages sidebar

* fix: review changes

* fix: canvas auto update width on sidebar toggle

* fix icon size

* Add support to wrap table's column header (#9565)

* Add support to wrap table's column header

* Resolve code comments

* commit

* fix: resize page width on window resize (#9164)

* feat: Button component revamp (#8361)

* init textinput revamp

* updated styles panel

* bugfix

* updates

* fix :: accordion

* fix :: styling

* add box shadow , additional property,tooltip

* fix conditional render for styles

* feat :: fixed order of each property and styles

* feat :: styling input

* bugfix

* feat :: add option to add icon

* add option to add icon

* adding option to toggle visibility

* updated password input with new design

* chnaging component location

* bugfix

* style fixes

* fix :: added loader

* updated :: few detailing

* few bugfixes

* fix :: for form widget label

* fixes

* added option to add icon color

* including label field for password input

* fix for label

* fix

* test fix backward compatibility for height

* updates

* revert

* adding key for distinguishing older and newer widgets

* testing

* test

* test

* update

* update

* migration testing

* limit vertical resizing in textinput

* testing

* throw test

* test

* adding check for label length

* fixing edge cases

* removing resize

* backward compatibility height

* backward compatibility

* number input review fixes

* added exposed items

* fixing csa

* ui fixes

* fix height compatibility

* feat :: csa for all inputs and exposed variables

* backward compatibility fixes and validation fixes

* fixes :: textinput positioning of loader and icon

* fix :: password input

* cleanup and fixes

* fixes

* cleanup

* fixes

* review fixes

* review fixes

* button component revamped

* fix :: csa

* typo fix

* typo

* fix :: styling

* exposed variables

* fix padding

* review fixes styles component panel

* fix naming

* fix padding

* default size

* fix :: icons position

* updates

* cleanup

* updates events , csa

* cleanup

* remove border

* backward compatibility

* clean

* feat :: change validation from properties

* ui fixes

* icon name

* disabled state

* removed 'px' text from tooltip

* fixes placeholder

* few updates :: removing label in form

* ui in form

* update :: number input validation behaviour

* testing fixes

* added side handlers

* removing unwanted fx

* disabling fx for padding field

* ordering change

* fix

* label issue + restricted side handler

* fix :: box shadow bug

* on change event doesnt propagate exposed vars correctly

* adding debounce for slider value change

* fix :: for modal ooen bug during onfocus event

* test slider

* fix :: bugs regarding state update in checbox , slider , slider bug

* update slider with radix slider

* bugfix

* merge fixes

* fix

* updates

* removing unwanted migrations

* fix: box shadow

* fix : import export issues

* fix : colouring

* cleanup

* fixes

* fix : design review comments

* add border color

* fix : review changes

* fix

* change display name to solid

* fix label

* fix : loader issue

* fix : button icon position

* fix : qa bugs

* feat: Checkbox + Toggle revamp (#8519)

* init textinput revamp

* updated styles panel

* bugfix

* updates

* fix :: accordion

* fix :: styling

* add box shadow , additional property,tooltip

* fix conditional render for styles

* feat :: fixed order of each property and styles

* feat :: styling input

* bugfix

* feat :: add option to add icon

* add option to add icon

* adding option to toggle visibility

* updated password input with new design

* chnaging component location

* bugfix

* style fixes

* fix :: added loader

* updated :: few detailing

* few bugfixes

* fix :: for form widget label

* fixes

* added option to add icon color

* including label field for password input

* fix for label

* fix

* test fix backward compatibility for height

* updates

* revert

* adding key for distinguishing older and newer widgets

* testing

* test

* test

* update

* update

* migration testing

* limit vertical resizing in textinput

* testing

* throw test

* test

* adding check for label length

* fixing edge cases

* removing resize

* backward compatibility height

* backward compatibility

* number input review fixes

* added exposed items

* fixing csa

* ui fixes

* fix height compatibility

* feat :: csa for all inputs and exposed variables

* backward compatibility fixes and validation fixes

* fixes :: textinput positioning of loader and icon

* fix :: password input

* cleanup and fixes

* fixes

* cleanup

* fixes

* review fixes

* review fixes

* typo fix

* fix padding

* review fixes styles component panel

* fix naming

* fix padding

* feat :: toggle switch revamp

* init checkbox

* fixes

* fixes

* switch fixes

* validation fix

* fixes

* cleanup

* height fix

* fix height toggle

* updates

* fix :: icons position

* updates

* cleanup

* updates events , csa

* cleanup

* backward compatibility

* clean

* backward compatibility fix

* label fixed to one line

* feat :: change validation from properties

* ui fixes

* icon name

* removed 'px' text from tooltip

* added onchange event for checkbox

* fixes placeholder

* few updates :: removing label in form

* ui in form

* fire onchange

* update :: number input validation behaviour

* testing fixes

* added side handlers

* removing unwanted fx

* disabling fx for padding field

* ordering change

* fix

* label issue + restricted side handler

* fix :: box shadow bug

* fix

* on change event doesnt propagate exposed vars correctly

* adding debounce for slider value change

* fix :: for modal ooen bug during onfocus event

* test slider

* fix :: bugs regarding state update in checbox , slider , slider bug

* update slider with radix slider

* bugfix

* update tooltip

* fix toggle switch

* fixes : inspector

* fix : checkbox label

* feat : checkbox completed

* update checkbox review changes

* feat : toggle component

* feat : added new toggle component

* fix : colors

* updated review changes

* update name for old and new version

* update

* case change

* update

* update icon

* removed padding from checkbox and toggle

* fix naming

* product review and bugfixes : changes

* fix : checkbox setvalue action

* Update setvalue action in toggle

* fixed: section for legacy and new components

* fix : checkbox tick mark not showing on resizing

* fix : icon in inspector

* fix : box shadow

* fix : ismandatory definition

* configs update

---------

Co-authored-by: Johnson Cherian <johnsonc.dev@gmail.com>

* fix : toggle switch import and switch component issues (#9874)

* fix : filter order of toggle (#9879)

* fix: appbuilder 1.7 integration testing bug fixes (#9880)

* fix: add only ids in deleted components map for component delete functionality

* Fix TableRowHeightInput in codehinter (#9881)

* fix: disable drag on calender popup and unblock other input interactions

* Bugfixes componentes 1.7 (#9892)

* fix : filter order of toggle (#9879)

* fix : Button alignment when label is empty

* fix : box shadow added

* fix : direction and default box shadow

* fix : disable csa not working for checkbox

* fix : button padding

* added toggle for box shadow check

* fixes : integration 1.7 (#9901)

* fix : exposed  vars not getting exposed on page change

* fix : validation error font

* fix : dynamic color for button

* fix : loading placement during CSA

* Move disabled, visibility, tooltip and box shadow to properties in Checkbox and Button (#9665)

* init textinput revamp

* updated styles panel

* bugfix

* updates

* fix :: accordion

* fix :: styling

* add box shadow , additional property,tooltip

* fix conditional render for styles

* feat :: fixed order of each property and styles

* feat :: styling input

* bugfix

* feat :: add option to add icon

* add option to add icon

* adding option to toggle visibility

* updated password input with new design

* chnaging component location

* bugfix

* style fixes

* fix :: added loader

* updated :: few detailing

* few bugfixes

* fix :: for form widget label

* fixes

* added option to add icon color

* including label field for password input

* fix for label

* fix

* test fix backward compatibility for height

* updates

* revert

* adding key for distinguishing older and newer widgets

* testing

* test

* test

* update

* update

* migration testing

* limit vertical resizing in textinput

* testing

* throw test

* test

* adding check for label length

* fixing edge cases

* removing resize

* backward compatibility height

* backward compatibility

* number input review fixes

* added exposed items

* fixing csa

* ui fixes

* fix height compatibility

* feat :: csa for all inputs and exposed variables

* backward compatibility fixes and validation fixes

* fixes :: textinput positioning of loader and icon

* fix :: password input

* cleanup and fixes

* fixes

* cleanup

* fixes

* review fixes

* review fixes

* typo fix

* fix padding

* review fixes styles component panel

* fix naming

* fix padding

* feat :: toggle switch revamp

* init checkbox

* fixes

* fixes

* switch fixes

* validation fix

* fixes

* cleanup

* height fix

* fix height toggle

* updates

* fix :: icons position

* updates

* cleanup

* updates events , csa

* cleanup

* backward compatibility

* clean

* backward compatibility fix

* label fixed to one line

* feat :: change validation from properties

* ui fixes

* icon name

* removed 'px' text from tooltip

* added onchange event for checkbox

* fixes placeholder

* few updates :: removing label in form

* ui in form

* fire onchange

* update :: number input validation behaviour

* testing fixes

* added side handlers

* removing unwanted fx

* disabling fx for padding field

* ordering change

* fix

* label issue + restricted side handler

* fix :: box shadow bug

* fix

* on change event doesnt propagate exposed vars correctly

* adding debounce for slider value change

* fix :: for modal ooen bug during onfocus event

* test slider

* fix :: bugs regarding state update in checbox , slider , slider bug

* update slider with radix slider

* bugfix

* update tooltip

* fix toggle switch

* fixes : inspector

* fix : checkbox label

* Move disabled, visibility, tooltip and box shadow to properties

* Remove toggleswitch and button

* Add down rollbackUpdates

* fix : filter order of toggle (#9879)

* fix merge issue

* fix

---------

Co-authored-by: stepinfwd <stepinfwd@gmail.com>

* fix: use onMouseOver and onMouseLeave together to fix hover event

* fix : Validation message to be shown only when use toggles on and off , code refactor (#9905)

* Fix naming logic for copying (#9909)

---------

Co-authored-by: Nakul Nagargade <133095394+nakulnagargade@users.noreply.github.com>
Co-authored-by: Kiran Ashok <stepinfwd@gmail.com>

* fix: update animateTransform for cross browser compatibility (#9917)

* fix: color transitions on sidebar pages in dark mode (#9920)

* Fix table all column editable (#9921)

* fix: add only ids in deleted components map for component delete functionality

* Fix TableRowHeightInput in codehinter (#9881)

* fix: disable drag on calender popup and unblock other input interactions

* Fix allColumns editable in Table

* Fix clientServer nor working in Table

* In widget config, update the total records as 10

---------

Co-authored-by: Johnson Cherian <johnsonc.dev@gmail.com>

* chore: bump minor version

---------

Co-authored-by: Nakul Nagargade <nakul@tooljet.com>
Co-authored-by: Nakul Nagargade <133095394+nakulnagargade@users.noreply.github.com>
Co-authored-by: vjaris42 <vjy239@gmail.com>
Co-authored-by: Kavin Venkatachalam <50441969+kavinvenkatachalam@users.noreply.github.com>
Co-authored-by: Kavin Venkatachalam <kavin.saratha@gmail.com>
Co-authored-by: Kiran Ashok <stepinfwd@gmail.com>
2024-05-30 18:12:10 +05:30

308 lines
8.5 KiB
JavaScript

import React, { useEffect, useState } from 'react';
import { resolveWidgetFieldValue } from '@/_helpers/utils';
import Loader from '@/ToolJetUI/Loader/Loader';
import OverflowTooltip from '@/_components/OverflowTooltip';
const Switch = ({
on,
onClick,
onChange,
disabledState,
color,
alignment,
borderColor,
setOn,
styles,
setExposedVariable,
fireEvent,
setUserInteracted,
}) => {
const handleToggleChange = () => {
setOn(!on);
setExposedVariable('value', !on);
fireEvent('onChange');
setUserInteracted(true);
};
const switchStyle = {
position: 'relative',
display: 'inline-block',
width: '28px',
height: '18px',
marginRight: '0px',
paddingRight: '0px',
};
const sliderStyle = {
position: 'absolute',
cursor: 'pointer',
top: 0,
left: 0,
right: 0,
bottom: 0,
backgroundColor: on ? styles.toggleSwitchColor : styles.uncheckedColor,
transition: 'background-color 0.2s',
borderRadius: '34px',
outline: `1px solid ${styles.borderColor}`,
};
const circleStyle = {
position: 'absolute',
content: '',
height: '12px',
width: '12px',
left: '2px',
bottom: '3px',
backgroundColor: styles.handleColor,
transition: 'transform 0.2s',
borderRadius: '50%',
transform: on ? 'translateX(12px)' : 'translateX(0)',
};
return (
<div>
<div className="d-flex" style={switchStyle} onClick={handleToggleChange}>
<input
type="checkbox"
style={{
opacity: 0,
width: 0,
height: 0,
backgroundColor: on ? `${color}` : 'white',
marginTop: '0px',
marginLeft: alignment === 'left' && '-2rem',
border: `1 px solid ${borderColor}`,
}}
disabled={disabledState}
className="form-check-input "
checked={on}
onChange={onChange}
onClick={onClick}
/>
<span style={sliderStyle}>
<span style={circleStyle}></span>
</span>
</div>
</div>
);
};
export const ToggleSwitchV2 = ({
height,
properties,
styles,
fireEvent,
setExposedVariable,
dataCy,
component,
validate,
width,
}) => {
const defaultValue = properties.defaultValue ?? false;
const [on, setOn] = useState(Boolean(defaultValue));
const label = properties.label;
const { isValid, validationError } = validate(on);
const [showValidationError, setShowValidationError] = useState(true);
const [loading, setLoading] = useState(properties?.loadingState);
const [disable, setDisable] = useState(properties.disabledState || properties.loadingState);
const [visibility, setVisibility] = useState(properties.visibility);
const [userInteracted, setUserInteracted] = useState(false);
const isMandatory = resolveWidgetFieldValue(component?.definition?.validation?.mandatory?.value);
const { toggleSwitchColor, boxShadow, alignment, borderColor } = styles;
const textColor = styles.textColor === '#1B1F24' ? 'var(--text-primary)' : styles.textColor;
const toggleValue = (e) => {
const toggled = e.target.checked;
setExposedVariable('value', toggled);
fireEvent('onChange');
setUserInteracted(true);
};
// Exposing the initially set false value once on load
useEffect(() => {
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);
setUserInteracted(true);
};
useEffect(() => {
if (disable !== properties.disabledState) setDisable(properties.disabledState);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [properties.disabledState]);
useEffect(() => {
if (visibility !== properties.visibility) setVisibility(properties.visibility);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [properties.visibility]);
useEffect(() => {
if (loading !== properties.loadingState) setLoading(properties.loadingState);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [properties.loadingState]);
useEffect(() => {
setExposedVariable('label', label);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [label]);
useEffect(() => {
setExposedVariable('isMandatory', isMandatory);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isMandatory]);
useEffect(() => {
setExposedVariable('isLoading', loading);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [loading]);
useEffect(() => {
setExposedVariable('isVisible', visibility);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [visibility]);
useEffect(() => {
setExposedVariable('isDisabled', disable);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [disable]);
useEffect(() => {
setExposedVariable('isValid', isValid);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isValid]);
useEffect(() => {
setExposedVariable('setLoading', async function (loading) {
setLoading(loading);
setExposedVariable('isLoading', loading);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [properties.loadingState]);
useEffect(() => {
setExposedVariable('setVisibility', async function (state) {
setVisibility(state);
setExposedVariable('isVisible', state);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [properties.visibility]);
useEffect(() => {
setExposedVariable('setDisable', async function (disable) {
setDisable(disable);
setExposedVariable('isDisabled', disable);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [properties.disabledState]);
useEffect(() => {
setExposedVariable('toggle', async function () {
setExposedVariable('value', !on);
fireEvent('onChange');
setOn(!on);
setUserInteracted(true);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [on]);
useEffect(() => {
setExposedVariable('setValue', async function (value) {
setOn(value);
setExposedVariable('value', value);
setUserInteracted(true);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const renderInput = () => (
<div
data-disabled={properties.disabledState}
className={`${alignment === 'right' ? 'flex-row-reverse' : 'flex-row'}`}
style={{
display: visibility ? 'flex' : 'none',
boxShadow,
alignItems: loading && 'center',
gap: '6px ',
justifyContent: `${properties.loadingState ? 'center' : alignment === 'left' ? 'space-between' : 'start'}`,
height,
whiteSpace: 'nowrap',
paddingTop: '3px',
}}
data-cy={dataCy}
>
{loading ? (
<Loader width="16" />
) : (
<>
<OverflowTooltip
className="form-check-label"
style={{
lineHeight: '20px',
color: textColor,
fontWeight: 400,
fontSize: '14px',
}}
whiteSpace="normal"
width={width - 20}
>
{label}
{isMandatory && !on && <span style={{ color: '#DB4324', marginLeft: '1px' }}>{'*'}</span>}
</OverflowTooltip>
<Switch
disabledState={disable}
on={on}
onClick={toggle}
onChange={toggleValue}
color={toggleSwitchColor}
alignment={alignment}
validationError={validationError}
isValid={isValid}
showValidationError={showValidationError}
properties={properties}
setShowValidationError={setShowValidationError}
borderColor={borderColor}
setOn={setOn}
styles={styles}
setExposedVariable={setExposedVariable}
fireEvent={fireEvent}
setUserInteracted={setUserInteracted}
/>
</>
)}
</div>
);
return (
<div
style={{
justifyContent: `${loading ? 'center' : 'flex-start'}`,
}}
>
{renderInput()}
{showValidationError && isMandatory && userInteracted && !on && visibility && (
<div
data-cy={`${String(component.name).toLowerCase()}-invalid-feedback`}
style={{
color: 'var(--status-error-strong)',
fontSize: '11px',
fontWeight: '400',
lineHeight: '16px',
}}
>
{validationError}
</div>
)}
</div>
);
};