Skip to content

Commit a1db063

Browse files
committed
fix(ComboBox): remove inputValue setting
1 parent 4957adf commit a1db063

File tree

11 files changed

+63
-63
lines changed

11 files changed

+63
-63
lines changed

.changeset/proud-colts-cheer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cube-dev/ui-kit': minor
3+
---
4+
5+
Fixes various issues with ComboBox input typing and selection.

.changeset/sweet-books-hope.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cube-dev/ui-kit': minor
3+
---
4+
5+
Remove inputValue abstraction from form fields.

src/components/fields/ComboBox/ComboBox.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ With1LongOptionFiltered.play = async ({ canvasElement }) => {
128128
await userEvent.type(combobox, 'Red');
129129
};
130130

131-
export const WithForm = TemplateForm.bind({});
132-
WithForm.play = async ({ canvasElement }) => {
131+
export const WithinForm = TemplateForm.bind({});
132+
WithinForm.play = async ({ canvasElement }) => {
133133
const { getByRole } = within(canvasElement);
134134

135135
const combobox = getByRole('combobox');

src/components/fields/ComboBox/ComboBox.tsx

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
ReactElement,
66
RefObject,
77
useMemo,
8-
useState,
98
} from 'react';
109
import {
1110
useButton,
@@ -140,21 +139,22 @@ export const ComboBox = forwardRef(function ComboBox<T extends object>(
140139
props: CubeComboBoxProps<T>,
141140
ref: ForwardedRef<HTMLDivElement>,
142141
) {
143-
const [, rerender] = useState({});
144-
145142
props = useProviderProps(props);
146143
props = useFormProps(props);
147144
props = useFieldProps(props, {
148-
valuePropsMapper: ({ value, onChange }) => ({
149-
inputValue: value != null ? value : '',
150-
onInputChange: (val) => {
151-
// It triggers on the blur event and passes an empty string if no changes we made
152-
// So we do a fallback to the real input value
153-
onChange(val || props.inputValue || '', !props.allowsCustomValue);
154-
rerender({});
155-
},
156-
onSelectionChange: onChange,
157-
}),
145+
valuePropsMapper: ({ value, onChange }) => {
146+
return {
147+
selectedKey: value ?? null,
148+
onSelectionChange(val: string) {
149+
if (val != null) {
150+
onChange(val);
151+
}
152+
},
153+
onBlur(evt) {
154+
evt?.stopPropagation();
155+
},
156+
};
157+
},
158158
});
159159

160160
let {
@@ -197,6 +197,8 @@ export const ComboBox = forwardRef(function ComboBox<T extends object>(
197197
filter,
198198
styles,
199199
labelSuffix,
200+
selectedKey,
201+
defaultSelectedKey,
200202
...otherProps
201203
} = props;
202204

@@ -270,7 +272,7 @@ export const ComboBox = forwardRef(function ComboBox<T extends object>(
270272
if (state.isOpen) {
271273
updatePosition();
272274
}
273-
}, [updatePosition, state.isOpen, state.collection.size]);
275+
}, [state.isOpen, state.collection.size]);
274276

275277
let isInvalid = validationState === 'invalid';
276278

src/components/fields/TextInput/TextInputBase.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ function _TextInputBase(props: CubeTextInputBaseProps, ref) {
247247
minLength,
248248
...otherProps
249249
} = props;
250+
250251
let styles = extractStyles(otherProps, STYLE_LIST);
251252
let type = otherProps.type;
252253

src/components/form/Form/Field.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ function getValueProps(
6565
};
6666
} else if (type === 'ComboBox') {
6767
return {
68-
inputValue: value != null ? value : '',
69-
onInputChange: (val) => onChange(val, !allowsCustomValue),
68+
value: value,
7069
onSelectionChange: onChange,
7170
};
7271
} else if (type === 'Select') {
@@ -101,7 +100,6 @@ interface CubeReplaceFieldProps<T extends FieldTypes>
101100
onChange?: (any) => void;
102101
onSelectionChange?: (any) => void;
103102
onBlur: () => void;
104-
onInputChange?: (any) => void;
105103
labelPosition?: LabelPosition;
106104
}
107105

@@ -255,7 +253,7 @@ export function Field<T extends FieldTypes>(props: CubeFieldProps<T>) {
255253
newProps,
256254
getValueProps(
257255
inputType,
258-
__props.field?.inputValue,
256+
__props.field?.value,
259257
__props.onChange,
260258
child.props.allowsCustomValue,
261259
),

src/components/form/Form/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export type CubeFieldData<Name extends string, Value> = {
88
readonly name: Name;
99
errors: ReactNode[];
1010
value?: Value;
11-
inputValue?: Value | string | undefined | null;
1211
touched?: boolean;
1312
rules?: any[];
1413
validating?: boolean;

src/components/form/Form/use-field/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export type FieldReturnValue<T extends FieldTypes> = {
4949
dontTouch: boolean,
5050
validateTrigger: ValidateTrigger,
5151
) => void;
52-
message?: React.ReactNode;
52+
message?: ReactNode;
5353
isRequired?: boolean;
5454
onBlur: () => void;
5555
nonInput: boolean;

src/components/form/Form/use-field/use-field-props.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ export function useFieldProps<
3333
const isDisabledRef = useRef(params.unsafe__isDisabled ?? false);
3434

3535
const {
36-
valuePropsMapper = ({ value, onChange }) => ({
37-
value: value ?? null,
38-
onChange,
39-
}),
36+
valuePropsMapper = ({ value, onChange }) => {
37+
return {
38+
value: value ?? null,
39+
onChange,
40+
};
41+
},
4042
defaultValidationTrigger = 'onBlur',
4143
} = params;
4244

@@ -81,16 +83,19 @@ export function useFieldProps<
8183
);
8284

8385
// eslint-disable-next-line react-hooks/rules-of-hooks
84-
const onChangeEvent = useEvent((value, dontTouch: boolean) =>
86+
const onChangeEvent = useEvent((value, dontTouch: boolean) => {
8587
field?.onChange?.(
8688
value,
8789
dontTouch,
8890
field?.validateTrigger ?? defaultValidationTrigger,
89-
),
90-
);
91+
);
92+
});
9193

9294
const valueProps = !isOutsideOfForm
93-
? valuePropsMapper({ value: field.value, onChange: onChangeEvent })
95+
? valuePropsMapper({
96+
value: field.value,
97+
onChange: onChangeEvent,
98+
})
9499
: {};
95100

96101
if (isInsideLegacyField && !isOutsideOfForm) {

src/components/form/Form/use-field/use-field.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export function useField<T extends FieldTypes, Props extends CubeFieldProps<T>>(
107107
if (field) {
108108
form.forceReRender();
109109
} else {
110-
form.createField(fieldName);
110+
field = form.createField(fieldName);
111111
}
112112
}, [field]);
113113

@@ -152,7 +152,7 @@ export function useField<T extends FieldTypes, Props extends CubeFieldProps<T>>(
152152
}
153153
}
154154

155-
form.setFieldValue(fieldName, val, !dontTouch, false, dontTouch);
155+
form.setFieldValue(fieldName, val, !dontTouch);
156156

157157
if (
158158
!dontTouch &&
@@ -172,13 +172,13 @@ export function useField<T extends FieldTypes, Props extends CubeFieldProps<T>>(
172172
}
173173
});
174174

175-
let inputValue = field?.inputValue;
175+
let value = field?.value;
176176

177177
return useMemo(
178178
() => ({
179179
id: fieldId,
180180
name: fieldName,
181-
value: inputValue,
181+
value,
182182
validateTrigger,
183183
form,
184184
field,
@@ -202,9 +202,9 @@ export function useField<T extends FieldTypes, Props extends CubeFieldProps<T>>(
202202
[
203203
form,
204204
field,
205+
field?.value,
205206
field?.errors?.length,
206207
field?.status,
207-
field?.inputValue,
208208
fieldId,
209209
fieldName,
210210
isRequired,

0 commit comments

Comments
 (0)