Skip to content

Commit f3154f1

Browse files
committed
fix: remove useless option and fix the Select value logic
1 parent 868e190 commit f3154f1

File tree

3 files changed

+61
-35
lines changed

3 files changed

+61
-35
lines changed

src/components/select/option.tsx

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { selectClassName } from './select';
77

88
export interface ISelectOption extends ComponentProps<'option'> {
99
value?: string;
10-
title?: string;
10+
name?: string;
1111
description?: string;
1212
disabled?: boolean;
1313
}
@@ -23,6 +23,7 @@ export function Option(props: ISelectOption) {
2323
className,
2424
value,
2525
title,
26+
name,
2627
description,
2728
disabled,
2829
children,
@@ -34,16 +35,16 @@ export function Option(props: ISelectOption) {
3435
className,
3536
disabled ? selectOptionDisabledClassName : ''
3637
);
37-
const content = children || title;
3838
return (
3939
<div
4040
className={claNames}
41-
title={content}
41+
title={title}
42+
data-name={name || children}
4243
data-value={value}
4344
data-desc={description}
4445
{...(custom as any)}
4546
>
46-
{content}
47+
{children}
4748
</div>
4849
);
4950
}

src/components/select/select.tsx

+28-31
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,14 @@ export interface ISelect extends ComponentProps<any> {
3434
const initialValue = {
3535
isOpen: false,
3636
option: {
37-
title: '',
37+
name: '',
3838
value: '',
3939
description: '',
4040
},
4141
};
4242

4343
type IState = {
4444
isOpen: boolean;
45-
lastUpdate?: number;
4645
option: ISelectOption;
4746
};
4847

@@ -65,7 +64,7 @@ export class Select extends PureComponent<ISelect, IState> {
6564
this.contextView = useContextView({
6665
shadowOutline: false,
6766
});
68-
this.state = this.getDefaultState();
67+
this.state = this.getDefaultState(this.props);
6968
this.selectElm = React.createRef();
7069
this.selectInput = React.createRef();
7170
}
@@ -80,48 +79,50 @@ export class Select extends PureComponent<ISelect, IState> {
8079
});
8180
}
8281

83-
public getDefaultState() {
84-
const defaultSelectedOption: ISelectOption = {};
85-
const options = Children.toArray(this.props.children);
82+
public getDefaultState(props) {
83+
let defaultSelectedOption: ISelectOption = {};
84+
const defaultValue = props.value || props.defaultValue;
85+
const options = Children.toArray(props.children);
8686
for (const option of options) {
8787
if (isValidElement(option)) {
8888
const optionProps = option.props as ISelectOption;
8989
if (
9090
optionProps.value &&
91-
optionProps.value === this.props.defaultValue
91+
optionProps.value === defaultValue
9292
) {
93-
defaultSelectedOption.title = optionProps.children as string;
94-
defaultSelectedOption.value = optionProps.value;
93+
defaultSelectedOption = { ...optionProps, name: optionProps.name || optionProps.children as string };
9594
break;
9695
}
9796
}
9897
}
9998
return {
10099
...initialValue,
101-
option: defaultSelectedOption,
100+
option: { ...defaultSelectedOption },
102101
};
103102
}
104103

105104
public handleOnClickOption = (e: React.MouseEvent) => {
106105
const option = e.target as HTMLDivElement;
107106
const value = getAttr(option, 'data-value');
108-
const title = getAttr(option, 'title');
107+
const name = getAttr(option, 'data-name');
109108
const desc = getAttr(option, 'data-desc');
110-
const optionItem: ISelectOption = {
111-
value: value,
112-
title: title,
113-
description: desc,
114-
};
115-
116-
this.setState(
117-
{
118-
option: optionItem,
119-
},
120-
() => {
121-
this.props.onSelect?.(e, optionItem);
122-
this.contextView.hide();
123-
}
124-
);
109+
if (name) {
110+
const optionItem: ISelectOption = {
111+
value: value,
112+
name: name,
113+
description: desc,
114+
};
115+
116+
this.setState(
117+
{
118+
option: optionItem,
119+
},
120+
() => {
121+
this.props.onSelect?.(e, optionItem);
122+
this.contextView.hide();
123+
}
124+
);
125+
}
125126
};
126127

127128
public handleOnHoverOption = (e: React.MouseEvent) => {
@@ -173,11 +174,7 @@ export class Select extends PureComponent<ISelect, IState> {
173174
const { option, isOpen } = this.state;
174175
const {
175176
className,
176-
children,
177-
defaultValue = '',
178177
placeholder,
179-
value,
180-
onSelect,
181178
...custom
182179
} = this.props;
183180

@@ -192,7 +189,7 @@ export class Select extends PureComponent<ISelect, IState> {
192189
autoComplete="off"
193190
placeholder={placeholder}
194191
className={inputClassName}
195-
value={option.title}
192+
value={option.name}
196193
readOnly
197194
/>
198195
<span className={selectArrowClassName}>

stories/components/15-Select.stories.tsx

+28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { storiesOf } from '@storybook/react';
33
import { withKnobs } from '@storybook/addon-knobs';
44
import { propsTable } from '../common/propsTable';
55
import { Select, Option } from 'mo/components/select';
6+
import { useState } from 'react';
67

78
const stories = storiesOf('Select', module);
89
stories.addDecorator(withKnobs);
@@ -20,9 +21,14 @@ const propDefinitions = [
2021
stories.add(
2122
'Basic Usage',
2223
() => {
24+
const [selectedVal3, setSelectedVal3] = useState('');
2325
const onSelectOption = (e, option) => {
2426
console.log('onSelectOption', e, option);
27+
if (option) {
28+
setSelectedVal3(option.value)
29+
}
2530
};
31+
2632
return (
2733
<div
2834
style={{
@@ -74,6 +80,28 @@ stories.add(
7480
option - 4
7581
</Option>
7682
</Select>
83+
84+
<h3>使用示例 - 3 Change Select Value</h3>
85+
<Select
86+
id="demo3"
87+
style={{
88+
width: 200,
89+
color: 'rgba(255, 255, 255, 0.4)',
90+
background: '#252526',
91+
}}
92+
placeholder="请选择"
93+
value={selectedVal3}
94+
key={`demo3-${selectedVal3}`}
95+
defaultValue={"1"}
96+
onSelect={onSelectOption}
97+
>
98+
<Option value="1">option - 1</Option>
99+
<Option value="2">option - 2</Option>
100+
<Option value="3">option - 3</Option>
101+
<Option value="4" description="Test option one">
102+
option - 4
103+
</Option>
104+
</Select>
77105
</div>
78106
</div>
79107
);

0 commit comments

Comments
 (0)