|
|
@@ -0,0 +1,81 @@
|
|
|
+import '../index.css';
|
|
|
+import {computed, defineComponent, ref} from 'vue';
|
|
|
+import {selectProps, NSelect} from 'naive-ui';
|
|
|
+import {useField} from 'vee-validate';
|
|
|
+import {debounce} from 'lodash-es';
|
|
|
+
|
|
|
+export default defineComponent({
|
|
|
+ name: 'LDModalInput',
|
|
|
+ props: {
|
|
|
+ name: {
|
|
|
+ type: String,
|
|
|
+ required: true,
|
|
|
+ },
|
|
|
+ label: {
|
|
|
+ type: String,
|
|
|
+ required: true,
|
|
|
+ },
|
|
|
+ optional: Boolean,
|
|
|
+ loading: selectProps.loading,
|
|
|
+ options: selectProps.options,
|
|
|
+ onSearch: selectProps.onSearch,
|
|
|
+ },
|
|
|
+ setup(props) {
|
|
|
+ const {value, setValue, errorMessage} = useField<string>(props.name);
|
|
|
+
|
|
|
+ const filterValue = ref<string>('');
|
|
|
+ const filterOptions = computed(function() {
|
|
|
+ const {options} = props;
|
|
|
+
|
|
|
+ if (!filterValue.value) return options;
|
|
|
+
|
|
|
+ return options.filter(function(val) {
|
|
|
+ return (val?.label as string ?? '').includes(filterValue.value)
|
|
|
+ || (val?.value as string ?? '').includes(filterValue.value);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ const onDefaultSearch = debounce(
|
|
|
+ function(value: string) {
|
|
|
+ filterValue.value = value;
|
|
|
+ },
|
|
|
+ 0,
|
|
|
+ {
|
|
|
+ trailing: true,
|
|
|
+ leading: false,
|
|
|
+ },
|
|
|
+ );
|
|
|
+ // 失去焦点之后判断是否有搜索结果
|
|
|
+ // 如果没有搜索结果将搜索值清空
|
|
|
+ // 防止没搜索到之后失去焦点再返回会显示空选项
|
|
|
+ function onBlur() {
|
|
|
+ filterOptions.value.length === 0
|
|
|
+ && filterValue.value.length > 0
|
|
|
+ && (filterValue.value = '');
|
|
|
+ }
|
|
|
+
|
|
|
+ return () => (
|
|
|
+ <div class="ld-modal-field-wrapper">
|
|
|
+ <div class="ld-modal-field-group">
|
|
|
+ <label class={props?.optional ? 'optional' : ''}>{props.label}</label>
|
|
|
+ <NSelect
|
|
|
+ bordered={false}
|
|
|
+ filterable
|
|
|
+ remote
|
|
|
+ loading={props.loading}
|
|
|
+ onClear={() => {
|
|
|
+ setValue('');
|
|
|
+ onDefaultSearch('');
|
|
|
+ }}
|
|
|
+ options={filterOptions.value}
|
|
|
+ // 防止空字符串没有placeholder
|
|
|
+ value={value.value || null}
|
|
|
+ onUpdateValue={setValue}
|
|
|
+ onSearch={props.onSearch ?? onDefaultSearch}
|
|
|
+ onBlur={onBlur}
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+ <p class="ld-modal-filed-error">{errorMessage.value}</p>
|
|
|
+ </div>
|
|
|
+ );
|
|
|
+ },
|
|
|
+});
|