Skip to content

Commit

Permalink
Merge pull request #230 from MetroStar/form-control-enhancements
Browse files Browse the repository at this point in the history
Form Control Enhancements
  • Loading branch information
jbouder authored Aug 19, 2024
2 parents e7e5ca5 + 4c5a8f2 commit 98d1d57
Show file tree
Hide file tree
Showing 21 changed files with 549 additions and 146 deletions.
23 changes: 14 additions & 9 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import globals from 'globals';
import pluginJs from '@eslint/js';
import stylisticTs from '@stylistic/eslint-plugin-ts';
import tseslint from 'typescript-eslint';
import prettierRecommended from 'eslint-plugin-prettier/recommended';
import reactPlugin from 'eslint-plugin-react';

export default [
Expand All @@ -20,23 +20,28 @@ export default [
},
},
},
plugins: {
'@stylistic/ts': stylisticTs,
},
},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
reactPlugin.configs.flat.recommended,
prettierRecommended,
{
rules: {
// Base Warnings
'no-console': 'warn',

// Stylistic Issues
'@stylistic/ts/quotes': ['error', 'single'],
'@stylistic/ts/indent': ['error', 2],
'@stylistic/ts/semi': ['error', 'always'],
'@stylistic/ts/comma-dangle': ['error', 'always-multiline'],
// Formatting
'prettier/prettier': [
'error',
{
semi: true,
tabWidth: 2,
singleQuote: true,
trailingComma: 'all',
bracketSpacing: true,
useTabs: false,
},
],

// TypeScript
'@typescript-eslint/no-unused-vars': 'error',
Expand Down
154 changes: 90 additions & 64 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"@storybook/react": "^8.2.1",
"@storybook/react-vite": "^8.2.1",
"@storybook/test": "^8.2.1",
"@stylistic/eslint-plugin-ts": "^2.6.2",
"@testing-library/dom": "10.4.0",
"@testing-library/jest-dom": "^6.4.8",
"@testing-library/react": "16.0.0",
Expand All @@ -45,6 +44,8 @@
"@vitejs/plugin-react": "^4.3.1",
"@vitest/coverage-v8": "^2.0.5",
"eslint": "^9.9.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-react": "^7.35.0",
"jest-axe": "^9.0.0",
"postcss": "^8.4.41",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ describe('ErrorMessage', () => {
expect(baseElement).toBeTruthy();
});

test('should render an error message as a string successfully', () => {
const { baseElement } = render(<ErrorMessages errors="error" />);
const errorMessages = baseElement.querySelectorAll('.usa-error-message');
expect(errorMessages).toHaveLength(1);
});

test('should render an error message successfully', () => {
const errors: string[] = ['error1'];
const { baseElement } = render(<ErrorMessages errors={errors} />);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface ErrorMessagesProps {
/**
* An array of error strings to display
*/
errors?: string[];
errors?: string | string[];
/**
* ReactNode components to display as children
*/
Expand All @@ -28,6 +28,10 @@ export const ErrorMessages = ({
return <></>;
}

if (typeof errors === 'string') {
errors = [errors];
}

return (
<>
{children ??
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { Meta, StoryFn } from '@storybook/react';
import { FormGroup, FormGroupProps } from './form-group';
import { TextInput, Label } from '../..';
import { TextInput, Label, HelperText, ErrorMessages } from '../..';

const meta: Meta<typeof FormGroup> = {
title: 'USWDS/Forms/Form Group',
Expand All @@ -13,6 +13,8 @@ const Template: StoryFn<typeof FormGroup> = (args: FormGroupProps) => (
<div className="padding-left-1">
<FormGroup id={args.id} errors={args.errors}>
<Label htmlFor="input1">Name</Label>
<HelperText>Enter your full name</HelperText>
<ErrorMessages errors={args.errors} />
<TextInput id="input1" name="input1" />
</FormGroup>
</div>
Expand All @@ -21,11 +23,10 @@ const Template: StoryFn<typeof FormGroup> = (args: FormGroupProps) => (
export const Standard = Template.bind({});
Standard.args = {
id: 'form-group-1',
errors: [],
};

export const WithErrors = Template.bind({});
WithErrors.args = {
id: 'form-group-2',
errors: ['some error'],
errors: 'some error',
};
34 changes: 34 additions & 0 deletions packages/comet-uswds/src/components/form-group/form-group.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { render } from '@testing-library/react';
import { axe } from 'jest-axe';
import FormGroup from './form-group';
import TextInput from '../text-input';

describe('FormGroup', () => {
test('should render with no accessibility violations', async () => {
Expand All @@ -19,6 +20,39 @@ describe('FormGroup', () => {
expect(formGroup).toBeTruthy();
});

test('should render a form group for a fieldContol', () => {
const { baseElement } = render(<FormGroup fieldControl={<></>} />);
const formGroup = baseElement.querySelector('.usa-form-group');
expect(formGroup).toBeTruthy();
});

test('should render empty when invalid fieldElement', () => {
const { baseElement } = render(<FormGroup fieldControl={3} />);
expect(baseElement).toBeTruthy();
});

test('should not render form group when no children or fieldControls provided', () => {
const { baseElement } = render(<FormGroup />);
const formGroup = baseElement.querySelector('.usa-form-group');
expect(formGroup).toBeFalsy();
});

test('should render with form group props', () => {
const { baseElement } = render(
<FormGroup
id="form-group-1"
fieldControl={<TextInput id="text-input-1" name="text-input-1" />}
required
label="label"
helperText="helper text"
errors="errors"
validationStatus="error"
/>,
);
const formGroup = baseElement.querySelector('.usa-form-group');
expect(formGroup).toBeTruthy();
});

test('should render multiple form groups successfully', () => {
const { baseElement } = render(
<>
Expand Down
Loading

0 comments on commit 98d1d57

Please sign in to comment.