forked from verdnatura/salix-front
feat: formatLocation when field is null
This commit is contained in:
parent
1150739de7
commit
271e33a999
|
@ -12,14 +12,46 @@ const props = defineProps({
|
|||
default: null,
|
||||
},
|
||||
});
|
||||
const formatLocation = (obj, properties) => {
|
||||
// Crear un array con las propiedades del objeto
|
||||
const parts = properties.map((prop) => {
|
||||
if (typeof prop === 'string') {
|
||||
return obj[prop];
|
||||
} else if (typeof prop === 'function') {
|
||||
return prop(obj);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
// Filtrar los valores que no son null o undefined
|
||||
const filteredParts = parts.filter(
|
||||
(part) => part !== null && part !== undefined && part !== ''
|
||||
);
|
||||
|
||||
// Unir los valores filtrados con el formato deseado
|
||||
return filteredParts.join(', ');
|
||||
};
|
||||
|
||||
const locationProperties = [
|
||||
'postcode',
|
||||
(obj) =>
|
||||
obj.city
|
||||
? `${obj.city}${obj.province?.name ? `(${obj.province.name})` : ''}`
|
||||
: null,
|
||||
(obj) => obj.country?.name,
|
||||
];
|
||||
const modelValue = ref(
|
||||
props.location
|
||||
? `${props.location?.postcode} - ${props.location?.city}(${props.location?.province?.name}), ${props.location?.country?.name}`
|
||||
: null
|
||||
props.location ? formatLocation(props.location, locationProperties) : null
|
||||
);
|
||||
function showLabel(data) {
|
||||
return `${data.code} - ${data.town}(${data.province}), ${data.country}`;
|
||||
const dataProperties = [
|
||||
'code',
|
||||
(obj) => (obj.town ? `${obj.town}(${obj.province})` : null),
|
||||
'country',
|
||||
];
|
||||
return formatLocation(data, dataProperties);
|
||||
}
|
||||
|
||||
const handleModelValue = (data) => {
|
||||
emit('update:model-value', data);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue