-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: applyDefaults and updated playground
- Loading branch information
Showing
10 changed files
with
176 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<template> | ||
<div class="flex cursor-grab"> | ||
<div | ||
v-for="tab in tabs" | ||
:key="tab" | ||
class="select-none px-3 mx-1 rounded" | ||
:class="[tab == modelValue ? 'bg-gray-400' : 'bg-gray-200']" | ||
@click="$emit('update:modelValue', tab)" | ||
> | ||
{{ tab[0].toUpperCase() + tab.substr(1) }} | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { defineComponent } from 'vue' | ||
export default defineComponent({ | ||
props: { | ||
tabs: { | ||
type: Array, | ||
default: () => [] | ||
}, | ||
modelValue: { | ||
type: String, | ||
default: '' | ||
} | ||
} | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export const defaultReference = ` | ||
export default { | ||
name: 'default', | ||
price: 12.5, | ||
/** | ||
* checked state | ||
*/ | ||
checked: false, | ||
dimensions: { | ||
/** width in px */ | ||
width: 10, | ||
/** height in px */ | ||
height: 10 | ||
}, | ||
tags: { | ||
$resolve: (val) => ['tag1'].concat(val).filter(Boolean) | ||
} | ||
} | ||
`.trim() | ||
|
||
export const defaultInput = ` | ||
export default { | ||
name: 'foo', | ||
dimensions: { | ||
height: 25 | ||
}, | ||
tags: ['custom'] | ||
} | ||
`.trim() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { resolveSchema } from './schema' | ||
import type { InputObject, Schema } from './types' | ||
|
||
export function applyDefaults (ref: InputObject, input: InputObject) { | ||
const schema = resolveSchema(ref, true) | ||
return _applyDefaults(schema, input) | ||
} | ||
|
||
export function _applyDefaults (schema: Schema, input: InputObject, root?: InputObject) { | ||
if (schema.type !== 'object') { | ||
return {} | ||
} | ||
const merged = {} | ||
for (const key in schema.properties!) { | ||
const prop = schema.properties[key] | ||
if (prop.type === 'object') { | ||
merged[key] = _applyDefaults(prop, input?.[key], root || input) | ||
} else if (typeof prop.resolve === 'function') { | ||
merged[key] = prop.resolve(input?.[key], merged, root || input) | ||
} else { | ||
merged[key] = input?.[key] || prop.default | ||
} | ||
} | ||
return merged | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { resolveSchema } from './schema' | ||
export { generateDts } from './dts' | ||
export { applyDefaults } from './defaults' | ||
|
||
export * from './types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.