-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: 支持配置必填项是否展示 #1890
refactor: 支持配置必填项是否展示 #1890
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,11 +26,16 @@ | |
</el-select> | ||
</el-form-item> | ||
<el-form-item | ||
label="默认值" | ||
:required="formValue.required" | ||
prop="default_value" | ||
:rules="formValue.required ? [{ required: true, message: '默认值 为必填属性' }] : []" | ||
> | ||
<template #label> | ||
<div class="flex-between"> | ||
默认值 | ||
<el-checkbox v-model="formValue.show_default_value" label="显示默认值" /> | ||
</div> | ||
</template> | ||
<el-date-picker | ||
v-model="formValue.default_value" | ||
:type="formValue.type" | ||
|
@@ -91,19 +96,22 @@ const getData = () => { | |
format: formValue.value.format, | ||
'value-format': formValue.value.format | ||
}, | ||
default_value: formValue.value.default_value | ||
default_value: formValue.value.default_value, | ||
show_default_value: formValue.value.show_default_value, | ||
} | ||
} | ||
const rander = (form_data: any) => { | ||
formValue.value.type = form_data.attrs.type | ||
formValue.value.format = form_data.attrs?.format | ||
formValue.value.default_value = form_data.default_value || '' | ||
formValue.value.show_default_value = form_data.show_default_value | ||
} | ||
defineExpose({ getData, rander }) | ||
onBeforeMount(() => { | ||
formValue.value.type = 'datetime' | ||
formValue.value.format = 'YYYY-MM-DD HH:mm:ss' | ||
formValue.value.default_value = '' | ||
formValue.value.show_default_value = true | ||
}) | ||
</script> | ||
<style lang="scss"></style> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided code looks mostly correct, but it can benefit from a few improvements for consistency and readability:
Here's the revised version with these considerations: <template>
<el-form-item :required="formValue.required" prop="default_value" :rules="formValue.required ? [{ required: true, message: '默认值为必填属性' }] : []">
<template #label>
<div class="flex-between">
默认值
<el-checkbox v-model="formValue.show_default_value" label="显示默认值" />
</div>
</template>
<el-date-picker
v-model="formValue.value.default_date"
:type="formValue.value.type"
placeholder="请选择时间"
value-format="YYYY-MM-DD HH:mm:ss"
/>
</el-form-item>
</template>
<script lang="ts">
import { ref } from 'vue';
interface FormValue {
type: string;
format?: string;
default_value?: any; // Assuming this could be an ISO8601 string or similar
show_default_value: boolean;
}
export default defineComponent({
name: 'DateTimePickerFormItem',
setup() {
const formValue = ref<FormValue>({
type: 'datetime',
format: undefined,
default_value: '',
show_default_value: false,
});
const getData = () => {
return {
key: formValue.value.key, // Ensure the key exists in the original data structure
...formValue.value,
format: formValue.value.format,
'value-format': formValue.value.format,
};
};
const rander = (form_data: any) => {
formValue.value.type = form_data.attrs.type || '';
formValue.value.format = form_data.attrs?.format ?? '';
formValue.value.default_value = form_data.default_value || '';
formValue.value.show_default_value = !!form_data.show_default_value;
};
defineExpose({ getData, rander });
onBeforeMount(() => {});
return {
formValue,
};
},
});
</script>
<style lang="scss"></style> Key Changes:
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
<template> | ||
<el-form-item | ||
label="默认值" | ||
:required="formValue.required" | ||
prop="default_value" | ||
:rules="[default_value_rule]" | ||
> | ||
<template #label> | ||
<div class="flex-between"> | ||
默认值 | ||
<el-checkbox v-model="formValue.show_default_value" label="显示默认值" /> | ||
</div> | ||
</template> | ||
<JsonInput ref="jsonInputRef" v-model="formValue.default_value"> </JsonInput> | ||
</el-form-item> | ||
</template> | ||
|
@@ -40,7 +45,8 @@ const getData = () => { | |
} | ||
] | ||
}, | ||
default_value: formValue.value.default_value | ||
default_value: formValue.value.default_value, | ||
show_default_value: formValue.value.show_default_value, | ||
} | ||
} | ||
|
||
|
@@ -55,10 +61,12 @@ const default_value_rule = { | |
|
||
const rander = (form_data: any) => { | ||
formValue.value.default_value = form_data.default_value | ||
formValue.value.show_default_value = form_data.show_default_value | ||
} | ||
defineExpose({ getData, rander }) | ||
onMounted(() => { | ||
formValue.value.default_value = {} | ||
formValue.value.show_default_value = true | ||
}) | ||
</script> | ||
<style lang="scss"></style> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided code seems generally correct, but there is one small issue that needs to be addressed: The CSS styling in the template might not be properly applied because you have nested Here is your updated code: <template>
<el-form-item
label="默认值"
:required="formValue.required"
prop="default_value"
:rules="[default_value_rule]"
>
<template #label>
<div>
<span>默认值</span>
<el-checkbox v-model="show_default_value" label="显示默认值" />
</div>
</template>
<JsonInput ref="jsonInputRef" v-model="default_value"> </JsonInput>
</el-form-item>
</template> This modification wraps everything inside a separate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code ReviewThe provided code snippet is for a Vue.js template with an Regularities & Issues
Optimization Suggestions
Revised Example ScriptHere’s a revised version of the script considering my suggestions: <script setup>
import { ref } from 'vue';
import JsonInput from './components/JsonInput.vue';
const formValue = ref({
required: true,
show_default_value: true,
});
// Dummy getData function simulating backend response
async function getData() {
try {
const responseData = await fetchData(); // Replace fetchData with actual API call
Object.assign(formValue.value, responseData);
} catch (error) {
console.error('Failed to get data:', error);
}
}
function rander(form_data: any) {
// Overwrite current form value with received data
Object.assign(formValue.value, form_data);
// Optionally handle specific changes here if necessary
}
defineExpose({ getData, rander });
// Simulate fetching data after mount
onMounted(async () => {
getData();
});
</script> Ensure to replace |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,11 +46,16 @@ | |
</el-row> | ||
</el-form-item> | ||
<el-form-item | ||
label="默认值" | ||
:required="formValue.required" | ||
prop="default_value" | ||
:rules="formValue.required ? [{ required: true, message: '默认值 为必填属性' }] : []" | ||
> | ||
<template #label> | ||
<div class="flex-between"> | ||
默认值 | ||
<el-checkbox v-model="formValue.show_default_value" label="显示默认值" /> | ||
</div> | ||
</template> | ||
<el-select | ||
class="m-2" | ||
multiple | ||
|
@@ -103,6 +108,7 @@ const getData = () => { | |
input_type: 'MultiSelect', | ||
attrs: {}, | ||
default_value: formValue.value.default_value, | ||
show_default_value: formValue.value.show_default_value, | ||
text_field: 'label', | ||
value_field: 'value', | ||
option_list: formValue.value.option_list | ||
|
@@ -111,12 +117,14 @@ const getData = () => { | |
const rander = (form_data: any) => { | ||
formValue.value.option_list = form_data.option_list || [] | ||
formValue.value.default_value = form_data.default_value | ||
formValue.value.show_default_value = form_data.show_default_value | ||
} | ||
|
||
defineExpose({ getData, rander }) | ||
onMounted(() => { | ||
formValue.value.option_list = [] | ||
formValue.value.default_value = '' | ||
formValue.value.show_default_value = true | ||
|
||
addOption() | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided code seems generally correct, though there are a couple of minor improvements that could be made:
This version ensures consistent initialization and provides additional handling for default value visibility and retrieval. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your Here are some suggestions:
Here is an optimized version of your code: const getData = () => {
formData.option_list = formData.option_list || [];
}
// Simplify onMounted setup
onMounted(() => {
formValue.value.option_list = [];
// Avoid initializing show_default_value here or using formData.show_default_value because it is handled elsewhere
})
defineExpose({
getData,
rander
}) This way, you reduce unnecessary reassignments and streamline the initialization process. |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The provided code has a few areas that need attention:
Vue Slots: The template uses Vue's
<template #label>
feature to customize the label of an element. However, there might be some inconsistencies if the slot is not properly utilized across different components.Variable Name Usage: In
rander
, you're setting properties (type
andformat
) based on attributes fromform_data
. It would be better to ensure these properties are correctly matched if they correspond to different data structures.Form Rules: You have a rule set up for the "默认值" field with the message "默认值为必填属性". If this rule applies consistently to only certain situations or conditions, it seems unnecessary unless explicitly defined elsewhere.
Data Initialization: In the
defineExpose()
function, you initializeshow_default_value
totrue
without specifying what triggers its value to change outside of the initial mount lifecycle event handler. This can lead to unexpected behavior.Code Comments: Some comments about form rules could use expansion to clarify their logic more clearly.
Optimization Suggestions
Consistent Use of Template Slots: Ensure consistent usage of slots to avoid confusion when rendering multiple items or conditional rendering within labels.
Type Safety Check: Verify if
attrs
includes all expected keys before accessing them.Refactor Form Validation Rules: Break down complex validation rules into smaller ones if possible, making the code easier to understand and maintain.
Here’s a revised version incorporating some suggested improvements:
This revision adds clear comments around form rules and provides context for why some values are initialized to specific defaults (like
show_default_value
). Additionally, I've improved error handling in the date picker component by providing placeholders and using shortcuts options.