1717import React , { SyntheticEvent , useCallback } from 'react'
1818import { Icon as IconComponent } from '@Shared/Components'
1919import { throttle , useEffectAfterMount } from '../Helper'
20+ import { CHECKBOX_VALUE } from '@Common/Types'
2021import './Toggle.scss'
2122
2223const Toggle = ( {
@@ -31,12 +32,14 @@ const Toggle = ({
3132 throttleOnChange = false ,
3233 shouldToggleValueOnLabelClick = false ,
3334 isLoading = false ,
35+ value = CHECKBOX_VALUE . CHECKED ,
36+ isControlled = false ,
3437 ...props
3538} ) => {
3639 const [ active , setActive ] = React . useState ( selected )
3740
3841 useEffectAfterMount ( ( ) => {
39- if ( typeof onSelect === 'function' ) {
42+ if ( typeof onSelect === 'function' && ! isControlled ) {
4043 if ( active !== selected ) {
4144 onSelect ( active )
4245 }
@@ -49,7 +52,11 @@ const Toggle = ({
4952
5053 function handleClick ( ) {
5154 if ( ! disabled ) {
52- setActive ( ( active ) => ! active )
55+ if ( isControlled ) {
56+ onSelect ( ! active )
57+ } else {
58+ setActive ( ( active ) => ! active )
59+ }
5360 }
5461 }
5562
@@ -70,8 +77,26 @@ const Toggle = ({
7077 }
7178 }
7279
80+ const isIntermediateView = selected && value === CHECKBOX_VALUE . INTERMEDIATE
81+
82+ const renderIcon = ( ) => {
83+ if ( isIntermediateView ) {
84+ return (
85+ < div className = "w-100 h-100 dc__position-abs flex" >
86+ < div className = "w-8 h-2 br-4 dc__no-shrink bg__white" />
87+ </ div >
88+ )
89+ }
90+
91+ if ( Icon ) {
92+ return < Icon className = { `icon-dim-20 br-4 p-2 ${ iconClass } ` } />
93+ }
94+
95+ return null
96+ }
97+
7398 return isLoading ? (
74- < IconComponent name = ' ic-circle-loader' color = ' B500' size = { 20 } />
99+ < IconComponent name = " ic-circle-loader" color = " B500" size = { 20 } />
75100 ) : (
76101 < label
77102 { ...props }
@@ -80,8 +105,11 @@ const Toggle = ({
80105 { ...( shouldToggleValueOnLabelClick ? { onClick : handleLabelClick } : { } ) }
81106 >
82107 < input type = "checkbox" checked = { ! ! active } onChange = { handleChange } className = "toggle__input" />
83- < span className = { `toggle__slider ${ Icon ? 'with-icon br-4 dc__border' : 'round' } ` } data-testid = { dataTestId } >
84- { Icon && < Icon className = { `icon-dim-20 br-4 p-2 ${ iconClass } ` } /> }
108+ < span
109+ className = { `toggle__slider ${ isIntermediateView ? 'intermediate' : '' } ${ Icon ? 'with-icon br-4 dc__border' : 'round' } ` }
110+ data-testid = { dataTestId }
111+ >
112+ { renderIcon ( ) }
85113 </ span >
86114 </ label >
87115 )
0 commit comments