Skip to content

Commit

Permalink
fix: value priority is greater than defaultValue
Browse files Browse the repository at this point in the history
  • Loading branch information
jin-sir committed Sep 25, 2024
1 parent 27c2322 commit b716e69
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/dropdown/select.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import React, { ReactNode, useEffect, useMemo, useState } from 'react';
import { Button, Checkbox, Col, Dropdown, type DropDownProps, Row, Space } from 'antd';
import type { CheckboxChangeEvent } from 'antd/lib/checkbox';
import type { CheckboxGroupProps, CheckboxValueType } from 'antd/lib/checkbox/Group';
import type {
CheckboxGroupProps,
CheckboxOptionType,
CheckboxValueType,
} from 'antd/lib/checkbox/Group';
import classNames from 'classnames';
import { isEqual } from 'lodash';
import List from 'rc-virtual-list';
Expand Down Expand Up @@ -29,7 +33,7 @@ export default function Select({
onChange,
}: IDropdownSelectProps) {
const [visible, setVisible] = useState(false);
const [selected, setSelected] = useState<CheckboxValueType[]>(defaultValue || []);
const [selected, setSelected] = useState<CheckboxValueType[]>(value || defaultValue || []);

const handleCheckedAll = (e: CheckboxChangeEvent) => {
if (e.target.checked) {
Expand Down Expand Up @@ -77,13 +81,13 @@ export default function Select({
};

useEffect(() => {
if (value && value !== selected) {
if (value !== selected) {
setSelected(value || []);
}
}, [value]);

// Always turn string and number options into complex options
const options = useMemo(() => {
const options = useMemo<CheckboxOptionType[]>(() => {
return (
rawOptions?.map((i) => {
if (typeof i === 'string' || typeof i === 'number') {
Expand All @@ -98,7 +102,7 @@ export default function Select({
);
}, [rawOptions]);

const disabledValue = useMemo(() => {
const disabledValue = useMemo<CheckboxValueType[]>(() => {
return options?.filter((i) => i.disabled).map((i) => i.value) || [];
}, [options]);

Expand Down

0 comments on commit b716e69

Please sign in to comment.