Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/computed-form-fields/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.11.0",
"private": true,
"dependencies": {
"@nmfs-radfish/radfish": "^1.0.0",
"@nmfs-radfish/radfish": "^1.1.1",
"@nmfs-radfish/react-radfish": "^1.0.0",
"@testing-library/user-event": "^14.5.2",
"@trussworks/react-uswds": "^9.1.0",
Expand Down
30 changes: 15 additions & 15 deletions examples/conditional-form-fields/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ return (
<Form
onSubmit={handleSubmit}
className="maxw-full margin-205 padding-205 bg-white radius-8px shadow-2"
>
>
<FormGroup>
<Label className="text-bold" htmlFor={FULL_NAME}>
Full Name
Expand All @@ -59,7 +59,7 @@ return (
type="text"
placeholder="Full Name"
value={formData[FULL_NAME] || ""}
/>
/>
```

### 4. Adding Input Handlers for Form Fields
Expand Down Expand Up @@ -95,19 +95,19 @@ Next, we want to conditionally render the `nickname` component. The condition de

```jsx
{
formData[FULL_NAME] && (
<>
<Label htmlFor={NICKNAME}>Nickname</Label>
<TextInput
id={NICKNAME}
name={NICKNAME}
type="text"
placeholder="Nickname"
value={formData[NICKNAME] || ""}
onChange={(event) => handleNickNameChange(event, formData)}
/>
</>
);
formData[FULL_NAME] && (
<>
<Label htmlFor={NICKNAME}>Nickname</Label>
<TextInput
id={NICKNAME}
name={NICKNAME}
type="text"
placeholder="Nickname"
value={formData[NICKNAME] || ""}
onChange={(event) => handleNickNameChange(event, formData)}
/>
</>
)
}
```

Expand Down
2 changes: 1 addition & 1 deletion examples/conditional-form-fields/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.11.0",
"private": true,
"dependencies": {
"@nmfs-radfish/radfish": "^1.0.0",
"@nmfs-radfish/radfish": "^1.1.1",
"@nmfs-radfish/react-radfish": "^1.0.0",
"@testing-library/user-event": "^14.5.2",
"@trussworks/react-uswds": "^9.1.0",
Expand Down
56 changes: 28 additions & 28 deletions examples/field-validators/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ In this step, we add validation logic to our form inputs using an `onBlur` handl
This function validates the input value. It uses the validation rules and updates the state with any validation errors:
```jsx
const handleBlur = (event, validators) => {
const { name, value } = event.target;
setValidationErrors((prev) => ({
...prev,
...handleInputValidationLogic(name, value, validators),
}));
const { name, value } = event.target;
setValidationErrors((prev) => ({
...prev,
...handleInputValidationLogic(name, value, validators),
}));
};
```

This helper function loops through the provided validators and checks if the input value passes the validation criteria. If not, it returns the appropriate error message:
```jsx
const handleInputValidationLogic = (name, value, validators) => {
if (validators && validators.length > 0) {
for (let validator of validators) {
if (!validator.test(value)) {
return { [name]: validator.message };
}
if (validators && validators.length > 0) {
for (let validator of validators) {
if (!validator.test(value)) {
return { [name]: validator.message };
}
}
}
}
return { [name]: null }; // Clear error if value is valid
return { [name]: null }; // Clear error if value is valid
};
```

Expand All @@ -50,7 +50,7 @@ Here’s how to use the onBlur handler in the TextInput component. The input dyn
aria-invalid={validationErrors[FULL_NAME] ? "true" : "false"}
validationStatus={validationErrors[FULL_NAME] ? "error" : undefined}
onChange={handleChange}
onBlur={(e) => handleBlur(e, fullNameValidators)}
onBlur={(e) => handleBlur(e, fullNameValidators)}
/>
```

Expand All @@ -60,10 +60,10 @@ We include the validator needed to check the contents of the field input. In thi
```jsx
// utilities/fieldValidators.js
const fullNameValidators = [
{
test: (value) => !/\d/.test(value) || value === "",
message: "Full Name should not contain numbers.",
},
{
test: (value) => !/\d/.test(value) || value === "",
message: "Full Name should not contain numbers.",
},
];

export { fullNameValidators };
Expand All @@ -77,17 +77,17 @@ We can update the properties on our input so the validation logic gets triggered

```jsx
<TextInput
id={FULL_NAME}
name={FULL_NAME}
type="text"
placeholder="Full Name"
value={formData[FULL_NAME] || ""}
aria-invalid={validationErrors[FULL_NAME] ? "true" : "false"}
validationStatus={validationErrors[FULL_NAME] ? "error" : undefined}
onChange={handleChange}
onBlur={(e) => handleBlur(e, fullNameValidators)}
/>;
id={FULL_NAME}
name={FULL_NAME}
type="text"
placeholder="Full Name"
value={formData[FULL_NAME] || ""}
aria-invalid={validationErrors[FULL_NAME] ? "true" : "false"}
validationStatus={validationErrors[FULL_NAME] ? "error" : undefined}
onChange={handleChange}
onBlur={(e) => handleBlur(e, fullNameValidators)}
/>
{
validationErrors[FULL_NAME] && <ErrorMessage>{validationErrors[FULL_NAME]}</ErrorMessage>;
validationErrors[FULL_NAME] && <ErrorMessage>{validationErrors[FULL_NAME]}</ErrorMessage>
}
```
2 changes: 1 addition & 1 deletion examples/field-validators/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.11.0",
"private": true,
"dependencies": {
"@nmfs-radfish/radfish": "^1.0.0",
"@nmfs-radfish/radfish": "^1.1.1",
"@nmfs-radfish/react-radfish": "^1.0.0",
"@testing-library/user-event": "^14.5.2",
"@trussworks/react-uswds": "^9.1.0",
Expand Down
8 changes: 4 additions & 4 deletions examples/form-structure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Use Trussworks `Grid` components to define the layout of your page in `src/pages
<Grid row>
<Grid col>
<p className="text-bold text-center">
Private Recreational Tilefish
Private Recreational Tilefish
</p>
</Grid>
</Grid>
Expand Down Expand Up @@ -83,7 +83,7 @@ const inputFocus = useRef(null);
useEffect(() => {
if (inputFocus.current) {
inputFocus.current.focus(); // Set focus on the input field
};
}
setResetToggle(false); // Reset the toggle state
}, [resetToggle]);

Expand Down Expand Up @@ -114,6 +114,6 @@ const handleSubmit = (event) => {
// Reset for after triggering Submit
event.target.reset();
// Set focus on first input after form is submitted.
setResetToggle(true)
}
setResetToggle(true);
};
```
2 changes: 1 addition & 1 deletion examples/form-structure/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "form-structure-example",
"version": "0.11.0",
"dependencies": {
"@nmfs-radfish/radfish": "^1.0.0",
"@nmfs-radfish/radfish": "^1.1.1",
"@nmfs-radfish/react-radfish": "^1.0.0",
"@testing-library/user-event": "^14.5.2",
"@trussworks/react-uswds": "^9.1.0",
Expand Down
Loading