-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: Table inline editing polish #9002
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
snowystinger
wants to merge
3
commits into
main
Choose a base branch
from
feat-table-inline-edit-polish
base: main
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.
+232
−20
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -49,7 +49,7 @@ import {style} from '@react-spectrum/s2/style' with {type: 'macro'}; | |
|
||
## Content | ||
|
||
`TableView` follows the [Collection Components API](collections.html?component=Table), accepting both static and dynamic collections. | ||
`TableView` follows the [Collection Components API](collections.html?component=Table), accepting both static and dynamic collections. | ||
In this example, both the columns and the rows are provided to the table via a render function, enabling the user to hide and show columns and add additional rows. | ||
|
||
```tsx render type="s2" | ||
|
@@ -686,6 +686,176 @@ function subscribe(fn) { | |
} | ||
``` | ||
|
||
## Editable Table | ||
|
||
`EditableCell` represents an editable value in a single cell. It opens a popover when the end user clicks the user provided `ActionButton` that can contain any editable input or combination of inputs. | ||
|
||
The `EditableCell` relies on form validation to ensure the value is valid before closing the popover. | ||
|
||
An `ActionButton` with `slot="edit"` must be provided as a child of the `EditableCell` to open the popover. | ||
|
||
```tsx render type="s2" | ||
"use client"; | ||
import {TableView, TableHeader, Column, TableBody, Row, Cell, EditableCell, TextField, ActionButton, Picker, PickerItem, Text, type Key} from '@react-spectrum/s2'; | ||
import {style} from '@react-spectrum/s2/style' with {type: 'macro'}; | ||
import User from '@react-spectrum/s2/icons/User'; | ||
import Edit from '@react-spectrum/s2/icons/Edit'; | ||
import {useCallback,useRef, useState} from 'react'; | ||
|
||
///- begin collapse -/// | ||
let defaultItems = [ | ||
{id: 1, fruits: 'Apples', task: 'Collect', farmer: 'Eva'}, | ||
{id: 2, fruits: 'Oranges', task: 'Collect', farmer: 'Steven'}, | ||
{id: 3, fruits: 'Pears', task: 'Collect', farmer: 'Michael'}, | ||
{id: 4, fruits: 'Cherries', task: 'Collect', farmer: 'Sara'}, | ||
{id: 5, fruits: 'Dates', task: 'Collect', farmer: 'Karina'}, | ||
{id: 6, fruits: 'Bananas', task: 'Collect', farmer: 'Otto'}, | ||
{id: 7, fruits: 'Melons', task: 'Collect', farmer: 'Matt'}, | ||
{id: 8, fruits: 'Figs', task: 'Collect', farmer: 'Emily'}, | ||
{id: 9, fruits: 'Blueberries', task: 'Collect', farmer: 'Amelia'}, | ||
{id: 10, fruits: 'Blackberries', task: 'Collect', farmer: 'Isla'} | ||
]; | ||
///- end collapse -/// | ||
|
||
///- begin collapse -/// | ||
let editableColumns: Array<Omit<ColumnProps, 'children'> & {name: string}> = [ | ||
{name: 'Fruits', id: 'fruits', isRowHeader: true, width: '6fr', minWidth: 300}, | ||
{name: 'Task', id: 'task', width: '2fr', minWidth: 100}, | ||
{name: 'Farmer', id: 'farmer', width: '2fr', minWidth: 150} | ||
]; | ||
///- end collapse -/// | ||
|
||
export default function EditableTable(props) { | ||
let columns = editableColumns; | ||
let [editableItems, setEditableItems] = useState(defaultItems); | ||
let intermediateValue = useRef<any>(null); | ||
|
||
let onChange = useCallback((id: Key, columnId: Key) => { | ||
let value = intermediateValue.current; | ||
if (value === null) { | ||
return; | ||
} | ||
intermediateValue.current = null; | ||
setEditableItems(prev => { | ||
let newItems = prev.map(i => i.id === id && i[columnId] !== value ? {...i, [columnId]: value} : i); | ||
return newItems; | ||
}); | ||
}, []); | ||
|
||
let onIntermediateChange = useCallback((value: any) => { | ||
intermediateValue.current = value; | ||
}, []); | ||
|
||
return ( | ||
<TableView aria-label="Dynamic table" {...props} styles={style({height: 208})}> | ||
<TableHeader columns={columns}> | ||
{(column) => ( | ||
<Column {...column}>{column.name}</Column> | ||
)} | ||
</TableHeader> | ||
<TableBody items={editableItems}> | ||
{item => ( | ||
<Row id={item.id} columns={columns}> | ||
{(column) => { | ||
if (column.id === 'fruits') { | ||
///- begin highlight -/// | ||
return ( | ||
<EditableCell | ||
align={column.align} | ||
showDivider={column.showDivider} | ||
onSubmit={() => onChange(item.id, column.id!)} | ||
renderEditing={() => ( | ||
<TextField | ||
aria-label="Edit fruit" | ||
autoFocus | ||
validate={value => value.length > 0 ? null : 'Fruit name is required'} | ||
styles={style({flexGrow: 1, flexShrink: 1, minWidth: 0})} | ||
defaultValue={item[column.id!]} | ||
onChange={value => onIntermediateChange(value)} /> | ||
)}> | ||
<div className={style({display: 'flex', alignItems: 'center', gap: 8, justifyContent: 'space-between'})}> | ||
{item[column.id]} | ||
<ActionButton slot="edit" aria-label="Edit fruit"> | ||
<Edit /> | ||
</ActionButton></div> | ||
</EditableCell> | ||
); | ||
///- end highlight -/// | ||
} | ||
if (column.id === 'farmer') { | ||
///- begin highlight -/// | ||
return ( | ||
<EditableCell | ||
align={column.align} | ||
showDivider={column.showDivider} | ||
onSubmit={() => onChange(item.id, column.id!)} | ||
renderEditing={() => ( | ||
<Picker | ||
aria-label="Edit farmer" | ||
autoFocus | ||
styles={style({flexGrow: 1, flexShrink: 1, minWidth: 0})} | ||
defaultValue={item[column.id!]} | ||
onChange={value => onIntermediateChange(value)}> | ||
<PickerItem textValue="Eva" id="Eva"> | ||
<User /> | ||
<Text>Eva</Text> | ||
</PickerItem> | ||
<PickerItem textValue="Steven" id="Steven"> | ||
<User /> | ||
<Text>Steven</Text> | ||
</PickerItem> | ||
<PickerItem textValue="Michael" id="Michael"> | ||
<User /> | ||
<Text>Michael</Text> | ||
</PickerItem> | ||
<PickerItem textValue="Sara" id="Sara"> | ||
<User /> | ||
<Text>Sara</Text> | ||
</PickerItem> | ||
<PickerItem textValue="Karina" id="Karina"> | ||
<User /> | ||
<Text>Karina</Text> | ||
</PickerItem> | ||
<PickerItem textValue="Otto" id="Otto"> | ||
<User /> | ||
<Text>Otto</Text> | ||
</PickerItem> | ||
<PickerItem textValue="Matt" id="Matt"> | ||
<User /> | ||
<Text>Matt</Text> | ||
</PickerItem> | ||
<PickerItem textValue="Emily" id="Emily"> | ||
<User /> | ||
<Text>Emily</Text> | ||
</PickerItem> | ||
<PickerItem textValue="Amelia" id="Amelia"> | ||
<User /> | ||
<Text>Amelia</Text> | ||
</PickerItem> | ||
<PickerItem textValue="Isla" id="Isla"> | ||
<User /> | ||
<Text>Isla</Text> | ||
</PickerItem> | ||
</Picker> | ||
)}> | ||
<div className={style({display: 'flex', alignItems: 'center', gap: 8, justifyContent: 'space-between'})}> | ||
{item[column.id]} | ||
<ActionButton slot="edit" aria-label="Edit fruit"><Edit /></ActionButton> | ||
</div> | ||
</EditableCell> | ||
); | ||
///- end highlight -/// | ||
} | ||
return <Cell align={column.align} showDivider={column.showDivider}>{item[column.id!]}</Cell>; | ||
}} | ||
</Row> | ||
)} | ||
</TableBody> | ||
</TableView> | ||
); | ||
} | ||
``` | ||
|
||
## API | ||
|
||
```tsx links={{TableView: '#tableview', TableHeader: '#tableheader', Column: '#column', TableBody: '#tablebody', Row: '#row', Cell: '#cell'}} | ||
|
@@ -724,3 +894,7 @@ function subscribe(fn) { | |
### Cell | ||
|
||
<PropTable component={docs.exports.Cell} links={docs.links} showDescription /> | ||
|
||
### EditableCell | ||
|
||
<PropTable component={docs.exports.EditableCell} links={docs.links} showDescription /> | ||
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. how do i debug the PropTable.tsx component? I can't tell why placement is being added to an Overlay group in the table Also, noticed that I cannot see the bottom of the TOC unless I scroll to the bottom of the page |
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.
still need to solve this issue