@@ -9,6 +9,8 @@ import * as ComponentStories from './Form.stories';
9
9
10
10
<DocsHeader since = " 0.7.0" />
11
11
12
+ ** Note:** For more detailed examples of how the form can be used or integrated with other external libraries such as "React Hook Form", you can visit our [ knowledge base] ( https://sap.github.io/ui5-webcomponents-react/?path=/docs/knowledge-base-form-handling--docs ) .
13
+
12
14
<br />
13
15
14
16
## Example
@@ -135,6 +137,122 @@ const FormComponent = (props) => {
135
137
136
138
</details >
137
139
140
+ ## Display-Only and Edit mode
141
+
142
+ <Canvas of = { ComponentStories .DisplayEditMode } sourceState = " none" />
143
+
144
+ ### Code
145
+
146
+ <details >
147
+
148
+ <summary >Show Code</summary >
149
+
150
+ ``` jsx
151
+ const StandardField = ({ editMode, value, inputType = InputType .None , onInput, ... rest }) => {
152
+ if (editMode) {
153
+ return < Input value= {value} style= {{ width: ' 100%' }} type= {inputType} onInput= {onInput} {... rest} / > ;
154
+ }
155
+ if (inputType === InputType .URL || inputType === InputType .Email ) {
156
+ return (
157
+ < Link href= {inputType === InputType .Email ? ` mailto:${ value} ` : value} target= " _blank" {... rest}>
158
+ {value}
159
+ < / Link>
160
+ );
161
+ }
162
+ return < Text {... rest}> {value}< / Text > ;
163
+ };
164
+
165
+ const reducer = (state , { field, value }) => {
166
+ return { ... state, [field]: value };
167
+ };
168
+
169
+ const FormComponent = (props ) => {
170
+ const [editMode , toggleEditMode ] = useReducer ((prev ) => ! prev, false , undefined );
171
+ const [formState , dispatch ] = useReducer (
172
+ reducer,
173
+ {
174
+ name: ' Red Point Stores' ,
175
+ street: ' Main St 1618' ,
176
+ zip: 31415 ,
177
+ city: ' Maintown' ,
178
+ country: ' Germany' ,
179
+ web: ' https://www.sap.com' ,
180
+
181
+ twitter: ' @sap' ,
182
+ phone: ' +49 1234 56789'
183
+ },
184
+ undefined
185
+ );
186
+ const { zip , city , name , street , country , web , mail , twitter , phone } = formState;
187
+
188
+ const handleInput = (e ) => {
189
+ dispatch ({ field: Object .keys (e .target .dataset )[0 ], value: e .target .value });
190
+ };
191
+
192
+ return (
193
+ <>
194
+ < Button onClick= {toggleEditMode}> Toggle {editMode ? ' Display-Only Mode' : ' Edit Mode' }< / Button>
195
+ < Form
196
+ {... props}
197
+ onSubmit= {(e ) => {
198
+ e .preventDefault ();
199
+ }}
200
+ >
201
+ < FormGroup titleText= " Address" >
202
+ < FormItem label= " Name" >
203
+ < StandardField editMode= {editMode} value= {name} onInput= {handleInput} data- name / >
204
+ < / FormItem>
205
+ < FormItem label= " Street" >
206
+ < StandardField editMode= {editMode} value= {street} onInput= {handleInput} data- street / >
207
+ < / FormItem>
208
+ < FormItem label= " ZIP Code / City" >
209
+ {editMode ? (
210
+ <>
211
+ < Input value= {zip} type= {InputType .Number } style= {{ width: ' 30%' }} onInput= {handleInput} data- zip / >
212
+ < Input value= {city} style= {{ width: ' 70%' }} onInput= {handleInput} data- city / >
213
+ < / >
214
+ ) : (
215
+ < Text > {` ${ zip} ${ city} ` }< / Text >
216
+ )}
217
+ < / FormItem>
218
+ < FormItem label= " Country" >
219
+ < StandardField editMode= {editMode} value= {country} onInput= {handleInput} data- country / >
220
+ < / FormItem>
221
+ < FormItem label= " Web" >
222
+ < StandardField editMode= {editMode} value= {web} inputType= {InputType .URL } onInput= {handleInput} data- web / >
223
+ < / FormItem>
224
+ < / FormGroup>
225
+ < FormGroup titleText= " Contact" >
226
+ < FormItem label= " Email" >
227
+ < StandardField
228
+ editMode= {editMode}
229
+ value= {mail}
230
+ inputType= {InputType .Email }
231
+ onInput= {handleInput}
232
+ data- email
233
+ / >
234
+ < / FormItem>
235
+ < FormItem label= " Twitter" >
236
+ < StandardField editMode= {editMode} value= {twitter} onInput= {handleInput} data- twitter / >
237
+ < / FormItem>
238
+ < FormItem label= " Phone" >
239
+ < StandardField
240
+ editMode= {editMode}
241
+ value= {phone}
242
+ inputType= {InputType .Tel }
243
+ onInput= {handleInput}
244
+ data- phone
245
+ / >
246
+ < / FormItem>
247
+ < / FormGroup>
248
+ < / Form>
249
+ < / >
250
+ );
251
+ };
252
+ ```
253
+
254
+ </details >
255
+
138
256
<Markdown >{ SubcomponentsSection } </Markdown >
139
257
140
258
## Form Group
0 commit comments