import type { ListItemProps } from '@planview/pv-uikit' import { ListGroup, ListItem, ListItemEmpty, ListItemInfo, ListSectionExpandable, } from '@planview/pv-uikit' import React from 'react' import { theme, useControlledOrDefault } from '@planview/pv-utilities' import { Info } from '@planview/pv-icons' import { defineMessages, FormattedMessage, useIntl } from 'react-intl' import type { NaturalLanguageDropdownMenuProps } from './dropdown-menu' import { NaturalLanguageDropdownMenu } from './dropdown-menu' const messages = defineMessages({ refineSearch: { id: 'pvds.form.nlf.select.refineSearch', defaultMessage: `Please refine your search. {br} {count, number} of {total, number} items displayed.`, description: 'Information item displayed in natural language form select when user needs to refine search', }, noMatchesFound: { id: 'pvds.form.nlf.select.noMatchesFound', defaultMessage: 'No matches found.', description: 'Text shown in select list when filtering or searching and there are no results', }, }) export type NaturalLanguageGroup = { /** * Can the group heading be collapsed and expanded. When set to false, a ListGroup is rendered instead of a ListExpandableSection. */ expandable?: boolean /** * Is the group heading currently expanded or collapsed? When omitted (uncontrolled), the state will be reset each time the menu opens. */ expanded?: boolean /** * Callback when the heading is expanded or collapsed */ onChange?: (nextState: boolean) => void /** * Label for the group heading */ label: string /** * Array of options in this group */ options: NaturalLanguageOption[] } export type NaturalLanguageSearchResults< T extends number | string = number | string > = { /** * Number of results being shown */ count: number /** * Total number of results available. When this number does not match count, a footer will be shown below the options asking the user to refine their search. */ total: number /** * Array of options or groups */ options: (NaturalLanguageOption | NaturalLanguageGroup)[] /** * Customized label overriding the default label in footer asking the user to refine their search. */ labelRefineSearch?: React.ReactNode } export type NaturalLanguageOption = Omit< ListItemProps, | 'focused' | 'id' | 'multiSelectable' | 'selected' | 'role' | 'highlight' | 'forceHover' | 'href' | 'onActivate' | 'hidden' | 'nextFocus' > & { /** * Unique value of the option */ value: T } export type NaturalLanguageSelectProps< T extends string | number = string | number > = { /** When no value or defaultValue are present, render this text as a placeholder */ placeholder?: string /** * Controlled value for the field. Can be a simple value that will be matched against * the provided options, or it can be a full NaturalLanguageOption object in situations * where the provided options may not include the currently selected value. * */ value?: null | T | NaturalLanguageOption /** Same as value, but uncontrolled */ defaultValue?: null | T | NaturalLanguageOption /** * Options configuration. May be an array of options, groups of options, or search results. * * Read more about each type on their respective documentation pages: * - [NaturalLanguageGroup](https://planview-ds.github.io/react-pvds/?path=/docs/pv-form-naturallanguageform-naturallanguageselect-naturallanguagegroup--docs) * - [NaturalLanguageOption](https://planview-ds.github.io/react-pvds/?path=/docs/pv-form-naturallanguageform-naturallanguageselect-naturallanguageoption--docs) * - [NaturalLanguageSearchResults](https://planview-ds.github.io/react-pvds/?path=/docs/pv-form-naturallanguageform-naturallanguageselect-naturallanguagesearchresults--docs) * */ options: | (NaturalLanguageGroup | NaturalLanguageOption)[] | NaturalLanguageSearchResults /** * Callback when a new option is selected. */ onChange?: (value: NaturalLanguageOption) => void /**TODO: Do we need a clearable option? */ } & Omit< NaturalLanguageDropdownMenuProps, 'children' | 'trigger' | 'role' | 'multiSelectable' | 'placeholder' > function getValue( value: null | T | NaturalLanguageOption | undefined, options: NaturalLanguageOption[] ): NaturalLanguageOption | undefined | null { if (value === undefined) { return undefined } if (value === null) { return null } if (typeof value === 'string' || typeof value === 'number') { return options.find((o) => o.value === value) } return value } export const NaturalLanguageSelect = < T extends string | number = string | number >({ value: valueIncoming, defaultValue, label, placeholder, options: optionsIncoming, onChange, ...props }: NaturalLanguageSelectProps) => { const intl = useIntl() const isSearch = 'count' in optionsIncoming const { count = 0, total = 0, options, labelRefineSearch, } = 'count' in optionsIncoming ? optionsIncoming : { count: 0, total: 0, options: optionsIncoming } const flatOptions = React.useMemo( () => options.flatMap((option) => { if ('options' in option) { return option.options } return option }), [options] ) const handleChange = React.useCallback( (val: NaturalLanguageOption | null) => { if (val) { onChange?.(val) } }, [onChange] ) const [selectedOption, setSelectedOption] = useControlledOrDefault | null>( getValue(valueIncoming, flatOptions), getValue(defaultValue, flatOptions), handleChange ) return ( {options.map((option) => { if ('options' in option) { const childOptions = option.options.map((o) => ( { setSelectedOption(o) }} /> )) if (option.expandable) { return ( {childOptions} ) } else { return ( {childOptions} ) } } return ( { setSelectedOption(option) }} /> ) })} {isSearch ? ( count === 0 && options.length === 0 ? ( ) : total !== count && !!options.length ? ( } hidden={false} > {labelRefineSearch ? ( labelRefineSearch ) : ( , }} /> )} ) : null ) : null} ) }