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

Allow paste + enter on Dropdown #1937

Closed
Closed
Changes from 2 commits
Commits
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
28 changes: 17 additions & 11 deletions packages/react-components/src/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { BareProps } from './types';

import React, { useEffect, useState } from 'react';
import React, { useEffect, useMemo, useState } from 'react';
import styled from 'styled-components';
import SUIButton from 'semantic-ui-react/dist/commonjs/elements/Button/Button';
import SUIDropdown, { DropdownProps } from 'semantic-ui-react/dist/commonjs/modules/Dropdown/Dropdown';
Expand Down Expand Up @@ -40,31 +40,37 @@ interface Props<Option> extends BareProps {
}

function Dropdown<Option> ({ allowAdd = false, className, defaultValue, dropdownClassName, help, isButton, isDisabled, isError, isMultiple, label, labelExtra, onAdd, onBlur, onChange, onClose, onSearch, options, placeholder, renderLabel, searchInput, style, transform, withEllipsis, withLabel, value }: Props<Option>): React.ReactElement<Props<Option>> {
const valueProp = useMemo(
() => isUndefined(value) ? defaultValue : value,
[defaultValue, value]
);
const [stateValue, setStateValue] = useState<any>();

const _onAdd = (_: React.SyntheticEvent<HTMLElement>, { value }: DropdownProps): void =>
onAdd && onAdd(value);

const _onChange = (_: React.SyntheticEvent<HTMLElement> | null, { value }: DropdownProps): void => {
const handleOnChange = (value: any): void => {
setStateValue(value);

onChange && onChange(
transform
? transform(value)
: value
);
};

useEffect((): void => {
const newValue = isUndefined(value)
? defaultValue
: value;
const _onChange = (_: React.SyntheticEvent<HTMLElement> | null, { value }: DropdownProps): void => {
if (JSON.stringify({ v: valueProp }) === JSON.stringify({ v: value })) {
return;
}
handleOnChange(value);
};

// only update parent if we have had something changed
if (JSON.stringify({ v: newValue }) !== JSON.stringify({ v: stateValue })) {
_onChange(null, { value: newValue });
useEffect(() => {
if (JSON.stringify({ v: valueProp }) === JSON.stringify({ v: stateValue })) {
jacogr marked this conversation as resolved.
Show resolved Hide resolved
return;
}
}, [defaultValue, stateValue, value]);
handleOnChange(valueProp);
}, [valueProp]);

const dropdown = (
<SUIDropdown
Expand Down