mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-11 03:24:16 +00:00
improve lock & unlock handling
This commit is contained in:
parent
b23d99f5a9
commit
53f5695ff3
@ -7,19 +7,18 @@ import "./index.less"
|
|||||||
export default class DrawerController extends React.Component {
|
export default class DrawerController extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props)
|
super(props)
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
addresses: {},
|
addresses: {},
|
||||||
refs: {},
|
refs: {},
|
||||||
drawers: [],
|
drawers: [],
|
||||||
}
|
}
|
||||||
|
|
||||||
this.DrawerController = {
|
window.app["DrawerController"] = {
|
||||||
open: this.open,
|
open: this.open,
|
||||||
close: this.close,
|
close: this.close,
|
||||||
closeAll: this.closeAll,
|
closeAll: this.closeAll,
|
||||||
}
|
}
|
||||||
|
|
||||||
window.app["DrawerController"] = this.DrawerController
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sendEvent = (id, ...context) => {
|
sendEvent = (id, ...context) => {
|
||||||
@ -60,33 +59,31 @@ export default class DrawerController extends React.Component {
|
|||||||
this.setState({ refs, addresses, drawers })
|
this.setState({ refs, addresses, drawers })
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy = (id) => {
|
close = (id) => {
|
||||||
let { addresses, drawers, refs } = this.state
|
let { addresses, drawers, refs } = this.state
|
||||||
|
|
||||||
const index = addresses[id]
|
const index = addresses[id]
|
||||||
|
|
||||||
|
const ref = this.state.refs[id]?.current
|
||||||
|
|
||||||
|
if (typeof ref === "undefined") {
|
||||||
|
return console.warn("This drawer not exists")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ref.state.locked && ref.state.visible) {
|
||||||
|
return console.warn("This drawer is locked and cannot be closed")
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof drawers[index] !== "undefined") {
|
if (typeof drawers[index] !== "undefined") {
|
||||||
drawers = drawers.filter((value, i) => i !== index)
|
drawers = drawers.filter((value, i) => i !== index)
|
||||||
}
|
}
|
||||||
|
|
||||||
delete addresses[id]
|
delete addresses[id]
|
||||||
delete refs[id]
|
delete refs[id]
|
||||||
|
|
||||||
this.setState({ addresses, drawers })
|
this.setState({ addresses, drawers })
|
||||||
}
|
}
|
||||||
|
|
||||||
close = (id) => {
|
|
||||||
const ref = this.state.refs[id]?.current
|
|
||||||
|
|
||||||
if (typeof ref !== "undefined") {
|
|
||||||
if (ref.state.locked && ref.state.visible) {
|
|
||||||
return console.warn("This drawer is locked and cannot be closed")
|
|
||||||
} else {
|
|
||||||
return ref.close()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return console.warn("This drawer not exists")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
closeAll = () => {
|
closeAll = () => {
|
||||||
this.state.drawers.forEach((drawer) => {
|
this.state.drawers.forEach((drawer) => {
|
||||||
drawer.ref.current.close()
|
drawer.ref.current.close()
|
||||||
@ -100,12 +97,19 @@ export default class DrawerController extends React.Component {
|
|||||||
|
|
||||||
export class Drawer extends React.Component {
|
export class Drawer extends React.Component {
|
||||||
options = this.props.options ?? {}
|
options = this.props.options ?? {}
|
||||||
|
|
||||||
events = new EventBus()
|
events = new EventBus()
|
||||||
|
|
||||||
state = {
|
state = {
|
||||||
visible: true,
|
visible: true,
|
||||||
|
locked: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount = async () => {
|
componentDidMount = async () => {
|
||||||
|
if (this.options.defaultLocked) {
|
||||||
|
this.setState({ locked: true })
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof this.props.controller === "undefined") {
|
if (typeof this.props.controller === "undefined") {
|
||||||
throw new Error(`Cannot mount an drawer without an controller`)
|
throw new Error(`Cannot mount an drawer without an controller`)
|
||||||
}
|
}
|
||||||
@ -118,8 +122,21 @@ export class Drawer extends React.Component {
|
|||||||
this.setState({ visible: to ?? !this.state.visible })
|
this.setState({ visible: to ?? !this.state.visible })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lock = async () => {
|
||||||
|
return await this.setState({ locked: true })
|
||||||
|
}
|
||||||
|
|
||||||
|
unlock = async () => {
|
||||||
|
return await this.setState({ locked: false })
|
||||||
|
}
|
||||||
|
|
||||||
close = () => {
|
close = () => {
|
||||||
|
if (this.state.locked) {
|
||||||
|
return console.warn("Cannot close a locked drawer")
|
||||||
|
}
|
||||||
|
|
||||||
this.toogleVisibility(false)
|
this.toogleVisibility(false)
|
||||||
|
|
||||||
this.events.emit("beforeClose")
|
this.events.emit("beforeClose")
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
@ -127,7 +144,7 @@ export class Drawer extends React.Component {
|
|||||||
this.options.onClose()
|
this.options.onClose()
|
||||||
}
|
}
|
||||||
|
|
||||||
this.props.controller.destroy(this.props.id)
|
this.props.controller.close(this.props.id)
|
||||||
}, 500)
|
}, 500)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user