Skip to content

Commit

Permalink
refactor: add disabled API to dropdown to simplify code (#2307)
Browse files Browse the repository at this point in the history
  • Loading branch information
wzhudev authored May 24, 2024
1 parent a5a7b33 commit 3a7f97c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 56 deletions.
8 changes: 7 additions & 1 deletion packages/design/src/components/dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,26 @@ export interface IDropdownProps {
* @param visible
*/
onVisibleChange?: (visible: boolean) => void;

/** Disable dropdown from showing up. */
disabled?: boolean;
}

export function Dropdown(props: IDropdownProps) {
const {
className,
trigger = ['click'],
placement,
children,
overlay,
alignPoint = false,
align,
disabled,
onVisibleChange,
} = props;

// eslint-disable-next-line react/prefer-destructuring-assignment
const trigger = disabled ? [] : (props.trigger || ['click']);

const { mountContainer } = useContext(ConfigContext);

return mountContainer && (
Expand Down
76 changes: 21 additions & 55 deletions packages/ui/src/views/components/doc-bars/ToolbarItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const ToolbarItem = forwardRef((props: IDisplayMenuItem<IMenuItem>, ref:
if (isObservable(selections)) {
return selections;
} else {
return new Observable< typeof selections>((subscribe) => {
return new Observable<typeof selections>((subscribe) => {
subscribe.next(selections);
});
}
Expand All @@ -81,7 +81,7 @@ export const ToolbarItem = forwardRef((props: IDisplayMenuItem<IMenuItem>, ref:
if (isObservable(icon)) {
return icon;
} else {
return new Observable< typeof icon>((subscribe) => {
return new Observable<typeof icon>((subscribe) => {
const v = options?.find((o) => o.value === value)?.icon ?? icon;
subscribe.next(v);
});
Expand Down Expand Up @@ -127,6 +127,7 @@ export const ToolbarItem = forwardRef((props: IDisplayMenuItem<IMenuItem>, ref:

return menuType === MenuItemType.BUTTON_SELECTOR
? (
// Button Selector
<div
className={clsx(styles.toolbarItemSelectButton, {
[styles.toolbarItemSelectButtonDisabled]: disabled,
Expand All @@ -143,66 +144,31 @@ export const ToolbarItem = forwardRef((props: IDisplayMenuItem<IMenuItem>, ref:
onChange={handleChange}
/>
</div>
{!disabled
? (
<Dropdown
overlay={<Menu overViewport="scroll" menuType={id} options={options} onOptionSelect={handleSelect} value={value} />}
onVisibleChange={handleDropdownVisibleChange}
>
<div
className={clsx(styles.toolbarItemSelectButtonArrow, {
[styles.toolbarItemSelectButtonArrowDisabled]: disabled,
[styles.toolbarItemSelectButtonArrowActivated]: activated,
})}
data-disabled={disabled}
>
<MoreDownSingle />
</div>
</Dropdown>
)
: (
<div
className={clsx(styles.toolbarItemSelectButtonArrow, {
[styles.toolbarItemSelectButtonArrowDisabled]: disabled,
[styles.toolbarItemSelectButtonArrowActivated]: activated,
})}
data-disabled={disabled}
>
<MoreDownSingle />
</div>
)}
</div>
)
: !disabled
? (
<Dropdown
overlay={<Menu overViewport="scroll" menuType={id} options={options} onOptionSelect={handleSelect} value={value} />}
onVisibleChange={handleDropdownVisibleChange}
disabled={disabled}
>
<div
className={clsx(styles.toolbarItemSelect, {
[styles.toolbarItemSelectDisabled]: disabled,
[styles.toolbarItemSelectActivated]: activated,
className={clsx(styles.toolbarItemSelectButtonArrow, {
[styles.toolbarItemSelectButtonArrowDisabled]: disabled,
[styles.toolbarItemSelectButtonArrowActivated]: activated,
})}
data-disabled={disabled}
>
<CustomLabel
icon={iconToDisplay}
title={title!}
value={value}
label={label}
onChange={handleChange}
/>
<div
className={clsx(styles.toolbarItemSelectArrow, {
[styles.toolbarItemSelectArrowDisabled]: disabled,
})}
>
<MoreDownSingle />
</div>
<MoreDownSingle />
</div>
</Dropdown>
)
: (

</div>
)
: (
// Selector
<Dropdown
overlay={<Menu overViewport="scroll" menuType={id} options={options} onOptionSelect={handleSelect} value={value} />}
onVisibleChange={handleDropdownVisibleChange}
disabled={disabled}
>
<div
className={clsx(styles.toolbarItemSelect, {
[styles.toolbarItemSelectDisabled]: disabled,
Expand All @@ -219,13 +185,13 @@ export const ToolbarItem = forwardRef((props: IDisplayMenuItem<IMenuItem>, ref:
<div
className={clsx(styles.toolbarItemSelectArrow, {
[styles.toolbarItemSelectArrowDisabled]: disabled,
[styles.toolbarItemSelectArrowActivated]: activated,
})}
>
<MoreDownSingle />
</div>
</div>
);
</Dropdown>
);
}

function renderButtonType() {
Expand Down

0 comments on commit 3a7f97c

Please sign in to comment.