-
Notifications
You must be signed in to change notification settings - Fork 350
refactor(text-input-with-copy-button): migrate TextInputWithCopyButto… #4779
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
Open
bonchevskyi
wants to merge
1
commit into
box:master
Choose a base branch
from
bonchevskyi:refactor/flow-to-ts-text-input-with-copy-button
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
File renamed without changes.
1 change: 0 additions & 1 deletion
1
...button/TextInputWithCopyButton.stories.js → ...utton/TextInputWithCopyButton.stories.tsx
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,4 +1,3 @@ | ||
| // @flow | ||
| import * as React from 'react'; | ||
|
|
||
| import TextInputWithCopyButton from './TextInputWithCopyButton'; | ||
|
|
||
244 changes: 244 additions & 0 deletions
244
src/components/text-input-with-copy-button/TextInputWithCopyButton.tsx
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 |
|---|---|---|
| @@ -0,0 +1,244 @@ | ||
| import * as React from 'react'; | ||
| import classNames from 'classnames'; | ||
| import omit from 'lodash/omit'; | ||
| import { FormattedMessage } from 'react-intl'; | ||
|
|
||
| import messages from '../../common/messages'; | ||
| import TextInput, { type TextInputProps } from '../text-input'; | ||
| import Button, { ButtonType } from '../button'; | ||
|
|
||
| import './TextInputWithCopyButton.scss'; | ||
|
|
||
| const DEFAULT_SUCCESS_STATE_DURATION = 3000; | ||
|
|
||
| const defaultCopyText = <FormattedMessage {...messages.copy} />; | ||
| const defaultCopiedText = <FormattedMessage {...messages.copied} />; | ||
|
|
||
| export interface TextInputWithCopyButtonProps | ||
| extends Omit<TextInputProps, 'className' | 'disabled' | 'label' | 'onFocus' | 'type' | 'value'> { | ||
| /** Array of nodes for additional buttons */ | ||
| additionalButtons?: React.ReactNode[]; | ||
| /** Set the focus to input when component loads */ | ||
| autofocus?: boolean; | ||
| /** Default copy button text */ | ||
| buttonDefaultText: React.ReactNode; | ||
| /** Props passed to the copy button */ | ||
| buttonProps?: Record<string, unknown>; | ||
| /** Copy button text when copy is successful */ | ||
| buttonSuccessText?: React.ReactNode; | ||
| /** Custom class for the component */ | ||
| className: string; | ||
| /** Disables the text input and copy button */ | ||
| disabled?: boolean; | ||
| /** Label displayed for the text input */ | ||
| // TODO: Make label required | ||
| label?: React.ReactNode; | ||
| /** Function called when link is copied by keyboard or button */ | ||
| onCopySuccess?: (event: React.SyntheticEvent) => void; | ||
| /** Focus handler for the input element */ | ||
| onFocus?: (event: React.SyntheticEvent) => void; | ||
| /** Duration (milliseconds) in which to show the copy success state */ | ||
| successStateDuration: number; | ||
| /** Triggers the copy animation when the component loads */ | ||
| triggerCopyOnLoad?: boolean; | ||
| /** HTML input type, defaults to "text" */ | ||
| type: string; | ||
| /** Value of the text input */ | ||
| value: React.ReactNode; | ||
| } | ||
|
|
||
| interface State { | ||
| /** Text currently displayed by the copy button */ | ||
| buttonText: React.ReactNode; | ||
| /** Whether the copy action succeeded */ | ||
| copySuccess: boolean; | ||
| /** Whether the input has already been focused */ | ||
| hasFocused: boolean; | ||
| } | ||
|
|
||
| class TextInputWithCopyButton extends React.PureComponent<TextInputWithCopyButtonProps, State> { | ||
| static defaultProps = { | ||
| buttonDefaultText: defaultCopyText, | ||
| buttonProps: {}, | ||
| buttonSuccessText: defaultCopiedText, | ||
| className: '', | ||
| hideOptionalLabel: true, | ||
| readOnly: true, | ||
| successStateDuration: DEFAULT_SUCCESS_STATE_DURATION, | ||
| type: 'text', | ||
| }; | ||
|
|
||
| constructor(props: TextInputWithCopyButtonProps) { | ||
| super(props); | ||
|
|
||
| this.isCopyCommandSupported = document.queryCommandSupported('copy'); | ||
|
|
||
| this.state = { | ||
| copySuccess: false, | ||
| buttonText: props.buttonDefaultText, | ||
| hasFocused: false, | ||
| }; | ||
| } | ||
|
|
||
| componentDidMount() { | ||
| const { autofocus, value } = this.props; | ||
|
|
||
| if (autofocus && value) { | ||
| this.performAutofocus(); | ||
| } | ||
| } | ||
|
|
||
| componentDidUpdate() { | ||
| const { autofocus, value, triggerCopyOnLoad } = this.props; | ||
| const { copySuccess, hasFocused } = this.state; | ||
|
|
||
| // if we've set focus before, and should auto focus on update, make sure to | ||
| // focus after component update | ||
| if (autofocus && value) { | ||
| this.performAutofocus(); | ||
| } | ||
|
|
||
| if (triggerCopyOnLoad && !copySuccess && !hasFocused) { | ||
| this.animateCopyButton(); | ||
| } | ||
| } | ||
|
|
||
| componentWillUnmount() { | ||
| this.clearCopySuccessTimeout(); | ||
| } | ||
|
|
||
| copyInputRef: HTMLInputElement | null = null; | ||
|
|
||
| copySuccessTimeout: number | null = null; | ||
|
|
||
| isCopyCommandSupported: boolean; | ||
|
|
||
| animateCopyButton() { | ||
| const { successStateDuration, buttonSuccessText } = this.props; | ||
| this.clearCopySuccessTimeout(); | ||
|
|
||
| this.setState( | ||
| { | ||
| copySuccess: true, | ||
| buttonText: buttonSuccessText, | ||
| hasFocused: true, | ||
| }, | ||
| () => { | ||
| this.copySuccessTimeout = window.setTimeout(() => { | ||
| this.restoreCopyButton(); | ||
| }, successStateDuration); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| clearCopySuccessTimeout() { | ||
| if (!this.copySuccessTimeout) { | ||
| return; | ||
| } | ||
|
|
||
| window.clearTimeout(this.copySuccessTimeout); | ||
| this.copySuccessTimeout = null; | ||
| } | ||
|
|
||
| copySelectedText = () => document.execCommand('copy'); | ||
|
|
||
| restoreCopyButton = () => { | ||
| this.setState({ | ||
| copySuccess: false, | ||
| buttonText: this.props.buttonDefaultText, | ||
| }); | ||
| }; | ||
|
|
||
| handleCopyButtonClick = () => { | ||
| this.performAutofocus(); | ||
| this.copySelectedText(); | ||
| this.animateCopyButton(); | ||
| }; | ||
|
|
||
| handleFocus = (event: React.SyntheticEvent) => { | ||
| if (this.copyInputRef) { | ||
| this.performAutofocus(); | ||
| } | ||
|
|
||
| if (this.props.onFocus) { | ||
| this.props.onFocus(event); | ||
| } | ||
| }; | ||
|
|
||
| handleCopyEvent = (event: React.SyntheticEvent) => { | ||
| const { disabled, onCopySuccess } = this.props; | ||
|
|
||
| if (disabled) { | ||
| event.preventDefault(); | ||
| } else { | ||
| this.animateCopyButton(); | ||
|
|
||
| if (onCopySuccess) { | ||
| onCopySuccess(event); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| performAutofocus = () => { | ||
| const { copyInputRef } = this; | ||
| if (copyInputRef) { | ||
| copyInputRef.select(); | ||
| copyInputRef.scrollLeft = 0; | ||
| } | ||
| }; | ||
|
|
||
| renderCopyButton = () => | ||
| this.isCopyCommandSupported ? ( | ||
| <Button | ||
| isDisabled={this.props.disabled} | ||
| onClick={this.handleCopyButtonClick} | ||
| type={ButtonType.BUTTON} | ||
| {...this.props.buttonProps} | ||
| > | ||
| {this.state.buttonText} | ||
| </Button> | ||
| ) : null; | ||
|
|
||
| render() { | ||
| const { additionalButtons, className, ...rest } = this.props; | ||
| const { copySuccess } = this.state; | ||
| const { isCopyCommandSupported } = this; | ||
|
|
||
| const inputProps = omit(rest, [ | ||
| 'autofocus', | ||
| 'buttonDefaultText', | ||
| 'buttonSuccessText', | ||
| 'buttonProps', | ||
| 'onCopySuccess', | ||
| 'successStateDuration', | ||
| 'triggerCopyOnLoad', | ||
| ]) as TextInputProps; | ||
|
|
||
| if (isCopyCommandSupported) { | ||
| inputProps.inputRef = ref => { | ||
| this.copyInputRef = ref; | ||
| }; | ||
| } | ||
|
|
||
| const wrapperClasses = classNames(className, { | ||
| 'copy-success': copySuccess, | ||
| 'text-input-with-copy-button-container': isCopyCommandSupported, | ||
| }); | ||
|
|
||
| const copyEvent = isCopyCommandSupported ? { onCopy: this.handleCopyEvent } : {}; | ||
|
|
||
| return ( | ||
| <div className={wrapperClasses} {...copyEvent}> | ||
| <TextInput | ||
| {...inputProps} | ||
| onFocus={this.handleFocus} | ||
| tooltipWrapperClassName="bdl-TextInputWithCopyButton-tooltipWrapper" | ||
| /> | ||
| {additionalButtons} | ||
| {this.renderCopyButton()} | ||
| </div> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export default TextInputWithCopyButton; | ||
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.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: box/box-ui-elements
Length of output: 40204
🏁 Script executed:
Repository: box/box-ui-elements
Length of output: 34061
🏁 Script executed:
Repository: box/box-ui-elements
Length of output: 18047
🏁 Script executed:
Repository: box/box-ui-elements
Length of output: 674
🏁 Script executed:
Repository: box/box-ui-elements
Length of output: 624
🏁 Script executed:
Repository: box/box-ui-elements
Length of output: 576
🏁 Script executed:
Repository: box/box-ui-elements
Length of output: 1557
🏁 Script executed:
Repository: box/box-ui-elements
Length of output: 993
🏁 Script executed:
Repository: box/box-ui-elements
Length of output: 790
Preserve typed button props and event handlers.
Record<string, unknown>rejects typed button-prop interfaces such asReact.ComponentProps<typeof Button>. Useobjector a compatible union.Use
React.FocusEventHandler<HTMLInputElement>foronFocusandReact.ClipboardEventHandler<HTMLDivElement>foronCopySuccess. UpdatehandleFocusandhandleCopyEventto use these event types before invoking the callbacks.🤖 Prompt for AI Agents