🐛 Fixing select still open on page change
This commit is contained in:
parent
4173c94874
commit
3efd0aa4df
@ -7,6 +7,7 @@ import React, { FormEvent, ReactNode } from "react";
|
|||||||
|
|
||||||
import Typography, { ITypo, ITypoColor } from "../../Typography";
|
import Typography, { ITypo, ITypoColor } from "../../Typography";
|
||||||
import classes from "./classes.module.scss";
|
import classes from "./classes.module.scss";
|
||||||
|
import { NextRouter, useRouter } from "next/router";
|
||||||
|
|
||||||
type IProps = {
|
type IProps = {
|
||||||
selectedOption?: IOption;
|
selectedOption?: IOption;
|
||||||
@ -16,7 +17,7 @@ type IProps = {
|
|||||||
placeholder?: string;
|
placeholder?: string;
|
||||||
className?: string;
|
className?: string;
|
||||||
name: string;
|
name: string;
|
||||||
disabled: boolean;
|
disabled?: boolean;
|
||||||
errors?: ValidationError;
|
errors?: ValidationError;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -35,7 +36,11 @@ type IState = {
|
|||||||
errors: ValidationError | null;
|
errors: ValidationError | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default class SelectField extends React.Component<IProps, IState> {
|
type IPropsClass = IProps & {
|
||||||
|
router: NextRouter;
|
||||||
|
};
|
||||||
|
|
||||||
|
class SelectFieldClass extends React.Component<IPropsClass, IState> {
|
||||||
private contentRef = React.createRef<HTMLUListElement>();
|
private contentRef = React.createRef<HTMLUListElement>();
|
||||||
private rootRef = React.createRef<HTMLDivElement>();
|
private rootRef = React.createRef<HTMLDivElement>();
|
||||||
private removeOnresize = () => {};
|
private removeOnresize = () => {};
|
||||||
@ -44,7 +49,7 @@ export default class SelectField extends React.Component<IProps, IState> {
|
|||||||
disabled: false,
|
disabled: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
constructor(props: IProps) {
|
constructor(props: IPropsClass) {
|
||||||
super(props);
|
super(props);
|
||||||
this.state = {
|
this.state = {
|
||||||
isOpen: false,
|
isOpen: false,
|
||||||
@ -64,7 +69,7 @@ export default class SelectField extends React.Component<IProps, IState> {
|
|||||||
<div
|
<div
|
||||||
className={classNames(classes["root"], this.props.className)}
|
className={classNames(classes["root"], this.props.className)}
|
||||||
ref={this.rootRef}
|
ref={this.rootRef}
|
||||||
data-disabled={this.props.disabled.toString()}
|
data-disabled={this.props.disabled?.toString()}
|
||||||
data-errored={(this.state.errors !== null).toString()}>
|
data-errored={(this.state.errors !== null).toString()}>
|
||||||
{selectedOption && <input type="text" defaultValue={selectedOption.value as string} name={this.props.name} hidden />}
|
{selectedOption && <input type="text" defaultValue={selectedOption.value as string} name={this.props.name} hidden />}
|
||||||
<label
|
<label
|
||||||
@ -116,6 +121,23 @@ export default class SelectField extends React.Component<IProps, IState> {
|
|||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
public override componentDidMount(): void {
|
||||||
|
this.onResize();
|
||||||
|
this.removeOnresize = WindowStore.getInstance().onResize(() => this.onResize());
|
||||||
|
|
||||||
|
this.props.router.events.on("routeChangeStart", () => {
|
||||||
|
this.setState({
|
||||||
|
isOpen: false,
|
||||||
|
selectedOption: null,
|
||||||
|
listHeight: 0,
|
||||||
|
listWidth: 0,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public override componentWillUnmount() {
|
||||||
|
this.removeOnresize();
|
||||||
|
}
|
||||||
|
|
||||||
public override componentDidUpdate(prevProps: IProps) {
|
public override componentDidUpdate(prevProps: IProps) {
|
||||||
if (this.props.errors !== prevProps.errors) {
|
if (this.props.errors !== prevProps.errors) {
|
||||||
@ -140,15 +162,6 @@ export default class SelectField extends React.Component<IProps, IState> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override componentDidMount(): void {
|
|
||||||
this.onResize();
|
|
||||||
this.removeOnresize = WindowStore.getInstance().onResize(() => this.onResize());
|
|
||||||
}
|
|
||||||
|
|
||||||
public override componentWillUnmount() {
|
|
||||||
this.removeOnresize();
|
|
||||||
}
|
|
||||||
|
|
||||||
private onResize() {
|
private onResize() {
|
||||||
let listHeight = 0;
|
let listHeight = 0;
|
||||||
let listWidth = 0;
|
let listWidth = 0;
|
||||||
@ -192,3 +205,8 @@ export default class SelectField extends React.Component<IProps, IState> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default function SelectField(props: IProps) {
|
||||||
|
const router = useRouter();
|
||||||
|
return <SelectFieldClass {...props} router={router} />;
|
||||||
|
}
|
||||||
|
@ -45,9 +45,9 @@ export default function CollaboratorInformations(props: IProps) {
|
|||||||
setRoleModalOpened(false);
|
setRoleModalOpened(false);
|
||||||
setSelectedOption({
|
setSelectedOption({
|
||||||
value: userSelected?.office_role ? userSelected?.office_role?.uid : userSelected?.role?.uid,
|
value: userSelected?.office_role ? userSelected?.office_role?.uid : userSelected?.role?.uid,
|
||||||
label: userSelected?.office_role ? userSelected?.office_role?.name : userSelected?.role?.name!,
|
label: userSelected?.office_role ? userSelected?.office_role?.name : "Utilisateur restreint",
|
||||||
});
|
});
|
||||||
}, [userSelected?.office_role, userSelected?.role?.name, userSelected?.role?.uid]);
|
}, [userSelected?.office_role, userSelected?.role?.uid]);
|
||||||
|
|
||||||
const changeRole = useCallback(async () => {
|
const changeRole = useCallback(async () => {
|
||||||
await Users.getInstance().put(
|
await Users.getInstance().put(
|
||||||
@ -133,7 +133,7 @@ export default function CollaboratorInformations(props: IProps) {
|
|||||||
setUserSelected(user);
|
setUserSelected(user);
|
||||||
setSelectedOption({
|
setSelectedOption({
|
||||||
value: user?.office_role ? user?.office_role?.uid : user?.role?.uid,
|
value: user?.office_role ? user?.office_role?.uid : user?.role?.uid,
|
||||||
label: user?.office_role ? user?.office_role?.name : user?.role?.name!,
|
label: user?.office_role ? user?.office_role?.name : "Utilisateur restreint",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,7 +197,6 @@ export default function CollaboratorInformations(props: IProps) {
|
|||||||
})}
|
})}
|
||||||
selectedOption={selectedOption!}
|
selectedOption={selectedOption!}
|
||||||
onChange={handleRoleChange}
|
onChange={handleRoleChange}
|
||||||
disabled={userSelected?.role?.name === "super-admin"}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
{userSelected?.role?.name !== "super-admin" && (
|
{userSelected?.role?.name !== "super-admin" && (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user