Fixed a select component bug
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import React, { useState, useRef } from 'react';
|
import React, { useState, useRef, useLayoutEffect } from 'react';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { uniq } from 'lodash';
|
import { uniq } from 'lodash';
|
||||||
|
|
||||||
@@ -47,6 +47,18 @@ const SelectDropdown = ({
|
|||||||
|
|
||||||
const $optionsRef = useRef();
|
const $optionsRef = useRef();
|
||||||
|
|
||||||
|
useLayoutEffect(() => {
|
||||||
|
const setFirstOptionAsActive = () => {
|
||||||
|
const $active = getActiveOptionNode();
|
||||||
|
if ($active) $active.classList.remove(activeOptionClass);
|
||||||
|
|
||||||
|
if ($optionsRef.current.firstElementChild) {
|
||||||
|
$optionsRef.current.firstElementChild.classList.add(activeOptionClass);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
setFirstOptionAsActive();
|
||||||
|
});
|
||||||
|
|
||||||
const selectOptionValue = optionValue => {
|
const selectOptionValue = optionValue => {
|
||||||
deactivateDropdown();
|
deactivateDropdown();
|
||||||
if (isMulti) {
|
if (isMulti) {
|
||||||
@@ -139,7 +151,7 @@ const SelectDropdown = ({
|
|||||||
const handleOptionMouseEnter = event => {
|
const handleOptionMouseEnter = event => {
|
||||||
const $active = getActiveOptionNode();
|
const $active = getActiveOptionNode();
|
||||||
if ($active) $active.classList.remove(activeOptionClass);
|
if ($active) $active.classList.remove(activeOptionClass);
|
||||||
event.target.classList.add(activeOptionClass);
|
event.currentTarget.classList.add(activeOptionClass);
|
||||||
};
|
};
|
||||||
|
|
||||||
const getActiveOptionNode = () => $optionsRef.current.querySelector(`.${activeOptionClass}`);
|
const getActiveOptionNode = () => $optionsRef.current.querySelector(`.${activeOptionClass}`);
|
||||||
@@ -161,10 +173,9 @@ const SelectDropdown = ({
|
|||||||
const isSearchValueInOptions = options.map(option => option.label).includes(searchValue);
|
const isSearchValueInOptions = options.map(option => option.label).includes(searchValue);
|
||||||
const isOptionCreatable = onCreate && searchValue && !isSearchValueInOptions;
|
const isOptionCreatable = onCreate && searchValue && !isSearchValueInOptions;
|
||||||
|
|
||||||
const renderSelectableOption = (option, i) => (
|
const renderSelectableOption = option => (
|
||||||
<Option
|
<Option
|
||||||
key={option.value}
|
key={option.value}
|
||||||
className={i === 0 ? activeOptionClass : undefined}
|
|
||||||
data-select-option-value={option.value}
|
data-select-option-value={option.value}
|
||||||
onMouseEnter={handleOptionMouseEnter}
|
onMouseEnter={handleOptionMouseEnter}
|
||||||
onClick={() => selectOptionValue(option.value)}
|
onClick={() => selectOptionValue(option.value)}
|
||||||
@@ -175,7 +186,6 @@ const SelectDropdown = ({
|
|||||||
|
|
||||||
const renderCreatableOption = () => (
|
const renderCreatableOption = () => (
|
||||||
<Option
|
<Option
|
||||||
className={filteredOptions.length === 0 ? activeOptionClass : undefined}
|
|
||||||
data-create-option-label={searchValue}
|
data-create-option-label={searchValue}
|
||||||
onMouseEnter={handleOptionMouseEnter}
|
onMouseEnter={handleOptionMouseEnter}
|
||||||
onClick={() => createOption(searchValue)}
|
onClick={() => createOption(searchValue)}
|
||||||
|
|||||||
Reference in New Issue
Block a user