|
1 | 1 | import * as React from 'react'; |
2 | | - |
3 | | -import { |
4 | | - TextField, |
5 | | - Flex, |
6 | | - FlexContainerStyleProps, |
7 | | - TextFieldProps, |
8 | | -} from '@aws-amplify/ui-react'; |
9 | | - |
| 2 | +import { TextField, TextFieldProps } from '@aws-amplify/ui-react'; |
10 | 3 | import { Demo } from '@/components/Demo'; |
| 4 | +import { TextFieldPropControls } from './TextFieldPropControls'; |
11 | 5 | import { useTextFieldProps } from './useTextFieldProps'; |
12 | | -import { GetFieldControls } from '../shared/GetFieldControls'; |
13 | | -import { useFlexContainerStyleProps } from '../shared/useFlexContainerStyleProps'; |
14 | | - |
15 | | -export const TextFieldDemo = () => { |
16 | | - const flexStyleProps = useFlexContainerStyleProps({ |
17 | | - alignItems: '', |
18 | | - alignContent: '', |
19 | | - direction: 'column', |
20 | | - gap: '', |
21 | | - justifyContent: '', |
22 | | - wrap: 'nowrap', |
23 | | - }); |
24 | | - const textFieldProps = useTextFieldProps({ |
25 | | - autoComplete: 'off', |
26 | | - defaultValue: null, |
27 | | - descriptiveText: 'Enter a valid last name', |
28 | | - errorMessage: '', |
29 | | - hasError: false, |
30 | | - inputMode: 'text', |
31 | | - isDisabled: false, |
32 | | - isReadOnly: false, |
33 | | - isRequired: false, |
34 | | - label: 'Last name', |
35 | | - labelHidden: false, |
36 | | - name: 'last_name', |
37 | | - placeholder: 'Baggins', |
38 | | - size: '', |
39 | | - type: 'text', |
40 | | - value: null, |
41 | | - variation: '', |
42 | | - }); |
43 | | - const FlexPropControls = GetFieldControls({ |
44 | | - typeName: 'Flex', |
45 | | - fields: flexStyleProps, |
46 | | - }); |
47 | | - const TextFieldPropControls = GetFieldControls({ |
48 | | - typeName: 'TextField', |
49 | | - fields: textFieldProps, |
50 | | - }); |
51 | | - const [ |
52 | | - [alignItems], |
53 | | - [alignContent], |
54 | | - [direction], |
55 | | - [gap], |
56 | | - [justifyContent], |
57 | | - [wrap], |
58 | | - ] = flexStyleProps; |
59 | | - const [ |
60 | | - [autoComplete], |
61 | | - [defaultValue], // leave unused `defaultValue` since destructuring order is important |
62 | | - [descriptiveText], |
63 | | - [errorMessage], |
64 | | - [hasError], |
65 | | - [inputMode], |
66 | | - [isDisabled], |
67 | | - [isReadOnly], |
68 | | - [isRequired], |
69 | | - [label], |
70 | | - [labelHidden], |
71 | | - [name], |
72 | | - [placeholder], |
73 | | - [size], |
74 | | - [type], |
75 | | - [value], |
76 | | - [variation], |
77 | | - ] = textFieldProps; |
| 6 | +import { demoState } from '@/utils/demoState'; |
| 7 | +import { getPropString } from '../utils/getPropString'; |
78 | 8 |
|
79 | | - const code = |
| 9 | +const propsToCode = (props) => { |
| 10 | + return ( |
80 | 11 | `<TextField` + |
81 | | - (alignContent |
82 | | - ? ` |
83 | | - alignContent={${alignContent}}` |
84 | | - : '') + |
85 | | - (alignItems |
86 | | - ? ` |
87 | | - alignItems={${alignItems}}` |
88 | | - : '') + |
89 | | - ` |
90 | | - autoComplete="${autoComplete}" |
91 | | - descriptiveText="${descriptiveText}"` + |
92 | | - (defaultValue |
93 | | - ? ` |
94 | | - defaultValue="${defaultValue}"` |
95 | | - : '') + |
96 | | - ` |
97 | | - direction="${direction}"` + |
98 | | - (errorMessage |
99 | | - ? ` |
100 | | - errorMessage="${errorMessage}"` |
101 | | - : '') + |
102 | | - (gap |
103 | | - ? ` |
104 | | - gap="${gap}"` |
105 | | - : '') + |
106 | | - ` |
107 | | - hasError={${hasError}} |
108 | | - inputMode="${inputMode}" |
109 | | - isDisabled={${isDisabled}} |
110 | | - isReadOnly={${isReadOnly}} |
111 | | - isRequired={${isRequired}}` + |
112 | | - (justifyContent |
113 | | - ? ` |
114 | | - justifyContent={${justifyContent}}` |
115 | | - : '') + |
116 | | - ` |
117 | | - label="${label}" |
118 | | - labelHidden={${labelHidden}} |
119 | | - name="${name}" |
120 | | - placeholder="${placeholder}"` + |
121 | | - (size |
122 | | - ? ` |
123 | | - size="${size}"` |
124 | | - : '') + |
125 | | - ` |
126 | | - type="${type}"` + |
127 | | - (value |
128 | | - ? ` |
129 | | - value="${value}"` |
130 | | - : '') + |
131 | | - (variation |
132 | | - ? ` |
133 | | - variation="${variation}"` |
134 | | - : '') + |
135 | | - ` |
136 | | - wrap="${wrap}" |
137 | | - onChange={(e) => console.info(e.currentTarget.value)} |
138 | | - onInput={(e) => console.info('input fired:', e.currentTarget.value)} |
139 | | - onCopy={(e) => console.info('onCopy fired:', e.currentTarget.value)} |
140 | | - onCut={(e) => console.info('onCut fired:', e.currentTarget.value)} |
141 | | - onPaste={(e) => console.info('onPaste fired:', e.currentTarget.value)} |
142 | | - onSelect={(e) => |
143 | | - console.info( |
144 | | - 'onSelect fired:', |
145 | | - e.currentTarget.value.substring( |
146 | | - e.currentTarget.selectionStart, |
147 | | - e.currentTarget.selectionEnd |
148 | | - ) |
149 | | - ) |
150 | | - } |
151 | | -/>`; |
| 12 | + getPropString(props.variation, 'variation') + |
| 13 | + getPropString(props.size, 'size') + |
| 14 | + getPropString(props.descriptiveText, 'descriptiveText') + |
| 15 | + getPropString(props.placeholder, 'placeholder') + |
| 16 | + getPropString(props.label, 'label') + |
| 17 | + (props.labelHidden ? '\n labelHidden' : '') + |
| 18 | + getPropString(props.errorMessage, 'errorMessage') + |
| 19 | + (props.hasError ? '\n hasError' : '') + |
| 20 | + (props.isDisabled ? '\n isDisabled' : '') + |
| 21 | + `\n/>` |
| 22 | + ); |
| 23 | +}; |
| 24 | + |
| 25 | +const defaultTextFieldProps: TextFieldProps = { |
| 26 | + descriptiveText: 'Enter a valid last name', |
| 27 | + placeholder: 'Baggins', |
| 28 | + label: 'Last name', |
| 29 | + errorMessage: 'There is an error', |
| 30 | +}; |
| 31 | + |
| 32 | +export const TextFieldDemo = () => { |
| 33 | + const textFieldProps = useTextFieldProps( |
| 34 | + (demoState.get('TextField') as TextFieldProps) || defaultTextFieldProps |
| 35 | + ); |
152 | 36 |
|
153 | 37 | return ( |
154 | 38 | <Demo |
155 | | - code={code} |
156 | | - propControls={ |
157 | | - <Flex direction="column"> |
158 | | - {TextFieldPropControls} |
159 | | - {FlexPropControls} |
160 | | - </Flex> |
161 | | - } |
| 39 | + code={propsToCode(textFieldProps)} |
| 40 | + propControls={<TextFieldPropControls {...textFieldProps} />} |
162 | 41 | > |
163 | 42 | <TextField |
164 | | - alignContent={alignContent as FlexContainerStyleProps['alignContent']} |
165 | | - alignItems={alignItems as FlexContainerStyleProps['alignItems']} |
166 | | - autoComplete={autoComplete as TextFieldProps<false>['autoComplete']} |
167 | | - descriptiveText={ |
168 | | - descriptiveText as TextFieldProps<false>['descriptiveText'] |
169 | | - } |
170 | | - defaultValue={defaultValue as TextFieldProps<false>['defaultValue']} |
171 | | - direction={direction as FlexContainerStyleProps['direction']} |
172 | | - errorMessage={errorMessage as TextFieldProps<false>['errorMessage']} |
173 | | - gap={gap as FlexContainerStyleProps['gap']} |
174 | | - hasError={hasError as unknown as boolean} |
175 | | - inputMode={inputMode as TextFieldProps<false>['inputMode']} |
176 | | - isDisabled={isDisabled as unknown as boolean} |
177 | | - isReadOnly={isReadOnly as unknown as boolean} |
178 | | - isRequired={isRequired as unknown as boolean} |
179 | | - justifyContent={ |
180 | | - justifyContent as FlexContainerStyleProps['justifyContent'] |
181 | | - } |
182 | | - label={label as TextFieldProps<false>['label']} |
183 | | - labelHidden={labelHidden as unknown as boolean} |
184 | | - name={name as TextFieldProps<false>['name']} |
185 | | - placeholder={placeholder as TextFieldProps<false>['placeholder']} |
186 | | - size={size as TextFieldProps<false>['size']} |
187 | | - type={type as TextFieldProps<false>['type']} |
188 | | - value={value as TextFieldProps<false>['value']} |
189 | | - variation={variation as TextFieldProps<false>['variation']} |
190 | | - wrap={wrap as FlexContainerStyleProps['wrap']} |
191 | | - onChange={(e) => console.info(e.currentTarget.value)} |
192 | | - onInput={(e) => console.info('input fired:', e.currentTarget.value)} |
193 | | - onCopy={(e) => console.info('onCopy fired:', e.currentTarget.value)} |
194 | | - onCut={(e) => console.info('onCut fired:', e.currentTarget.value)} |
195 | | - onPaste={(e) => console.info('onPaste fired:', e.currentTarget.value)} |
196 | | - onSelect={(e) => |
197 | | - console.info( |
198 | | - 'onSelect fired:', |
199 | | - e.currentTarget.value.substring( |
200 | | - e.currentTarget.selectionStart, |
201 | | - e.currentTarget.selectionEnd |
202 | | - ) |
203 | | - ) |
204 | | - } |
| 43 | + variation={textFieldProps.variation} |
| 44 | + size={textFieldProps.size} |
| 45 | + descriptiveText={textFieldProps.descriptiveText} |
| 46 | + placeholder={textFieldProps.placeholder} |
| 47 | + label={textFieldProps.label} |
| 48 | + labelHidden={textFieldProps.labelHidden} |
| 49 | + errorMessage={textFieldProps.errorMessage} |
| 50 | + hasError={textFieldProps.hasError} |
| 51 | + isDisabled={textFieldProps.isDisabled} |
205 | 52 | /> |
206 | 53 | </Demo> |
207 | 54 | ); |
|
0 commit comments