common.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /**
  2. * Copyright (c) 2018-2020 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 { Icon } from './icons';
  9. import { ArrowRight, ArrowDropDown, Remove, Add } from '@material-ui/icons';
  10. export class ControlGroup extends React.Component<{
  11. header: string,
  12. initialExpanded?: boolean,
  13. hideExpander?: boolean,
  14. hideOffset?: boolean,
  15. topRightIcon?: React.FC,
  16. headerLeftMargin?: string,
  17. onHeaderClick?: () => void,
  18. noTopMargin?: boolean,
  19. childrenClassName?: string
  20. }, { isExpanded: boolean }> {
  21. state = { isExpanded: !!this.props.initialExpanded }
  22. headerClicked = () => {
  23. if (this.props.onHeaderClick) {
  24. this.props.onHeaderClick();
  25. } else {
  26. this.setState({ isExpanded: !this.state.isExpanded });
  27. }
  28. }
  29. render() {
  30. let groupClassName = this.props.hideOffset ? 'msp-control-group-children' : 'msp-control-group-children msp-control-offset';
  31. if (this.props.childrenClassName) groupClassName += ' ' + this.props.childrenClassName;
  32. // TODO: customize header style (bg color, togle button etc)
  33. return <div className='msp-control-group-wrapper' style={{ position: 'relative', marginTop: this.props.noTopMargin ? 0 : void 0 }}>
  34. <div className='msp-control-group-header' style={{ marginLeft: this.props.headerLeftMargin }}>
  35. <Button onClick={this.headerClicked}>
  36. {!this.props.hideExpander && <Icon svg={this.state.isExpanded ? ArrowRight : ArrowDropDown} />}
  37. {this.props.topRightIcon && <Icon svg={this.props.topRightIcon} style={{ position: 'absolute', right: '2px', top: 0 }} />}
  38. <b>{this.props.header}</b>
  39. </Button>
  40. </div>
  41. {this.state.isExpanded && <div className={groupClassName} style={{ display: this.state.isExpanded ? 'block' : 'none' }}>
  42. {this.props.children}
  43. </div>}
  44. </div>;
  45. }
  46. }
  47. export interface TextInputProps<T> {
  48. className?: string,
  49. style?: React.CSSProperties,
  50. value: T,
  51. fromValue?(v: T): string,
  52. toValue?(s: string): T,
  53. // TODO: add error/help messages here?
  54. isValid?(s: string): boolean,
  55. onChange(value: T): void,
  56. onEnter?(): void,
  57. onBlur?(): void,
  58. delayMs?: number,
  59. blurOnEnter?: boolean,
  60. blurOnEscape?: boolean,
  61. isDisabled?: boolean,
  62. placeholder?: string,
  63. numeric?: boolean
  64. }
  65. interface TextInputState {
  66. originalValue: string,
  67. value: string
  68. }
  69. function _id(x: any) { return x; }
  70. export class TextInput<T = string> extends React.PureComponent<TextInputProps<T>, TextInputState> {
  71. private input = React.createRef<HTMLInputElement>();
  72. private delayHandle: any = void 0;
  73. private pendingValue: T | undefined = void 0;
  74. state = { originalValue: '', value: '' }
  75. onBlur = () => {
  76. this.setState({ value: '' + this.state.originalValue });
  77. if (this.props.onBlur) this.props.onBlur();
  78. }
  79. get isPending() { return typeof this.delayHandle !== 'undefined'; }
  80. clearTimeout() {
  81. if (this.isPending) {
  82. clearTimeout(this.delayHandle);
  83. this.delayHandle = void 0;
  84. }
  85. }
  86. raiseOnChange = () => {
  87. if (this.pendingValue === void 0) return;
  88. this.props.onChange(this.pendingValue!);
  89. this.pendingValue = void 0;
  90. }
  91. triggerChanged(formatted: string, converted: T) {
  92. this.clearTimeout();
  93. if (formatted === this.state.originalValue) return;
  94. if (this.props.delayMs) {
  95. this.pendingValue = converted;
  96. this.delayHandle = setTimeout(this.raiseOnChange, this.props.delayMs);
  97. } else {
  98. this.props.onChange(converted);
  99. }
  100. }
  101. onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
  102. const value = e.target.value;
  103. let isInvalid = (this.props.isValid && !this.props.isValid(value)) || (this.props.numeric && Number.isNaN(+value));
  104. if (isInvalid) {
  105. this.clearTimeout();
  106. this.setState({ value });
  107. return;
  108. }
  109. if (this.props.numeric) {
  110. this.setState({ value }, () => this.triggerChanged(value, +value as any));
  111. } else {
  112. const converted = (this.props.toValue || _id)(value);
  113. const formatted = (this.props.fromValue || _id)(converted);
  114. this.setState({ value: formatted }, () => this.triggerChanged(formatted, converted));
  115. }
  116. }
  117. onKeyUp = (e: React.KeyboardEvent<HTMLInputElement>) => {
  118. if (e.charCode === 27 || e.keyCode === 27 /* esc */) {
  119. if (this.props.blurOnEscape && this.input.current) {
  120. this.input.current.blur();
  121. }
  122. }
  123. }
  124. onKeyPress = (e: React.KeyboardEvent<HTMLInputElement>) => {
  125. if (e.keyCode === 13 || e.charCode === 13 /* enter */) {
  126. if (this.isPending) {
  127. this.clearTimeout();
  128. this.raiseOnChange();
  129. }
  130. if (this.props.blurOnEnter && this.input.current) {
  131. this.input.current.blur();
  132. }
  133. if (this.props.onEnter) this.props.onEnter();
  134. }
  135. e.stopPropagation();
  136. }
  137. static getDerivedStateFromProps(props: TextInputProps<any>, state: TextInputState) {
  138. const value = props.fromValue ? props.fromValue(props.value) : props.value;
  139. if (value === state.originalValue) return null;
  140. return { originalValue: value, value };
  141. }
  142. render() {
  143. return <input type='text'
  144. className={this.props.className}
  145. style={this.props.style}
  146. ref={this.input}
  147. onBlur={this.onBlur}
  148. value={this.state.value}
  149. placeholder={this.props.placeholder}
  150. onChange={this.onChange}
  151. onKeyPress={this.props.onEnter || this.props.blurOnEnter || this.props.blurOnEscape ? this.onKeyPress : void 0}
  152. onKeyDown={this.props.blurOnEscape ? this.onKeyUp : void 0}
  153. disabled={!!this.props.isDisabled}
  154. />;
  155. }
  156. }
  157. export class ExpandableControlRow extends React.Component<{
  158. label: string,
  159. colorStripe?: Color,
  160. pivot: JSX.Element,
  161. controls: JSX.Element
  162. }, { isExpanded: boolean }> {
  163. state = { isExpanded: false };
  164. toggleExpanded = () => this.setState({ isExpanded: !this.state.isExpanded });
  165. render() {
  166. const { label, pivot, controls } = this.props;
  167. // TODO: fix the inline CSS
  168. return <>
  169. <ControlRow label={<>
  170. {label}
  171. <button className='msp-btn-link msp-btn-icon msp-control-group-expander' onClick={this.toggleExpanded} title={`${this.state.isExpanded ? 'Less' : 'More'} options`}
  172. style={{ background: 'transparent', textAlign: 'left', padding: '0' }}>
  173. <Icon svg={this.state.isExpanded ? Remove : Add} style={{ display: 'inline-block' }} />
  174. </button>
  175. </>} control={pivot}>
  176. {this.props.colorStripe && <div className='msp-expandable-group-color-stripe' style={{ backgroundColor: Color.toStyle(this.props.colorStripe) }} />}
  177. </ControlRow>
  178. {this.state.isExpanded && <div className='msp-control-offset'>
  179. {controls}
  180. </div>}
  181. </>;
  182. }
  183. }
  184. export function SectionHeader(props: { icon?: React.FC, title: string | JSX.Element, desc?: string }) {
  185. return <div className='msp-section-header'>
  186. {props.icon && <Icon svg={props.icon} />}
  187. {props.title} <small>{props.desc}</small>
  188. </div>;
  189. }
  190. export type ButtonProps = {
  191. style?: React.CSSProperties,
  192. className?: string,
  193. disabled?: boolean,
  194. title?: string,
  195. icon?: React.FC,
  196. children?: React.ReactNode,
  197. onClick?: (e: React.MouseEvent<HTMLButtonElement>) => void,
  198. onContextMenu?: (e: React.MouseEvent<HTMLButtonElement>) => void,
  199. onMouseEnter?: (e: React.MouseEvent<HTMLButtonElement>) => void,
  200. onMouseLeave?: (e: React.MouseEvent<HTMLButtonElement>) => void,
  201. inline?: boolean,
  202. 'data-id'?: string,
  203. 'data-color'?: Color,
  204. flex?: boolean | string | number,
  205. noOverflow?: boolean
  206. }
  207. export function Button(props: ButtonProps) {
  208. let className = 'msp-btn';
  209. if (!props.inline) className += ' msp-btn-block';
  210. if (props.noOverflow) className += ' msp-no-overflow';
  211. if (props.flex) className += ' msp-flex-item';
  212. if (!props.children) className += ' msp-btn-childless';
  213. if (props.className) className += ' ' + props.className;
  214. let style: React.CSSProperties | undefined = void 0;
  215. if (props.flex) {
  216. if (typeof props.flex === 'number') style = { flex: `0 0 ${props.flex}px`, padding: 0, maxWidth: `${props.flex}px` };
  217. else if (typeof props.flex === 'string') style = { flex: `0 0 ${props.flex}`, padding: 0, maxWidth: props.flex };
  218. }
  219. if (props.style) {
  220. if (style) Object.assign(style, props.style);
  221. else style = props.style;
  222. }
  223. return <button onClick={props.onClick} title={props.title} disabled={props.disabled} style={style} className={className} data-id={props['data-id']} data-color={props['data-color']}
  224. onContextMenu={props.onContextMenu} onMouseEnter={props.onMouseEnter} onMouseLeave={props.onMouseLeave}>
  225. {props.icon && <Icon svg={props.icon} />}
  226. {props.children}
  227. </button>;
  228. }
  229. export function IconButton(props: {
  230. svg?: React.FC,
  231. small?: boolean,
  232. onClick: (e: React.MouseEvent<HTMLButtonElement>) => void,
  233. title?: string,
  234. toggleState?: boolean,
  235. disabled?: boolean,
  236. className?: string,
  237. style?: React.CSSProperties,
  238. 'data-id'?: string,
  239. extraContent?: JSX.Element,
  240. flex?: boolean | string | number,
  241. transparent?: boolean
  242. }) {
  243. let className = `msp-btn msp-btn-icon${props.small ? '-small' : ''}${props.className ? ' ' + props.className : ''}`;
  244. if (typeof props.toggleState !== 'undefined') {
  245. className += ` msp-btn-link-toggle-${props.toggleState ? 'on' : 'off'}`;
  246. }
  247. if (props.transparent) {
  248. className += ' msp-transparent-bg';
  249. }
  250. let style: React.CSSProperties | undefined = void 0;
  251. if (props.flex) {
  252. if (typeof props.flex === 'boolean') style = { flex: '0 0 32px', padding: 0 };
  253. else if (typeof props.flex === 'number') style = { flex: `0 0 ${props.flex}px`, padding: 0, maxWidth: `${props.flex}px` };
  254. else style = { flex: `0 0 ${props.flex}`, padding: 0, maxWidth: props.flex };
  255. }
  256. if (props.style) {
  257. if (style) Object.assign(style, props.style);
  258. else style = props.style;
  259. }
  260. return <button className={className} onClick={props.onClick} title={props.title} disabled={props.disabled} data-id={props['data-id']} style={style}>
  261. {props.svg && <Icon svg={props.svg} />}
  262. {props.extraContent}
  263. </button>;
  264. }
  265. export type ToggleButtonProps = {
  266. style?: React.CSSProperties,
  267. className?: string,
  268. disabled?: boolean,
  269. label?: string | JSX.Element,
  270. title?: string,
  271. icon?: React.FC,
  272. isSelected?: boolean,
  273. toggle: () => void
  274. }
  275. export class ToggleButton extends React.PureComponent<ToggleButtonProps> {
  276. onClick = (e: React.MouseEvent<HTMLButtonElement>) => {
  277. e.currentTarget.blur();
  278. this.props.toggle();
  279. }
  280. render() {
  281. const props = this.props;
  282. const label = props.label;
  283. const className = props.isSelected ? `${props.className || ''} msp-control-current` : props.className;
  284. return <Button icon={this.props.icon} onClick={this.onClick} title={this.props.title}
  285. disabled={props.disabled} style={props.style} className={className}>
  286. {label && this.props.isSelected ? <b>{label}</b> : label}
  287. </Button>;
  288. }
  289. }
  290. export class ExpandGroup extends React.PureComponent<{ header: string, headerStyle?: React.CSSProperties, initiallyExpanded?: boolean, accent?: boolean, noOffset?: boolean, marginTop?: 0 | string, headerLeftMargin?: string }, { isExpanded: boolean }> {
  291. state = { isExpanded: !!this.props.initiallyExpanded };
  292. toggleExpanded = () => this.setState({ isExpanded: !this.state.isExpanded });
  293. render() {
  294. return <>
  295. <div className='msp-control-group-header' style={{ marginTop: this.props.marginTop !== void 0 ? this.props.marginTop : '1px', marginLeft: this.props.headerLeftMargin }}>
  296. <button className='msp-btn msp-form-control msp-btn-block' onClick={this.toggleExpanded} style={this.props.headerStyle}>
  297. <Icon svg={this.state.isExpanded ? ArrowDropDown : ArrowRight} />
  298. {this.props.header}
  299. </button>
  300. </div>
  301. {this.state.isExpanded &&
  302. (this.props.noOffset
  303. ? this.props.children
  304. : <div className={this.props.accent ? 'msp-accent-offset' : 'msp-control-offset'}>
  305. {this.props.children}
  306. </div>)}
  307. </>;
  308. }
  309. }
  310. export type ControlRowProps = {
  311. title?: string,
  312. label?: React.ReactNode,
  313. control?: React.ReactNode,
  314. className?: string,
  315. children?: React.ReactNode
  316. }
  317. export function ControlRow(props: ControlRowProps) {
  318. let className = 'msp-control-row';
  319. if (props.className) className += ' ' + props.className;
  320. return <div className={className}>
  321. <span className='msp-control-row-label' title={props.title}>{props.label}</span>
  322. <div className='msp-control-row-ctrl'>{props.control}</div>
  323. {props.children}
  324. </div>;
  325. }