11import { CompositeNumberInput , Flex , FormControl , FormLabel , Select , Switch } from '@invoke-ai/ui-library' ;
22import { useAppDispatch } from 'app/store/storeHooks' ;
3+ import { roundDownToMultiple , roundUpToMultiple } from 'common/util/roundDownToMultiple' ;
34import { useIntegerField } from 'features/nodes/components/flow/nodes/Invocation/fields/IntegerField/useIntegerField' ;
45import { useInputFieldInstance } from 'features/nodes/hooks/useInputFieldInstance' ;
56import { fieldIntegerValueChanged } from 'features/nodes/store/nodesSlice' ;
@@ -9,7 +10,7 @@ import type { NodeFieldIntegerSettings } from 'features/nodes/types/workflow';
910import { zNumberComponent } from 'features/nodes/types/workflow' ;
1011import { constrainNumber } from 'features/nodes/util/constrainNumber' ;
1112import type { ChangeEvent } from 'react' ;
12- import { memo , useCallback } from 'react' ;
13+ import { memo , useCallback , useMemo } from 'react' ;
1314import { useTranslation } from 'react-i18next' ;
1415
1516type Props = {
@@ -64,32 +65,42 @@ const SettingMin = memo(({ id, config, nodeId, fieldName, fieldTemplate }: Props
6465 const dispatch = useAppDispatch ( ) ;
6566 const field = useInputFieldInstance < IntegerFieldInputInstance > ( nodeId , fieldName ) ;
6667
67- const floatField = useIntegerField ( nodeId , fieldName , fieldTemplate ) ;
68+ const integerField = useIntegerField ( nodeId , fieldName , fieldTemplate ) ;
6869
6970 const onToggleSetting = useCallback ( ( ) => {
7071 const newConfig : NodeFieldIntegerSettings = {
7172 ...config ,
72- min : config . min !== undefined ? undefined : floatField . min ,
73+ min : config . min !== undefined ? undefined : integerField . min ,
7374 } ;
75+
7476 dispatch ( formElementNodeFieldDataChanged ( { id, changes : { settings : newConfig } } ) ) ;
75- } , [ config , dispatch , floatField . min , id ] ) ;
77+ } , [ config , dispatch , integerField . min , id ] ) ;
7678
7779 const onChange = useCallback (
78- ( v : number ) => {
80+ ( min : number ) => {
7981 const newConfig : NodeFieldIntegerSettings = {
8082 ...config ,
81- min : v ,
83+ min,
8284 } ;
83-
8485 dispatch ( formElementNodeFieldDataChanged ( { id, changes : { settings : newConfig } } ) ) ;
8586
8687 // We may need to update the value if it is outside the new min/max range
87- const constrained = constrainNumber ( field . value , { ... floatField } , newConfig ) ;
88+ const constrained = constrainNumber ( field . value , integerField , newConfig ) ;
8889 if ( field . value !== constrained ) {
8990 dispatch ( fieldIntegerValueChanged ( { nodeId, fieldName, value : constrained } ) ) ;
9091 }
9192 } ,
92- [ config , dispatch , field . value , fieldName , floatField , id , nodeId ]
93+ [ config , dispatch , id , field , integerField , nodeId , fieldName ]
94+ ) ;
95+
96+ const constraintMin = useMemo (
97+ ( ) => roundUpToMultiple ( integerField . min , integerField . step ) ,
98+ [ integerField . min , integerField . step ]
99+ ) ;
100+
101+ const constraintMax = useMemo (
102+ ( ) => ( config . max ?? integerField . max ) - integerField . step ,
103+ [ config . max , integerField . max , integerField . step ]
93104 ) ;
94105
95106 return (
@@ -101,10 +112,11 @@ const SettingMin = memo(({ id, config, nodeId, fieldName, fieldTemplate }: Props
101112 < CompositeNumberInput
102113 w = "full"
103114 isDisabled = { config . min === undefined }
104- value = { config . min === undefined ? ( `${ floatField . min } (inherited)` as unknown as number ) : config . min }
115+ value = { config . min ?? ( `${ integerField . min } (inherited)` as unknown as number ) }
105116 onChange = { onChange }
106- min = { floatField . min }
107- max = { ( config . max ?? floatField . max ) - 1 }
117+ min = { constraintMin }
118+ max = { constraintMax }
119+ step = { integerField . step }
108120 />
109121 </ FormControl >
110122 ) ;
@@ -116,32 +128,42 @@ const SettingMax = memo(({ id, config, nodeId, fieldName, fieldTemplate }: Props
116128 const dispatch = useAppDispatch ( ) ;
117129 const field = useInputFieldInstance < IntegerFieldInputInstance > ( nodeId , fieldName ) ;
118130
119- const floatField = useIntegerField ( nodeId , fieldName , fieldTemplate ) ;
131+ const integerField = useIntegerField ( nodeId , fieldName , fieldTemplate ) ;
120132
121133 const onToggleSetting = useCallback ( ( ) => {
122134 const newConfig : NodeFieldIntegerSettings = {
123135 ...config ,
124- max : config . max !== undefined ? undefined : floatField . max ,
136+ max : config . max !== undefined ? undefined : integerField . max ,
125137 } ;
126138 dispatch ( formElementNodeFieldDataChanged ( { id, changes : { settings : newConfig } } ) ) ;
127- } , [ config , dispatch , floatField . max , id ] ) ;
139+ } , [ config , dispatch , integerField . max , id ] ) ;
128140
129141 const onChange = useCallback (
130- ( v : number ) => {
142+ ( max : number ) => {
131143 const newConfig : NodeFieldIntegerSettings = {
132144 ...config ,
133- max : v ,
145+ max,
134146 } ;
135147
136148 dispatch ( formElementNodeFieldDataChanged ( { id, changes : { settings : newConfig } } ) ) ;
137149
138150 // We may need to update the value if it is outside the new min/max range
139- const constrained = constrainNumber ( field . value , floatField , newConfig ) ;
151+ const constrained = constrainNumber ( field . value , integerField , newConfig ) ;
140152 if ( field . value !== constrained ) {
141153 dispatch ( fieldIntegerValueChanged ( { nodeId, fieldName, value : constrained } ) ) ;
142154 }
143155 } ,
144- [ config , dispatch , field . value , fieldName , floatField , id , nodeId ]
156+ [ config , dispatch , field . value , fieldName , integerField , id , nodeId ]
157+ ) ;
158+
159+ const constraintMin = useMemo (
160+ ( ) => ( config . min ?? integerField . min ) + integerField . step ,
161+ [ config . min , integerField . min , integerField . step ]
162+ ) ;
163+
164+ const constraintMax = useMemo (
165+ ( ) => roundDownToMultiple ( integerField . max , integerField . step ) ,
166+ [ integerField . max , integerField . step ]
145167 ) ;
146168
147169 return (
@@ -153,10 +175,11 @@ const SettingMax = memo(({ id, config, nodeId, fieldName, fieldTemplate }: Props
153175 < CompositeNumberInput
154176 w = "full"
155177 isDisabled = { config . max === undefined }
156- value = { config . max === undefined ? ( `${ floatField . max } (inherited)` as unknown as number ) : config . max }
178+ value = { config . max ?? ( `${ integerField . max } (inherited)` as unknown as number ) }
157179 onChange = { onChange }
158- min = { ( config . min ?? floatField . min ) + 1 }
159- max = { floatField . max }
180+ min = { constraintMin }
181+ max = { constraintMax }
182+ step = { integerField . step }
160183 />
161184 </ FormControl >
162185 ) ;
0 commit comments