optionsLineComponent.tsx 3.8 KB

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