sliderLineComponent.tsx 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import * as React from "react";
  2. import { Observable } from "babylonjs/Misc/observable";
  3. import { PropertyChangedEvent } from "../../propertyChangedEvent";
  4. interface ISliderLineComponentProps {
  5. label: string,
  6. target?: any,
  7. propertyName?: string,
  8. minimum: number,
  9. maximum: number,
  10. step: number,
  11. directValue?: number,
  12. onChange?: (value: number) => void,
  13. onInput?: (value: number) => void,
  14. onPropertyChangedObservable?: Observable<PropertyChangedEvent>,
  15. decimalCount?: number
  16. }
  17. export class SliderLineComponent extends React.Component<ISliderLineComponentProps, { value: number }> {
  18. private _localChange = false;
  19. constructor(props: ISliderLineComponentProps) {
  20. super(props);
  21. if (this.props.directValue !== undefined) {
  22. this.state = {
  23. value: this.props.directValue
  24. }
  25. } else {
  26. this.state = { value: this.props.target![this.props.propertyName!] };
  27. }
  28. }
  29. shouldComponentUpdate(nextProps: ISliderLineComponentProps, nextState: { value: number }) {
  30. if (nextProps.directValue !== undefined) {
  31. nextState.value = nextProps.directValue;
  32. return true;
  33. }
  34. const currentState = nextProps.target![nextProps.propertyName!];
  35. if (currentState !== nextState.value || this._localChange) {
  36. nextState.value = currentState;
  37. this._localChange = false;
  38. return true;
  39. }
  40. return false;
  41. }
  42. onChange(newValueString: any) {
  43. this._localChange = true;
  44. const newValue = parseFloat(newValueString);
  45. if (this.props.target) {
  46. if (this.props.onPropertyChangedObservable) {
  47. this.props.onPropertyChangedObservable.notifyObservers({
  48. object: this.props.target,
  49. property: this.props.propertyName!,
  50. value: newValue,
  51. initialValue: this.state.value
  52. });
  53. }
  54. this.props.target[this.props.propertyName!] = newValue;
  55. }
  56. if (this.props.onChange) {
  57. this.props.onChange(newValue);
  58. }
  59. this.setState({ value: newValue });
  60. }
  61. onInput(newValueString: any) {
  62. const newValue = parseFloat(newValueString);
  63. if (this.props.onInput) {
  64. this.props.onInput(newValue);
  65. }
  66. }
  67. render() {
  68. let decimalCount = this.props.decimalCount !== undefined ? this.props.decimalCount : 2;
  69. return (
  70. <div className="sliderLine">
  71. <div className="label">
  72. {this.props.label}
  73. </div>
  74. <div className="slider">
  75. {this.state.value ? this.state.value.toFixed(decimalCount) : "0"}&nbsp;<input className="range" type="range" step={this.props.step} min={this.props.minimum} max={this.props.maximum} value={this.state.value}
  76. onInput={evt => this.onInput((evt.target as HTMLInputElement).value)}
  77. onChange={evt => this.onChange(evt.target.value)} />
  78. </div>
  79. </div>
  80. );
  81. }
  82. }