optionsLineComponent.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import * as React from "react";
  2. import { Observable } from "babylonjs/Misc/observable";
  3. import { PropertyChangedEvent } from "../../propertyChangedEvent";
  4. export const Null_Value = Number.MAX_SAFE_INTEGER;
  5. class ListLineOption {
  6. public label: string;
  7. public value: number;
  8. }
  9. interface IOptionsLineComponentProps {
  10. label: string;
  11. target: any;
  12. propertyName: string;
  13. options: ListLineOption[];
  14. noDirectUpdate?: boolean;
  15. onSelect?: (value: number) => void;
  16. extractValue?: () => number;
  17. onPropertyChangedObservable?: Observable<PropertyChangedEvent>;
  18. allowNullValue?: boolean;
  19. }
  20. export class OptionsLineComponent extends React.Component<IOptionsLineComponentProps, { value: number }> {
  21. private _localChange = false;
  22. private remapValueIn(value: number | null): number {
  23. return this.props.allowNullValue && value === null ? Null_Value : value!;
  24. }
  25. private remapValueOut(value: number): number | null {
  26. return this.props.allowNullValue && value === Null_Value ? null : value;
  27. }
  28. constructor(props: IOptionsLineComponentProps) {
  29. super(props);
  30. this.state = { value: this.remapValueIn(this.props.extractValue ? this.props.extractValue() : props.target[props.propertyName]) };
  31. }
  32. shouldComponentUpdate(nextProps: IOptionsLineComponentProps, nextState: { value: number }) {
  33. if (this._localChange) {
  34. this._localChange = false;
  35. return true;
  36. }
  37. let newValue = this.remapValueIn(nextProps.extractValue ? nextProps.extractValue() : nextProps.target[nextProps.propertyName]);
  38. if (newValue != null && newValue !== nextState.value) {
  39. nextState.value = newValue;
  40. return true;
  41. }
  42. return false;
  43. }
  44. raiseOnPropertyChanged(newValue: number, previousValue: number) {
  45. if (!this.props.onPropertyChangedObservable) {
  46. return;
  47. }
  48. this.props.onPropertyChangedObservable.notifyObservers({
  49. object: this.props.target,
  50. property: this.props.propertyName,
  51. value: newValue,
  52. initialValue: previousValue,
  53. allowNullValue: this.props.allowNullValue,
  54. });
  55. }
  56. updateValue(valueString: string) {
  57. const value = parseInt(valueString);
  58. this._localChange = true;
  59. const store = this.props.extractValue ? this.props.extractValue() : this.props.target[this.props.propertyName];
  60. if (!this.props.noDirectUpdate) {
  61. this.props.target[this.props.propertyName] = this.remapValueOut(value);
  62. }
  63. this.setState({ value: value });
  64. if (this.props.onSelect) {
  65. this.props.onSelect(value);
  66. }
  67. const newValue = this.props.extractValue ? this.props.extractValue() : this.props.target[this.props.propertyName];
  68. this.raiseOnPropertyChanged(newValue, store);
  69. }
  70. render() {
  71. return (
  72. <div className="listLine">
  73. <div className="label">
  74. {this.props.label}
  75. </div>
  76. <div className="options">
  77. <select onChange={(evt) => this.updateValue(evt.target.value)} value={this.state.value ?? ""}>
  78. {
  79. this.props.options.map((option, i) => {
  80. return (
  81. <option key={option.label + i} value={option.value}>{option.label}</option>
  82. );
  83. })
  84. }
  85. </select>
  86. </div>
  87. </div>
  88. );
  89. }
  90. }