-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds a model-level validation capability to our validation library #49543
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,15 +21,35 @@ import { ButtonSecondary } from 'design/Button'; | |
import ButtonIcon from 'design/ButtonIcon'; | ||
import Flex from 'design/Flex'; | ||
import * as Icon from 'design/Icon'; | ||
import Input from 'design/Input'; | ||
import { useRef } from 'react'; | ||
import styled, { useTheme } from 'styled-components'; | ||
|
||
import { | ||
precomputed, | ||
Rule, | ||
ValidationResult, | ||
} from 'shared/components/Validation/rules'; | ||
import { useRule } from 'shared/components/Validation'; | ||
|
||
import FieldInput from '../FieldInput'; | ||
|
||
type StringListValidationResult = ValidationResult & { | ||
/** | ||
* A list of validation results, one per label. Note: items are optional just | ||
* because `useRule` by default returns only `ValidationResult`. For the | ||
* actual validation, it's not optional; if it's undefined, or there are | ||
* fewer items in this list than the labels, the corresponding items will be | ||
* treated as valid. | ||
*/ | ||
items?: ValidationResult[]; | ||
}; | ||
|
||
export type FieldMultiInputProps = { | ||
label?: string; | ||
value: string[]; | ||
disabled?: boolean; | ||
onChange?(val: string[]): void; | ||
rule?: Rule<string[], StringListValidationResult>; | ||
}; | ||
|
||
/** | ||
|
@@ -45,7 +65,13 @@ export function FieldMultiInput({ | |
value, | ||
disabled, | ||
onChange, | ||
rule = defaultRule, | ||
}: FieldMultiInputProps) { | ||
// It's important to first validate, and then treat an empty array as a | ||
// single-item list with an empty string, since this "synthetic" empty | ||
// string is technically not a part of the model and should not be | ||
// validated. | ||
const validationResult: StringListValidationResult = useRule(rule(value)); | ||
if (value.length === 0) { | ||
value = ['']; | ||
} | ||
|
@@ -90,15 +116,19 @@ export function FieldMultiInput({ | |
// procedure whose complexity would probably outweigh the benefits). | ||
<Flex key={i} alignItems="center" gap={2}> | ||
<Box flex="1"> | ||
<Input | ||
<FieldInput | ||
value={val} | ||
rule={precomputed( | ||
validationResult.items?.[i] ?? { valid: true } | ||
)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you help me understand how this is interacting with default rule? The default rule just returns There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
ref={toFocus.current === i ? setFocus : null} | ||
onChange={e => | ||
onChange?.( | ||
value.map((v, j) => (j === i ? e.target.value : v)) | ||
) | ||
} | ||
onKeyDown={e => handleKeyDown(i, e)} | ||
mb={0} | ||
/> | ||
</Box> | ||
<ButtonIcon | ||
|
@@ -123,6 +153,8 @@ export function FieldMultiInput({ | |
); | ||
} | ||
|
||
const defaultRule = () => () => ({ valid: true }); | ||
|
||
const Fieldset = styled.fieldset` | ||
border: none; | ||
margin: 0; | ||
|
gzdunek marked this conversation as resolved.
Show resolved
Hide resolved
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if i received this field in some result i would have no idea what it meant. Based on the comment, should this be
validItems
or ... unchecked items? (based on label number? im confused)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@avatus How about
itemResults
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the context of the result? valid or not valid? checked vs unchecked? thats the part I'm getting lost with (forgive my brain)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see what you mean. The results are "valid or not valid". In other words, these are per-item validation results that enable us to show a validity state and message for each item of the string list separately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahhhh, that makes sense. with that context,
items
probably makes most sense. I think maybe evenresults
might be better but, I'll leave it up to you if you want to make any changesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for
results
.