-
Notifications
You must be signed in to change notification settings - Fork 102
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(ebay-table): selection mode #2286
Open
bill-min
wants to merge
24
commits into
14.4.0
Choose a base branch
from
table-select-1
base: 14.4.0
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.
+558
−334
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
8b57cd1
feat(ebay-table): selection mode
bill-min d7d5b28
docs: adding changeset
bill-min 28a4a15
test: add one more test
bill-min 88f3d8e
refactor: address comments
bill-min b4b64ad
refactor: add oninput
bill-min e88bddf
test: update unit test
bill-min 67bef2a
fix: wrong logic for defined selected
bill-min e5d40cd
fix: address comments
bill-min 9b7c238
chore: lint
bill-min 1961748
chore: lint
bill-min 9226a69
refactor: clear state
bill-min d858b22
refactor: clear code
bill-min b9efbc5
chore: remove unused imports
bill-min 05310e6
chore: add type reference
bill-min 0b93408
refactor: remove redundant file
bill-min eae72c2
refactor: refine code structure
bill-min d1b5571
feat: state management
bill-min b88d06b
refactor: refine table types
bill-min e7424f1
refactor: row iteration
bill-min 19ab661
fix: missing selection event property
bill-min d735d81
refactor: address comments
bill-min e21a81a
test: add unit test
bill-min 0213376
docs: update story
bill-min 9510737
docs: update marko tag json
bill-min 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@ebay/ebayui-core": minor | ||
--- | ||
|
||
feat(ebay-table): adding support for selection mode |
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 |
---|---|---|
@@ -1,7 +1,12 @@ | ||
import { AttrTriState } from "marko/tags-html"; | ||
import { WithNormalizedProps } from "../../global"; | ||
import { CheckboxEvent } from "../ebay-checkbox/component-browser"; | ||
|
||
// export type ColumnType = "normal" | "numeric" | "row-header" | "none"; | ||
export type TableDensity = "compact" | "relaxed" | "none"; | ||
type TableColRowName = string | number; | ||
export type TableSelectEvent = { | ||
selected: Record<TableColRowName, boolean>; | ||
allSelected?: AttrTriState; | ||
}; | ||
export interface TableHeader extends Omit<Marko.Input<"th">, `on${string}`> { | ||
columnType?: string; // Use ColumnType after marko fix the ts error for attr tags | ||
renderBody: Marko.Body; | ||
|
@@ -11,13 +16,94 @@ export interface TableCell | |
renderBody: Marko.Body; | ||
} | ||
export interface TableRow extends Omit<Marko.Input<"tr">, `on${string}`> { | ||
name?: TableColRowName; | ||
selected?: boolean; | ||
cell: Marko.RepeatableAttrTag<TableCell> | Marko.AttrTag<TableCell>[]; | ||
} | ||
export interface TableInput extends Omit<Marko.Input<"div">, `on${string}`> { | ||
header: Marko.RepeatableAttrTag<TableHeader> | Marko.AttrTag<TableHeader>[]; | ||
mode?: "none" | "selection"; | ||
allSelected?: AttrTriState; | ||
row?: Marko.RepeatableAttrTag<TableRow> | Marko.AttrTag<TableRow>[]; | ||
density?: TableDensity; | ||
density?: "compact" | "relaxed" | "none"; | ||
"a11y-select-all-text"?: string; | ||
"a11y-select-row-text"?: string; | ||
"on-select"?: (event: TableSelectEvent) => void; | ||
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. on-select should also include the new select data as an argument |
||
} | ||
export interface Input extends WithNormalizedProps<TableInput> {} | ||
|
||
export default class extends Marko.Component<Input> {} | ||
interface State { | ||
selected: Record<TableColRowName, boolean>; | ||
allSelected: AttrTriState; | ||
} | ||
|
||
export default class EbayTable extends Marko.Component<Input, State> { | ||
onCreate() { | ||
this.state = { | ||
selected: {}, | ||
allSelected: "false", | ||
}; | ||
} | ||
|
||
onInput(input: Input) { | ||
this.state.selected = this.getSelectedRowStateFromInput(input); | ||
this.state.allSelected = this.getAllSelectedState(input); | ||
} | ||
|
||
getSelectedRowStateFromInput(input: Input) { | ||
const selected: Record<TableColRowName, boolean> = {}; | ||
if (input.row) { | ||
for (const [i, row] of Object.entries([...input.row])) { | ||
const name = row.name || i; | ||
selected[name] = row.selected || false; | ||
} | ||
} | ||
return selected; | ||
} | ||
|
||
getAllSelectedState(input: Input): AttrTriState { | ||
if (input.allSelected) { | ||
return input.allSelected; | ||
} | ||
let selectedCount = 0; | ||
let rowCount = 0; | ||
for (const [_name, selected] of Object.entries(this.state.selected)) { | ||
if (selected) { | ||
selectedCount++; | ||
} | ||
rowCount++; | ||
} | ||
if (selectedCount === 0) { | ||
return "false"; | ||
} | ||
if (selectedCount === rowCount) { | ||
return "true"; | ||
} | ||
return "mixed"; | ||
} | ||
|
||
headerSelect() { | ||
const { allSelected } = this.state; | ||
this.state.selected = [...(this.input.row || [])].reduce( | ||
(acc, { name }, i) => { | ||
acc[name || i] = allSelected !== "true"; | ||
return acc; | ||
}, | ||
{} as Record<TableColRowName, boolean>, | ||
); | ||
this.state.allSelected = allSelected !== "true" ? "true" : "false"; | ||
this.emit("select", { | ||
selected: this.state.selected, | ||
allSelected: this.state.allSelected, | ||
}); | ||
} | ||
|
||
rowSelect(name: TableColRowName, { checked }: CheckboxEvent) { | ||
this.state.selected[name] = checked; | ||
this.setStateDirty("selected"); | ||
this.state.allSelected = this.getAllSelectedState(this.input); | ||
this.emit("select", { | ||
selected: this.state.selected, | ||
}); | ||
} | ||
} |
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,48 @@ | ||
import data from "./data.json"; | ||
|
||
<ebay-table mode="selection" on-select("emit", "select") ...input> | ||
<@header column-type="row-header"> | ||
Seller | ||
</@header> | ||
<@header>Item</@header> | ||
<@header>Status</@header> | ||
<@header column-type="numeric"> | ||
List Price | ||
</@header> | ||
<@header column-type="numeric"> | ||
Quantity Available | ||
</@header> | ||
<@header>Orders</@header> | ||
<@header column-type="numeric"> | ||
Watchers | ||
</@header> | ||
<@header column-type="numeric"> | ||
Protection | ||
</@header> | ||
<@header>Shipping</@header> | ||
<@header>Delivery</@header> | ||
<for|r, i| of=data> | ||
<!-- first row selected by default --> | ||
$ const selected = i === 0; | ||
<@row name=`row_${i}` selected=selected> | ||
<@cell>${r.seller}</@cell> | ||
<@cell>${r.item.title}</@cell> | ||
<@cell> | ||
<ebay-signal status=r.statusType as any> | ||
${r.status} | ||
</ebay-signal> | ||
</@cell> | ||
<@cell>${r.listPrice}</@cell> | ||
<@cell>${r.quantityAvailable}</@cell> | ||
<@cell> | ||
<a href="https://ebay.com"> | ||
${r.orders.number} | ||
</a> | ||
</@cell> | ||
<@cell>${r.watchers}</@cell> | ||
<@cell>${r.protection}</@cell> | ||
<@cell>${r.shipping}</@cell> | ||
<@cell>${r.delivery}</@cell> | ||
</@row> | ||
</for> | ||
</ebay-table> |
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
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
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.
Do we need this? Might as well just define this inline. Thats what we do in other places
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.
Do you mean
AttrTriState
or thisTableSelectEvent
? AttrTriState is the same as tri-state checkbox.For this TableSelectEvent, I can do it inline. But I find it useful when ppl actually using typescript and need to handle the event.
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.
Actually, for event, tri-state checkbox is using same? https://github.com/eBay/ebayui-core/blob/master/src/components/ebay-tri-state-checkbox/component.ts#L4