Skip to content

Commit 17cbc7e

Browse files
committed
remove recursive instantiation to improve readability
1 parent 38d7e98 commit 17cbc7e

File tree

2 files changed

+52
-83
lines changed

2 files changed

+52
-83
lines changed

src/scripts/Input.tsx

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,29 @@ export type InputProps = {
101101
export const Input = createFC<InputProps, { isFormElement: boolean }>(
102102
(props) => {
103103
const {
104+
id: id_,
105+
className,
106+
label,
107+
required,
108+
error,
109+
readOnly,
110+
cols,
111+
type,
112+
bare,
113+
value,
114+
defaultValue,
115+
htmlReadOnly,
116+
iconLeft,
117+
iconRight,
118+
addonLeft,
119+
addonRight,
104120
symbolPattern,
121+
elementRef,
122+
inputRef,
105123
onKeyDown: onKeyDown_,
106124
onChange: onChange_,
107125
onValueChange,
126+
...rprops
108127
} = props;
109128

110129
useInitComponentStyle();
@@ -129,55 +148,8 @@ export const Input = createFC<InputProps, { isFormElement: boolean }>(
129148
prevValueRef.current = e.target.value;
130149
});
131150

132-
const {
133-
id: id_,
134-
label,
135-
required,
136-
error,
137-
readOnly,
138-
cols,
139-
elementRef,
140-
...rprops
141-
} = props;
142151
const id = useFormElementId(id_, 'input');
143152
const { isFieldSetColumn } = useContext(FieldSetColumnContext);
144-
if (isFieldSetColumn || label || required || error || cols) {
145-
const formElemProps = {
146-
id,
147-
label,
148-
required,
149-
error,
150-
readOnly,
151-
cols,
152-
elementRef,
153-
};
154-
return (
155-
<FormElement {...formElemProps}>
156-
<Input {...{ id, readOnly, ...rprops }} />
157-
</FormElement>
158-
);
159-
}
160-
161-
const {
162-
className,
163-
inputRef,
164-
type,
165-
bare,
166-
value,
167-
defaultValue,
168-
htmlReadOnly,
169-
iconLeft,
170-
iconRight,
171-
addonLeft,
172-
addonRight,
173-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
174-
onChange: _unused_1,
175-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
176-
onValueChange: _unused_2,
177-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
178-
onKeyDown: _unused_3,
179-
...rprops2
180-
} = rprops;
181153
const inputClassNames = classnames(
182154
className,
183155
bare ? 'slds-input_bare' : 'slds-input'
@@ -200,12 +172,13 @@ export const Input = createFC<InputProps, { isFormElement: boolean }>(
200172
value={value}
201173
defaultValue={defaultValue}
202174
readOnly={htmlReadOnly}
203-
{...rprops2}
175+
{...rprops}
204176
onChange={onChange}
205177
onKeyDown={onKeyDown}
206178
/>
207179
);
208180

181+
let contentElem = inputElem;
209182
if (iconLeft || iconRight || addonLeft || addonRight) {
210183
const wrapperClassName = classnames(
211184
'slds-form-element__control',
@@ -215,7 +188,7 @@ export const Input = createFC<InputProps, { isFormElement: boolean }>(
215188
{ 'slds-input-has-icon_right': iconRight },
216189
{ 'slds-input-has-fixed-addon': addonLeft || addonRight }
217190
);
218-
return (
191+
contentElem = (
219192
<div className={wrapperClassName}>
220193
{addonLeft ? <InputAddon content={addonLeft} /> : undefined}
221194
{iconLeft ? <InputIcon icon={iconLeft} align='left' /> : undefined}
@@ -225,7 +198,19 @@ export const Input = createFC<InputProps, { isFormElement: boolean }>(
225198
</div>
226199
);
227200
}
228-
return inputElem;
201+
if (isFieldSetColumn || label || required || error || cols) {
202+
const formElemProps = {
203+
id,
204+
label,
205+
required,
206+
error,
207+
readOnly,
208+
cols,
209+
elementRef,
210+
};
211+
return <FormElement {...formElemProps}>{contentElem}</FormElement>;
212+
}
213+
return contentElem;
229214
},
230215
{ isFormElement: true }
231216
);

src/scripts/Textarea.tsx

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,58 +29,42 @@ export type TextareaProps = {
2929
*/
3030
export const Textarea = createFC<TextareaProps, { isFormElement: boolean }>(
3131
(props) => {
32-
const { onChange: onChange_, onValueChange } = props;
33-
const prevValueRef = useRef<string>();
34-
const onChange = useEventCallback((e: ChangeEvent<HTMLTextAreaElement>) => {
35-
onChange_?.(e);
36-
onValueChange?.(e.target.value, prevValueRef.current);
37-
prevValueRef.current = e.target.value;
38-
});
3932
const {
4033
id: id_,
34+
className,
4135
label,
4236
required,
4337
error,
4438
cols,
4539
elementRef,
40+
textareaRef,
41+
onChange: onChange_,
42+
onValueChange,
4643
...rprops
4744
} = props;
45+
const prevValueRef = useRef<string>();
46+
const onChange = useEventCallback((e: ChangeEvent<HTMLTextAreaElement>) => {
47+
onChange_?.(e);
48+
onValueChange?.(e.target.value, prevValueRef.current);
49+
prevValueRef.current = e.target.value;
50+
});
4851
const id = useFormElementId(id_, 'textarea');
4952
const { isFieldSetColumn } = useContext(FieldSetColumnContext);
50-
if (isFieldSetColumn || label || required || error || cols) {
51-
const formElemProps = {
52-
id,
53-
label,
54-
required,
55-
error,
56-
cols,
57-
elementRef,
58-
};
59-
return (
60-
<FormElement {...formElemProps}>
61-
<Textarea {...{ ...rprops, id, onChange }} />
62-
</FormElement>
63-
);
64-
}
65-
const {
66-
className,
67-
textareaRef,
68-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
69-
onChange: _unused_1,
70-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
71-
onValueChange: _unused_2,
72-
...rprops2
73-
} = rprops;
7453
const taClassNames = classnames(className, 'slds-input');
75-
return (
54+
const textareaElem = (
7655
<textarea
7756
id={id}
7857
ref={textareaRef}
7958
className={taClassNames}
80-
{...rprops2}
59+
{...rprops}
8160
onChange={onChange}
8261
/>
8362
);
63+
if (isFieldSetColumn || label || required || error || cols) {
64+
const formElemProps = { id, label, required, error, cols, elementRef };
65+
return <FormElement {...formElemProps}>{textareaElem}</FormElement>;
66+
}
67+
return textareaElem;
8468
},
8569
{ isFormElement: true }
8670
);

0 commit comments

Comments
 (0)