@@ -26,7 +26,7 @@ import {
2626} from "lowcoder-sdk" ;
2727import { i18nObjs , trans } from "./i18n/comps" ;
2828import _ from 'lodash'
29-
29+ import { isValid } from "date-fns"
3030export enum DEP_TYPE {
3131 CONTRAST_TEXT = 'contrastText' ,
3232 SELF = 'toSelf' ,
@@ -198,7 +198,7 @@ const createTaskListLocal = (
198198 rowHeight : number ;
199199 rowWidth : string ;
200200 locale : string ;
201- tasks : ( Task & { title :string } ) [ ] ;
201+ tasks : ( Task & { label :string } ) [ ] ;
202202 selectedTaskId : string ;
203203 setSelectedTask : ( taskId : string ) => void ;
204204 onExpanderClick : ( task : Task ) => void ;
@@ -257,7 +257,7 @@ const createTaskListLocal = (
257257 width : "33%" ,
258258 paddingTop : rowHeight * 0.3 ,
259259 } }
260- title = { t . title }
260+ title = { t . label }
261261 >
262262 < div className = "Gantt-Task-List_Name-Container" style = { { flexDirection : "row" , display : "flex" , } } >
263263 < div
@@ -282,7 +282,7 @@ const createTaskListLocal = (
282282 } }
283283 onClick = { ( ) => onClick ( t ) }
284284 >
285- { t . title }
285+ { t . label }
286286 </ div >
287287 </ div >
288288 </ div >
@@ -340,7 +340,7 @@ const createTooltip = (
340340 borderWidth : string ,
341341 borderColor : string ,
342342) : React . FunctionComponent < {
343- task : Task & { title :string } ;
343+ task : Task & { label :string } ;
344344} > => {
345345 return ( { task } ) => {
346346 const style = {
@@ -360,7 +360,7 @@ const createTooltip = (
360360 className = { "Gantt-Tooltip_Paragraph Gantt-Tooltip_Paragraph__Information" }
361361 style = { { fontSize : textSize } }
362362 >
363- { task . title }
363+ { task . label }
364364 </ p >
365365 < p className = { "Gantt-Tooltip_Paragraph" } style = { { fontSize : textSize } } >
366366 { `${ startDisplayName } : ${ formatDateShort ( task . start , includeTime ) } ` }
@@ -393,9 +393,9 @@ const getStartEndDateForProject = (tasks: Task[], projectId: string) => {
393393 return [ start , end ] ;
394394} ;
395395
396- const filterTaskFields = ( task : Task & { barChildren : Omit < OptionPropertyParam , 'label' | 'hideChildren' > [ ] , title :string } ) => ( {
396+ const filterTaskFields = ( task : Task & { barChildren : Omit < OptionPropertyParam , 'hideChildren' > [ ] , label :string } ) => ( {
397397 id : task . id ,
398- title : task . title ,
398+ label : task . label ,
399399 type : task . type ,
400400 start : task . start ,
401401 end : task . end ,
@@ -404,7 +404,7 @@ const filterTaskFields = (task: Task & { barChildren: Omit<OptionPropertyParam,
404404 dependencies : task . dependencies ,
405405 barChildren : task . barChildren ? task . barChildren . map ( child => ( {
406406 id : child . id ,
407- title : child . title ,
407+ label : child . label ,
408408 type : child . type ,
409409 start : child . start ,
410410 end : child . end ,
@@ -414,12 +414,32 @@ const filterTaskFields = (task: Task & { barChildren: Omit<OptionPropertyParam,
414414 } ) ) : [ ] ,
415415} ) ;
416416
417+ function checkValidDate ( date : any ) {
418+ // Convert the input to a Date object if it's a string or number
419+ if (
420+ ! ( date instanceof Date ) &&
421+ typeof date !== 'number' &&
422+ date !== undefined
423+ ) {
424+ date = new Date ( date ) ;
425+ }
426+
427+ // Check if the converted Date object is valid
428+ if ( ! ( date instanceof Date ) || isNaN ( date . getTime ( ) ) ) {
429+ return false ;
430+ }
431+
432+ return isValid ( date ) ;
433+ }
434+
417435let GanttOption = new MultiCompBuilder (
418436 {
419- title : StringControl ,
420- start : jsonControl ( ( data : any ) => data ? new Date ( data ) : new Date ( ) ) ,
421- end : jsonControl ( ( data : any ) => data ? new Date ( data ) : new Date ( ) ) ,
422437 label : StringControl ,
438+ start : jsonControl ( ( data : any ) => data ?checkValidDate ( data ) ? new Date ( data ) : new Date ( ) :new Date ( ) ) ,
439+ end : jsonControl ( ( data : any ) =>
440+ data ? checkValidDate ( data ) ? new Date ( data ) : new Date ( ) : new Date ( )
441+ ) ,
442+ // label: StringControl,
423443 id : StringControl ,
424444 project : StringControl ,
425445 progress : NumberControl ,
@@ -434,8 +454,8 @@ let GanttOption = new MultiCompBuilder(
434454type OptionPropertyParam = {
435455 start ?: Date ;
436456 end ?: Date ;
437- title ?: string ;
438457 label ?: string ;
458+ // label?: string;
439459 id ?: string ;
440460 progress ?: number ;
441461 type ?: string ;
@@ -452,7 +472,7 @@ GanttOption = class extends GanttOption implements OptionCompProperty {
452472 propertyView ( param : any ) {
453473 return (
454474 < >
455- { this . children . title . propertyView ( { label : trans ( "component.name" ) } ) }
475+ { this . children . label . propertyView ( { label : trans ( "component.name" ) } ) }
456476 { this . children . start . propertyView ( { label : trans ( "component.start" ) } ) }
457477 { this . children . end . propertyView ( { label : trans ( "component.end" ) } ) }
458478 { this . children . progress . propertyView ( { label : trans ( "component.progress" ) } ) }
@@ -469,7 +489,7 @@ GanttOption = class extends GanttOption implements OptionCompProperty {
469489
470490export const GanttOptionControl = optionsControl ( GanttOption , {
471491 initOptions : i18nObjs . defaultTasks ,
472- uniqField : "id " ,
492+ uniqField : "label " ,
473493} ) ;
474494
475495const viewModeOptions = [
@@ -541,6 +561,8 @@ let GanttChartCompBase = (function () {
541561 const [ tasks , setTasks ] = useState < Task [ ] > ( props . data ?? [ ] ) ;
542562 const [ dimensions , setDimensions ] = useState ( { width : 480 , height : 300 } ) ;
543563 const [ updatedGanttTasks , setUpdatedGanttTasks ] = useState < Task [ ] > ( [ ] ) ;
564+ const [ previousData , setPreviousData ] = useState < Task [ ] > ( props ?. data ) ;
565+
544566 // useMergeCompStyles(props as Record<string, any>, dispatch);
545567
546568 const { width, height, ref : conRef } = useResizeDetector ( {
@@ -557,13 +579,20 @@ let GanttChartCompBase = (function () {
557579 } ) ;
558580
559581 useEffect ( ( ) => {
560- if ( tasks . length === 0 ) {
561- if ( props . data . length > 0 ) {
562- setTasks ( props . data ) ;
582+ if ( ! _ . isEqual ( previousData , props . data ) ) {
583+ //stops unnecessary props.data re-render
584+ if ( tasks . length === 0 ) {
585+ //handle map mode
586+ if ( props . data . length > 0 ) {
587+ setTasks ( props . data ) ;
588+ setPreviousData ( props . data ) ;
589+ }
590+ } else if ( ! _ . isEqual ( props . data , tasks ) ) {
591+ //detect any change in data
592+ setTasks ( props . data ) ; //pass updated data to gantt chart
593+ setPreviousData ( props . data ) ;
594+ }
563595 }
564- } else if ( ! _ . isEqual ( props . data , tasks ) ) {
565- setTasks ( props . data )
566- }
567596 } , [ props . data ] )
568597
569598 useEffect ( ( ) => {
@@ -572,7 +601,7 @@ let GanttChartCompBase = (function () {
572601
573602 const updateGanttTasks = ( newTasks : Task [ ] , taskId : string ) => {
574603 const filteredTasks = newTasks . map ( filterTaskFields ) ;
575- filteredTasks . currentChagedTask = taskId ;
604+ filteredTasks . currentChangedTask = taskId ;
576605 setUpdatedGanttTasks ( filteredTasks ) ;
577606 props . onEvent ( "handleTaskUpdate" ) ;
578607 } ;
@@ -599,7 +628,7 @@ let GanttChartCompBase = (function () {
599628 } ;
600629
601630 const handleTaskDelete = ( task : Task ) => {
602- const conf = window . confirm ( "Are you sure about " + task . title + " ?" ) ;
631+ const conf = window . confirm ( "Are you sure about " + task . label + " ?" ) ;
603632 if ( conf ) {
604633 const newTasks = tasks . filter ( t => t . id !== task . id ) ;
605634 setTasks ( newTasks ) ;
@@ -644,7 +673,7 @@ let GanttChartCompBase = (function () {
644673 < div className = "Wrapper" ref = { conRef } >
645674 { tasks . length > 0 ? (
646675 < Gantt
647- tasks = { tasks . map ( task => ( { ...task , name :task . title } ) ) }
676+ tasks = { tasks . map ( task => ( { ...task , name :task . label } ) ) }
648677 viewMode = { activeViewMode }
649678 onDateChange = { handleTaskChange }
650679 onDelete = { handleTaskDelete }
0 commit comments