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 all 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
34 changes: 20 additions & 14 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, useRef } 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 [stateValue, setStateValue] = useState<any>();
const valueProp = useMemo(
() => isUndefined(value) ? defaultValue : value,
[defaultValue, value]
);
const valueRef = useRef<any>();

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

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

const handleOnChange = (value: any): void => {
valueRef.current = 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: valueRef.current })) {
return;
}
}, [defaultValue, stateValue, value]);
handleOnChange(valueProp);
}, [valueProp]);

const dropdown = (
<SUIDropdown
Expand All @@ -86,7 +92,7 @@ function Dropdown<Option> ({ allowAdd = false, className, defaultValue, dropdown
search={onSearch || allowAdd}
searchInput={searchInput}
selection
value={stateValue}
value={valueRef.current}
/>
);

Expand Down