Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2170 from teamleadercrm/Email-selector-update
Browse files Browse the repository at this point in the history
  • Loading branch information
qubis741 authored May 26, 2022
2 parents 6ed0288 + 640844d commit 56ee226
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Added

- `EmailSelector`: `warning` property ([@qubis741](https://github.com/qubis741) in [#2170](https://github.com/teamleadercrm/ui/pull/2170))

### Changed

### Deprecated
Expand Down
20 changes: 11 additions & 9 deletions src/components/emailSelector/EmailSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import EmailSuggestion from './EmailSuggestion';

interface EmailSelectorProps {
error?: boolean | string;
warning?: boolean | string;
defaultSelection?: Suggestion[];
suggestions?: Suggestions;
validator?: (option: Suggestion) => boolean | string | undefined;
Expand All @@ -31,6 +32,7 @@ const EmailSelector = ({
id,
suggestions,
renderSuggestion,
warning,
...rest
}: EmailSelectorProps) => {
const ref = useRef<HTMLElement>();
Expand All @@ -47,7 +49,7 @@ const EmailSelector = ({
Array.isArray(defaultSelection) ? defaultSelection.map(validateLabel) : [],
);
const [editingLabel, setEditingLabel] = useState<number | null>(null);
const [warning, setWarning] = useState<boolean | string>(false);
const [internalWarning, setInternalWarning] = useState<boolean | string>(false);

const validSuggestions = useMemo(() => {
if (!suggestions) {
Expand Down Expand Up @@ -91,19 +93,19 @@ const EmailSelector = ({
} else {
const result = validator ? validator(newLabel) : true;
if (result === false) {
setWarning(true);
setInternalWarning(true);
return false;
}
if (typeof result === 'string') {
setWarning(result);
setInternalWarning(result);
return false;
}

newSelection.splice(index, 1, validateLabel(newLabel));
createNewLabel(newSelection);
}

setWarning(false);
setInternalWarning(false);
changeHandler(newSelection);
return true;
},
Expand All @@ -120,7 +122,7 @@ const EmailSelector = ({
newSelection.splice(index, 1, validateLabel(newLabel));
}

setWarning(false);
setInternalWarning(false);
setSelection(newSelection);
setEditingLabel(null);
onBlur && onBlur();
Expand Down Expand Up @@ -152,7 +154,7 @@ const EmailSelector = ({
event.relatedTarget &&
(!ref.current?.contains(event.relatedTarget) || event.relatedTarget === inputRef.current)
) {
setWarning(false);
setInternalWarning(false);
setSelection(selection.filter((selection) => selection.email.trim() !== ''));
setEditingLabel(null);
onBlur && onBlur(event);
Expand Down Expand Up @@ -184,13 +186,13 @@ const EmailSelector = ({
},
[editingLabel, selection, onUpdateLabel, changeHandler],
);

const displayedWarning = warning ?? internalWarning;
return (
<>
<Box
ref={ref}
className={cx(theme['label-input'], {
[theme['label-input--warning']]: warning,
[theme['label-input--warning']]: displayedWarning,
[theme['label-input--error']]: error,
[theme['label-input--active']]: editingLabel !== null,
})}
Expand Down Expand Up @@ -219,7 +221,7 @@ const EmailSelector = ({
<Box className={theme['label-input-focuser']} element="input" id={id} ref={inputRef} onFocus={onInputFocus} />
)}
</Box>
<ValidationText warning={warning} error={!warning && error} />
<ValidationText warning={displayedWarning} error={!displayedWarning && error} />
</>
);
};
Expand Down

0 comments on commit 56ee226

Please sign in to comment.