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

[Fix]: Dropdown selected item parsing #730

Merged
merged 2 commits into from
May 26, 2023
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "suomifi-ui-components",
"version": "10.0.2",
"version": "10.0.3",
"description": "Suomi.fi UI component library",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
19 changes: 9 additions & 10 deletions src/core/Dropdown/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { SuomifiThemeProp, SuomifiThemeConsumer } from '../../theme';
import {
forkRefs,
getOwnerDocument,
getRecursiveChildText,
HTMLAttributesIncludingDataAttributes,
} from '../../../utils/common/common';
import { Popover } from '../../../core/Popover/Popover';
Expand Down Expand Up @@ -77,7 +76,7 @@ const { Provider: DropdownProvider, Consumer: DropdownConsumer } =

interface DropdownState {
selectedValue: string | undefined | null;
selectedValueText: string | undefined | null;
selectedValueNode: ReactNode | undefined | null;
ariaExpanded: boolean;
showPopover: boolean;
focusedDescendantId: string | null | undefined;
Expand Down Expand Up @@ -168,7 +167,7 @@ class BaseDropdown extends Component<DropdownProps> {
: 'defaultValue' in this.props
? this.props.defaultValue
: undefined,
selectedValueText: BaseDropdown.parseSelectedValueText(
selectedValueNode: BaseDropdown.getSelectedValueNode(
'value' in this.props
? this.props.value
: 'defaultValue' in this.props
Expand Down Expand Up @@ -207,7 +206,7 @@ class BaseDropdown extends Component<DropdownProps> {
if ('value' in nextProps && value !== prevState.selectedValue) {
return {
selectedValue: value,
selectedValueText: BaseDropdown.parseSelectedValueText(
selectedValueNode: BaseDropdown.getSelectedValueNode(
value,
nextProps.children,
),
Expand All @@ -216,24 +215,24 @@ class BaseDropdown extends Component<DropdownProps> {
return null;
}

static parseSelectedValueText(
static getSelectedValueNode(
selectedValue: string | undefined,
children:
| Array<ReactElement<DropdownItemProps>>
| ReactElement<DropdownItemProps>
| undefined,
): string | undefined {
): ReactNode | undefined {
jenkrisu marked this conversation as resolved.
Show resolved Hide resolved
if (selectedValue === undefined || children === undefined) return undefined;

if (Array.isArray(children)) {
for (let index = 0; index < children.length; index += 1) {
const element = children[index];
if (element.props.value === selectedValue) {
return getRecursiveChildText(element);
return element.props.children;
}
}
} else {
return getRecursiveChildText(children);
return children.props.children;
}
}

Expand All @@ -258,7 +257,7 @@ class BaseDropdown extends Component<DropdownProps> {
}
this.setState({
selectedValue: itemValue,
selectedValueText: BaseDropdown.parseSelectedValueText(
selectedValueNode: BaseDropdown.getSelectedValueNode(
itemValue,
this.props.children,
),
Expand Down Expand Up @@ -429,7 +428,7 @@ class BaseDropdown extends Component<DropdownProps> {
if (this.props.alwaysShowVisualPlaceholder) {
return this.props.visualPlaceholder;
}
return this.state.selectedValueText ?? this.props.visualPlaceholder;
return this.state.selectedValueNode ?? this.props.visualPlaceholder;
}

private focusToButtonAndClosePopover() {
Expand Down
30 changes: 1 addition & 29 deletions src/utils/common/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { MutableRefObject, ReactElement, Ref } from 'react';
import React, { MutableRefObject, Ref } from 'react';
import { getLogger } from '../../utils/log';

export function windowAvailable() {
Expand Down Expand Up @@ -47,34 +47,6 @@ export const forkRefs =
});
};

export const getRecursiveChildText = (reactNode: ReactElement): any => {
const children = reactNode.props?.children || undefined;
if (Array.isArray(reactNode)) {
// Multiple children
const joinedNodes: Array<ReactElement | string> = [];
reactNode.forEach((node) => {
if (typeof node === 'object') {
joinedNodes.push(getRecursiveChildText(node));
} else if (typeof node === 'string') {
joinedNodes.push(node);
}
});
return joinedNodes.join(' ');
}
if (children === undefined) {
if (typeof reactNode === 'string') return reactNode;
return '';
}
if (typeof children === 'object') {
// Found direct child
return getRecursiveChildText(children);
}
if (typeof children === 'string' || typeof children === 'number') {
// Found searchable string
return children;
}
};

/**
* The following interface allows data-* attributes.
* The basic React.HTMLAttributes interface throws errors when trying to do something like
Expand Down