Skip to content

add support for setting JSX for title on Dialog props #6032

Closed

Description

@shiftkey

We want to avoid contributors using Dialog and nesting DialogHeader within if they only want the default behaviour. See #6027 (comment) for more discussion.

Activity

  1. added
    tech-debt
    help wanted
    on Oct 30, 2018
  2. shiftkey commented on Oct 30, 2018

    @shiftkey
    MemberAuthor

    I was going to propose using react-to-string here which seems to fit the bill, based on the example:

    import React, { Component } from 'react';
    import reactToString from 'react-to-string';
     
    class App extends Component {
      render() {
        return <div>Hello world!</div>
      }
    }
     
    reactToString(<App />);
    // Returns "Hello world!"
     
    reactToString(<p>Are you interested in this library? <a href="signup">Click here!</a></p>);
    // Returns "Are you interested in this library? Click here!"

    But the library does not have a license, so I've opened kevinzwhuang/react-to-string#3 to ask them to define one before I look at pulling this into our codebase.

  3. j-f1 commented on Oct 30, 2018

    @j-f1
    Contributor

    It looks like that doesn’t actually render the element so the code might not work as described.

  4. shiftkey commented on Oct 30, 2018

    @shiftkey
    MemberAuthor

    It looks like that doesn’t actually render the element so the code might not work as described.

    To be clear, I'm only looking for a string-like representation of the element for situations where we need something distinct to work with (the original issue was using the title to generate a unique id). I'm not looking to actually render the elements.

  5. shiftkey commented on Oct 30, 2018

    @shiftkey
    MemberAuthor

    This was the snippet I used in #6027 as a starting point:

    diff --git a/app/src/ui/dialog/dialog.tsx b/app/src/ui/dialog/dialog.tsx
    index 63162f312..9ff9cdee7 100644
    --- a/app/src/ui/dialog/dialog.tsx
    +++ b/app/src/ui/dialog/dialog.tsx
    @@ -31,7 +31,7 @@ interface IDialogProps {
        * By omitting this consumers may use their own custom DialogHeader
        * for when the default component doesn't cut it.
        */
    -  readonly title?: string
    +  readonly title?: string | JSX.Element
     
       /**
        * Whether or not the dialog should be dismissable. A dismissable dialog
    @@ -178,9 +178,11 @@ export class Dialog extends React.Component<IDialogProps, IDialogState> {
           this.setState({ titleId: undefined })
         }
     
    -    if (this.props.title) {
    +    if (this.props.title != null) {
    +      // TODO: how can we get a string-like representation of a JSX element?
    +      const id = typeof this.props.title === 'string' ? this.props.title : '???'
           this.setState({
    -        titleId: createUniqueId(`Dialog_${this.props.id}_${this.props.title}`),
    +        titleId: createUniqueId(`Dialog_${this.props.id}_${id}`),
           })
         }
       }
    diff --git a/app/src/ui/merge-branch/merge.tsx b/app/src/ui/merge-branch/merge.tsx
    index bd4239bc7..0d0ed27a3 100644
    --- a/app/src/ui/merge-branch/merge.tsx
    +++ b/app/src/ui/merge-branch/merge.tsx
    @@ -19,7 +19,6 @@ import { MergeResultKind } from '../../models/merge'
     import { MergeStatusHeader } from '../history/merge-status-header'
     import { promiseWithMinimumTimeout } from '../../lib/promise'
     import { truncateWithEllipsis } from '../../lib/truncate-with-ellipsis'
    -import { DialogHeader } from '../dialog/header'
     
     interface IMergeProps {
       readonly dispatcher: Dispatcher
    @@ -293,18 +292,15 @@ export class Merge extends React.Component<IMergeProps, IMergeState> {
         return (
           <Dialog
             id="merge"
    +        title={
    +          <div className="merge-dialog-header">
    +            Merge into <b>{currentBranchName}</b>
    +          </div>
    +        }
             onDismissed={this.props.onDismissed}
    +        dismissable={true}
             onSubmit={this.merge}
           >
    -        <DialogHeader
    -          title={
    -            <div className={'merge-dialog-header'}>
    -              Merge into <b>{currentBranchName}</b>
    -            </div>
    -          }
    -          dismissable={true}
    -          onDismissed={this.props.onDismissed}
    -        />
             <DialogContent>
               <BranchList
                 allBranches={this.props.allBranches}
  6. niik commented on Mar 8, 2019

    @niik
    Member

    Hey @shiftkey, sorry, I've completed missed this.

    // TODO: how can we get a string-like representation of a JSX element?
     const id = typeof this.props.title === 'string' ? this.props.title : '???'```

    You can ignore this entirely. CreateUniqueId will ensure it's a unique id regardless of what you pass it so just leave that as-is (ie. pass the raw JSX.Element to it, that's fine). You can use createUniqueId without giving it anything if you like and it'll take care of it, we only give it something that's slightly human readable for sanity's sake.

  7. shiftkey commented on Mar 8, 2019

    @shiftkey
    MemberAuthor

    @niik cool, that should simplify the other things in here

  8. self-assigned this
    on Mar 8, 2019
  9. shiftkey commented on Mar 8, 2019

    @shiftkey
    MemberAuthor

    The commit to add this support is 6cf4a2a and this should be resolved in #6961.

  10. removed
    help wanted
    on Mar 8, 2019
  11. shiftkey commented on Mar 15, 2019

    @shiftkey
    MemberAuthor

    Resolved in #6961, and aside from the usages reported in #7043 there are two others:

    <DialogHeader
    title={headerTitle}
    dismissable={true}
    onDismissed={this.onDismissed}
    />

    I'll move these out to help wanted tasks that can be picked up by others before closing this out.

  12. shiftkey commented on Mar 15, 2019

    @shiftkey
    MemberAuthor

    Closing this out as the remaining work to cleanup this anti-pattern has been written up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

tech-debt

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions