Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display an error when duplicate values entered into Autocompelte #2945

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Changes from 27 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
0286c01
mui#2923 Autocomplete will occur an error when set same value
Nov 29, 2023
16d1c00
Merge branch 'master' into 2923_limit_same_value
JerryWu1234 Nov 29, 2023
2c4f21f
Merge branch 'master' into 2923_limit_same_value
JerryWu1234 Dec 1, 2023
a5e9c13
fix
Dec 1, 2023
88851b4
Merge branch '2923_limit_same_value' of https://github.com/JerryWu123…
Dec 1, 2023
af7ad04
fix
Dec 21, 2023
d174d40
Merge branch 'master' into 2923_limit_same_value
JerryWu1234 Dec 21, 2023
83fe1f4
fix
Dec 22, 2023
b396634
Merge branch 'master' into 2923_limit_same_value
JerryWu1234 Dec 22, 2023
967ebfb
fix
Dec 22, 2023
25c926f
Merge branch '2923_limit_same_value' of https://github.com/JerryWu123…
Dec 22, 2023
ce9aaa9
Merge branch 'master' into 2923_limit_same_value
JerryWu1234 Dec 23, 2023
5854ac1
Merge branch 'master' into 2923_limit_same_value
JerryWu1234 Dec 26, 2023
6fe7dd9
Merge branch 'master' into 2923_limit_same_value
JerryWu1234 Jan 10, 2024
52153ce
Merge branch 'master' into 2923_limit_same_value
JerryWu1234 Jan 11, 2024
cb6d07b
Merge branch 'master' into 2923_limit_same_value
JerryWu1234 Jan 16, 2024
560fbad
Merge branch 'master' into 2923_limit_same_value
JerryWu1234 Jan 26, 2024
232756b
optimize code
Jan 26, 2024
f6eec05
Merge branch 'master' into 2923_limit_same_value
JerryWu1234 Jan 29, 2024
3582cfd
fix type
Jan 30, 2024
e3007ff
Merge branch 'master' into 2923_limit_same_value
JerryWu1234 Jan 30, 2024
493f037
fix
Jan 31, 2024
6833fd6
Merge branch 'master' into 2923_limit_same_value
JerryWu1234 Feb 19, 2024
a09faf1
add fix
Feb 19, 2024
3d79b5c
Merge branch 'master' of https://github.com/mui/mui-toolpad into 2923…
Mar 31, 2024
cdb0a80
add optimize code
Mar 31, 2024
3f3c648
fix optimize code
Mar 31, 2024
b63755b
Merge branch 'master' into 2923_limit_same_value
JerryWu1234 Apr 3, 2024
881a6a8
modify log level
Apr 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function SelectOptionsPropEditor({
value = [],
onChange,
}: EditorProps<(string | SelectOption)[]>) {
const [optionErrorMessage, setOptionErrorMessage] = React.useState('');
const [editOptionsDialogOpen, setEditOptionsDialogOpen] = React.useState(false);
const optionInputRef = React.useRef<HTMLInputElement | null>(null);
const [editingIndex, setEditingIndex] = React.useState<number | null>(null);
Expand All @@ -47,17 +48,35 @@ function SelectOptionsPropEditor({
return null;
}, [editingIndex, value]);

const validateOptionValue = React.useCallback(
(inputValue: string) => {
const errorState = value.some((item) =>
typeof item !== 'string' ? item.value === inputValue : item === inputValue,
);
if (errorState) {
setOptionErrorMessage('Must not have duplicate values');
} else {
setOptionErrorMessage('');
}
},
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
[value],
);

const handleOptionTextInput = React.useCallback(
(event: React.KeyboardEvent<HTMLInputElement>) => {
validateOptionValue((event.target as HTMLInputElement).value);
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
if (event.key === 'Enter') {
const inputText = (event.target as HTMLInputElement).value;
if (optionErrorMessage || !inputText) {
return;
}
onChange([...value, inputText]);
if (optionInputRef.current) {
optionInputRef.current.value = '';
}
}
},
[onChange, value],
[onChange, value, optionErrorMessage, validateOptionValue],
);

const handleOptionDelete = React.useCallback(
Expand All @@ -81,14 +100,19 @@ function SelectOptionsPropEditor({

const handleOptionChange = React.useCallback(
(newOption: string | SelectOption) => {
const newOptionValue = (typeof newOption !== 'string' && newOption.value) ?? newOption;

validateOptionValue(newOptionValue as string);

if (typeof newOption === 'object') {
if (!newOption.label) {
newOption = newOption.value;
}
}

onChange(value.map((option, i) => (i === editingIndex ? newOption : option)));
},
[editingIndex, onChange, value],
[editingIndex, onChange, value, validateOptionValue],
);

const handleEditOptionsDialogClose = React.useCallback(() => {
Expand All @@ -114,6 +138,7 @@ function SelectOptionsPropEditor({
fullWidth
open={editOptionsDialogOpen}
onClose={() => {
setOptionErrorMessage('');
setEditOptionsDialogOpen(false);
}}
>
Expand All @@ -129,6 +154,8 @@ function SelectOptionsPropEditor({
<Stack gap={1} py={1}>
<TextField
label="Value"
error={!!optionErrorMessage}
helperText={optionErrorMessage ? <span>This option already exists</span> : null}
value={editingOption.value}
onChange={(event) => {
handleOptionChange({ ...editingOption, value: event.target.value });
Expand Down Expand Up @@ -202,15 +229,20 @@ function SelectOptionsPropEditor({
) : null}
<TextField
fullWidth
error={!!optionErrorMessage}
sx={{ my: 1 }}
variant="outlined"
inputRef={optionInputRef}
onKeyUp={handleOptionTextInput}
label={'Add option'}
helperText={
<span>
Press <kbd>Enter</kbd> or <kbd>Return</kbd> to add
</span>
optionErrorMessage ? (
<span>This option already exists</span>
) : (
<span>
Press <kbd>Enter</kbd> or <kbd>Return</kbd> to add
</span>
)
}
/>
</DialogContent>
Expand Down
Loading