diff --git a/src/scripts/Picklist.tsx b/src/scripts/Picklist.tsx index eb9fdce1..979f782e 100644 --- a/src/scripts/Picklist.tsx +++ b/src/scripts/Picklist.tsx @@ -674,13 +674,17 @@ export const PicklistItem: FC = ({ useContext(PicklistContext); const selected = selected_ ?? (value != null ? values.indexOf(value) >= 0 : false); - const isFocused = focusedValue === value; + const isFocused = value != null && focusedValue === value; const onClick = useEventCallback((e: React.SyntheticEvent) => { - if (!disabled && value != null) { + if (disabled) { + return; + } + + if (value != null) { onSelect(value); - onClick_?.(e); } + onClick_?.(e); }); const itemClassNames = classnames( diff --git a/stories/Picklist.stories.tsx b/stories/Picklist.stories.tsx index 97f293a5..19d5b381 100644 --- a/stories/Picklist.stories.tsx +++ b/stories/Picklist.stories.tsx @@ -1,11 +1,17 @@ -import React from 'react'; +import React, { ComponentProps } from 'react'; import { Picklist, PicklistItem } from '../src/scripts'; -import { ComponentMeta, ComponentStoryObj } from '@storybook/react'; +import { Meta, StoryObj } from '@storybook/react'; + +type StoryProps = ComponentProps & { + picklistItem1_onClick: ComponentProps['onClick']; + picklistItem2_onClick: ComponentProps['onClick']; + picklistItem3_onClick: ComponentProps['onClick']; +}; /** * */ -const meta: ComponentMeta = { +const meta: Meta = { title: 'Picklist', component: Picklist, subcomponents: { PicklistItem }, @@ -14,6 +20,9 @@ const meta: ComponentMeta = { onValueChange: { action: 'valueChange' }, onBlur: { action: 'blur' }, onComplete: { action: 'complete' }, + picklistItem1_onClick: { action: 'picklistItem1_click' }, + picklistItem2_onClick: { action: 'picklistItem2_click' }, + picklistItem3_onClick: { action: 'picklistItem3_click' }, }, parameters: { docs: { @@ -27,15 +36,37 @@ export default meta; /** * */ -export const ControlledWithKnobs: ComponentStoryObj = { +export const ControlledWithKnobs: StoryObj = { name: 'Controlled with knobs', + render: ({ + picklistItem1_onClick, + picklistItem2_onClick, + picklistItem3_onClick, + ...args + }) => ( + + + + + + ), args: { label: 'Picklist Label', - children: [ - , - , - , - ], }, parameters: { docs: { @@ -49,7 +80,8 @@ export const ControlledWithKnobs: ComponentStoryObj = { /** * */ -export const Default: ComponentStoryObj = { +export const Default: StoryObj = { + render: ControlledWithKnobs.render, args: { ...ControlledWithKnobs.args, selectedText: 'Select item from here', @@ -67,7 +99,8 @@ export const Default: ComponentStoryObj = { /** * */ -export const Required: ComponentStoryObj = { +export const Required: StoryObj = { + render: ControlledWithKnobs.render, args: { ...ControlledWithKnobs.args, required: true, @@ -85,7 +118,8 @@ export const Required: ComponentStoryObj = { /** * */ -export const Error: ComponentStoryObj = { +export const Error: StoryObj = { + render: ControlledWithKnobs.render, args: { ...ControlledWithKnobs.args, required: true, @@ -104,15 +138,40 @@ export const Error: ComponentStoryObj = { /** * */ -export const DisabledItems: ComponentStoryObj = { +export const DisabledItems: StoryObj = { + render: ({ + picklistItem1_onClick, + picklistItem2_onClick, + picklistItem3_onClick, + ...args + }) => ( + + + + + + ), args: { label: 'Picklist Label', defaultOpened: true, - children: [ - , - , - , - ], }, parameters: { docs: { @@ -126,7 +185,8 @@ export const DisabledItems: ComponentStoryObj = { /** * */ -export const Disabled: ComponentStoryObj = { +export const Disabled: StoryObj = { + render: ControlledWithKnobs.render, args: { ...ControlledWithKnobs.args, disabled: true, @@ -143,7 +203,8 @@ export const Disabled: ComponentStoryObj = { /** * */ -export const MenuSizeMedium: ComponentStoryObj = { +export const MenuSizeMedium: StoryObj = { + render: ControlledWithKnobs.render, name: 'Menu Size - Medium', args: { ...ControlledWithKnobs.args, @@ -163,7 +224,8 @@ export const MenuSizeMedium: ComponentStoryObj = { /** * */ -export const MenuSizeLarge: ComponentStoryObj = { +export const MenuSizeLarge: StoryObj = { + render: ControlledWithKnobs.render, name: 'Menu Size - Large', args: { ...ControlledWithKnobs.args, @@ -183,7 +245,8 @@ export const MenuSizeLarge: ComponentStoryObj = { /** * */ -export const SingleItemSelected: ComponentStoryObj = { +export const SingleItemSelected: StoryObj = { + render: ControlledWithKnobs.render, name: 'Single item selected', args: { ...ControlledWithKnobs.args, @@ -202,7 +265,8 @@ export const SingleItemSelected: ComponentStoryObj = { /** * */ -export const MultipleItemsSelected: ComponentStoryObj = { +export const MultipleItemsSelected: StoryObj = { + render: ControlledWithKnobs.render, name: 'Multiple items selected', args: { ...ControlledWithKnobs.args, @@ -223,7 +287,7 @@ export const MultipleItemsSelected: ComponentStoryObj = { /** * */ -export const DropdownScroll: ComponentStoryObj = { +export const DropdownScroll: StoryObj = { args: { ...ControlledWithKnobs.args, selectedText: 'Select item from here', @@ -254,7 +318,8 @@ export const DropdownScroll: ComponentStoryObj = { /** * */ -export const WithTooltip: ComponentStoryObj = { +export const WithTooltip: StoryObj = { + render: ControlledWithKnobs.render, name: 'With tooltip', args: { ...ControlledWithKnobs.args, @@ -272,26 +337,40 @@ export const WithTooltip: ComponentStoryObj = { /** * */ -export const WithDividers: ComponentStoryObj = { +export const WithDividers: StoryObj = { name: 'With Dividers', - args: { - label: 'Picklist Label', - defaultOpened: true, - children: [ + render: ({ + picklistItem1_onClick, + picklistItem2_onClick, + picklistItem3_onClick, + ...args + }) => ( + , - , + /> + , - ], + /> + + ), + args: { + label: 'Picklist Label', + defaultOpened: true, }, parameters: { docs: { @@ -301,3 +380,36 @@ export const WithDividers: ComponentStoryObj = { }, }, }; + +/** + * + */ +export const WithoutValue: StoryObj = { + name: 'Without Value', + render: ({ picklistItem1_onClick, picklistItem2_onClick, ...args }) => ( + + + + + ), + args: { + label: 'Picklist Label', + defaultOpened: true, + }, + parameters: { + docs: { + description: { + story: + 'Picklist with no value specified. Initially, no item should be focused. Then, on clicking an item, at least `onClick()` of it should be triggered.', + }, + }, + }, +};