Skip to content

Commit 7f5e66f

Browse files
joebuonoJoe Buono
andauthored
docs fix: switching themes wipes out state (#1815)
* persist state for all updated demos * added explanation for using Map * added explanation for why we need to persist demo state * added demo props as dependencies to the useEffect hooks * implement useMemo for use[Component]Props * moved useEffect hook into use[Component]Props Co-authored-by: Joe Buono <[email protected]>
1 parent 5a1aa3a commit 7f5e66f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1127
-420
lines changed

docs/src/pages/components/alert/demo.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import * as React from 'react';
12
import { Alert } from '@aws-amplify/ui-react';
2-
33
import { Demo } from '@/components/Demo';
44
import { AlertPropControls } from './AlertPropControls';
55
import { useAlertProps } from './useAlertProps';
6+
import { demoState } from '@/utils/demoState';
67

78
const propsToCode = (props) => {
89
return (
@@ -20,13 +21,17 @@ const propsToCode = (props) => {
2021
);
2122
};
2223

24+
const defaultAlertProps = {
25+
isDismissible: false,
26+
hasIcon: true,
27+
heading: 'Alert heading',
28+
body: 'This is the alert message',
29+
};
30+
2331
export const AlertDemo = () => {
24-
const alertProps = useAlertProps({
25-
isDismissible: false,
26-
hasIcon: true,
27-
heading: 'Alert heading',
28-
body: 'This is the alert message',
29-
});
32+
const alertProps = useAlertProps(
33+
demoState.get(Alert.displayName) || defaultAlertProps
34+
);
3035

3136
return (
3237
<Demo
Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { AlertProps } from '@aws-amplify/ui-react';
1+
import { demoState } from '@/utils/demoState';
2+
import { Alert, AlertProps } from '@aws-amplify/ui-react';
23
import * as React from 'react';
34

45
import { AlertPropControlsProps } from './AlertPropControls';
56

67
interface UseAlertProps {
7-
(initialValues: AlertProps & { body: string }): AlertPropControlsProps;
8+
(initialValues: AlertProps & { body?: string }): AlertPropControlsProps;
89
}
910

1011
export const useAlertProps: UseAlertProps = (initialValues) => {
@@ -22,16 +23,40 @@ export const useAlertProps: UseAlertProps = (initialValues) => {
2223
);
2324
const [body, setBody] = React.useState<string>(initialValues.body);
2425

25-
return {
26-
variation,
27-
setVariation,
28-
isDismissible,
29-
setIsDismissible,
30-
hasIcon,
31-
setHasIcon,
32-
heading,
33-
setHeading,
34-
body,
35-
setBody,
36-
};
26+
React.useEffect(() => {
27+
demoState.set(Alert.displayName, {
28+
variation,
29+
isDismissible,
30+
hasIcon,
31+
heading,
32+
body,
33+
});
34+
}, [variation, isDismissible, hasIcon, heading, body]);
35+
36+
return React.useMemo(
37+
() => ({
38+
variation,
39+
setVariation,
40+
isDismissible,
41+
setIsDismissible,
42+
hasIcon,
43+
setHasIcon,
44+
heading,
45+
setHeading,
46+
body,
47+
setBody,
48+
}),
49+
[
50+
variation,
51+
setVariation,
52+
isDismissible,
53+
setIsDismissible,
54+
hasIcon,
55+
setHasIcon,
56+
heading,
57+
setHeading,
58+
body,
59+
setBody,
60+
]
61+
);
3762
};

docs/src/pages/components/badge/demo.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Badge } from '@aws-amplify/ui-react';
44
import { BadgePropControls } from './BadgePropControls';
55
import { useBadgeProps } from './useBadgeProps';
66
import { Demo } from '@/components/Demo';
7+
import { demoState } from '@/utils/demoState';
78

89
const propsToCode = (badgeProps) => {
910
return (
@@ -18,10 +19,14 @@ const propsToCode = (badgeProps) => {
1819
);
1920
};
2021

22+
const defaultBadgeProps = {
23+
body: 'Badge',
24+
};
25+
2126
export const BadgeDemo = () => {
22-
const badgeProps = useBadgeProps({
23-
body: 'Badge',
24-
});
27+
const badgeProps = useBadgeProps(
28+
demoState.get(Badge.displayName) || defaultBadgeProps
29+
);
2530

2631
return (
2732
<Demo
Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { BadgeProps } from '@aws-amplify/ui-react';
1+
import { Badge, BadgeProps } from '@aws-amplify/ui-react';
22
import * as React from 'react';
3-
3+
import { demoState } from '@/utils/demoState';
44
import { BadgePropControlsProps } from './BadgePropControls';
55

66
interface UseBadgeProps {
7-
(initialValues: BadgeProps): BadgePropControlsProps;
7+
(initialValues: BadgeProps & { body?: string }): BadgePropControlsProps;
88
}
99

1010
export const useBadgeProps: UseBadgeProps = (initialValues) => {
@@ -16,12 +16,23 @@ export const useBadgeProps: UseBadgeProps = (initialValues) => {
1616
);
1717
const [body, setBody] = React.useState<string>(initialValues.body);
1818

19-
return {
20-
variation,
21-
setVariation,
22-
size,
23-
setSize,
24-
body,
25-
setBody,
26-
};
19+
React.useEffect(() => {
20+
demoState.set(Badge.displayName, {
21+
variation,
22+
size,
23+
body,
24+
});
25+
}, [variation, size, body]);
26+
27+
return React.useMemo(
28+
() => ({
29+
variation,
30+
setVariation,
31+
size,
32+
setSize,
33+
body,
34+
setBody,
35+
}),
36+
[variation, setVariation, size, setSize, body, setBody]
37+
);
2738
};

docs/src/pages/components/checkboxfield/demo.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import * as React from 'react';
12
import { CheckboxField, CheckboxFieldProps, Flex } from '@aws-amplify/ui-react';
2-
33
import { Demo } from '@/components/Demo';
44
import { CheckboxFieldPropControls } from './CheckboxFieldPropControls';
55
import { useCheckboxFieldProps } from './useCheckboxFieldProps';
6+
import { demoState } from '@/utils/demoState';
67

78
const propsToCode = ({
89
label,
@@ -22,12 +23,18 @@ const propsToCode = ({
2223
);
2324
};
2425

26+
const defaultCheckboxFieldProps = {
27+
label: 'Subscribe',
28+
name: 'subscribe',
29+
value: 'yes',
30+
};
31+
2532
export const CheckboxDemo = () => {
26-
const props = useCheckboxFieldProps({
27-
label: 'Subscribe',
28-
name: 'subscribe',
29-
value: 'yes',
30-
});
33+
const props = useCheckboxFieldProps(
34+
(demoState.get(CheckboxField.displayName) as CheckboxFieldProps) ||
35+
defaultCheckboxFieldProps
36+
);
37+
3138
return (
3239
<Demo
3340
code={propsToCode(props)}

docs/src/pages/components/checkboxfield/useCheckboxFieldProps.tsx

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as React from 'react';
22

3-
import { CheckboxFieldProps } from '@aws-amplify/ui-react';
3+
import { CheckboxFieldProps, CheckboxField } from '@aws-amplify/ui-react';
44

55
import { CheckboxFieldPropControlsProps } from './CheckboxFieldPropControls';
6+
import { demoState } from '@/utils/demoState';
67

78
interface UseCheckboxFieldProps {
89
(initialValues: CheckboxFieldProps): CheckboxFieldPropControlsProps;
@@ -31,21 +32,51 @@ export const useCheckboxFieldProps: UseCheckboxFieldProps = (initialValues) => {
3132
CheckboxFieldProps['labelPosition']
3233
>(initialValues.labelPosition);
3334

34-
return {
35-
checked,
36-
children: label,
37-
setChecked,
38-
isDisabled,
39-
setIsDisabled,
40-
label,
41-
setLabel,
42-
labelPosition,
43-
setLabelPosition,
44-
name,
45-
setName,
46-
size,
47-
setSize,
48-
value,
49-
setValue,
50-
};
35+
React.useEffect(() => {
36+
demoState.set(CheckboxField.displayName, {
37+
checked,
38+
isDisabled,
39+
label,
40+
name,
41+
size,
42+
value,
43+
labelPosition,
44+
});
45+
}, [checked, isDisabled, label, name, size, value, labelPosition]);
46+
47+
return React.useMemo(
48+
() => ({
49+
checked,
50+
children: label,
51+
setChecked,
52+
isDisabled,
53+
setIsDisabled,
54+
label,
55+
setLabel,
56+
labelPosition,
57+
setLabelPosition,
58+
name,
59+
setName,
60+
size,
61+
setSize,
62+
value,
63+
setValue,
64+
}),
65+
[
66+
checked,
67+
setChecked,
68+
isDisabled,
69+
setIsDisabled,
70+
label,
71+
setLabel,
72+
labelPosition,
73+
setLabelPosition,
74+
name,
75+
setName,
76+
size,
77+
setSize,
78+
value,
79+
setValue,
80+
]
81+
);
5182
};

docs/src/pages/components/divider/demo.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Divider, Flex, Text } from '@aws-amplify/ui-react';
33
import { DividerPropControls } from './DividerPropControls';
44
import { useDividerProps } from './useDividerProps';
55
import { Demo } from '@/components/Demo';
6+
import { demoState } from '@/utils/demoState';
67

78
const propsToCode = (props) => {
89
return (
@@ -18,10 +19,14 @@ const propsToCode = (props) => {
1819
);
1920
};
2021

22+
const defaultDividerProps = {
23+
orientation: 'horizontal',
24+
};
25+
2126
export const DividerDemo = () => {
22-
const dividerProps = useDividerProps({
23-
orientation: 'horizontal',
24-
});
27+
const dividerProps = useDividerProps(
28+
demoState.get(Divider.displayName) || defaultDividerProps
29+
);
2530
const direction =
2631
dividerProps.orientation === 'horizontal' ? 'column' : 'row';
2732

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { DividerOptions } from '@aws-amplify/ui-react';
1+
import { Divider, DividerOptions } from '@aws-amplify/ui-react';
22
import * as React from 'react';
33

44
import { DividerPropControlsProps } from './DividerPropControls';
5+
import { demoState } from '@/utils/demoState';
56

67
interface UseDividerProps {
78
(initialValues: DividerOptions): DividerPropControlsProps;
@@ -18,12 +19,23 @@ export const useDividerProps: UseDividerProps = (initialValues) => {
1819
initialValues.label
1920
);
2021

21-
return {
22-
size,
23-
setSize,
24-
orientation,
25-
setOrientation,
26-
label,
27-
setLabel,
28-
};
22+
React.useEffect(() => {
23+
demoState.set(Divider.displayName, {
24+
size,
25+
orientation,
26+
label,
27+
});
28+
}, [size, orientation, label]);
29+
30+
return React.useMemo(
31+
() => ({
32+
size,
33+
setSize,
34+
orientation,
35+
setOrientation,
36+
label,
37+
setLabel,
38+
}),
39+
[size, setSize, orientation, setOrientation, label, setLabel]
40+
);
2941
};

docs/src/pages/components/expander/demo.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Expander, ExpanderItem } from '@aws-amplify/ui-react';
44
import { ExpanderPropControls } from './ExpanderPropControls';
55
import { useExpanderProps } from './useExpanderProps';
66
import { Demo } from '@/components/Demo';
7+
import { demoState } from '@/utils/demoState';
78

89
const propsToCode = (expanderProps) => {
910
return (
@@ -29,11 +30,15 @@ const propsToCode = (expanderProps) => {
2930
);
3031
};
3132

33+
const defaultExpanderProps = {
34+
type: 'single',
35+
isCollapsible: false,
36+
};
37+
3238
export const ExpanderDemo = () => {
33-
const expanderProps = useExpanderProps({
34-
type: 'single',
35-
isCollapsible: false,
36-
});
39+
const expanderProps = useExpanderProps(
40+
demoState.get(Expander.displayName) || defaultExpanderProps
41+
);
3742

3843
return (
3944
<Demo

0 commit comments

Comments
 (0)