Skip to content

Commit

Permalink
feat(plasma-*, sdds-*): fix renderValue prop in Select
Browse files Browse the repository at this point in the history
  • Loading branch information
shuga2704 committed Dec 9, 2024
1 parent e23ae11 commit c589255
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 1 deletion.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,69 @@ describe('plasma-b2c: Select', () => {
cy.get('[data-floating-ui-portal] > div').should('have.css', 'z-index', '10000');
});

it('prop: renderValue', () => {
cy.viewport(400, 800);

const Component = () => {
const [valueSingle, setValueSingle] = React.useState('paris');
const [valueMultiple, setValueMultiple] = React.useState(['paris', 'lyon']);

return (
<CypressTestDecoratorWithTypo>
<div style={{ width: '300px' }}>
<Select
items={items}
value={valueSingle}
onChange={setValueSingle}
renderValue={(item) => item.label.toUpperCase()}
/>
</div>

<br />

<div style={{ width: '300px' }}>
<Select
items={items}
multiselect
value={valueMultiple}
onChange={setValueMultiple}
renderValue={(item) => item.label.toUpperCase()}
/>
</div>

<br />

<div style={{ width: '300px' }}>
<Select
target="button-like"
items={items}
value={valueSingle}
onChange={setValueSingle}
renderValue={(item) => item.label.toUpperCase()}
/>
</div>

<br />

<div style={{ width: '300px' }}>
<Select
target="button-like"
items={items}
multiselect
value={valueMultiple}
onChange={setValueMultiple}
renderValue={(item) => item.label.toUpperCase()}
/>
</div>
</CypressTestDecoratorWithTypo>
);
};

mount(<Component />);

cy.matchImageSnapshot();
});

it('basic logic', () => {
cy.viewport(1000, 500);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ export const Textfield = forwardRef<HTMLInputElement, TextfieldProps>(
) => {
const withArrowInverse = opened ? classes.arrowInverse : undefined;

const getValue = () => {
if (multiselect || !value) {
return '';
}

if (renderValue) {
return renderValue(valueToItemMap.get(value.toString())!);
}

return valueToItemMap.get(value.toString())?.label || '';
};

const getChips = (): string[] => {
if (multiselect && Array.isArray(value)) {
if (value.length === 0) return [];
Expand Down Expand Up @@ -87,7 +99,7 @@ export const Textfield = forwardRef<HTMLInputElement, TextfieldProps>(
ref={ref}
inputWrapperRef={inputWrapperRef}
readOnly
value={multiselect ? undefined : valueToItemMap.get(value.toString())?.label || ''}
value={getValue()}
size={size}
view={view}
labelPlacement={labelPlacement}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,69 @@ describe('plasma-web: Select', () => {
cy.get('[data-floating-ui-portal] > div').should('have.css', 'z-index', '10000');
});

it('prop: renderValue', () => {
cy.viewport(400, 800);

const Component = () => {
const [valueSingle, setValueSingle] = React.useState('paris');
const [valueMultiple, setValueMultiple] = React.useState(['paris', 'lyon']);

return (
<CypressTestDecoratorWithTypo>
<div style={{ width: '300px' }}>
<Select
items={items}
value={valueSingle}
onChange={setValueSingle}
renderValue={(item) => item.label.toUpperCase()}
/>
</div>

<br />

<div style={{ width: '300px' }}>
<Select
items={items}
multiselect
value={valueMultiple}
onChange={setValueMultiple}
renderValue={(item) => item.label.toUpperCase()}
/>
</div>

<br />

<div style={{ width: '300px' }}>
<Select
target="button-like"
items={items}
value={valueSingle}
onChange={setValueSingle}
renderValue={(item) => item.label.toUpperCase()}
/>
</div>

<br />

<div style={{ width: '300px' }}>
<Select
target="button-like"
items={items}
multiselect
value={valueMultiple}
onChange={setValueMultiple}
renderValue={(item) => item.label.toUpperCase()}
/>
</div>
</CypressTestDecoratorWithTypo>
);
};

mount(<Component />);

cy.matchImageSnapshot();
});

it('basic logic', () => {
cy.viewport(1000, 500);

Expand Down

0 comments on commit c589255

Please sign in to comment.