common.tsx 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import * as React from 'react';
  7. import { Color } from '../../../mol-util/color';
  8. import { PurePluginUIComponent } from '../base';
  9. export class ControlGroup extends React.Component<{ header: string, initialExpanded?: boolean }, { isExpanded: boolean }> {
  10. state = { isExpanded: !!this.props.initialExpanded }
  11. toggleExpanded = () => this.setState({ isExpanded: !this.state.isExpanded });
  12. render() {
  13. return <div className='msp-control-group-wrapper'>
  14. <div className='msp-control-group-header'>
  15. <button className='msp-btn msp-btn-block' onClick={this.toggleExpanded}>
  16. <span className={`msp-icon msp-icon-${this.state.isExpanded ? 'collapse' : 'expand'}`} />
  17. {this.props.header}
  18. </button>
  19. </div>
  20. {this.state.isExpanded && <div className='msp-control-offset' style={{ display: this.state.isExpanded ? 'block' : 'none' }}>
  21. {this.props.children}
  22. </div>
  23. }
  24. </div>
  25. }
  26. }
  27. export interface TextInputProps<T> {
  28. className?: string,
  29. style?: React.CSSProperties,
  30. value: T,
  31. fromValue?(v: T): string,
  32. toValue?(s: string): T,
  33. // TODO: add error/help messages here?
  34. isValid?(s: string): boolean,
  35. onChange(value: T): void,
  36. onEnter?(): void,
  37. onBlur?(): void,
  38. delayMs?: number,
  39. blurOnEnter?: boolean,
  40. blurOnEscape?: boolean,
  41. isDisabled?: boolean,
  42. placeholder?: string
  43. }
  44. interface TextInputState {
  45. originalValue: string,
  46. value: string
  47. }
  48. function _id(x: any) { return x; }
  49. export class TextInput<T = string> extends PurePluginUIComponent<TextInputProps<T>, TextInputState> {
  50. private input = React.createRef<HTMLInputElement>();
  51. private delayHandle: any = void 0;
  52. private pendingValue: T | undefined = void 0;
  53. state = { originalValue: '', value: '' }
  54. onBlur = () => {
  55. this.setState({ value: '' + this.state.originalValue });
  56. if (this.props.onBlur) this.props.onBlur();
  57. }
  58. get isPending() { return typeof this.delayHandle !== 'undefined'; }
  59. clearTimeout() {
  60. if (this.isPending) {
  61. clearTimeout(this.delayHandle);
  62. this.delayHandle = void 0;
  63. }
  64. }
  65. raiseOnChange = () => {
  66. this.props.onChange(this.pendingValue!);
  67. this.pendingValue = void 0;
  68. }
  69. triggerChanged(formatted: string, converted: T) {
  70. this.clearTimeout();
  71. if (formatted === this.state.originalValue) return;
  72. if (this.props.delayMs) {
  73. this.pendingValue = converted;
  74. this.delayHandle = setTimeout(this.raiseOnChange, this.props.delayMs);
  75. } else {
  76. this.props.onChange(converted);
  77. }
  78. }
  79. onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
  80. const value = e.target.value;
  81. if (this.props.isValid && !this.props.isValid(value)) {
  82. this.clearTimeout();
  83. this.setState({ value });
  84. return;
  85. }
  86. const converted = (this.props.toValue || _id)(value);
  87. const formatted = (this.props.fromValue || _id)(converted);
  88. this.setState({ value: formatted }, () => this.triggerChanged(formatted, converted));
  89. }
  90. onKeyUp = (e: React.KeyboardEvent<HTMLInputElement>) => {
  91. if (e.charCode === 27 || e.keyCode === 27 /* esc */) {
  92. if (this.props.blurOnEscape && this.input.current) {
  93. this.input.current.blur();
  94. }
  95. }
  96. }
  97. onKeyPress = (e: React.KeyboardEvent<HTMLInputElement>) => {
  98. if (e.keyCode === 13 || e.charCode === 13 /* enter */) {
  99. if (this.isPending) {
  100. this.clearTimeout();
  101. this.raiseOnChange();
  102. }
  103. if (this.props.blurOnEnter && this.input.current) {
  104. this.input.current.blur();
  105. }
  106. if (this.props.onEnter) this.props.onEnter();
  107. }
  108. }
  109. static getDerivedStateFromProps(props: TextInputProps<any>, state: TextInputState) {
  110. const value = props.fromValue ? props.fromValue(props.value) : props.value;
  111. if (value === state.originalValue) return null;
  112. return { originalValue: value, value };
  113. }
  114. render() {
  115. return <input type='text'
  116. className={this.props.className}
  117. style={this.props.style}
  118. ref={this.input}
  119. onBlur={this.onBlur}
  120. value={this.state.value}
  121. placeholder={this.props.placeholder}
  122. onChange={this.onChange}
  123. onKeyPress={this.props.onEnter || this.props.blurOnEnter || this.props.blurOnEscape ? this.onKeyPress : void 0}
  124. onKeyDown={this.props.blurOnEscape ? this.onKeyUp : void 0}
  125. disabled={!!this.props.isDisabled}
  126. />;
  127. }
  128. }
  129. // TODO: replace this with parametrized TextInput
  130. export class NumericInput extends React.PureComponent<{
  131. value: number,
  132. onChange: (v: number) => void,
  133. onEnter?: () => void,
  134. onBlur?: () => void,
  135. blurOnEnter?: boolean,
  136. isDisabled?: boolean,
  137. placeholder?: string
  138. }, { value: string }> {
  139. state = { value: '0' };
  140. input = React.createRef<HTMLInputElement>();
  141. onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
  142. const value = +e.target.value;
  143. this.setState({ value: e.target.value }, () => {
  144. if (!Number.isNaN(value) && value !== this.props.value) {
  145. this.props.onChange(value);
  146. }
  147. });
  148. }
  149. onKeyPress = (e: React.KeyboardEvent<HTMLInputElement>) => {
  150. if ((e.keyCode === 13 || e.charCode === 13)) {
  151. if (this.props.blurOnEnter && this.input.current) {
  152. this.input.current.blur();
  153. }
  154. if (this.props.onEnter) this.props.onEnter();
  155. }
  156. }
  157. onBlur = () => {
  158. this.setState({ value: '' + this.props.value });
  159. if (this.props.onBlur) this.props.onBlur();
  160. }
  161. static getDerivedStateFromProps(props: { value: number }, state: { value: string }) {
  162. const value = +state.value;
  163. if (Number.isNaN(value) || value === props.value) return null;
  164. return { value: '' + props.value };
  165. }
  166. render() {
  167. return <input type='text'
  168. ref={this.input}
  169. onBlur={this.onBlur}
  170. value={this.state.value}
  171. placeholder={this.props.placeholder}
  172. onChange={this.onChange}
  173. onKeyPress={this.props.onEnter || this.props.blurOnEnter ? this.onKeyPress : void 0}
  174. disabled={!!this.props.isDisabled}
  175. />
  176. }
  177. }
  178. export function Icon(props: {
  179. name: string
  180. }) {
  181. return <span className={`msp-icon msp-icon-${props.name}`} />;
  182. }
  183. export function IconButton(props: {
  184. icon: string,
  185. isSmall?: boolean,
  186. onClick: (e: React.MouseEvent<HTMLButtonElement>) => void,
  187. title?: string,
  188. toggleState?: boolean,
  189. disabled?: boolean,
  190. customClass?: string,
  191. 'data-id'?: string
  192. }) {
  193. let className = `msp-btn-link msp-btn-icon${props.isSmall ? '-small' : ''}${props.customClass ? ' ' + props.customClass : ''}`;
  194. if (typeof props.toggleState !== 'undefined') className += ` msp-btn-link-toggle-${props.toggleState ? 'on' : 'off'}`
  195. return <button className={className} onClick={props.onClick} title={props.title} disabled={props.disabled} data-id={props['data-id']}>
  196. <span className={`msp-icon msp-icon-${props.icon}`}/>
  197. </button>;
  198. }
  199. export class ExpandableGroup extends React.Component<{
  200. label: string,
  201. colorStripe?: Color,
  202. pivot: JSX.Element,
  203. controls: JSX.Element
  204. }, { isExpanded: boolean }> {
  205. state = { isExpanded: false };
  206. toggleExpanded = () => this.setState({ isExpanded: !this.state.isExpanded });
  207. render() {
  208. const { label, pivot, controls } = this.props;
  209. // TODO: fix the inline CSS
  210. return <>
  211. <div className='msp-control-row'>
  212. <span>
  213. {label}
  214. <button className='msp-btn-link msp-btn-icon msp-control-group-expander' onClick={this.toggleExpanded} title={`${this.state.isExpanded ? 'Less' : 'More'} options`}
  215. style={{ background: 'transparent', textAlign: 'left', padding: '0' }}>
  216. <span className={`msp-icon msp-icon-${this.state.isExpanded ? 'minus' : 'plus'}`} style={{ display: 'inline-block' }} />
  217. </button>
  218. </span>
  219. <div>{pivot}</div>
  220. {this.props.colorStripe && <div className='msp-expandable-group-color-stripe' style={{ backgroundColor: Color.toStyle(this.props.colorStripe) }} /> }
  221. </div>
  222. {this.state.isExpanded && <div className='msp-control-offset'>
  223. {controls}
  224. </div>}
  225. </>;
  226. }
  227. }
  228. export class ButtonSelect extends React.PureComponent<{ label: string, onChange: (value: string) => void }> {
  229. onChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
  230. e.preventDefault()
  231. this.props.onChange(e.target.value)
  232. e.target.value = '_'
  233. }
  234. render() {
  235. return <select value='_' onChange={this.onChange}>
  236. <option key='_' value='_'>{this.props.label}</option>
  237. {this.props.children}
  238. </select>
  239. }
  240. }
  241. export function Options(options: [string, string][]) {
  242. return options.map(([value, label]) => <option key={value} value={value}>{label}</option>)
  243. }
  244. // export const ToggleButton = (props: {
  245. // onChange: (v: boolean) => void,
  246. // value: boolean,
  247. // label: string,
  248. // title?: string
  249. // }) => <div className='lm-control-row lm-toggle-button' title={props.title}>
  250. // <span>{props.label}</span>
  251. // <div>
  252. // <button onClick={e => { props.onChange.call(null, !props.value); (e.target as HTMLElement).blur(); }}>
  253. // <span className={ `lm-icon lm-icon-${props.value ? 'ok' : 'off'}` }></span> {props.value ? 'On' : 'Off'}
  254. // </button>
  255. // </div>
  256. // </div>