Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. import { PikFormControl } from 'common/components/PikFormControl';
  2. import { action, observable } from 'mobx';
  3. import { observer } from 'mobx-react';
  4. import React, { Props } from 'react';
  5. import { guid } from 'utils/guid';
  6. import { Input, Label, Wrapper } from './components';
  7. import { IPikCheckboxProps } from './types';
  8.  
  9. @observer
  10. export class PikCheckbox extends PikFormControl<boolean, IPikCheckboxProps> {
  11. @observable isChecked: boolean;
  12. @observable isCheckedValue: string = 'false';
  13. @observable prev: boolean;
  14. private textarea: any;
  15. guid: string;
  16.  
  17. constructor(props: IPikCheckboxProps) {
  18. super(props);
  19. // console.log('constructor');
  20. const { defaultValue } = props;
  21. if (defaultValue) {
  22. this.isChecked = defaultValue;
  23. this.prev = defaultValue;
  24. }
  25. if (defaultValue) {
  26. this.isCheckedValue = defaultValue.toString();
  27. this.isChecked = defaultValue;
  28. }
  29. this.guid = this.props.id || guid();
  30. }
  31.  
  32. componentDidUpdate(): void {
  33. console.log('----UPDATE START-------');
  34. console.log('prev', this.prev);
  35. console.log('now', this.props.checkboxValue);
  36. if (this.prev !== this.props.defaultValue) {
  37. this.isChecked = this.props.defaultValue;
  38. this.props.onChange(this.props.name, this.isChecked);
  39. console.log('G');
  40. }
  41. this.prev = this.props.defaultValue;
  42. console.log('----UPDATE END-------');
  43. }
  44.  
  45. @action.bound
  46. handleChange(e: React.ChangeEvent<HTMLInputElement>): void {
  47. // console.log('DSD');
  48. // this.isChecked = !this.isChecked;
  49. // const isChecked = e.currentTarget.checked;
  50. // this.isCheckedValue = isChecked.toString();
  51. this.isChecked = !this.isChecked;
  52. e.stopPropagation();
  53. if (this.props.nativeOnChange) {
  54. return this.props.nativeOnChange(e);
  55. }
  56. if (!this.props.onChange) {
  57. return;
  58. }
  59. this.props.onChange(this.props.name, this.isChecked);
  60. }
  61.  
  62. @action.bound
  63. handleClick(e: any): void {
  64. this.isChecked = !this.isChecked;
  65. // this.props.onChange(this.props.name, this.isChecked);
  66. }
  67.  
  68. render(): JSX.Element {
  69. const { children, defaultValue, id, checkboxValue, ...checkboxAttributes } = this.props;
  70. // console.log('q', this.isChecked);
  71. return (
  72. <Wrapper>
  73. <Input
  74. {...checkboxAttributes}
  75. // onClick={this.handleClick}
  76. onChange={this.handleChange}
  77. value={this.isCheckedValue}
  78. id={this.guid}
  79. checked={this.isChecked}
  80. />
  81. <Label htmlFor={this.guid}>{children}</Label>
  82. </Wrapper>
  83. );
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement