Skip to content
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

add placeholder to the div, update example with additional prop #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

costagolub
Copy link

No description provided.

@costagolub costagolub mentioned this pull request Sep 5, 2017
@amensouissi
Copy link

Please merge...

@gettinToasty
Copy link

please merge this :)

@atagulalan
Copy link

atagulalan commented Mar 17, 2018

Well, it could be done easily with some CSS - Without this merge.

Just add "placeholder" attribute like this:

<ContentEditable
   ...
   placeholder={"Hello, World!"}
/>

And add this to your CSS:

[contenteditable=true]:empty:before {
  content: attr(placeholder);
  display: block;
  color: #aaa;
}

@smoleniuch
Copy link

@atagulalan

I agree that it is easy to do it sounds like a basic feature to have in such component.I would vote in favor to have placeholder built in.

Cheers!

@lovasoa
Copy link
Owner

lovasoa commented Aug 10, 2018

I do not really like the way the placeholder is implemented in this PR. If the placeholder is Hello World, and someone explicitly types Hello World, then blurs the component, then focuses it again, the text they typed will disappear.

The solution with CSS is shorter, simpler, and does not have this problem.

@enkhchuluun
Copy link

please merge this! placeholder is very important

@lovasoa
Copy link
Owner

lovasoa commented May 21, 2019

@enkhchuluun Didn't you read the above ?

@RobertB4
Copy link

RobertB4 commented Jun 25, 2019

For Typescript users:

To get past the Typescipt compiler using the solution of @atagulalan , I had to write my own type definition file.
I also noticed that the current type definitions of this package are rather loose, so I adjusted them to be a bit more strict.

file: contentEditable.d.ts

declare module "react-contenteditable" {
  export default class ContentEditable<T> extends React.Component<{
    html: string
    // I am currently only using onChange. I didn't update the other event handler definitions,
    // but if you want strict typing, they all follow a similar pattern.
    onChange?: (event: React.ChangeEvent<ContentEditableElement>) => T
    onBlur?: Function
    onKeyUp?: Function
    onKeyDown?: Function
    disabled?: boolean
    tagName?: string
    className?: string
    // Was originally typed as Object.
    style?: React.CSSProperties
    innerRef?: React.RefObject<HTMLElement> | Function
    // Add this so the Typescript compiler will let you add a placeholder prop to
    // the ContentEditable component.
    placeholder?: string
  }> {}

  // Extending value to HTMLElement is enough for my use case.
  // I haven't had a look at the source code in detail, there may be other keys
  // that were appended to the underlying HTMLElement.
  interface ContentEditableElement extends HTMLElement {
    value: string
  }
}

@lovasoa Let me know if you want stricter types, I'd be happy to update them.

@lovasoa
Copy link
Owner

lovasoa commented Jun 25, 2019

@RobertB4 Thank you for your work. Yes, I would definitely merge a PR implementing stricter types !

@eyea
Copy link

eyea commented Apr 2, 2020

WOW NICE!

@ZhangPeng4242
Copy link

@atagulalan hii, thank you for your solution! However, I've encountered a problem that if I need to generate

tag or other tags under the contenteditable component( press enter generates p instead of div), the

tag should be inside the component in the first place, then it conflicts with the pseudo-element :empty in css. I am wondering if there's any solution to this. Thank you.

@syed3kt
Copy link

syed3kt commented Feb 24, 2022

Pls merge this PR. Am in need of this Placeholder

@rajasurya7
Copy link

please merge this! placeholder is very important

@n3oney
Copy link

n3oney commented Mar 13, 2022

@syed3kt @rajasurya7 just use this solution #64 (comment)

@richardaum
Copy link

The suggested solution makes the caret to disappear when the input is empty and focused.

@Pulseline-Tech
Copy link

For Typescript users:

To get past the Typescipt compiler using the solution of @atagulalan , I had to write my own type definition file. I also noticed that the current type definitions of this package are rather loose, so I adjusted them to be a bit more strict.

file: contentEditable.d.ts

declare module "react-contenteditable" {
  export default class ContentEditable<T> extends React.Component<{
    html: string
    // I am currently only using onChange. I didn't update the other event handler definitions,
    // but if you want strict typing, they all follow a similar pattern.
    onChange?: (event: React.ChangeEvent<ContentEditableElement>) => T
    onBlur?: Function
    onKeyUp?: Function
    onKeyDown?: Function
    disabled?: boolean
    tagName?: string
    className?: string
    // Was originally typed as Object.
    style?: React.CSSProperties
    innerRef?: React.RefObject<HTMLElement> | Function
    // Add this so the Typescript compiler will let you add a placeholder prop to
    // the ContentEditable component.
    placeholder?: string
  }> {}

  // Extending value to HTMLElement is enough for my use case.
  // I haven't had a look at the source code in detail, there may be other keys
  // that were appended to the underlying HTMLElement.
  interface ContentEditableElement extends HTMLElement {
    value: string
  }
}

@lovasoa Let me know if you want stricter types, I'd be happy to update them.

For anyone having trouble with typings or do not want to update them you can just use the aria-placeholder and change the attr(placeholder) to attr(aria-placeholder) in the CSS file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet