|
| 1 | +import { PasswordField, PasswordFieldProps } from '@aws-amplify/ui-react'; |
1 | 2 | import * as React from 'react'; |
2 | 3 |
|
| 4 | +import { Demo } from '@/components/Demo'; |
3 | 5 | import { |
4 | | - Flex, |
5 | | - FlexContainerStyleProps, |
6 | | - PasswordField, |
7 | | - PasswordFieldProps, |
8 | | - TextFieldProps, |
9 | | - View, |
10 | | -} from '@aws-amplify/ui-react'; |
11 | | - |
12 | | -import { Example } from '@/components/Example'; |
13 | | -import { GetFieldControls } from '../shared/GetFieldControls'; |
14 | | -import { useFlexContainerStyleProps } from '../shared/useFlexContainerStyleProps'; |
| 6 | + filterDemoProps, |
| 7 | + getDemoProps, |
| 8 | + objectEntriesToPropString, |
| 9 | +} from '../utils/demoProps'; |
| 10 | +import { PasswordFieldPropControls } from './passwordFieldPropControls'; |
15 | 11 | import { usePasswordFieldProps } from './usePasswordFieldProps'; |
| 12 | +import { demoState } from '@/utils/demoState'; |
| 13 | + |
| 14 | +export const propsToCode = (props) => { |
| 15 | + const filteredProps = filterDemoProps(props); |
| 16 | + return ` |
| 17 | +<PasswordField |
| 18 | +${objectEntriesToPropString(Object.entries(filteredProps))} |
| 19 | +/>`; |
| 20 | +}; |
16 | 21 |
|
17 | 22 | export const PasswordFieldDemo = () => { |
18 | | - const flexStyleProps = useFlexContainerStyleProps({ |
19 | | - alignItems: '', |
20 | | - alignContent: '', |
21 | | - direction: 'column', |
22 | | - gap: '', |
23 | | - justifyContent: '', |
24 | | - wrap: 'nowrap', |
25 | | - }); |
26 | | - const textFieldProps = usePasswordFieldProps({ |
27 | | - autoComplete: 'new-password', |
28 | | - defaultValue: '', |
29 | | - descriptiveText: 'Please enter password with at least 8 characters', |
30 | | - errorMessage: '', |
31 | | - hasError: false, |
32 | | - inputMode: 'text', |
33 | | - isDisabled: false, |
34 | | - isReadOnly: false, |
35 | | - isRequired: false, |
36 | | - label: 'Password', |
37 | | - labelHidden: false, |
38 | | - name: 'password', |
39 | | - placeholder: '', |
40 | | - size: '', |
41 | | - type: null, |
42 | | - value: undefined, |
43 | | - variation: '', |
44 | | - hideShowPassword: false, |
45 | | - }); |
46 | | - const FlexPropControls = GetFieldControls({ |
47 | | - typeName: 'Flex', |
48 | | - fields: flexStyleProps, |
49 | | - }); |
50 | | - const TextFieldPropControls = GetFieldControls({ |
51 | | - typeName: 'PasswordField', |
52 | | - fields: textFieldProps, |
53 | | - }); |
54 | | - const [ |
55 | | - [alignItems], |
56 | | - [alignContent], |
57 | | - [direction], |
58 | | - [gap], |
59 | | - [justifyContent], |
60 | | - [wrap], |
61 | | - ] = flexStyleProps; |
62 | | - const [ |
63 | | - [autoComplete], |
64 | | - [defaultValue], |
65 | | - [descriptiveText], |
66 | | - [errorMessage], |
67 | | - [hasError], |
68 | | - [inputMode], |
69 | | - [isDisabled], |
70 | | - [isReadOnly], |
71 | | - [isRequired], |
72 | | - [label], |
73 | | - [labelHidden], |
74 | | - [name], |
75 | | - [placeholder], |
76 | | - [size], |
77 | | - [type], |
78 | | - [value], |
79 | | - [variation], |
80 | | - [hideShowPassword], |
81 | | - ] = textFieldProps; |
| 23 | + const passwordFieldProps = usePasswordFieldProps( |
| 24 | + (demoState.get(PasswordField.displayName) as PasswordFieldProps) || { |
| 25 | + autoComplete: 'new-password', |
| 26 | + defaultValue: '', |
| 27 | + descriptiveText: 'Please enter password', |
| 28 | + errorMessage: '', |
| 29 | + hasError: false, |
| 30 | + hideShowPassword: false, |
| 31 | + isDisabled: false, |
| 32 | + isReadOnly: false, |
| 33 | + isRequired: false, |
| 34 | + label: 'Password', |
| 35 | + labelHidden: false, |
| 36 | + name: 'password', |
| 37 | + placeholder: '', |
| 38 | + size: 'small', |
| 39 | + value: '', |
| 40 | + variation: null, |
| 41 | + } |
| 42 | + ); |
| 43 | + const demoProps = [ |
| 44 | + 'autoComplete', |
| 45 | + 'defaultValue', |
| 46 | + 'descriptiveText', |
| 47 | + 'errorMessage', |
| 48 | + 'hasError', |
| 49 | + 'hideShowPassword', |
| 50 | + 'isDisabled', |
| 51 | + 'isReadOnly', |
| 52 | + 'isRequired', |
| 53 | + 'label', |
| 54 | + 'labelHidden', |
| 55 | + 'name', |
| 56 | + 'placeholder', |
| 57 | + 'size', |
| 58 | + 'value', |
| 59 | + 'variation', |
| 60 | + ]; |
| 61 | + const passwordFieldDemoProps = getDemoProps(passwordFieldProps, demoProps); |
82 | 62 | return ( |
83 | | - <View width="100%"> |
84 | | - {TextFieldPropControls} |
85 | | - {FlexPropControls} |
86 | | - <Example> |
87 | | - <View maxWidth="500px" padding="2rem"> |
88 | | - <Flex gap="2rem" direction="column"> |
89 | | - <form> |
90 | | - <input |
91 | | - autoComplete="username" |
92 | | - name="username" |
93 | | - |
94 | | - type="hidden" |
95 | | - /> |
96 | | - <PasswordField |
97 | | - alignContent={ |
98 | | - alignContent as FlexContainerStyleProps['alignContent'] |
99 | | - } |
100 | | - alignItems={alignItems as FlexContainerStyleProps['alignItems']} |
101 | | - autoComplete={autoComplete as TextFieldProps['autoComplete']} |
102 | | - descriptiveText={ |
103 | | - descriptiveText as TextFieldProps['descriptiveText'] |
104 | | - } |
105 | | - defaultValue={defaultValue as TextFieldProps['defaultValue']} |
106 | | - direction={direction as FlexContainerStyleProps['direction']} |
107 | | - errorMessage={errorMessage as TextFieldProps['errorMessage']} |
108 | | - gap={gap as FlexContainerStyleProps['gap']} |
109 | | - hasError={hasError as unknown as boolean} |
110 | | - inputMode={inputMode as TextFieldProps['inputMode']} |
111 | | - isDisabled={isDisabled as unknown as boolean} |
112 | | - isReadOnly={isReadOnly as unknown as boolean} |
113 | | - isRequired={isRequired as unknown as boolean} |
114 | | - justifyContent={ |
115 | | - justifyContent as FlexContainerStyleProps['justifyContent'] |
116 | | - } |
117 | | - label={label as TextFieldProps['label']} |
118 | | - labelHidden={labelHidden as unknown as boolean} |
119 | | - name={name as TextFieldProps['name']} |
120 | | - placeholder={placeholder as TextFieldProps['placeholder']} |
121 | | - size={size as TextFieldProps['size']} |
122 | | - variation={variation as TextFieldProps['variation']} |
123 | | - wrap={wrap as FlexContainerStyleProps['wrap']} |
124 | | - hideShowPassword={ |
125 | | - hideShowPassword as unknown as PasswordFieldProps['hideShowPassword'] |
126 | | - } |
127 | | - /> |
128 | | - </form> |
129 | | - </Flex> |
130 | | - </View> |
131 | | - </Example> |
132 | | - </View> |
| 63 | + <Demo |
| 64 | + code={propsToCode(passwordFieldDemoProps)} |
| 65 | + propControls={<PasswordFieldPropControls {...passwordFieldProps} />} |
| 66 | + > |
| 67 | + <PasswordField |
| 68 | + autoComplete={passwordFieldProps.autoComplete} |
| 69 | + descriptiveText={passwordFieldProps.descriptiveText} |
| 70 | + defaultValue={passwordFieldProps.defaultValue} |
| 71 | + errorMessage={passwordFieldProps.errorMessage} |
| 72 | + hasError={passwordFieldProps.hasError} |
| 73 | + hideShowPassword={passwordFieldProps.hideShowPassword} |
| 74 | + isDisabled={passwordFieldProps.isDisabled} |
| 75 | + isReadOnly={passwordFieldProps.isReadOnly} |
| 76 | + isRequired={passwordFieldProps.isRequired} |
| 77 | + label={passwordFieldProps.label} |
| 78 | + labelHidden={passwordFieldProps.labelHidden} |
| 79 | + name={passwordFieldProps.name} |
| 80 | + placeholder={passwordFieldProps.placeholder} |
| 81 | + size={passwordFieldProps.size} |
| 82 | + value={passwordFieldProps.value || null} |
| 83 | + variation={passwordFieldProps.variation} |
| 84 | + /> |
| 85 | + </Demo> |
133 | 86 | ); |
134 | 87 | }; |
0 commit comments