Skip to content

Commit

Permalink
feat: Init result on state while TagToolInstanceAdaptor mounted
Browse files Browse the repository at this point in the history
  • Loading branch information
Glenfiddish committed Jun 8, 2022
1 parent cadd8c7 commit 954ba29
Showing 1 changed file with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { IStepInfo } from '@/types/step';
import _ from 'lodash';
import type { ObjectString } from './types';
import { getKeyCodeNumber } from './utils';
import { IFileItem } from '@/types/data';

export interface IVideoTagInstanceAdaptorProps {
imgIndex: number;
Expand All @@ -28,15 +29,15 @@ export interface IVideoTagInstanceAdaptorProps {
stepList: IStepInfo[];
}

interface ITagInstanceAdaptorState {
interface IVideoTagInstanceAdaptorState {
tagResult: any[];
labelSelectedList: any;
valid: boolean;
}

export class TagToolInstanceAdaptor extends React.Component<
IVideoTagInstanceAdaptorProps,
ITagInstanceAdaptorState
IVideoTagInstanceAdaptorState
> {
public fns: { [key: string]: () => void } = {};

Expand Down Expand Up @@ -235,21 +236,32 @@ export class TagToolInstanceAdaptor extends React.Component<
public componentDidMount() {
document.addEventListener('keydown', this.keydown);
this.props.onMounted(this);
this.setResultFromImgList(this.props);
}

public componentWillUnmount() {
document.addEventListener('keydown', this.keydown);
this.props.onUnmounted();
}

public setResultFromImgList = (props: IVideoTagInstanceAdaptorProps) => {
const { imgList, imgIndex, step } = props;

if (!imgList[imgIndex]) {
return;
}

const res = jsonParser(imgList[imgIndex].result)[`step_${step}`];
this.setState({
tagResult: res?.result ?? [],
valid: res?.valid === undefined ? true : res.valid,
});
};

/** Observer imgIndex and set tagResult */
public shouldComponentUpdate({ imgIndex, imgList, step }: IVideoTagInstanceAdaptorProps) {
if (imgIndex !== this.props.imgIndex) {
const res = jsonParser(imgList[imgIndex].result)[`step_${step}`];
this.setState({
tagResult: res?.result ?? [],
valid: res.valid,
});
public shouldComponentUpdate(props: IVideoTagInstanceAdaptorProps) {
if (props.imgIndex !== this.props.imgIndex) {
this.setResultFromImgList(props);
}
return true;
}
Expand Down

0 comments on commit 954ba29

Please sign in to comment.