While your initial inclination might be to respond just as if they had shared that they had a physical ailment, keep in mind that a lot depends on 1) the employee's rationale for divulging such sensitive information and 2) the nature and quality of your relationship with the coworker.
If you are close coworkers and know a lot about one another's lives and families, then it may seem a natural extension of an existing, trusting relationship to reveal that detail. Hold it in the same trusted confidence that you would any other medical information. Don't repeat it and don't get nosey about it. They may be sharing the information more out of friendship than anything else. Perhaps they're not wanting you to specifically SAY anything but rather listen and empathize. Maybe they're trying to provide an explanation for a behavior that you find confusing. Listen.
There are others in the workforce, however, who tend to share their personal information inappropriately. In these situations, there's no obvious point in why these employees are divulging this information. For example, they may overshare by telling people they barely know WAY too much information about themselves. You can usually spot these folks by their verbal diarrhea. (Resist the temptation to return the "favor" of sharing your own deeply personal information.)
If coworkers' personal and medical information is not something you wish to know (unless it impacts the work environment and specifically your job), then you can say something benign to shut down additional sharing. For example, "That doesn't change how I see you as a coworker." Then change the topic or remove yourself from the scene.
There is another circumstance, however, in which an employee may self-identify as mentally ill because they are requesting an accommodation for a disability. Keep in mind that an employee is NOT required to use certain words when requesting an accommodation.
If you serve in a leadership capacity and the coworker confides his/her mental illness, it's important to clarify whether they are requesting an accommodation for a disability (and if so, what your company's procedures are for addressing this request). Ask whether they are making an accommodation request or simply sharing information about themselves. You can do this in a caring and sincere way. Don't pry into the medical information but instead simply inquire what accommodation, if any, they are requesting. Then immediately contact HR for assistance, if it's an accommodation request. (Again, this is for people in leadership roles.) Examples of accommodation requests include: wanting to work a modified schedule because of a recently diagnosed mood disorder, requesting an emotional support animal to accompany one to work because of anxiety and panic attacks, or relocating one's cubicle to an area with less traffic and noise.
We spend about a third of our lives at work, so it's natural to learn sensitive information about one another. While sometimes it enriches work relationships, at other times it can be extremely distracting. Ultimately, the reason we're all there is to work.