Skip to content

Commit

Permalink
why not
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Sep 14, 2023
1 parent 2bd95c5 commit 728e2f0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
23 changes: 10 additions & 13 deletions docs/data/base/components/select/UnstyledSelectCustomRenderValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ import { styled } from '@mui/system';

export default function UnstyledSelectCustomRenderValue() {
return (
<CustomSelect renderValue={renderValue} placeholder="Select an option…">
<CustomSelect
defaultValue={10}
renderValue={(option) => {
if (option == null || option.value === null) {
return 'Select an option…';
}
return `${option.label} (${option.value})`;
}}
>
<StyledOption value={null}>None</StyledOption>
<StyledOption value={10}>Ten</StyledOption>
<StyledOption value={20}>Twenty</StyledOption>
<StyledOption value={30}>Thirty</StyledOption>
Expand Down Expand Up @@ -40,18 +49,6 @@ CustomSelect.propTypes = {
}),
};

function renderValue(option) {
if (option == null) {
return null;
}

return (
<span>
{option.label} ({option.value})
</span>
);
}

const blue = {
100: '#DAECFF',
200: '#99CCF3',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ import { styled } from '@mui/system';

export default function UnstyledSelectCustomRenderValue() {
return (
<CustomSelect renderValue={renderValue} placeholder="Select an option…">
<CustomSelect
defaultValue={10}
renderValue={(option: SelectOption<number> | null) => {
if (option == null || option.value === null) {
return 'Select an option…';
}
return `${option.label} (${option.value})`;
}}
>
<StyledOption value={null}>None</StyledOption>
<StyledOption value={10}>Ten</StyledOption>
<StyledOption value={20}>Twenty</StyledOption>
<StyledOption value={30}>Thirty</StyledOption>
<StyledOption value={30}>Thrity</StyledOption>
</CustomSelect>
);
}
Expand All @@ -26,18 +35,6 @@ function CustomSelect(props: SelectProps<number, false>) {
return <Select {...props} slots={slots} />;
}

function renderValue(option: SelectOption<number> | null) {
if (option == null) {
return null;
}

return (
<span>
{option.label} ({option.value})
</span>
);
}

const blue = {
100: '#DAECFF',
200: '#99CCF3',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<CustomSelect renderValue={renderValue} placeholder="Select an option…">
<CustomSelect
defaultValue={10}
renderValue={(option: SelectOption<number> | null) => {
if (option == null || option.value === null) {
return 'Select an option…';
}
return `${option.label} (${option.value})`;
}}
>
<StyledOption value={null}>None</StyledOption>
<StyledOption value={10}>Ten</StyledOption>
<StyledOption value={20}>Twenty</StyledOption>
<StyledOption value={30}>Thirty</StyledOption>
</CustomSelect>
</CustomSelect>
2 changes: 1 addition & 1 deletion docs/data/base/components/select/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ For the sake of simplicity, demos and code snippets primarily feature components
You can customize the appearance of the selected value display by providing a function to the `renderValue` prop.
The element returned by this function will be rendered inside the Select's button.

{{"demo": "UnstyledSelectCustomRenderValue.js", "defaultCodeOpen": false}}
{{"demo": "UnstyledSelectCustomRenderValue.js"}}

### Option appearance

Expand Down

0 comments on commit 728e2f0

Please sign in to comment.