common.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. e.stopPropagation();
  109. }
  110. static getDerivedStateFromProps(props: TextInputProps<any>, state: TextInputState) {
  111. const value = props.fromValue ? props.fromValue(props.value) : props.value;
  112. if (value === state.originalValue) return null;
  113. return { originalValue: value, value };
  114. }
  115. render() {
  116. return <input type='text'
  117. className={this.props.className}
  118. style={this.props.style}
  119. ref={this.input}
  120. onBlur={this.onBlur}
  121. value={this.state.value}
  122. placeholder={this.props.placeholder}
  123. onChange={this.onChange}
  124. onKeyPress={this.props.onEnter || this.props.blurOnEnter || this.props.blurOnEscape ? this.onKeyPress : void 0}
  125. onKeyDown={this.props.blurOnEscape ? this.onKeyUp : void 0}
  126. disabled={!!this.props.isDisabled}
  127. />;
  128. }
  129. }
  130. // TODO: replace this with parametrized TextInput
  131. export class NumericInput extends React.PureComponent<{
  132. value: number,
  133. onChange: (v: number) => void,
  134. onEnter?: () => void,
  135. onBlur?: () => void,
  136. blurOnEnter?: boolean,
  137. isDisabled?: boolean,
  138. placeholder?: string
  139. }, { value: string }> {
  140. state = { value: '0' };
  141. input = React.createRef<HTMLInputElement>();
  142. onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
  143. const value = +e.target.value;
  144. this.setState({ value: e.target.value }, () => {
  145. if (!Number.isNaN(value) && value !== this.props.value) {
  146. this.props.onChange(value);
  147. }
  148. });
  149. }
  150. onKeyPress = (e: React.KeyboardEvent<HTMLInputElement>) => {
  151. if ((e.keyCode === 13 || e.charCode === 13)) {
  152. if (this.props.blurOnEnter && this.input.current) {
  153. this.input.current.blur();
  154. }
  155. if (this.props.onEnter) this.props.onEnter();
  156. }
  157. e.stopPropagation();
  158. }
  159. onBlur = () => {
  160. this.setState({ value: '' + this.props.value });
  161. if (this.props.onBlur) this.props.onBlur();
  162. }
  163. static getDerivedStateFromProps(props: { value: number }, state: { value: string }) {
  164. const value = +state.value;
  165. if (Number.isNaN(value) || value === props.value) return null;
  166. return { value: '' + props.value };
  167. }
  168. render() {
  169. return <input type='text'
  170. ref={this.input}
  171. onBlur={this.onBlur}
  172. value={this.state.value}
  173. placeholder={this.props.placeholder}
  174. onChange={this.onChange}
  175. onKeyPress={this.props.onEnter || this.props.blurOnEnter ? this.onKeyPress : void 0}
  176. disabled={!!this.props.isDisabled}
  177. />
  178. }
  179. }
  180. export function Icon(props: {
  181. name: string
  182. }) {
  183. return <span className={`msp-icon msp-icon-${props.name}`} />;
  184. }
  185. export function IconButton(props: {
  186. icon: string,
  187. isSmall?: boolean,
  188. onClick: (e: React.MouseEvent<HTMLButtonElement>) => void,
  189. title?: string,
  190. toggleState?: boolean,
  191. disabled?: boolean,
  192. customClass?: string,
  193. 'data-id'?: string
  194. }) {
  195. let className = `msp-btn-link msp-btn-icon${props.isSmall ? '-small' : ''}${props.customClass ? ' ' + props.customClass : ''}`;
  196. if (typeof props.toggleState !== 'undefined') className += ` msp-btn-link-toggle-${props.toggleState ? 'on' : 'off'}`
  197. return <button className={className} onClick={props.onClick} title={props.title} disabled={props.disabled} data-id={props['data-id']}>
  198. <span className={`msp-icon msp-icon-${props.icon}`}/>
  199. </button>;
  200. }
  201. export class ExpandableGroup extends React.Component<{
  202. label: string,
  203. colorStripe?: Color,
  204. pivot: JSX.Element,
  205. controls: JSX.Element
  206. }, { isExpanded: boolean }> {
  207. state = { isExpanded: false };
  208. toggleExpanded = () => this.setState({ isExpanded: !this.state.isExpanded });
  209. render() {
  210. const { label, pivot, controls } = this.props;
  211. // TODO: fix the inline CSS
  212. return <>
  213. <div className='msp-control-row'>
  214. <span>
  215. {label}
  216. <button className='msp-btn-link msp-btn-icon msp-control-group-expander' onClick={this.toggleExpanded} title={`${this.state.isExpanded ? 'Less' : 'More'} options`}
  217. style={{ background: 'transparent', textAlign: 'left', padding: '0' }}>
  218. <span className={`msp-icon msp-icon-${this.state.isExpanded ? 'minus' : 'plus'}`} style={{ display: 'inline-block' }} />
  219. </button>
  220. </span>
  221. <div>{pivot}</div>
  222. {this.props.colorStripe && <div className='msp-expandable-group-color-stripe' style={{ backgroundColor: Color.toStyle(this.props.colorStripe) }} /> }
  223. </div>
  224. {this.state.isExpanded && <div className='msp-control-offset'>
  225. {controls}
  226. </div>}
  227. </>;
  228. }
  229. }
  230. export class ButtonSelect extends React.PureComponent<{ label: string, onChange: (value: string) => void, disabled?: boolean }> {
  231. onChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
  232. e.preventDefault()
  233. this.props.onChange(e.target.value)
  234. e.target.value = '_'
  235. }
  236. render() {
  237. return <select value='_' onChange={this.onChange} disabled={this.props.disabled}>
  238. <option key='_' value='_'>{this.props.label}</option>
  239. {this.props.children}
  240. </select>
  241. }
  242. }
  243. export function Options(options: [string, string][]) {
  244. return options.map(([value, label]) => <option key={value} value={value}>{label}</option>)
  245. }
  246. // export const ToggleButton = (props: {
  247. // onChange: (v: boolean) => void,
  248. // value: boolean,
  249. // label: string,
  250. // title?: string
  251. // }) => <div className='lm-control-row lm-toggle-button' title={props.title}>
  252. // <span>{props.label}</span>
  253. // <div>
  254. // <button onClick={e => { props.onChange.call(null, !props.value); (e.target as HTMLElement).blur(); }}>
  255. // <span className={ `lm-icon lm-icon-${props.value ? 'ok' : 'off'}` }></span> {props.value ? 'On' : 'Off'}
  256. // </button>
  257. // </div>
  258. // </div>