UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

93 lines (92 loc) 5.22 kB
import type { CSSProperties, ComponentProps, JSX, ReactNode } from 'react'; import type { FieldOptionProps as OptionFieldProps } from '../Option'; import type { FieldBlockWidth } from '../../FieldBlock'; import type { FieldProps, Path } from '../../types'; import type { FormStatusText } from '../../../../components/FormStatus'; import type { AutocompleteAllProps } from '../../../../components/Autocomplete'; import type { DropdownAllProps } from '../../../../components/Dropdown'; import type { HelpProps } from '../../../../components/help-button/HelpButtonInline'; import type { DrawerListDataArrayObjectStrict, DrawerListProps } from '../../../../fragments/DrawerList'; import type { FormError } from '../../utils'; import type { RadioProps } from '../../../../components/Radio'; import type { ToggleButtonProps } from '../../../../components/ToggleButton'; import type { RadioGroupProps } from '../../../../components/radio/RadioGroup'; import type { ToggleButtonGroupProps } from '../../../../components/toggle-button/ToggleButtonGroup'; type IOption = { title: string | ReactNode; value: number | string; status: FormStatusText; }; export type Data = Array<{ value: number | string; title: ReactNode; text?: ReactNode; disabled?: boolean; style?: CSSProperties; [key: string]: any; } & Partial<DrawerListDataArrayObjectStrict>>; type RenderSelectionChildren = (params: { value: IOption['value']; options: FieldSelectionProps['data']; }) => ReactNode; export type FieldSelectionProps = FieldProps<IOption['value']> & { /** * Choice of UI feature. Can be: `dropdown`, `autocomplete`, `button`, `radio`. */ variant?: 'dropdown' | 'autocomplete' | 'radio' | 'button'; /** * `small`, `medium` or `large` for predefined standard widths, `stretch` for fill available width. */ width?: FieldBlockWidth; /** * Layout for the list of options. Can be `horizontal` or `vertical`. */ optionsLayout?: 'horizontal' | 'vertical'; /** * Transform the displayed selection for Dropdown and Autocomplete variant. Use it to display a different value than the one in the data set. The first parameter is the properties of the Option component or data item. You can return a `React.ReactNode` that will be displayed in the selection. */ transformSelection?: (props: OptionFieldProps) => ReactNode; /** * The path to the context data (Form.Handler). The context data object needs to have a `value` and a `title` property. The generated options will be placed above given JSX based children. When `children` is a function, the generated options are instead provided as `options` to the function. */ dataPath?: Path; /** * Data to be used for the component. The object needs to have a `value` and a `title` property. Provide the Dropdown or Autocomplete data in the format documented here: [Dropdown](/uilib/components/dropdown) and [Autocomplete](/uilib/components/autocomplete) documentation. */ data?: Data; /** * An array of group titles for the list items. Only the first group can be `undefined`. */ groups?: ReactNode[]; /** * Forward any additional properties to the [Autocomplete](/uilib/components/autocomplete/) component. `onType` will additionally provide the `value` parameter with `emptyValue` support in addition to the internal `dataContext`. */ autocompleteProps?: AutocompleteAllProps; /** * Forward any additional properties to the [Dropdown](/uilib/components/dropdown/) component. */ dropdownProps?: DropdownAllProps; /** * The sizes you can choose are `small` (1.5rem), `default` (2rem), `medium` (2.5rem) and `large` (3rem). Defaults to `default` / `null`. Also, if you define a number like `size="2"` then it will be forwarded as the input element attribute. Consider rather setting field sizes with [Form.Appearance](/uilib/extensions/forms/Form/Appearance/). */ size?: ToggleButtonGroupProps['size'] | RadioGroupProps['size'] | AutocompleteAllProps['size'] | DropdownAllProps['size']; /** * For providing [Field.Option](/uilib/extensions/forms/base-fields/Option/) components and other children. Can also be a render function that receives `{ value, options }`, where `options` are from `data` or `dataPath` and may include additional custom properties. */ children?: ReactNode | RenderSelectionChildren; }; declare function Selection(props: FieldSelectionProps): import("react/jsx-runtime").JSX.Element; type OptionProps = ComponentProps<(props: { value: FieldSelectionProps['value']; error: Error | FormError | undefined; help: HelpProps; title: ReactNode; children: ReactNode; size?: ToggleButtonProps['size'] | RadioProps['size']; }) => JSX.Element>; export declare function countOptions(children: ReactNode): number; export declare function mapOptions(children: ReactNode, { createOption, }: { createOption(props: OptionProps, i: number): ReactNode; }): any; export declare function makeOptions<T = DrawerListProps['data']>(children: ReactNode, transformSelection?: FieldSelectionProps['transformSelection']): T; export default Selection;