From 72554ac016209677b4575d398c3d8dbaf5575f3a Mon Sep 17 00:00:00 2001 From: Yuan Qing Lim Date: Sat, 23 Sep 2023 10:03:22 +0800 Subject: [PATCH] Refactor --- packages/ui/src/components/banner/banner.tsx | 15 +- packages/ui/src/components/button/button.tsx | 52 +- .../button/stories/button-danger.stories.tsx | 8 + .../button/stories/button-default.stories.tsx | 4 + .../button-secondary-danger.stories.tsx | 8 + .../stories/button-secondary.stories.tsx | 8 + .../ui/src/components/checkbox/checkbox.tsx | 141 ++-- .../src/components/disclosure/disclosure.tsx | 104 +-- .../ui/src/components/divider/divider.tsx | 10 +- .../components/dropdown/dropdown.module.css | 8 +- .../ui/src/components/dropdown/dropdown.tsx | 641 ++++++++++-------- .../stories/dropdown-border.stories.tsx | 60 +- .../stories/dropdown-default.stories.tsx | 44 +- .../stories/dropdown-underline.stories.tsx | 44 +- .../file-upload-button/file-upload-button.tsx | 95 +-- .../file-upload-dropzone.tsx | 164 +++-- .../components/icon-button/icon-button.tsx | 84 ++- .../icon-toggle-button/icon-toggle-button.tsx | 62 +- packages/ui/src/components/layer/layer.tsx | 64 +- .../loading-indicator/loading-indicator.tsx | 14 +- packages/ui/src/components/modal/modal.tsx | 2 +- .../ui/src/components/preview/preview.tsx | 14 +- .../radio-buttons/radio-buttons.tsx | 190 +++--- .../radio-buttons-selected.stories.tsx | 33 - .../components/range-slider/range-slider.tsx | 138 ++-- .../search-textbox/search-textbox.tsx | 93 ++- .../stories/search-textbox.stories.tsx | 2 +- .../segmented-control/segmented-control.tsx | 66 +- .../stories/segmented-control.stories.tsx | 40 +- .../selectable-item/selectable-item.tsx | 66 +- packages/ui/src/components/tabs/tabs.tsx | 55 +- packages/ui/src/components/text/text.tsx | 8 +- .../textbox-autocomplete-border.stories.tsx | 4 +- .../textbox-autocomplete-default.stories.tsx | 4 +- ...textbox-autocomplete-underline.stories.tsx | 4 +- .../textbox-autocomplete.tsx | 199 ++++-- .../private/create-rgba-color.ts | 2 + .../stories/textbox-color-border.stories.tsx | 66 +- .../stories/textbox-color-default.stories.tsx | 64 +- .../textbox-color-underline.stories.tsx | 66 +- .../textbox-color/textbox-color.module.css | 4 +- .../textbox/textbox-color/textbox-color.tsx | 621 ++++++++--------- .../textbox-multiline-border.stories.tsx | 4 +- .../textbox-multiline-default.stories.tsx | 4 +- .../textbox-multiline-underline.stories.tsx | 4 +- .../textbox-multiline/textbox-multiline.tsx | 106 +-- .../private/raw-textbox-numeric.tsx | 122 ++-- .../textbox-numeric-border.stories.tsx | 47 +- .../textbox-numeric-default.stories.tsx | 39 +- .../textbox-numeric-underline.stories.tsx | 47 +- .../textbox-numeric/textbox-numeric.tsx | 23 +- .../textbox/textbox/private/raw-textbox.tsx | 308 +++++---- .../stories/textbox-border.stories.tsx | 42 +- .../stories/textbox-default.stories.tsx | 35 +- .../stories/textbox-underline.stories.tsx | 47 +- .../components/textbox/textbox/textbox.tsx | 58 +- packages/ui/src/components/toggle/toggle.tsx | 46 +- packages/ui/src/css/base.css | 6 - packages/ui/src/css/theme.css | 2 + .../use-form/stories/use-form.stories.tsx | 10 +- packages/ui/src/icons/create-icon.tsx | 23 +- packages/ui/src/inline-text/bold/bold.tsx | 14 +- packages/ui/src/inline-text/code/code.tsx | 14 +- packages/ui/src/inline-text/link/link.tsx | 51 +- packages/ui/src/inline-text/muted/muted.tsx | 14 +- packages/ui/src/layout/columns/columns.tsx | 22 +- .../ui/src/layout/container/container.tsx | 21 +- packages/ui/src/layout/inline/inline.tsx | 22 +- .../src/layout/middle-align/middle-align.tsx | 21 +- packages/ui/src/layout/stack/stack.tsx | 23 +- .../layout/vertical-space/vertical-space.tsx | 22 +- packages/ui/src/types/colors.ts | 2 + packages/ui/src/types/event-handler.ts | 131 ++++ .../ui/src/types/focusable-component-props.ts | 7 + packages/ui/src/types/rgba.ts | 6 + packages/ui/src/types/space.ts | 1 + packages/ui/src/types/types.ts | 13 - packages/ui/src/utilities/create-component.ts | 19 + packages/ui/src/utilities/no-op.ts | 1 + 79 files changed, 2852 insertions(+), 1896 deletions(-) create mode 100644 packages/ui/src/types/event-handler.ts create mode 100644 packages/ui/src/types/focusable-component-props.ts create mode 100644 packages/ui/src/types/rgba.ts create mode 100644 packages/ui/src/types/space.ts delete mode 100644 packages/ui/src/types/types.ts create mode 100644 packages/ui/src/utilities/create-component.ts create mode 100644 packages/ui/src/utilities/no-op.ts diff --git a/packages/ui/src/components/banner/banner.tsx b/packages/ui/src/components/banner/banner.tsx index bf7909b1c..570dde959 100644 --- a/packages/ui/src/components/banner/banner.tsx +++ b/packages/ui/src/components/banner/banner.tsx @@ -1,7 +1,7 @@ import { ComponentChildren, h, JSX } from 'preact' -import { Props } from '../../types/types.js' import { createClassName } from '../../utilities/create-class-name.js' +import { createComponent } from '../../utilities/create-component.js' import styles from './banner.module.css' export type BannerProps = { @@ -11,15 +11,14 @@ export type BannerProps = { } export type BannerVariant = 'success' | 'warning' -export function Banner({ - children, - icon, - variant, - ...rest -}: Props): JSX.Element { +export const Banner = createComponent(function ( + { children, icon, variant, ...rest }, + ref +): JSX.Element { return (
{children}
) -} +}) diff --git a/packages/ui/src/components/button/button.tsx b/packages/ui/src/components/button/button.tsx index d5a8ad53e..fb8ecc85e 100644 --- a/packages/ui/src/components/button/button.tsx +++ b/packages/ui/src/components/button/button.tsx @@ -1,44 +1,55 @@ import { ComponentChildren, h, JSX } from 'preact' import { useCallback } from 'preact/hooks' -import { Props } from '../../types/types.js' +import { Event, EventHandler } from '../../types/event-handler.js' +import { FocusableComponentProps } from '../../types/focusable-component-props.js' import { createClassName } from '../../utilities/create-class-name.js' +import { createComponent } from '../../utilities/create-component.js' +import { noop } from '../../utilities/no-op.js' import { LoadingIndicator } from '../loading-indicator/loading-indicator.js' import styles from './button.module.css' -export type ButtonProps = { +export interface ButtonProps + extends FocusableComponentProps { children: ComponentChildren danger?: boolean disabled?: boolean fullWidth?: boolean loading?: boolean - onClick?: JSX.MouseEventHandler - propagateEscapeKeyDown?: boolean + onClick?: EventHandler.onClick secondary?: boolean } -export function Button({ - children, - danger = false, - disabled = false, - fullWidth = false, - loading = false, - onClick, - propagateEscapeKeyDown = true, - secondary = false, - ...rest -}: Props): JSX.Element { +export const Button = createComponent(function ( + { + blurOnEscapeKeyDown = true, + children, + danger = false, + disabled = false, + fullWidth = false, + loading = false, + onClick = noop, + onKeyDown = noop, + propagateEscapeKeyDown = true, + secondary = false, + ...rest + }, + ref +): JSX.Element { const handleKeyDown = useCallback( - function (event: JSX.TargetedKeyboardEvent): void { + function (event: Event.onKeyDown): void { + onKeyDown(event) if (event.key !== 'Escape') { return } if (propagateEscapeKeyDown === false) { event.stopPropagation() } - event.currentTarget.blur() + if (blurOnEscapeKeyDown === true) { + event.currentTarget.blur() + } }, - [propagateEscapeKeyDown] + [blurOnEscapeKeyDown, onKeyDown, propagateEscapeKeyDown] ) return ( @@ -59,15 +70,16 @@ export function Button({ ) : null} ) -} +}) diff --git a/packages/ui/src/components/button/stories/button-danger.stories.tsx b/packages/ui/src/components/button/stories/button-danger.stories.tsx index 569c925ec..6f081a85c 100644 --- a/packages/ui/src/components/button/stories/button-danger.stories.tsx +++ b/packages/ui/src/components/button/stories/button-danger.stories.tsx @@ -105,3 +105,11 @@ export const FullWidthLongText = function () { FullWidthLongText.parameters = { fixedWidth: true } + +export const BlurOnEscapeKeyDown = function () { + return ( + + ) +} diff --git a/packages/ui/src/components/button/stories/button-default.stories.tsx b/packages/ui/src/components/button/stories/button-default.stories.tsx index 6816a4074..66896cd33 100644 --- a/packages/ui/src/components/button/stories/button-default.stories.tsx +++ b/packages/ui/src/components/button/stories/button-default.stories.tsx @@ -101,3 +101,7 @@ export const FullWidthLongText = function () { FullWidthLongText.parameters = { fixedWidth: true } + +export const BlurOnEscapeKeyDown = function () { + return +} diff --git a/packages/ui/src/components/button/stories/button-secondary-danger.stories.tsx b/packages/ui/src/components/button/stories/button-secondary-danger.stories.tsx index 31b9f19c3..c155a18cb 100644 --- a/packages/ui/src/components/button/stories/button-secondary-danger.stories.tsx +++ b/packages/ui/src/components/button/stories/button-secondary-danger.stories.tsx @@ -111,3 +111,11 @@ export const FullWidthLongText = function () { FullWidthLongText.parameters = { fixedWidth: true } + +export const BlurOnEscapeKeyDown = function () { + return ( + + ) +} diff --git a/packages/ui/src/components/button/stories/button-secondary.stories.tsx b/packages/ui/src/components/button/stories/button-secondary.stories.tsx index 56d267192..bae98de6d 100644 --- a/packages/ui/src/components/button/stories/button-secondary.stories.tsx +++ b/packages/ui/src/components/button/stories/button-secondary.stories.tsx @@ -105,3 +105,11 @@ export const FullWidthLongText = function () { FullWidthLongText.parameters = { fixedWidth: true } + +export const BlurOnEscapeKeyDown = function () { + return ( + + ) +} diff --git a/packages/ui/src/components/checkbox/checkbox.tsx b/packages/ui/src/components/checkbox/checkbox.tsx index 4349cee5c..75381e60a 100644 --- a/packages/ui/src/components/checkbox/checkbox.tsx +++ b/packages/ui/src/components/checkbox/checkbox.tsx @@ -2,79 +2,90 @@ import { ComponentChildren, h, JSX } from 'preact' import { useCallback } from 'preact/hooks' import { IconControlCheckboxChecked12 } from '../../icons/icon-12/icon-control-checkbox-checked-12.js' -import { OnValueChange, Props } from '../../types/types.js' +import { Event, EventHandler } from '../../types/event-handler.js' +import { FocusableComponentProps } from '../../types/focusable-component-props.js' import { createClassName } from '../../utilities/create-class-name.js' +import { createComponent } from '../../utilities/create-component.js' +import { noop } from '../../utilities/no-op.js' import styles from './checkbox.module.css' -export type CheckboxProps = { +export interface CheckboxProps + extends FocusableComponentProps { children: ComponentChildren disabled?: boolean - name?: Name - onChange?: OmitThisParameter> - onValueChange?: OnValueChange - propagateEscapeKeyDown?: boolean + onChange?: EventHandler.onChange + onValueChange?: EventHandler.onValueChange value: boolean } -export function Checkbox({ - children, - disabled = false, - name, - onChange = function () {}, - onValueChange = function () {}, - propagateEscapeKeyDown = true, - value = false, - ...rest -}: Props>): JSX.Element { - const handleChange = useCallback( - function (event: JSX.TargetedEvent): void { - const newValue = event.currentTarget.checked - onValueChange(newValue, name) - onChange(event) +export const Checkbox = createComponent( + function ( + { + blurOnEscapeKeyDown = true, + children, + disabled = false, + onChange = noop, + onKeyDown = noop, + onValueChange = noop, + propagateEscapeKeyDown = true, + value, + ...rest }, - [name, onChange, onValueChange] - ) + ref + ): JSX.Element { + const handleChange = useCallback( + function (event: Event.onChange): void { + const newValue = event.currentTarget.checked + onValueChange(newValue) + onChange(event) + }, + [onChange, onValueChange] + ) - const handleKeyDown = useCallback( - function (event: JSX.TargetedKeyboardEvent): void { - if (event.key !== 'Escape') { - return - } - if (propagateEscapeKeyDown === false) { - event.stopPropagation() - } - event.currentTarget.blur() - }, - [propagateEscapeKeyDown] - ) + const handleKeyDown = useCallback( + function (event: Event.onKeyDown): void { + onKeyDown(event) + if (event.key !== 'Escape') { + return + } + if (propagateEscapeKeyDown === false) { + event.stopPropagation() + } + if (blurOnEscapeKeyDown === true) { + event.currentTarget.blur() + } + }, + [blurOnEscapeKeyDown, onKeyDown, propagateEscapeKeyDown] + ) - return ( -