Skip to content

Commit

Permalink
[Select][joy-ui] Fix displaying placeholder when multiple is true (mu…
Browse files Browse the repository at this point in the history
  • Loading branch information
sai6855 authored Nov 13, 2023
1 parent 7fcc27f commit 20c3ee6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
23 changes: 23 additions & 0 deletions packages/mui-joy/src/Select/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -673,5 +673,28 @@ describe('Joy <Select />', () => {

expect(getByRole('combobox')).to.have.text('One, Two');
});

it('should render placeholder when options are not selected', () => {
const { getByRole } = render(
<Select multiple defaultValue={[]} placeholder="hello">
<Option value={1}>One</Option>
<Option value={2}>Two</Option>
</Select>,
);

expect(getByRole('combobox')).to.have.text('hello');
});

it('renders the selected values inplace of placeholder', () => {
const { getByRole } = render(
<Select multiple defaultValue={[1, 2]} placeholder="hello">
<Option value={1}>One</Option>
<Option value={2}>Two</Option>
</Select>,
);

expect(getByRole('combobox')).to.have.text('One, Two');
expect(getByRole('combobox')).not.to.have.text('hello');
});
});
});
13 changes: 10 additions & 3 deletions packages/mui-joy/src/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -571,16 +571,23 @@ const Select = React.forwardRef(function Select<OptionValue extends {}, Multiple
[listboxProps.modifiers],
);

let displayValue = placeholder;

if (
(Array.isArray(selectedOption) && selectedOption.length > 0) ||
(!Array.isArray(selectedOption) && !!selectedOption)
) {
displayValue = renderValue(selectedOption);
}

return (
<React.Fragment>
<SlotRoot {...rootProps}>
{startDecorator && (
<SlotStartDecorator {...startDecoratorProps}>{startDecorator}</SlotStartDecorator>
)}

<SlotButton {...buttonProps}>
{selectedOption ? renderValue(selectedOption) : placeholder}
</SlotButton>
<SlotButton {...buttonProps}>{displayValue}</SlotButton>
{endDecorator && <SlotEndDecorator {...endDecoratorProps}>{endDecorator}</SlotEndDecorator>}

{indicator && <SlotIndicator {...indicatorProps}>{indicator}</SlotIndicator>}
Expand Down

0 comments on commit 20c3ee6

Please sign in to comment.