Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/cms/src/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const {
CharacterCounter,
Checkbox,
CheckboxGroup,
ChoiceGrid,
CodeEditor,
Combobox,
CommandPaletteItem,
Expand Down Expand Up @@ -91,13 +92,15 @@ export const {
PublishTabs,
Radio,
RadioGroup,
RankList,
Select,
Separator,
Slider,
Skeleton,
SplitterGroup,
SplitterPanel,
SplitterResizeHandle,
StarRating,
StatusIndicator,
Subheading,
Switch,
Expand Down
1 change: 0 additions & 1 deletion resources/css/cp.css
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,5 @@
@import './components/fieldtypes/partial.css';
@import './components/fieldtypes/relationship.css';
@import './components/fieldtypes/section.css';
@import './components/fieldtypes/star-rating.css';
@import './components/fieldtypes/table.css';
@import './components/fieldtypes/width.css';
3 changes: 3 additions & 0 deletions resources/js/bootstrap/cms/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export {
CharacterCounter,
Checkbox,
CheckboxGroup,
ChoiceGrid,
CodeEditor,
Combobox,
CommandPaletteItem,
Expand Down Expand Up @@ -89,13 +90,15 @@ export {
PublishTabs,
Radio,
RadioGroup,
RankList,
Select,
Separator,
Slider,
Skeleton,
SplitterGroup,
SplitterPanel,
SplitterResizeHandle,
StarRating,
StatusIndicator,
Subheading,
Switch,
Expand Down
138 changes: 37 additions & 101 deletions resources/js/components/fieldtypes/ImageChoiceFieldtype.vue
Original file line number Diff line number Diff line change
@@ -1,115 +1,51 @@
<script setup>
import Fieldtype from '@/components/fieldtypes/fieldtype.js';
import { Icon } from '@/components/ui';
import { computed, ref, watch } from 'vue';
import { ChoiceGrid } from '@/components/ui';
import { __ } from '@/bootstrap/globals';
import { computed } from 'vue';

const emit = defineEmits(Fieldtype.emits);
const props = defineProps(Fieldtype.props);
const { expose, update, isReadOnly, name } = Fieldtype.use(emit, props);
defineExpose(expose);

const multiple = computed(() => Boolean(props.config.multiple));
const isDisabled = computed(() => props.config.disabled || isReadOnly.value);
const options = computed(() => props.meta?.options ?? []);
const columns = computed(() => props.config.columns ?? 3);
const aspectRatio = computed(() => props.config.aspect_ratio ?? '16/9');
const gap = computed(() => props.config.gap ?? 3);

const selectedValues = ref(props.value);

const cardClasses = [
'flex flex-col gap-2 h-full p-1 pb-2 rounded-lg border shadow-ui-xs',
'border-gray-300 bg-white',
'dark:border-gray-700 dark:bg-gray-900',
'hover:border-gray-400 dark:hover:border-gray-700',
'peer-checked:border-primary peer-checked:ring-[1px] peer-checked:ring-primary',
'peer-focus-visible:focus-outline peer-focus-visible:transition-none',
];
const gaps = {
2: 'sm',
3: 'base',
4: 'lg',
};

const gridClass = computed(() => ({
1: 'grid-cols-1',
2: 'grid-cols-2',
3: 'grid-cols-3',
4: 'grid-cols-4',
})[columns.value] ?? 'grid-cols-3');

const gapClass = computed(() => ({
2: 'gap-2',
3: 'gap-3',
4: 'gap-4',
})[gap.value] ?? 'gap-3');

function isSelected(key) {
return selectedValues.value.includes(key);
}

function toggleOption(key) {
if (isDisabled.value) return;

if (isSelected(key)) {
selectedValues.value = selectedValues.value.filter((v) => v !== key);
} else if (multiple.value) {
selectedValues.value = [...selectedValues.value, key];
} else {
selectedValues.value = [key];
}

update(selectedValues.value);
const multiple = computed(() => Boolean(props.config.multiple));
const label = computed(() => (props.config.display ? __(props.config.display) : null));
const gap = computed(() => gaps[props.config.gap] ?? 'base');

const options = computed(() =>
(props.meta?.options ?? []).map((option) => ({
value: option.key,
label: option.label ? __(option.label) : null,
image: option.image,
badge: option.letter,
})),
);

const selected = computed(() => (multiple.value ? props.value : (props.value?.[0] ?? null)));

function updateSelected(value) {
update(multiple.value ? value : [value]);
}

watch(() => props.value, (value) => selectedValues.value = value);
</script>

<template>
<div
class="grid"
:class="[gapClass, gridClass]"
:role="multiple ? 'group' : 'radiogroup'"
>
<label
v-for="option in options"
:key="option.key"
class="group cursor-pointer has-disabled:cursor-not-allowed has-disabled:opacity-60"
>
<input
class="peer sr-only"
:type="multiple ? 'checkbox' : 'radio'"
:name="multiple ? `${name}[]` : name"
:value="option.key"
:checked="isSelected(option.key)"
:disabled="isDisabled"
@change="toggleOption(option.key)"
>
<span :class="cardClasses">
<span
class="flex items-center justify-center overflow-hidden rounded-md bg-gray-100 dark:bg-gray-800"
:style="{ aspectRatio }"
>
<img
v-if="option.image"
class="block size-full object-cover"
:src="option.image"
:alt="option.label"
>
<span v-else class="flex size-full items-center justify-center" aria-hidden="true">
<Icon name="image" class="size-6 text-gray-400" />
</span>
</span>
<span
v-if="option.label"
class="flex items-center justify-center gap-2"
>
<span
v-if="option.letter"
class="flex size-6 shrink-0 items-center justify-center rounded-md border border-gray-300 bg-white text-xs font-bold text-gray-800 dark:border-gray-600 dark:bg-gray-900 dark:text-gray-200"
>
{{ option.letter }}
</span>
<span class="text-xs text-gray-800 dark:text-gray-200" style="text-box-trim: trim-start;">
{{ __(option.label) }}
</span>
</span>
</span>
</label>
</div>
<ChoiceGrid
:model-value="selected"
:options
:multiple
:name
:label
:gap
:columns="config.columns ?? 3"
:aspect-ratio="config.aspect_ratio ?? '16/9'"
:disabled="config.disabled || isReadOnly"
@update:model-value="updateSelected"
/>
</template>
156 changes: 15 additions & 141 deletions resources/js/components/fieldtypes/RankingFieldtype.vue
Original file line number Diff line number Diff line change
@@ -1,154 +1,28 @@
<script setup>
import Fieldtype from '@/components/fieldtypes/fieldtype.js';
import { SortableList } from '@/components/sortable/Sortable.js';
import { DragHandle } from '@/components/ui';
import { computed, ref, watch } from 'vue';
import { RankList } from '@/components/ui';
import { __ } from '@/bootstrap/globals';
import { computed } from 'vue';

const emit = defineEmits(Fieldtype.emits);
const props = defineProps(Fieldtype.props);
const { expose, update, isReadOnly, name } = Fieldtype.use(emit, props);
defineExpose(expose);

const sortableItemClass = 'ranking-item';
const sortableHandleClass = 'ranking-handle';

const rankedValues = ref([]);

const options = computed(() => {
const raw = props.config.options ?? {};
return Object.entries(raw).map(([value, label]) => ({ value, label: label || value }));
});

const isDisabled = computed(() => props.config.disabled || isReadOnly.value);

const orderedRows = computed(() => rankedValues.value.map((value, index) => ({
value,
label: options.value.find((option) => option.value === value)?.label ?? value,
index,
})));

function defaultOrder() {
return options.value.map((option) => option.value);
}

function buildRankedValues(value) {
const order = Array.isArray(value) && value.length ? [...value] : defaultOrder();
const valid = new Set(options.value.map((option) => option.value));

const ranked = order.filter((item) => valid.has(item));

options.value.forEach((option) => {
if (! ranked.includes(option.value)) {
ranked.push(option.value);
}
});

return ranked;
}

const hasCustomOrder = computed(() => {
return JSON.stringify(rankedValues.value) !== JSON.stringify(defaultOrder());
});

function moveToRank(itemValue, event) {
const fromIndex = rankedValues.value.indexOf(itemValue);
const parsed = Number(event.target.value);

if (! isDisabled.value && fromIndex !== -1 && Number.isFinite(parsed)) {
const toIndex = Math.max(0, Math.min(rankedValues.value.length - 1, Math.round(parsed) - 1));

if (toIndex !== fromIndex) {
const next = [...rankedValues.value];
next.splice(fromIndex, 1);
next.splice(toIndex, 0, itemValue);
rankedValues.value = next;
}
}

// Reset the input to the item's canonical rank. When the requested rank is
// clamped to the item's current position, no reorder happens, so the binding
// wouldn't otherwise overwrite the invalid value left in the input.
event.target.value = rankedValues.value.indexOf(itemValue) + 1;
}

watch(
[() => props.value, options],
() => rankedValues.value = buildRankedValues(props.value),
{ deep: true, immediate: true },
);

watch(
rankedValues,
(value) => {
const current = Array.isArray(props.value) ? props.value : [];

if (JSON.stringify(value) === JSON.stringify(current)) {
return;
}

update([...value]);
},
{ deep: true },
const options = computed(() =>
Object.entries(props.config.options ?? {}).map(([value, label]) => ({
value,
label: label ? __(label) : value,
})),
);
</script>

<template>
<SortableList
v-if="orderedRows.length"
v-model="rankedValues"
:vertical="true"
:item-class="sortableItemClass"
:handle-class="sortableHandleClass"
:mirror="false"
:disabled="isDisabled"
>
<ul class="flex flex-col gap-2" role="list">
<li
v-for="row in orderedRows"
:key="row.value"
:class="sortableItemClass"
class="flex items-center gap-2 rounded-lg border border-gray-300 bg-white p-2 dark:border-gray-700 dark:bg-gray-900"
>
<DragHandle
v-if="!isDisabled"
:class="[
sortableHandleClass,
hasCustomOrder
? 'text-primary! [&_svg]:opacity-100'
: '[&_svg]:opacity-75 dark:[&_svg]:opacity-50',
]"
class="cursor-grab"
/>
<input
v-if="!isDisabled"
type="number"
class="size-6 shrink-0 rounded-md border border-gray-300 bg-white px-0 text-center text-xs font-semibold text-gray-800 shadow-ui-xs focus:focus-outline dark:border-gray-700 dark:bg-gray-925 dark:text-gray-200 [appearance:textfield] [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none"
:min="1"
:max="orderedRows.length"
step="1"
:value="row.index + 1"
:aria-label="`${__('Rank')} ${__(row.label)}`"
@change="moveToRank(row.value, $event)"
>
<span
v-else
class="flex size-6 shrink-0 items-center justify-center rounded-md border border-gray-300 shadow-ui-xs bg-white text-xs font-semibold text-gray-800 dark:border-gray-700 dark:bg-gray-925 dark:text-gray-200"
aria-hidden="true"
>
{{ row.index + 1 }}
</span>
<span class="min-w-0 flex-1 text-sm text-gray-800 dark:text-gray-200">
{{ __(row.label) }}
</span>
<input type="hidden" :name="`${name}[]`" :value="row.value">
</li>
</ul>
</SortableList>
<RankList
:model-value="value ?? []"
:options
:name
:disabled="config.disabled || isReadOnly"
@update:model-value="update"
/>
</template>

<style scoped>
.ranking-item.draggable-source--is-dragging {
/* Make the item slightly transparent than the default dragging opacity */
opacity: 0.65;
}
</style>
Loading
Loading