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

feat: Select add showTip to show option title #229

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions .storybook/stories/Components/Select.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ Basic usages for `Select` component.
<p>Normal</p>
<Select name="select" options={options3} value={43200} />
</div>
<div>
<p>ShowTip</p>
<Select name="select" options={options3} showTip={true} />
</div>
<div style={{ marginTop: "10px" }}>
<p>Disabled</p>
<Select name="select" disabled options={options} />
Expand Down
16 changes: 13 additions & 3 deletions src/components/Select/Option.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,25 @@ export default class Option extends React.Component {
};

renderContent = () => {
const { multi, children } = this.props;
const { multi, children, showTip } = this.props;
return multi ? (
<>
{this.renderCheckbox()}
<span className="select-option-label">{children}</span>
<span
className="select-option-label"
title={showTip ? children || "" : ""}
>
{children}
</span>
</>
) : (
<>
<span className="select-option-label">{children}</span>
<span
className="select-option-label"
title={showTip ? children || "" : ""}
>
{children}
</span>
{this.renderIcon()}
</>
);
Expand Down
24 changes: 21 additions & 3 deletions src/components/Select/Select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default class Select extends React.Component {
valueRenderer: PropTypes.func,
dorpdownRender: PropTypes.func,
disableRemoteSearch: PropTypes.bool,
showTip: PropTypes.bool,
};

static defaultProps = {
Expand All @@ -63,6 +64,7 @@ export default class Select extends React.Component {
onChange() {},
onPaging() {},
disableRemoteSearch: false,
showTip: false,
};

state = {
Expand Down Expand Up @@ -494,7 +496,7 @@ export default class Select extends React.Component {

renderOption = (options) => {
const { value } = this.state;
const { multi, optionRenderer } = this.props;
const { multi, optionRenderer, showTip } = this.props;
const isActive = (v) =>
multi ? value.includes(v.value) : v.disabled ? false : v.value === value;

Expand All @@ -515,6 +517,7 @@ export default class Select extends React.Component {
onClick={this.handleOptionClick}
isActive={isActive(item)}
option={item}
showTip={showTip}
>
{optionRenderer ? optionRenderer(item) : item.label}
</Option>
Expand All @@ -524,8 +527,9 @@ export default class Select extends React.Component {
};

renderInput = () => {
const { searchable, name, placeholder, multi } = this.props;
const { inputVisible, inputValue, value } = this.state;
const { searchable, name, placeholder, multi, showTip } = this.props;
const { inputVisible, inputValue, value, options } = this.state;

const multiClassName =
multi && searchable
? classNames("select-input", {
Expand All @@ -544,6 +548,19 @@ export default class Select extends React.Component {
: ""
: localePlaceholder;

let option = { label: value, value };
options.forEach((item) => {
if (item.value === value) {
option = item;
} else if (item.options) {
item.options.forEach((op) => {
if (op.value === value) {
option = op;
}
});
}
});

return (
<div className={multiClassName}>
<input
Expand All @@ -554,6 +571,7 @@ export default class Select extends React.Component {
onChange={this.handleInputChange}
readOnly={!searchable}
autoComplete="off"
title={showTip ? option?.label || "" : ""}
/>
<span className="select-input-search" ref={this.inputValueRef}>
{inputValue}
Expand Down
Loading