common.tsx 14 KB

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