Skip to content

Commit

Permalink
feat(react/ui): add Fieldset Primitive (#4348)
Browse files Browse the repository at this point in the history
* initial commit

* clean up, add new tokens

* docs  snapshots

* docs

* docs

* types and docs

* row column docs stuff

* add size tokens, update docs

* docs snaps

* reset css changes

* fieldset context

* Update fieldset to use context, update form inputs to use fieldset context

* update detecting if disabled by fieldset

* type, theme, and docs updates

* docs updates, snapshots

* Remove isNestedFieldset from context, update usage across controls. Update docs

* Update packages/react/src/primitives/Fieldset/useFieldset.ts

Co-authored-by: Caleb Pollman <cpollman@amazon.com>

* Update packages/react/src/primitives/Fieldset/Fieldset.tsx

Co-authored-by: Caleb Pollman <cpollman@amazon.com>

* Update legend prop description, remove test file in docs

* update theme and docs styling examples

* Update type for fieldset size, update description

* Update snapshots, tests

* update tests for nested fieldsets

* Create afraid-waves-switch.md

* remove unused imports

* Update docs/src/pages/[platform]/components/fieldset/examples/DisabledFieldset.tsx

Co-authored-by: Danny Banks <djb@amazon.com>

* Update docs/src/pages/[platform]/components/fieldset/react.mdx

Co-authored-by: Danny Banks <djb@amazon.com>

* use proper BEM syntax

* udpate snapshots

* update changeset

* update useFieldset to return isFieldsetDisabled to avoid re-mapping

* Update docs/src/pages/[platform]/components/fieldset/examples/FieldsetDirection.tsx

---------

Co-authored-by: Heather Buchel <buchel@amazon.com>
Co-authored-by: Caleb Pollman <cpollman@amazon.com>
Co-authored-by: Danny Banks <djb@amazon.com>
  • Loading branch information
4 people committed Aug 29, 2023
1 parent ec495a6 commit 2769685
Show file tree
Hide file tree
Showing 49 changed files with 2,067 additions and 21 deletions.
13 changes: 13 additions & 0 deletions .changeset/afraid-waves-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"@aws-amplify/ui-react": minor
"@aws-amplify/ui": minor
---

feat(react/ui): add new Fieldset Primitive

Usage:
```
<Fieldset legend="Legend test">
// Fieldset content/form controls
</Fieldset>
```
72 changes: 72 additions & 0 deletions docs/__tests__/__snapshots__/cssvars-table.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3142,6 +3142,78 @@ exports[`CSS Variables Table 1`] = `
\\"variable\\": \\"--amplify-components-fieldmessages-error-font-size\\",
\\"value\\": \\"var(--amplify-font-sizes-small)\\"
},
{
\\"variable\\": \\"--amplify-components-fieldset-background-color\\",
\\"value\\": \\"transparent\\"
},
{
\\"variable\\": \\"--amplify-components-fieldset-border-radius\\",
\\"value\\": \\"var(--amplify-radii-xs)\\"
},
{
\\"variable\\": \\"--amplify-components-fieldset-flex-direction\\",
\\"value\\": \\"column\\"
},
{
\\"variable\\": \\"--amplify-components-fieldset-gap\\",
\\"value\\": \\"var(--amplify-components-field-gap)\\"
},
{
\\"variable\\": \\"--amplify-components-fieldset-large-gap\\",
\\"value\\": \\"var(--amplify-components-field-large-gap)\\"
},
{
\\"variable\\": \\"--amplify-components-fieldset-legend-color\\",
\\"value\\": \\"var(--amplify-colors-font-primary)\\"
},
{
\\"variable\\": \\"--amplify-components-fieldset-legend-font-size\\",
\\"value\\": \\"var(--amplify-components-field-font-size)\\"
},
{
\\"variable\\": \\"--amplify-components-fieldset-legend-font-weight\\",
\\"value\\": \\"var(--amplify-font-weights-bold)\\"
},
{
\\"variable\\": \\"--amplify-components-fieldset-legend-large-font-size\\",
\\"value\\": \\"var(--amplify-components-field-large-font-size)\\"
},
{
\\"variable\\": \\"--amplify-components-fieldset-legend-line-height\\",
\\"value\\": \\"var(--amplify-line-heights-medium)\\"
},
{
\\"variable\\": \\"--amplify-components-fieldset-legend-small-font-size\\",
\\"value\\": \\"var(--amplify-components-field-small-font-size)\\"
},
{
\\"variable\\": \\"--amplify-components-fieldset-outlined-border-color\\",
\\"value\\": \\"var(--amplify-colors-neutral-40)\\"
},
{
\\"variable\\": \\"--amplify-components-fieldset-outlined-border-style\\",
\\"value\\": \\"solid\\"
},
{
\\"variable\\": \\"--amplify-components-fieldset-outlined-border-width\\",
\\"value\\": \\"var(--amplify-border-widths-small)\\"
},
{
\\"variable\\": \\"--amplify-components-fieldset-outlined-large-padding\\",
\\"value\\": \\"var(--amplify-space-large)\\"
},
{
\\"variable\\": \\"--amplify-components-fieldset-outlined-padding\\",
\\"value\\": \\"var(--amplify-space-medium)\\"
},
{
\\"variable\\": \\"--amplify-components-fieldset-outlined-small-padding\\",
\\"value\\": \\"var(--amplify-space-small)\\"
},
{
\\"variable\\": \\"--amplify-components-fieldset-small-gap\\",
\\"value\\": \\"var(--amplify-components-field-small-gap)\\"
},
{
\\"variable\\": \\"--amplify-components-fileuploader-dropzone-active-background-color\\",
\\"value\\": \\"var(--amplify-colors-brand-primary-10)\\"
Expand Down
257 changes: 257 additions & 0 deletions docs/__tests__/__snapshots__/props-table.test.ts.snap

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/__tests__/__snapshots__/sitemap.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ exports[`Sitemap Snapshot 1`] = `
/react/components/divider,
/react/components/dropzone,
/react/components/expander,
/react/components/fieldset,
/react/components/flex,
/react/components/grid,
/react/components/heading,
Expand Down
7 changes: 7 additions & 0 deletions docs/src/data/links.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,13 @@ export const inputComponents: ComponentNavItem[] = [
platforms: ['react'],
icon: MdSearch,
},
{
href: '/components/fieldset',
label: 'Fieldset',
body: `Fieldsets are used to group related form fields.`,
platforms: ['react'],
icon: MdSearch,
},
{
href: '/components/input',
label: 'Input',
Expand Down
111 changes: 111 additions & 0 deletions docs/src/pages/[platform]/components/fieldset/FieldsetPropControls.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import {
FieldsetProps,
Flex,
SelectField,
SwitchField,
TextField,
} from '@aws-amplify/ui-react';
import * as React from 'react';

export interface FieldsetPropControlsProps extends FieldsetProps {
setDirection: (
value: React.SetStateAction<FieldsetProps['direction']>
) => void;
setIsDisabled: (
value: React.SetStateAction<FieldsetProps['isDisabled']>
) => void;
setLegend: (value: React.SetStateAction<FieldsetProps['legend']>) => void;
setLegendHidden: (
value: React.SetStateAction<FieldsetProps['legendHidden']>
) => void;
setSize: (value: React.SetStateAction<FieldsetProps['size']>) => void;
setVariation: (
value: React.SetStateAction<FieldsetProps['variation']>
) => void;
}

interface FieldsetPropControlsInterface {
(props: FieldsetPropControlsProps): JSX.Element;
}

export const FieldsetPropControls: FieldsetPropControlsInterface = ({
direction,
isDisabled,
legend,
legendHidden,
size,
variation,
setDirection,
setIsDisabled,
setLegend,
setLegendHidden,
setSize,
setVariation,
}) => {
return (
<Flex direction="column">
<TextField
label="Legend"
value={legend as string}
onChange={(event) =>
setLegend(event.target.value as FieldsetProps['legend'])
}
/>
<SelectField
name="variation"
label="Variation"
value={variation}
onChange={(event) =>
setVariation(event.target.value as FieldsetProps['variation'])
}
>
<option value="outlined">outlined (default)</option>
<option value="plain">plain</option>
</SelectField>
<SelectField
label="direction"
name="direction"
value={direction as string}
onChange={(event) =>
setDirection(event.target.value as FieldsetProps['direction'])
}
>
<option value="column">column</option>
<option value="column-reverse">column-reverse</option>
<option value="row">row</option>
<option value="row-reverse">row-reverse</option>
</SelectField>
<SelectField
name="size"
label="Size"
defaultValue=""
value={size}
onChange={(event) =>
setSize(event.target.value as FieldsetProps['size'])
}
>
<option value="small">small</option>
<option value="">default</option>
<option value="large">large</option>
</SelectField>
<SwitchField
label="isDisabled"
isChecked={isDisabled}
labelPosition="end"
onChange={(event) => {
setIsDisabled(event.target.checked as FieldsetProps['isDisabled']);
}}
/>
<SwitchField
label="legendHidden"
isChecked={legendHidden}
labelPosition="end"
onChange={(event) => {
setLegendHidden(
event.target.checked as FieldsetProps['legendHidden']
);
}}
/>
</Flex>
);
};
75 changes: 75 additions & 0 deletions docs/src/pages/[platform]/components/fieldset/demo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import * as React from 'react';
import { Fieldset, CheckboxField, FieldsetProps } from '@aws-amplify/ui-react';
import { Demo } from '@/components/Demo';
import { FieldsetPropControls } from './FieldsetPropControls';
import { useFieldsetProps } from './useFieldsetProps';
import { demoState } from '@/utils/demoState';
import { getPropString } from '../utils/getPropString';

const propsToCode = (props) => {
return (
`<Fieldset` +
getPropString(props.legend, 'legend') +
getPropString(props.variation, 'variation') +
getPropString(props.size, 'size') +
getPropString(props.isDisabled, 'isDisabled') +
getPropString(props.direction, 'direction') +
`>
<CheckboxField
label="Apple"` +
(props.size ? `\n size=${JSON.stringify(props.size)}` : '') +
`
name="apple"
/>
<CheckboxField
label="Pear"` +
(props.size ? `\n size=${JSON.stringify(props.size)}` : '') +
`
name="pear"
/>
</Fieldset>`
);
};

const defaultFieldsetProps: FieldsetProps = {
legend: 'Favorite fruits',
legendHidden: false,
isDisabled: false,
direction: 'column',
variation: 'outlined',
};

export const FieldsetDemo = () => {
const fieldsetProps = useFieldsetProps(
(demoState.get('Fieldset') as FieldsetProps) || defaultFieldsetProps
);

return (
<Demo
code={propsToCode(fieldsetProps)}
propControls={<FieldsetPropControls {...fieldsetProps} />}
>
<Fieldset
variation={fieldsetProps.variation}
size={fieldsetProps.size}
isDisabled={fieldsetProps.isDisabled}
legend={fieldsetProps.legend}
legendHidden={fieldsetProps.legendHidden}
direction={fieldsetProps.direction}
>
<CheckboxField
label="Apple"
size={fieldsetProps.size}
name="apple"
value="yes"
/>
<CheckboxField
label="Pear"
size={fieldsetProps.size}
name="pear"
value="yes"
/>
</Fieldset>
</Demo>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Fieldset } from '@aws-amplify/ui-react';

export const BasicFieldset = () => {
return <Fieldset legend="Basic fieldset">Fieldset content</Fieldset>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Fieldset, Text, TextField } from '@aws-amplify/ui-react';

export const DisabledFieldset = () => {
return (
<Fieldset legend="Disabled Fieldset" isDisabled>
<Text fontStyle="italic" variation="tertiary">
The input in this fieldset is disabled because of the parent fieldset.
</Text>
<TextField label="Test input" />
<Fieldset legend="Nested fieldset">
<Text fontStyle="italic" variation="tertiary">
This input is also disabled because the fieldset above it is disabled,
even though its immediate parent fieldset is not disabled.
</Text>
<TextField label="Test nested input" />
</Fieldset>
</Fieldset>
);
};
Loading

0 comments on commit 2769685

Please sign in to comment.