-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): update field validation logic to handle collection sizes
- Loading branch information
1 parent
bddccf6
commit b1359b6
Showing
5 changed files
with
85 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
invokeai/frontend/web/src/features/nodes/hooks/useDoesInputHaveValue.ts
This file was deleted.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
invokeai/frontend/web/src/features/nodes/hooks/useFieldIsInvalid.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { createSelector } from '@reduxjs/toolkit'; | ||
import { useAppSelector } from 'app/store/storeHooks'; | ||
import { useConnectionState } from 'features/nodes/hooks/useConnectionState'; | ||
import { useFieldInputTemplate } from 'features/nodes/hooks/useFieldInputTemplate'; | ||
import { selectFieldInputInstance, selectNodesSlice } from 'features/nodes/store/selectors'; | ||
import { isImageFieldCollectionInputInstance, isImageFieldCollectionInputTemplate } from 'features/nodes/types/field'; | ||
import { useMemo } from 'react'; | ||
|
||
export const useFieldIsInvalid = (nodeId: string, fieldName: string) => { | ||
const template = useFieldInputTemplate(nodeId, fieldName); | ||
const connectionState = useConnectionState({ nodeId, fieldName, kind: 'inputs' }); | ||
|
||
const selectIsInvalid = useMemo(() => { | ||
return createSelector(selectNodesSlice, (nodes) => { | ||
const field = selectFieldInputInstance(nodes, nodeId, fieldName); | ||
|
||
// No field instance is a problem - should not happen | ||
if (!field) { | ||
return true; | ||
} | ||
|
||
// 'connection' input fields have no data validation - only connection validation | ||
if (template.input === 'connection') { | ||
return template.required && !connectionState.isConnected; | ||
} | ||
|
||
// 'any' input fields are valid if they are connected | ||
if (template.input === 'any' && connectionState.isConnected) { | ||
return false; | ||
} | ||
|
||
// If there is no valid for the field & the field is required, it is invalid | ||
if (field.value === undefined) { | ||
return template.required; | ||
} | ||
|
||
// Else special handling for individual field types | ||
if (isImageFieldCollectionInputInstance(field) && isImageFieldCollectionInputTemplate(template)) { | ||
// Image collections may have min or max item counts | ||
if (template.minItems !== undefined && field.value.length < template.minItems) { | ||
return true; | ||
} | ||
|
||
if (template.maxItems !== undefined && field.value.length > template.maxItems) { | ||
return true; | ||
} | ||
} | ||
|
||
// Field looks OK | ||
return false; | ||
}); | ||
}, [connectionState.isConnected, fieldName, nodeId, template]); | ||
|
||
const isInvalid = useAppSelector(selectIsInvalid); | ||
|
||
return isInvalid; | ||
}; |