Skip to content

Commit 353f5ce

Browse files
authored
Combine valueState and valueStateSetter into a single object (#9)
1 parent f90a0d9 commit 353f5ce

File tree

3 files changed

+20
-53
lines changed

3 files changed

+20
-53
lines changed

src/demo/App.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ export default function App(): JSX.Element {
2525
<AutoNumericInput />
2626

2727
<AutoNumericInput
28-
valueState={controlledInputState}
29-
valueStateSetter={setControlledInputState}
28+
valueState={{
29+
state: controlledInputState,
30+
stateSetter: setControlledInputState,
31+
}}
3032
/>
3133
<button
3234
onClick={() => {

src/lib/AutoNumericInput.tsx

+12-46
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,6 @@ export type InputProps = Omit<
2525
"ref"
2626
>;
2727

28-
/** React {@link JSX!IntrinsicElements.input} component integrated with {@link !AutoNumeric}.
29-
*
30-
* @param options - Options of the component.
31-
* @param options.inputProps - Options passed to {@link JSX!IntrinsicElements.input}. Same as {@link
32-
* JSX!IntrinsicElements.input.props} excluding the `ref` property.
33-
* @param options.autoNumericOptions - Options passed to {@link !AutoNumeric}. Same as {@link
34-
* AutoNumeric!Options}.
35-
* @returns The React component.
36-
*/
37-
export function AutoNumericInput({
38-
inputProps,
39-
autoNumericOptions,
40-
}: {
41-
inputProps?: InputProps;
42-
autoNumericOptions?: CallbackOptions;
43-
}): JSX.Element;
44-
4528
/** React {@link JSX!IntrinsicElements.input} component integrated with {@link !AutoNumeric} and
4629
* permits interaction with a React state.
4730
*
@@ -50,48 +33,31 @@ export function AutoNumericInput({
5033
* JSX!IntrinsicElements.input.props}.
5134
* @param options.autoNumericOptions - Options passed to {@link !AutoNumeric}. Same as {@link
5235
* AutoNumeric!Options}.
53-
* @param options.valueState - The state from the parent component to be passed in.
54-
* @param options.valueStateSetter - The callback function that sets `options.valueState`.
36+
* @param options.valueState - The state and state setter from the parent component.
37+
* @param options.valueState.state - The state from the parent component to be passed in.
38+
* @param options.valueState.stateSetter - The callback function that sets
39+
* `options.valueState.state`.
5540
* @returns The React component.
5641
*/
57-
// eslint-disable-next-line @typescript-eslint/unified-signatures
5842
export function AutoNumericInput({
5943
inputProps,
6044
autoNumericOptions,
6145
valueState,
62-
valueStateSetter,
6346
}: {
6447
inputProps?: InputProps;
6548
autoNumericOptions?: CallbackOptions;
66-
valueState: string;
67-
valueStateSetter: React.Dispatch<React.SetStateAction<string>>;
68-
}): JSX.Element;
69-
70-
export function AutoNumericInput({
71-
inputProps,
72-
autoNumericOptions,
73-
valueState,
74-
valueStateSetter,
75-
}:
76-
| {
77-
inputProps?: InputProps;
78-
autoNumericOptions?: CallbackOptions;
79-
valueState: string;
80-
valueStateSetter: React.Dispatch<React.SetStateAction<string>>;
81-
}
82-
| {
83-
inputProps?: InputProps;
84-
autoNumericOptions?: CallbackOptions;
85-
valueState?: undefined;
86-
valueStateSetter?: undefined;
87-
}): JSX.Element {
49+
valueState?: {
50+
state: string;
51+
stateSetter: React.Dispatch<React.SetStateAction<string>>;
52+
};
53+
}): JSX.Element {
8854
const stateProps =
8955
valueState !== undefined
9056
? {
91-
value: valueState,
57+
value: valueState.state,
9258
onChange: (e: ChangeEvent<HTMLInputElement>): void => {
9359
// For input, it is required set value in onChange.
94-
valueStateSetter(e.currentTarget.value);
60+
valueState.stateSetter(e.currentTarget.value);
9561
if (inputProps?.onChange !== undefined) {
9662
inputProps.onChange(e);
9763
}
@@ -104,7 +70,7 @@ export function AutoNumericInput({
10470
refKey="ref"
10571
props={{ ...inputProps, ...stateProps }}
10672
autoNumericOptions={autoNumericOptions}
107-
state={valueState}
73+
state={valueState?.state}
10874
/>
10975
);
11076
}

src/test/AutoNumericInput.test.tsx

+4-5
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ function ControlledAutoNumericInputWrapper({
3535
<AutoNumericInput
3636
inputProps={inputProps}
3737
autoNumericOptions={autoNumericOptions}
38-
valueState={state}
39-
valueStateSetter={setState}
38+
valueState={{ state, stateSetter: setState }}
4039
/>
4140
);
4241
}
@@ -98,7 +97,7 @@ test("Controlled AutoNumericInput changes the input state", async () => {
9897
}): JSX.Element {
9998
const [state, setState] = useState("");
10099
stateParent.state = state;
101-
return <AutoNumericInput valueState={state} valueStateSetter={setState} />;
100+
return <AutoNumericInput valueState={{ state, stateSetter: setState }} />;
102101
}
103102

104103
const stateParent = { state: "" };
@@ -120,7 +119,7 @@ test("ControlledAutoNumericInput changes the input state", async () => {
120119
}): JSX.Element {
121120
const [state, setState] = useState("");
122121
stateParent.state = state;
123-
return <AutoNumericInput valueState={state} valueStateSetter={setState} />;
122+
return <AutoNumericInput valueState={{ state, stateSetter: setState }} />;
124123
}
125124

126125
const stateParent = { state: "" };
@@ -144,7 +143,7 @@ test("Changing the input state of AutoNumericInput changes the display value", a
144143
setState("1111");
145144
}}
146145
/>
147-
<AutoNumericInput valueState={state} valueStateSetter={setState} />
146+
<AutoNumericInput valueState={{ state, stateSetter: setState }} />
148147
</>
149148
);
150149
}

0 commit comments

Comments
 (0)