-
Notifications
You must be signed in to change notification settings - Fork 13.9k
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
feat(explore): Move chart header to top of the page #19529
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
73b16c7
Move chart header to top of the page
kgabryje 1f830c1
Implement truncating and dynamic input
kgabryje c38e0a8
fix typing
kgabryje 17098fa
Prevent cmd+z undoing changes when not in edit mode
kgabryje 31e3a28
Fix tests, add missing types
kgabryje 0559f67
Show changed title in altered
kgabryje 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 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 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
68 changes: 68 additions & 0 deletions
68
.../src/explore/components/ExploreChartHeader/ChartEditableTitle/ChartEditableTitle.test.tsx
This file contains 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,68 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import React from 'react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { render, screen } from 'spec/helpers/testing-library'; | ||
import { ChartEditableTitle } from './index'; | ||
|
||
const createProps = (overrides: Record<string, any> = {}) => ({ | ||
title: 'Chart title', | ||
placeholder: 'Add the name of the chart', | ||
canEdit: true, | ||
onSave: jest.fn(), | ||
...overrides, | ||
}); | ||
|
||
describe('Chart editable title', () => { | ||
it('renders chart title', () => { | ||
const props = createProps(); | ||
render(<ChartEditableTitle {...props} />); | ||
expect(screen.getByText('Chart title')).toBeVisible(); | ||
}); | ||
|
||
it('renders placeholder', () => { | ||
const props = createProps({ | ||
title: '', | ||
}); | ||
render(<ChartEditableTitle {...props} />); | ||
expect(screen.getByText('Add the name of the chart')).toBeVisible(); | ||
}); | ||
|
||
it('click, edit and save title', () => { | ||
const props = createProps(); | ||
render(<ChartEditableTitle {...props} />); | ||
const textboxElement = screen.getByRole('textbox'); | ||
userEvent.click(textboxElement); | ||
userEvent.type(textboxElement, ' edited'); | ||
expect(screen.getByText('Chart title edited')).toBeVisible(); | ||
userEvent.type(textboxElement, '{enter}'); | ||
expect(props.onSave).toHaveBeenCalled(); | ||
}); | ||
|
||
it('renders in non-editable mode', () => { | ||
const props = createProps({ canEdit: false }); | ||
render(<ChartEditableTitle {...props} />); | ||
const titleElement = screen.getByLabelText('Chart title'); | ||
expect(screen.queryByRole('textbox')).not.toBeInTheDocument(); | ||
expect(titleElement).toBeVisible(); | ||
userEvent.click(titleElement); | ||
userEvent.type(titleElement, ' edited{enter}'); | ||
expect(props.onSave).not.toHaveBeenCalled(); | ||
}); | ||
}); |
199 changes: 199 additions & 0 deletions
199
superset-frontend/src/explore/components/ExploreChartHeader/ChartEditableTitle/index.tsx
This file contains 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,199 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import React, { useEffect, useLayoutEffect, useRef, useState } from 'react'; | ||
import { css, styled, t } from '@superset-ui/core'; | ||
import { Tooltip } from 'src/components/Tooltip'; | ||
import { useResizeDetector } from 'react-resize-detector'; | ||
|
||
export type ChartEditableTitleProps = { | ||
title: string; | ||
placeholder: string; | ||
onSave: (title: string) => void; | ||
canEdit: boolean; | ||
}; | ||
|
||
const Styles = styled.div` | ||
${({ theme }) => css` | ||
display: flex; | ||
font-size: ${theme.typography.sizes.xl}px; | ||
font-weight: ${theme.typography.weights.bold}; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
white-space: nowrap; | ||
|
||
& .chart-title, | ||
& .chart-title-input { | ||
display: inline-block; | ||
max-width: 100%; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
white-space: nowrap; | ||
} | ||
|
||
& .chart-title { | ||
cursor: default; | ||
} | ||
& .chart-title-input { | ||
border: none; | ||
padding: 0; | ||
outline: none; | ||
|
||
&::placeholder { | ||
color: ${theme.colors.grayscale.light1}; | ||
} | ||
} | ||
|
||
& .input-sizer { | ||
position: absolute; | ||
left: -9999px; | ||
display: inline-block; | ||
} | ||
`} | ||
`; | ||
|
||
export const ChartEditableTitle = ({ | ||
title, | ||
placeholder, | ||
onSave, | ||
canEdit, | ||
}: ChartEditableTitleProps) => { | ||
const [isEditing, setIsEditing] = useState(false); | ||
const [currentTitle, setCurrentTitle] = useState(title || ''); | ||
const contentRef = useRef<HTMLInputElement>(null); | ||
const [showTooltip, setShowTooltip] = useState(false); | ||
|
||
const { width: inputWidth, ref: sizerRef } = useResizeDetector(); | ||
const { width: containerWidth, ref: containerRef } = useResizeDetector({ | ||
refreshMode: 'debounce', | ||
}); | ||
|
||
useEffect(() => { | ||
if (isEditing && contentRef?.current) { | ||
contentRef.current.focus(); | ||
// move cursor and scroll to the end | ||
if (contentRef.current.setSelectionRange) { | ||
const { length } = contentRef.current.value; | ||
contentRef.current.setSelectionRange(length, length); | ||
contentRef.current.scrollLeft = contentRef.current.scrollWidth; | ||
} | ||
} | ||
}, [isEditing]); | ||
|
||
// a trick to make the input grow when user types text | ||
// we make additional span component, place it somewhere out of view and copy input | ||
// then we can measure the width of that span to resize the input element | ||
useLayoutEffect(() => { | ||
if (sizerRef.current) { | ||
sizerRef.current.innerHTML = (currentTitle || placeholder).replace( | ||
/\s/g, | ||
' ', | ||
); | ||
} | ||
}, [currentTitle, placeholder, sizerRef]); | ||
|
||
useEffect(() => { | ||
if ( | ||
contentRef.current && | ||
contentRef.current.scrollWidth > contentRef.current.clientWidth | ||
) { | ||
setShowTooltip(true); | ||
} else { | ||
setShowTooltip(false); | ||
} | ||
}, [inputWidth, containerWidth]); | ||
|
||
const handleClick = () => { | ||
if (!canEdit || isEditing) { | ||
return; | ||
} | ||
setIsEditing(true); | ||
}; | ||
|
||
const handleBlur = () => { | ||
if (!canEdit) { | ||
return; | ||
} | ||
const formattedTitle = currentTitle.trim(); | ||
setCurrentTitle(formattedTitle); | ||
if (title !== formattedTitle) { | ||
onSave(formattedTitle); | ||
} | ||
setIsEditing(false); | ||
}; | ||
|
||
const handleChange = (ev: any) => { | ||
if (!canEdit || !isEditing) { | ||
return; | ||
} | ||
setCurrentTitle(ev.target.value); | ||
}; | ||
|
||
const handleKeyPress = (ev: any) => { | ||
if (!canEdit) { | ||
return; | ||
} | ||
if (ev.key === 'Enter') { | ||
ev.preventDefault(); | ||
contentRef.current?.blur(); | ||
} | ||
}; | ||
|
||
return ( | ||
<Styles ref={containerRef}> | ||
<Tooltip | ||
id="title-tooltip" | ||
title={showTooltip && currentTitle && !isEditing ? currentTitle : null} | ||
> | ||
{canEdit ? ( | ||
<input | ||
data-test="editable-title-input" | ||
className="chart-title-input" | ||
aria-label={t('Chart title')} | ||
ref={contentRef} | ||
onChange={handleChange} | ||
onBlur={handleBlur} | ||
onClick={handleClick} | ||
onKeyPress={handleKeyPress} | ||
placeholder={placeholder} | ||
value={currentTitle} | ||
css={css` | ||
cursor: ${isEditing ? 'text' : 'pointer'}; | ||
|
||
${inputWidth && | ||
inputWidth > 0 && | ||
css` | ||
width: ${inputWidth}px; | ||
`} | ||
`} | ||
/> | ||
) : ( | ||
<span | ||
className="chart-title" | ||
aria-label={t('Chart title')} | ||
ref={contentRef} | ||
> | ||
{currentTitle} | ||
</span> | ||
)} | ||
</Tooltip> | ||
<span ref={sizerRef} className="input-sizer" aria-hidden tabIndex={-1} /> | ||
</Styles> | ||
); | ||
}; |
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.
That span is invisible, we need it to measure text width in order to scale input's width dynamically