Skip to content

Commit

Permalink
feat: [Select] - related #384 - add translations, style(wip), add new…
Browse files Browse the repository at this point in the history
… props(disabled feature)
  • Loading branch information
mattgoud committed Oct 16, 2024
1 parent 1db2e5b commit e9c0083
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 40 deletions.
1 change: 1 addition & 0 deletions packages/components/option/style/css.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '@prestashopcorp/puik-components/base/style/css';
import '@prestashopcorp/puik-theme/puik-option.css';
import '@prestashopcorp/puik-theme/puik-checkbox.css';
import '@prestashopcorp/puik-theme/puik-icon.css';
6 changes: 4 additions & 2 deletions packages/components/select/src/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ export interface OptionType {
}
export interface OptionProps {
option: OptionType
labelKey: string
valueKey: string
labelKey?: string
valueKey?: string
disabledKey?: string
disabled?: boolean
selectedOptions: OptionType[]
multiSelect?: boolean
}
Expand Down
14 changes: 12 additions & 2 deletions packages/components/select/src/option.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
<div
:class="[
'puik-option',
{ 'puik-option-single--selected' : isSelected && !props.multiSelect}
{ 'puik-option-single--selected' : isSelected && !props.multiSelect},
{ 'puik-option--disabled' : props.disabled },
]"
>
<template v-if="props.multiSelect">
<PuikCheckbox
v-model="isSelected"
:disabled="disabled"
:sr-label="option[props.labelKey]"
@change="selectOption"
/>
Expand Down Expand Up @@ -35,6 +37,10 @@ defineOptions({
});
const props = withDefaults(defineProps<OptionProps>(), {
labelKey: 'label',
valueKey: 'value',
disabledKey: 'disabled',
disabled: false,
multiSelect: false,
selectedOptions: () => { return []; }
});
Expand All @@ -46,11 +52,15 @@ const isSelected = computed(() => {
});
const selectOption = () => {
emit('select', props.option);
if (props.disabled) {
emit('select', props.option);
}
};
</script>

