adonisjs-livewire
Version:
A front-end framework for AdonisJS
54 lines (53 loc) • 1.55 kB
JavaScript
import { cuid } from '@adonisjs/core/helpers';
import { Decorator } from '../support_decorators/decorator.js';
export default class Url extends Decorator {
name;
as;
history;
keep;
except;
nullable;
constructor(name, as = null, history = false, keep = false, except = null, nullable = null) {
super();
this.name = name;
this.as = as;
this.history = history;
this.keep = keep;
this.except = except;
this.nullable = nullable;
}
mount() {
let nonExistentValue = 'livewire:' + cuid();
let value;
let initialValue = this.component.ctx.request.input(this.name, nonExistentValue);
if (initialValue === nonExistentValue) {
return;
}
if (initialValue === null) {
value = this.nullable ? null : '';
}
else {
value = initialValue;
}
this.component[this.name] = value;
}
dehydrate(context) {
if (!context.mounting)
return;
let queryString = {
as: this.as || null,
use: this.history ? 'push' : 'replace',
alwaysShow: this.keep,
except: this.except,
};
context.pushEffect('url', queryString, this.name);
}
async update(propertyName, newValue) {
if (propertyName !== this.name) {
return;
}
if (newValue === null && !this.nullable && this.component) {
this.component[this.name] = '';
}
}
}