Select
This guide outlines the steps needed to migrate from the previous <Select> component (likely from @ohif/ui) to the new compound <Select> component provided by @ohif/ui-next.
Key Changes
- Deprecated Component: The previous standalone
<Select>component is deprecated. - New Compound Component: The new implementation uses a compound component pattern, requiring multiple specific sub-components (
Select,SelectTrigger,SelectValue,SelectContent,SelectItem). - Option Definition: Options are no longer passed as a single
optionsprop. Instead, each option is rendered as an individual<SelectItem>component within<SelectContent>. - Placeholder: The
placeholderprop is now applied to the<SelectValue>sub-component. - Value Handling: The
valueandonValueChangeprops are now managed by the root<Select>component. Note the change fromonChangetoonValueChange.
Migration Steps
-
Update Imports: Replace the import for the old
Selectcomponent with imports for the new compound components from@ohif/ui-next.- import { Select } from '@ohif/ui';+ import {+ Select,+ SelectContent,+ SelectItem,+ SelectTrigger,+ SelectValue,+ } from '@ohif/ui-next'; -
Adapt Component Structure: Replace the single
<Select>tag with the new compound structure. Map your existingoptionsarray to individual<SelectItem>components.Example Diff:
- <Select- label={t('Strategy')}- closeMenuOnSelect={true}- className="border-primary-main mr-2 bg-black text-white"- options={options}- placeholder={options.find(option => option.value === config.strategy).placeHolder}- value={config.strategy}- onChange={({ value }) => {- dispatch({- type: 'setStrategy',- payload: {- strategy: value,- },- });- }}- />+ <Select+ value={config.strategy}+ onValueChange={value => {+ dispatch({+ type: 'setStrategy',+ payload: {+ strategy: value,+ },+ });+ }}+ >+ <SelectTrigger className="w-full">+ <SelectValue+ placeholder={options.find(option => option.value === config.strategy)?.placeHolder}+ />+ </SelectTrigger>+ <SelectContent className="">+ {options.map(option => (+ <SelectItem+ key={option.value}+ value={option.value}+ >+ {option.label}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>Explanation:
- The main logic container is now the root
<Select>component, which takes thevalueand theonValueChangehandler (note:onValueChangedirectly receives the value, not an event object). <SelectTrigger>wraps the element that opens the dropdown (often styled like the previous select input).<SelectValue>displays the currently selected value or theplaceholdertext if no value is selected.<SelectContent>contains the list of options.- Each option is rendered using
<SelectItem>, where thevalueprop corresponds to the option's value and the children ({option.label}) represent the text displayed for that option. - Props like
closeMenuOnSelectare generally handled by default in the new component.
- The main logic container is now the root
-
Adjust Styling: The internal structure and default styling have changed. Remove or update previous CSS class names (
className) applied to the old component and apply new Tailwind/CSS classes to the appropriate sub-components (Select,SelectTrigger,SelectContent,SelectItem) as needed to match your desired appearance. Note thatborder-primary-mainandbg-blackmight no longer be necessary or applied differently with the new component's structure and variants.