<style lang="scss">
@use '@prestashopcorp/puik-theme/src/base.scss';
@use '@prestashopcorp/puik-theme/src/puik-option.scss';
@use '@prestashopcorp/puik-theme/src/puik-checkbox.scss';
@use '@prestashopcorp/puik-theme/src/puik-icon.scss';
</style>
8 changes: 6 additions & 2 deletions packages/components/select/src/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import type Select from './select.vue';
import type { OptionType } from './option';
export interface SelectProps {
options: OptionType[]
labelKey: string
valueKey: string
optionLabelKey?: string
optionValueKey?: string
optionDisabledKey?: string
modelValue: any
multiSelect?: boolean
searchable?: boolean
placeholder?: string
searchPlaceholder?: string
noMatchText?: string
disabled?: boolean
error?: string
}
Expand Down
70 changes: 48 additions & 22 deletions packages/components/select/src/select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
/>
<puik-chip
v-for="option in selectedOptions"
:id="`chip-${option[props.labelKey]}`"
:key="option[props.valueKey]"
:content="option[props.labelKey]"
:id="`chip-${option[props.optionLabelKey]}`"
:key="option[props.optionValueKey]"
:content="option[props.optionLabelKey]"
size="small"
@close="deselectOption(option)"
@click.stop="openOptions"
Expand All @@ -28,7 +28,7 @@
v-else
class="puik-select__multiple-input"
type="text"
placeholder="Select an option"
:placeholder="props.placeholder ?? defaultPlaceholder"
readonly
@click.stop="toggleOptions"
>
Expand All @@ -41,7 +41,7 @@
v-else
v-model="selectedSingleOption"
type="text"
placeholder="Select an option"
:placeholder="props.placeholder ?? defaultPlaceholder"
readonly
@click.stop="toggleOptions"
/>
Expand All @@ -54,7 +54,7 @@
v-model="searchQuery"
class="puik-select__search-input"
type="text"
placeholder="Search..."
:placeholder="props.searchPlaceholder ?? defaultSearchPlaceholder"
@input="searchOptions"
>
<template #prepend>
Expand All @@ -63,19 +63,20 @@
</puik-input>
<PuikCheckbox
v-if="multiSelect"
v-model="isAllSelected"
v-model="internalIsAllSelected"
class="puik-select__select-all"
:label="isAllSelected ? 'Deselect All' : 'Select All'"
:label="isAllSelected ? `${t('puik.select.deselectAll')}` : `${t('puik.select.selectAll')}`"
:indeterminate="selectAllIndeterminate"
@change="toggleSelectAll"
/>
<puik-option
v-for="option in filteredOptions"
:key="option[props.valueKey]"
:label-key="labelKey"
:value-key="valueKey"
:key="option[props.optionValueKey]"
:label-key="props.optionLabelKey"
:value-key="props.optionValueKey"
:selected-options="selectedOptions"
:option="option"
:disabled="option[props.optionDisabledKey]"
:multi-select="props.multiSelect"
@click="selectOption(option)"
/>
Expand All @@ -84,8 +85,9 @@
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { ref, computed, watch } from 'vue';
import { vOnClickOutside } from '@vueuse/components';
import { useLocale } from '@prestashopcorp/puik-locale';
import { PuikCheckbox, PuikChip, PuikIcon, PuikInput, PuikOption } from '@prestashopcorp/puik-components';
import type { OptionType } from './option';
import type { SelectProps } from './select';
Expand All @@ -94,7 +96,14 @@ defineOptions({
name: 'PuikSelect'
});
const { t } = useLocale();
const defaultPlaceholder = t('puik.select.placeholder');
const defaultSearchPlaceholder = t('puik.select.searchPlaceholder');
const props = withDefaults(defineProps<SelectProps>(), {
optionLabelKey: 'label',
optionValueKey: 'value',
optionDisabledKey: 'disabled',
multiSelect: false
});
Expand All @@ -109,11 +118,17 @@ const selectedSingleOption = computed(() => {
return selectedOptions.value.length > 0 ? selectedOptions.value[0].label : '';
});
const filteredOptions = computed(() => {
return props.options.filter(option => option.label.includes(searchQuery.value));
if (props.options) {
return props.options.filter(option => option.label.includes(searchQuery.value));
} else {
return null;
}
});
const isAllSelected = computed(() => {
return selectedOptions.value.length === props.options.length;
});
const internalIsAllSelected = ref(isAllSelected.value);
const updateSelectAllIndeterminate = () => {
selectAllIndeterminate.value = selectedOptions.value.length > 0 && selectedOptions.value.length < props.options.length;
};
Expand All @@ -127,19 +142,22 @@ const closeOptions = () => {
showOptions.value = false;
};
const selectOption = (option: any) => {
if (props.multiSelect) {
if (selectedOptions.value.includes(option)) {
selectedOptions.value = selectedOptions.value.filter(opt => opt !== option);
if (!option[props.optionDisabledKey]) {
if (props.multiSelect) {
if (selectedOptions.value.includes(option)) {
selectedOptions.value = selectedOptions.value.filter(opt => opt !== option);
} else {
selectedOptions.value.push(option);
}
updateSelectAllIndeterminate();
} else {
selectedOptions.value.push(option);
selectedOptions.value = [option];
showOptions.value = false;
}
updateSelectAllIndeterminate();
} else {
selectedOptions.value = [option];
showOptions.value = false;
emit('update:modelValue', selectedOptions.value);
}
emit('update:modelValue', selectedOptions.value);
};
const deselectOption = (option: OptionType) => {
selectedOptions.value = selectedOptions.value.filter(opt => opt !== option);
updateSelectAllIndeterminate();
Expand All @@ -157,10 +175,18 @@ const toggleSelectAll = () => {
const searchOptions = () => {
emit('search', searchQuery.value);
};
watch(isAllSelected, (newValue) => {
internalIsAllSelected.value = newValue;
});
</script>

<style lang="scss">
@use '@prestashopcorp/puik-theme/src/base.scss';
@use '@prestashopcorp/puik-theme/src/puik-select.scss';
@use '@prestashopcorp/puik-theme/src/puik-option.scss';
@use '@prestashopcorp/puik-theme/src/puik-checkbox.scss';
@use '@prestashopcorp/puik-theme/src/puik-chip.scss';
@use '@prestashopcorp/puik-theme/src/puik-icon.scss';
@use '@prestashopcorp/puik-theme/src/puik-input.scss';
</style>
5 changes: 4 additions & 1 deletion packages/components/select/style/css.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import '@prestashopcorp/puik-components/base/style/css';
import '@prestashopcorp/puik-theme/puik-select.css';
import '@prestashopcorp/puik-theme/puik-input.css';
import '@prestashopcorp/puik-theme/puik-option.css';
import '@prestashopcorp/puik-theme/puik-checkbox.css';
import '@prestashopcorp/puik-theme/puik-chip.css';
import '@prestashopcorp/puik-theme/puik-icon.css';
import '@prestashopcorp/puik-theme/puik-input.css';
1 change: 1 addition & 0 deletions packages/components/table/src/table-search-input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ const sendRangeValue = (
</script>

<style lang="scss">
@use '@prestashopcorp/puik-theme/src/puik-button.scss';
@use '@prestashopcorp/puik-theme/src/puik-input.scss';
@use '@prestashopcorp/puik-theme/src/puik-icon.scss';
</style>
7 changes: 5 additions & 2 deletions packages/locale/lang/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ export default {
readonly: 'Read\xa0only'
},
select: {
searchPlaceholder: 'Search',
noResults: 'No results matched'
placeholder: 'Select on option',
searchPlaceholder: 'Search...',
noResults: 'No results matched',
selectAll: 'Select all',
deselectAll: 'Deselect all'
},
table: {
expandableHeaderLabel: 'header column for the expandable rows feature',
Expand Down
7 changes: 5 additions & 2 deletions packages/locale/lang/fr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ export default {
readonly: 'Lecture uniquement'
},
select: {
searchPlaceholder: 'Chercher',
noResults: 'Aucun résultat'
placeholder: 'Select on option',
searchPlaceholder: 'Chercher...',
noResults: 'Aucun résultat',
selectAll: 'Tout sélectionnner',
deselectAll: 'Tout déselectionner'
},
table: {
expandableHeaderLabel: 'colonne d\'en-tête pour la fonctionnalité de lignes extensibles',
Expand Down
4 changes: 2 additions & 2 deletions packages/theme/src/puik-option.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.puik-option {
@apply px-3 py-2 flex justify-between space-x-2 relative cursor-default select-none text-primary-800 transition-colors;
@apply px-3 py-2 flex justify-start space-x-2 relative cursor-default select-none text-primary-800 transition-colors;
&-single--selected {
@apply bg-primary-300;
}
Expand All @@ -13,7 +13,7 @@
@apply bg-white;
}
&--disabled {
@apply bg-white text-primary-300 pointer-events-none;
@apply bg-white text-primary-500 cursor-not-allowed;
}
&__label {
@apply font-primary font-normal block truncate;
Expand Down
7 changes: 2 additions & 5 deletions packages/theme/src/puik-select.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
focus-visible:outline-none sm:text-sm overflow-x-hidden;
}
&__select-all {
@apply px-3 py-2 flex relative select-none text-primary-800 transition-colors;
@apply px-3 flex relative select-none text-primary-800 transition-colors;
label {
@apply grow cursor-default;
@apply min-h-9 flex items-center grow cursor-default;
}
&--selected {
@apply bg-primary-300;
Expand All @@ -52,7 +52,4 @@
&__search-input {
@apply px-3 py-2;
}
label {
@apply font-primary font-normal block truncate;
}
}

0 comments on commit e9c0083

Please sign in to comment.