-
Notifications
You must be signed in to change notification settings - Fork 149
chore: Buttonの内部処理を整理する #5347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
chore: Buttonの内部処理を整理する #5347
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
3bb40d3
chore: decoratorsのデフォルト文字列の持ち方を調整する
AtsushiM 5ef9eda
chore: DecoratorsTypeの設置場所を移動
AtsushiM 0d284e1
chore: useDecoratorsを定義
AtsushiM 139d213
Merge branch 'master' of https://github.com/kufu/smarthr-ui into chor…
AtsushiM 7f05277
chore: useDecoratorsにわたすgenericsを調整
AtsushiM 10baf9d
chore: libs/decorator を hooks/useDecorators に移動
AtsushiM 660292c
chore: fix stories
AtsushiM 74f1c62
Merge branch 'master' of https://github.com/kufu/smarthr-ui into chor…
AtsushiM a8510c1
chore: Button/DisabledDetailのstyle生成をmemo化
AtsushiM 31a93be
chore: Button/DisabledDetailのTooltipIconを切り出す
AtsushiM e1dbbb8
chore: ButtonWrapperのdisalbed時のonClick時の処理を定数化する
AtsushiM f41ab81
chore: ButtonWrapperのtvのslotsにdefaultではなくbuttonでstyleを定義する
AtsushiM d5128bc
chore: ButtonWrapperのstyleを整理する
AtsushiM 7033ea7
chore: ButtonWrapperとButtonInnerを統合する
AtsushiM a903172
chore: ButtonWrapperのstyle生成を整理
AtsushiM b6841c4
chore: Buttonのstyle生成をmemo化
AtsushiM 71a68ef
chore: Buttonの表示内容の条件判定を整理
AtsushiM 8c8187f
Merge branch 'chore-add-use-decorators' into chore-refactoring-Button
AtsushiM 125b965
chore: fix ci
AtsushiM 09d8706
chore: fix ci
AtsushiM 00ba3b5
chore: AncherButtonのrelはmemo化の効率がわるいため解除
AtsushiM bf8d76b
Merge branch 'master' of https://github.com/kufu/smarthr-ui into chor…
AtsushiM f9a9afd
chore: style -> className
AtsushiM 036ea63
Merge branch 'master' of https://github.com/kufu/smarthr-ui into chor…
AtsushiM 9f5ec1f
chore: classNameの生成を調整
AtsushiM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,20 @@ | ||
| 'use client' | ||
|
|
||
| import React, { ButtonHTMLAttributes, forwardRef, useMemo } from 'react' | ||
| import React, { ButtonHTMLAttributes, PropsWithChildren, forwardRef, useMemo } from 'react' | ||
| import { tv } from 'tailwind-variants' | ||
|
|
||
| import { type DecoratorsType } from '../../hooks/useDecorators' | ||
| import { type DecoratorsType, useDecorators } from '../../hooks/useDecorators' | ||
| import { usePortal } from '../../hooks/usePortal' | ||
| import { Loader } from '../Loader' | ||
| import { VisuallyHiddenText } from '../VisuallyHiddenText' | ||
|
|
||
| import { ButtonInner } from './ButtonInner' | ||
| import { ButtonWrapper } from './ButtonWrapper' | ||
| import { DisabledDetail } from './DisabledDetail' | ||
| import { BaseProps } from './types' | ||
|
|
||
| type ElementProps = Omit<ButtonHTMLAttributes<HTMLButtonElement>, keyof BaseProps> | ||
|
|
||
| const buttonStyle = tv({ | ||
| const classNameGenerator = tv({ | ||
| slots: { | ||
| wrapper: 'smarthr-ui-Button', | ||
| loader: [ | ||
|
|
@@ -39,10 +38,13 @@ const buttonStyle = tv({ | |
| }) | ||
|
|
||
| export type Props = { | ||
| decorators?: DecoratorsType<'loading'> | ||
| decorators?: DecoratorsType<DecoratorKeyTypes> | ||
| } | ||
|
|
||
| const LOADING_TEXT = '処理中' | ||
| const DECORATOR_DEFAULT_TEXTS = { | ||
| loading: '処理中', | ||
| } as const | ||
| type DecoratorKeyTypes = keyof typeof DECORATOR_DEFAULT_TEXTS | ||
|
|
||
| export const Button = forwardRef<HTMLButtonElement, BaseProps & ElementProps & Props>( | ||
| ( | ||
|
|
@@ -64,24 +66,34 @@ export const Button = forwardRef<HTMLButtonElement, BaseProps & ElementProps & P | |
| }, | ||
| ref, | ||
| ) => { | ||
| const { wrapper, loader: loaderSlot } = buttonStyle() | ||
| const wrapperStyle = useMemo(() => wrapper({ className }), [className, wrapper]) | ||
| const loaderStyle = useMemo( | ||
| () => loaderSlot({ isSecondary: variant === 'secondary' }), | ||
| [loaderSlot, variant], | ||
| ) | ||
| const { createPortal } = usePortal() | ||
| const classNames = useMemo(() => { | ||
| const { wrapper, loader } = classNameGenerator() | ||
|
|
||
| return { | ||
| wrapper: wrapper({ className }), | ||
| loader: loader({ isSecondary: variant === 'secondary' }), | ||
| } | ||
| }, [variant, className]) | ||
|
|
||
| let actualPrefix = prefix | ||
| let actualSuffix = suffix | ||
| let disabledOnLoading = disabled | ||
| let actualChildren = children | ||
|
|
||
| const loader = <Loader size="s" className={loaderStyle} role="presentation" /> | ||
| const actualPrefix = !loading && prefix | ||
| const actualSuffix = loading && !square ? loader : suffix | ||
| const disabledOnLoading = loading || disabled | ||
| const actualChildren = loading && square ? loader : children | ||
| if (loading) { | ||
| actualPrefix = undefined | ||
| disabledOnLoading = true | ||
|
|
||
| const loader = <Loader size="s" className={classNames.loader} role="presentation" /> | ||
|
|
||
| if (square) { | ||
| actualChildren = loader | ||
| } else { | ||
| actualSuffix = loader | ||
| } | ||
| } | ||
|
Comment on lines
+78
to
+94
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 元のロジックでは以下が問題だったので整理しました
以下の問題点を解決するため
|
||
|
|
||
| const statusText = useMemo(() => { | ||
| const loadingText = decorators?.loading?.(LOADING_TEXT) ?? LOADING_TEXT | ||
| return loading ? loadingText : '' | ||
| }, [decorators, loading]) | ||
| const decorated = useDecorators<DecoratorKeyTypes>(DECORATOR_DEFAULT_TEXTS, decorators) | ||
|
|
||
| const button = ( | ||
| <ButtonWrapper | ||
|
|
@@ -91,18 +103,15 @@ export const Button = forwardRef<HTMLButtonElement, BaseProps & ElementProps & P | |
| square={square} | ||
| wide={wide} | ||
| variant={variant} | ||
| className={wrapperStyle} | ||
| className={classNames.wrapper} | ||
| buttonRef={ref} | ||
| disabled={disabledOnLoading} | ||
| $loading={loading} | ||
| prefix={actualPrefix} | ||
| suffix={actualSuffix} | ||
| > | ||
| { | ||
| // `button` 要素内で live region を使うことはできないので、`role="status"` を持つ要素を外側に配置している。 https://github.com/kufu/smarthr-ui/pull/4558 | ||
| createPortal(<VisuallyHiddenText role="status">{statusText}</VisuallyHiddenText>) | ||
| } | ||
| <ButtonInner prefix={actualPrefix} suffix={actualSuffix} size={size}> | ||
| {actualChildren} | ||
| </ButtonInner> | ||
| <LoadingStatus loading={loading}>{decorated.loading}</LoadingStatus> | ||
| {actualChildren} | ||
| </ButtonWrapper> | ||
| ) | ||
|
|
||
|
|
@@ -115,3 +124,14 @@ export const Button = forwardRef<HTMLButtonElement, BaseProps & ElementProps & P | |
| ) | ||
| // BottomFixedArea での判定に用いるために displayName を明示的に設定する | ||
| Button.displayName = 'Button' | ||
|
|
||
| const LoadingStatus = React.memo<PropsWithChildren<{ loading: boolean }>>( | ||
| ({ loading, children }) => { | ||
| const { createPortal } = usePortal() | ||
|
|
||
| // `button` 要素内で live region を使うことはできないので、`role="status"` を持つ要素を外側に配置している。 https://github.com/kufu/smarthr-ui/pull/4558 | ||
| return createPortal( | ||
| <VisuallyHiddenText role="status">{loading && children}</VisuallyHiddenText>, | ||
| ) | ||
| }, | ||
| ) | ||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ButtonInnerは共通化するほどの内容がない(prefix, children, suffixの並び順とstyle)だけだったのでButtonWrapperに統合しました