Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
feat: support custom props for l-model
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenybai committed Apr 11, 2021
1 parent 8af154e commit c29b36b
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/core/directives/model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DirectiveProps, DirectiveData, State } from '../../models/structs';

import { getElementCustomProp, setElementCustomProp } from '../utils/elementCustomProp';
import computeExpression from '../utils/computeExpression';

export const inputCallback = (
el: HTMLInputElement,
Expand All @@ -23,7 +24,7 @@ export const inputCallback = (
// Perform type coercion
let payload;
if (isNumber) {
payload = parseFloat(el.value);
payload = Number(el.value);
} else if (isBoolean) {
payload = el.value === 'true';
} else if (isNullish) {
Expand All @@ -33,14 +34,19 @@ export const inputCallback = (
payload = String(el.value);
}

state[data.value] = payload;
if (state[data.value]) {
state[data.value] = payload;
} else {
payload = typeof payload === 'string' ? `'${payload}'` : payload;
computeExpression(`${data.value} = ${payload}`, el, true)(state);
}

return payload;
};

export const modelDirective = ({ el: awaitingTypecastEl, parts, data, state }: DirectiveProps) => {
const el = awaitingTypecastEl as HTMLInputElement;
const hydratedValue = state[data.value];
const hydratedValue = state[data.value] ?? computeExpression(data.value, el, true)(state);
const accessor = el.type === 'checkbox' ? 'checked' : 'value';
if (el[accessor] !== String(hydratedValue)) {
el[accessor] = hydratedValue as never;
Expand Down

0 comments on commit c29b36b

Please sign in to comment.