-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
[Table] Add menu to change cell content in Table playground #1229
Conversation
Delete 'Fill with random text' optionPreview: documentation | table |
packages/table/preview/index.tsx
Outdated
const ALPHANUMERIC_CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
|
||
interface ICellContentGeneratorDictionary { | ||
[key: string]: (rowIndex?: number, columnIndex?: number) => string; |
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.
key: CellContent
?
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.
Seems to require [key: number | string]
:/
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.
how about Record<CellContent, (signature) => string>
?
Record
is TS's shorthand for this, a la Pick
or Partial
.
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.
Doesn't work:
[ts] Type 'CellContent' does not satisfy the constraint 'string'.
enum CellContent
Using enum
s as keys is a huge discussion: microsoft/TypeScript#2491. I'll just get rid of the type safety; it's only an example page.
packages/table/preview/index.tsx
Outdated
private toCellContentLabel(cellContent: CellContent) { | ||
if (cellContent === "CellNames") { return "Cell names"; } | ||
if (cellContent === "Empty") { return "Empty"; } | ||
if (cellContent === "LongText") { return "Long text"; } |
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.
why not just use the english string as the enum constant?
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.
Given that I need to make way for an upcoming usage that will map TruncatedPopoverMode
values to string
labels, maybe I'll just change CellContent
to a number-based enum
too.
packages/table/preview/index.tsx
Outdated
if (isNextStateDefined && | ||
nextState.cellContent === this.state.cellContent | ||
&& nextState.numRows === this.state.numRows | ||
&& nextState.numCols === this.state.numCols |
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.
inconsistent &&
alignment here. put all at the beginning of lines.
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.
Oops
packages/table/preview/index.tsx
Outdated
|
||
const cellContent = isNextStateDefined ? nextState.cellContent : this.state.cellContent; | ||
const numRows = isNextStateDefined ? nextState.numRows : this.state.numRows; | ||
const numCols = isNextStateDefined ? nextState.numCols : this.state.numCols; |
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.
👎 either use this.state
as default value for argument, or deconstruct all at once like so:
const { ... } = isNextStateDefined ? nextState : this.state;
packages/table/preview/index.tsx
Outdated
@@ -468,6 +528,31 @@ class MutableTable extends React.Component<{}, IMutableTableState> { | |||
// State updates | |||
// ============= | |||
|
|||
// designed to be called from componentWillMount and componentWillUpdate, hence it expects nextProps | |||
private syncCellContent = (nextState?: IMutableTableState) => { |
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.
(nextState = this.state) => {
?
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 would cause the equality checks below to return
on componentWillMount
(because the default value would cause isNextStateDefined
to never be false
); I do want to go through all the content generation at that time, so I'll presumably need to know if the real nextState
is defined or not.
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.
you could pull that isNextStateDefined
check up a level to componentWillUpdate
itself, cuz it only applies to that one usage of this function.
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.
Nice, done
CR feedbackPreview: documentation | table |
Very exciting! |
EMPTY, | ||
CELL_NAMES, | ||
LONG_TEXT, | ||
}; |
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.
logically, i'd define this immediately before CELL_CONTENTS
instead of inserting this MutableTableState in the way.
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.
k
packages/table/preview/index.tsx
Outdated
CellContent.EMPTY, | ||
CellContent.CELL_NAMES, | ||
CellContent.LONG_TEXT, | ||
] as CellContent[]; |
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.
is this cast necessary now that it's an enum? if so, def preferable as type declaration instead of cast.
const CELL_CONTENTS: CellContent[] = ...
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.
done
packages/table/preview/index.tsx
Outdated
const ALPHANUMERIC_CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
|
||
interface ICellContentGeneratorDictionary { | ||
[key: string]: (rowIndex?: number, columnIndex?: number) => string; |
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.
how about Record<CellContent, (signature) => string>
?
Record
is TS's shorthand for this, a la Pick
or Partial
.
packages/table/preview/index.tsx
Outdated
if (cellContent === CellContent.CELL_NAMES) { return "Cell names"; } | ||
if (cellContent === CellContent.EMPTY) { return "Empty"; } | ||
if (cellContent === CellContent.LONG_TEXT) { return "Long text"; } | ||
return ""; |
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.
switch
? 🤷♂️
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.
yep, done
packages/table/preview/index.tsx
Outdated
@@ -468,6 +528,31 @@ class MutableTable extends React.Component<{}, IMutableTableState> { | |||
// State updates | |||
// ============= | |||
|
|||
// designed to be called from componentWillMount and componentWillUpdate, hence it expects nextProps | |||
private syncCellContent = (nextState?: IMutableTableState) => { |
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.
you could pull that isNextStateDefined
check up a level to componentWillUpdate
itself, cuz it only applies to that one usage of this function.
packages/table/preview/index.tsx
Outdated
return handleNumberChange((value: number) => { | ||
this.setState({ [stateKey]: value }); | ||
}); | ||
} | ||
|
||
private handleStringStateChange = (stateKey: keyof IMutableTableState) => { | ||
return handleStringChange((value: string) => { |
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.
def don't need the typing on value
param in all three cases here, handle*Change
does that for you.
then you could make these bodies into one-liners:
private handleNumberStateChange = (stateKey: keyof IMutableTableState) => {
return handleNumberChange((value) => this.setState({ [stateKey]: value }));
}
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.
Done
packages/table/preview/index.tsx
Outdated
private generateRandomAlphanumericString(minLength = 5, maxLength = 40) { | ||
const randomLength = Math.floor(minLength + (Math.random() * (maxLength - minLength))); | ||
return Utils.times(randomLength, () => { | ||
const randomIndex = Math.floor(Math.random() * maxLength); |
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.
this doesn't seem safe... did you mean ALPHANUMERIC_CHARS.length
?
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.
I did.
Respond to CR feedbackPreview: documentation | table |
packages/table/preview/index.tsx
Outdated
[CellContent.CELL_NAMES]: (row: number, col: number) => Utils.toBase26Alpha(col) + (row + 1), | ||
[CellContent.EMPTY]: () => "", | ||
[CellContent.LONG_TEXT]: () => this.generateRandomAlphanumericString(), | ||
}; |
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.
seems like a good candidate for const
above, no need for private member it seems. could inline generateRandomAlphanumericString
for the third one.
More CR feedbackPreview: documentation | table |
Changes proposed in this pull request:
<select>
menu to change cell content in the entire table in one fell swoop.Reviewers should focus on:
Table
, without requiring scrolling (this goes for anyTable
, not just our preview). This constraint is why you have to scroll before seeing the cell content change.Screenshot