Skip to content
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

fix(plugins/plugin-client-common): when switching tabs, no prompt is … #8209

Merged
merged 1 commit into from
Oct 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions plugins/plugin-client-common/src/components/Client/TabContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Props = TabContentOptions &
}

type State = Partial<WithTab> & {
active: boolean
sessionInit: SessionInitStatus
sessionInitError?: Error
showSessionInitDone: boolean
Expand Down Expand Up @@ -99,6 +100,7 @@ export default class TabContent extends React.PureComponent<Props, State> {
super(props)

this.state = {
active: props.active,
tab: React.createRef(),
sessionInit: 'NotYet',
showSessionInitDone: true,
Expand All @@ -109,17 +111,35 @@ export default class TabContent extends React.PureComponent<Props, State> {
}
}

public static getDerivedStateFromProps(props: Props, state: State) {
const isNowActive = props.active
const isCurrentlyActive = state.active
state.active = props.active

if (isNowActive && !isCurrentlyActive) {
// see https://github.com/kubernetes-sigs/kui/issues/8208
TabContent.delayedFocus(state, 0)
}

return state
}

public static delayedFocus(state: State, delayMillis = 300) {
if (state.active) {
setTimeout(() => {
if (state.active && state._terminal.current) {
state._terminal.current.doFocusIfNeeded()
}
}, delayMillis)
}
}

public componentDidMount() {
this.oneTimeInit()

const onTabNew = () => {
this.setState({ sessionInit: 'Done' })

setTimeout(() => {
if (this.state._terminal.current) {
this.state._terminal.current.doFocusIfNeeded()
}
}, 300)
TabContent.delayedFocus(this.state)

try {
if (this.props.onTabReady) {
Expand Down Expand Up @@ -321,7 +341,7 @@ export default class TabContent extends React.PureComponent<Props, State> {
return React.cloneElement(node, {
key,
uuid: this.props.uuid,
active: this.props.active
active: this.state.active
})
} else {
return node
Expand Down Expand Up @@ -354,13 +374,13 @@ export default class TabContent extends React.PureComponent<Props, State> {
private tabClassName() {
return (
'kui--tab-content' +
(this.props.active ? ' visible' : '') +
(this.state.active ? ' visible' : '') +
(!this.state.tabClassList ? '' : ' ' + Object.keys(this.state.tabClassList).join(' '))
)
}

private body() {
if (!this.props.active && !this._firstRenderDone) {
if (!this.state.active && !this._firstRenderDone) {
return <React.Fragment />
}

Expand All @@ -385,7 +405,7 @@ export default class TabContent extends React.PureComponent<Props, State> {
}

public render() {
this.activateHandlers.forEach(handler => handler(this.props.active))
this.activateHandlers.forEach(handler => handler(this.state.active))

return (
<MutabilityContext.Provider value={this.state.mutability}>
Expand Down