Skip to content

Commit

Permalink
Merge pull request #3118 from emqx/dev/1.10.4
Browse files Browse the repository at this point in the history
Sync code from refs/heads/dev/1.10.4 to enterprise
  • Loading branch information
Kinplemelon authored Dec 19, 2024
2 parents d1e1d47 + 263cb81 commit 65e8977
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
29 changes: 25 additions & 4 deletions src/components/InputWithPlaceholderSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
</template>

<script setup lang="ts">
import { NUM_REG } from '@/common/constants'
import useSQLAvailablePlaceholder from '@/hooks/Rule/useSQLAvailablePlaceholder'
import { PropType } from '@/types/enum'
import { Property } from '@/types/schemaForm'
import { escapeRegExp } from 'lodash'
import { computed, defineEmits, defineProps, ref, watch } from 'vue'
const props = defineProps<{
modelValue?: string
modelValue?: string | number | boolean
[key: string]: any
customPlaceholders?: Array<string>
/**
Expand All @@ -36,17 +39,35 @@ const props = defineProps<{
* the options are added at the end of the list.
*/
options?: Array<string | number>
oneOf?: Array<Property>
}>()
const emit = defineEmits<{
(e: 'update:modelValue', v: string): void
(e: 'update:modelValue', v: string | number | boolean): void
(e: 'input', v: any): void
}>()
const getValueForEmit = (val: string) => {
if (!props.oneOf || props.oneOf.length === 0) {
return val
}
const oneOfBoolean = props.oneOf.find(({ type }) => type === PropType.Boolean)
const oneOfNum = props.oneOf.find(({ type }) =>
[PropType.Integer, PropType.Number].includes(type),
)
if (oneOfBoolean && /^(true|false)$/i.test(val)) {
return /^true/i.test(val)
}
if (oneOfNum && NUM_REG.test(val)) {
return Number(val)
}
return val
}
const inputValue = computed({
get: () => props.modelValue || '',
get: () => props.modelValue?.toString() || '',
set: (value: string) => {
emit('update:modelValue', value)
emit('update:modelValue', getValueForEmit(value))
},
})
Expand Down
2 changes: 2 additions & 0 deletions src/components/SchemaForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ const SchemaForm = defineComponent({
placeholder={property.default?.toString()}
modelValue={modelValue}
type={inputType}
oneOf={property.oneOf}
{...handleUpdateModelValue}
clearable
{...customProps}
Expand Down Expand Up @@ -415,6 +416,7 @@ const SchemaForm = defineComponent({
disabled={isPropertyDisabled}
placeholder={property.default?.toString()}
modelValue={modelValue}
oneOf={property.oneOf}
{...handleUpdateModelValue}
clearable={clearableValue}
options={property.symbols}
Expand Down
2 changes: 2 additions & 0 deletions src/components/SchemaFormItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export default defineComponent({
disabled={isDisabled}
type={inputType}
clearable
oneOf={props.property?.oneOf}
{...customProps}
/>
)
Expand Down Expand Up @@ -193,6 +194,7 @@ export default defineComponent({
clearable
{...customProps}
options={props.symbols}
oneOf={props.property?.oneOf}
/>
)
}
Expand Down
4 changes: 0 additions & 4 deletions src/hooks/Rule/bridge/useComponentsHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ export default (
prop.is_template
) {
setComponentProps(prop, { completionProvider })
} else if (prop.type === 'boolean' && prop.is_template) {
prop.type = 'enum'
prop.symbols = [true, false]
prop.default ??= ''
} else if (prop.type === 'object' && !prop.properties && prop.is_template) {
setComponentProps(prop, { supportPlaceholder: ['key', 'value'] })
}
Expand Down
10 changes: 9 additions & 1 deletion src/hooks/Schema/useSchemaForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export default function useSchemaForm(
}
return property
}
const simpleTypes = ['string', 'number', 'boolean', 'integer']
const getComponents = (objForGetComponent: { path?: string; ref?: string | Array<string> }) => {
let lastLabel = ''
/**
Expand Down Expand Up @@ -209,7 +210,14 @@ export default function useSchemaForm(
}
return item
}) as Property[]
property.is_template = getIsTemplateFromOneOfArr(property.oneOf)
property.is_template = property.is_template ?? getIsTemplateFromOneOfArr(property.oneOf)
const isSimpleOneof = oneOf.every(({ type }) => simpleTypes.includes(type))
if (isSimpleOneof && property.is_template) {
const withBoolean = oneOf.find(({ type }) => type === 'boolean')
property.type = withBoolean ? 'enum' : 'string'
property.symbols = withBoolean ? [true, false] : undefined
property.default = property.default ?? (withBoolean ? '' : undefined)
}
}
if (!label) {
property.label = lastLabel
Expand Down
1 change: 1 addition & 0 deletions src/types/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ export enum PropType {
Boolean = 'boolean',
Duration = 'duration',
Number = 'number',
Integer = 'integer',
IPPort = 'ip_port',
ByteSize = 'byteSize',
Object = 'object',
Expand Down

0 comments on commit 65e8977

Please sign in to comment.