Skip to content
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

feat(Fieldset)!: remove top-level sub-component(s) #1695

Merged
merged 1 commit into from
Jul 18, 2023
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
36 changes: 36 additions & 0 deletions src/components/Fieldset/Fieldset.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,39 @@
/* Overrides default browser styling. */
margin: var(--eds-size-2);
}

/*------------------------------------*\
# FIELDSET ITEMS
\*------------------------------------*/

/**
* The contents of the fieldset. Spaces them apart.
*/
.fieldset-items > :not(:last-child) {
margin-bottom: var(--eds-size-1-and-half);
}

/*------------------------------------*\
# FIELDSET LEGEND
\*------------------------------------*/

/**
* A label that's rendered as a <legend> for fieldsets.
* It contains the same characteristics as a label (ability to add flag for optional field, etc),
* but semantically/stylistically a bit different.
*/
.fieldset-legend {
/* Removes some default browser styles. */
@mixin legendReset;
font: var(--eds-theme-typography-form-label);
/* Adjust margin between legend and option list */
margin-bottom: var(--eds-size-2);
}

/**
* Label flag to mark whether or not a field is required
* or optional. Currently a flag is only present for optional fields
*/
.fieldset-legend__flag {
font: var(--eds-theme-typography-body-sm);
}
9 changes: 5 additions & 4 deletions src/components/Fieldset/Fieldset.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import type { StoryObj, Meta } from '@storybook/react';
import React, { useState } from 'react';
import { Fieldset } from './Fieldset';
import type { FieldsetLegendProps } from './Fieldset';
import Checkbox from '../Checkbox';
import FieldsetItems from '../FieldsetItems';
import type { FieldsetLegendProps } from '../FieldsetLegend';
import FieldsetLegend from '../FieldsetLegend';

export default {
title: 'Components/Fieldset',
component: Fieldset,
parameters: {
badges: ['1.0'],
},
subcomponents: { FieldsetLegend, FieldsetItems },
subcomponents: {
FieldsetLegend: Fieldset.Legend,
FieldsetItems: Fieldset.Items,
},
argTypes: {
children: {
control: {
Expand Down
79 changes: 76 additions & 3 deletions src/components/Fieldset/Fieldset.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import clsx from 'clsx';
import type { ReactNode } from 'react';
import type { ComponentPropsWithoutRef, ElementType, ReactNode } from 'react';

import React from 'react';
import FieldsetItems from '../FieldsetItems';
import FieldsetLegend from '../FieldsetLegend';
import styles from './Fieldset.module.css';

type FieldsetProps = {
Expand All @@ -20,6 +19,37 @@ type FieldsetProps = {
className?: string;
} & React.HTMLAttributes<HTMLFieldSetElement>;

export type FieldsetItemsProps<T extends ElementType> = {
/**
* The content of the control elements in the fieldset.
*/
children: ReactNode;
/**
* Type of element the immediate wrapper around the contents should be.
* @default 'div'
*/
as?: T;
/**
* Additional classnames passed in for styling.
*/
className?: string;
};

export type FieldsetLegendProps = {
/**
* CSS class names that can be appended to the component.
*/
className?: string;
/**
* String to indicate required or optional state.
*/
optionalLabel?: '(required)' | '(optional)';
/**
* Legend text string that names the fieldset.
*/
text: string;
};

/**
* `import {Fieldset} from "@chanzuckerberg/eds";`
*
Expand All @@ -30,5 +60,48 @@ export function Fieldset({ children, className }: FieldsetProps) {
return <fieldset className={componentClassName}>{children}</fieldset>;
}

/**
* Helper sub-component for styling the control elements in the component.
*/
export const FieldsetItems = <T extends ElementType = 'div'>({
children,
as,
className,
...props
}: FieldsetItemsProps<T> &
Omit<ComponentPropsWithoutRef<T>, keyof FieldsetItemsProps<T>>) => {
const componentClassName = clsx(styles['fieldset-items'], className);
const Component = as || 'div';
// Disable this once EDS is updated to React 18+ or lib specifies version.
// There is a type mismatch betwwen what gets pulled in via react-beautiful-dnd
// and React 16. that library imports the latest react types regardless of
// installed version.
return (
<Component className={componentClassName} {...props}>
{children}
</Component>
);
};

/**
* Helper sub-component for styling the legend in a fieldset.
*/
const FieldsetLegend = ({
className,
optionalLabel,
text,
...other
}: FieldsetLegendProps) => {
const componentClassName = clsx(styles['fieldset-legend'], className);
return (
<legend className={componentClassName} {...other}>
{text}{' '}
{optionalLabel && (
<span className={styles['fieldset-legend__flag']}>{optionalLabel}</span>
)}
</legend>
);
};

Fieldset.Items = FieldsetItems;
Fieldset.Legend = FieldsetLegend;
10 changes: 0 additions & 10 deletions src/components/FieldsetItems/FieldsetItems.module.css

This file was deleted.

45 changes: 0 additions & 45 deletions src/components/FieldsetItems/FieldsetItems.tsx

This file was deleted.

2 changes: 0 additions & 2 deletions src/components/FieldsetItems/index.ts

This file was deleted.

26 changes: 0 additions & 26 deletions src/components/FieldsetLegend/FieldsetLegend.module.css

This file was deleted.

40 changes: 0 additions & 40 deletions src/components/FieldsetLegend/FieldsetLegend.tsx

This file was deleted.

2 changes: 0 additions & 2 deletions src/components/FieldsetLegend/index.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/components/FiltersCheckboxField/FiltersCheckboxField.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import clsx from 'clsx';
import React from 'react';
import Fieldset from '../Fieldset';
import FieldsetLegend from '../FieldsetLegend';
import styles from './FiltersCheckboxField.module.css';

export type Props = {
Expand Down Expand Up @@ -40,7 +39,7 @@ export const FiltersCheckboxField = ({
return (
<Fieldset className={componentClassName}>
{legend && (
<FieldsetLegend
<Fieldset.Legend
className={styles['filters-fieldset__legend']}
text={legend}
/>
Expand Down
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ export { default as DropdownMenu } from './components/DropdownMenu';
export { default as DropdownMenuItem } from './components/DropdownMenuItem';
export { default as FieldNote } from './components/FieldNote';
export { default as Fieldset } from './components/Fieldset';
export { default as FieldsetItems } from './components/FieldsetItems';
export { default as FieldsetLegend } from './components/FieldsetLegend';
export { default as FiltersButton } from './components/FiltersButton';
export { default as FiltersCheckboxField } from './components/FiltersCheckboxField';
export { default as FiltersDrawer } from './components/FiltersDrawer';
Expand Down