Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
43 changes: 26 additions & 17 deletions src/reactive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ReactiveSetupAdapter
} from "@cfcs/core";

import Flicking from "../Flicking";
import Flicking, { FlickingOptions } from "../Flicking";

// Check if Flicking has reached the first panel
const getIsReachStart = (flicking: Flicking) => !flicking.circular && flicking.index === 0;
Expand Down Expand Up @@ -137,7 +137,11 @@ export interface FlickingReactiveData {
/**
* Flicking instance to connect<ko>연결할 Flicking 인스턴스</ko>
*/
flicking: Flicking | null;
flicking: Flicking | undefined;
/**
* Flicking options used for initialization<ko>초기화에 사용되는 Flicking 옵션</ko>
*/
options: FlickingOptions | undefined;
}

/**
Expand All @@ -158,11 +162,11 @@ FlickingReactiveState,
"moveTo",
FlickingReactiveData
> = ({ onInit, onDestroy, setMethods, getProps }) => {
let flicking: Flicking | null = null;
let flicking: Flicking | undefined;

// Move to a specific panel index
const moveTo = (i: number) => {
if (flicking === null) {
if (flicking === undefined) {
return Promise.reject(new Error("Flicking instance is not available"));
}
if (flicking?.animating) {
Expand All @@ -172,19 +176,23 @@ FlickingReactiveData
};
setMethods(["moveTo"]);

const options = getProps().options;

// options를 고려하지 않고 초기값을 설정해도 동작에는 아무런 문제가 없으나, 이 시점의 초기값과 컴포넌트 init 단계에서의 초기값이 다르면 화면 리렌더링이 발생할 수 있으므로
// 이렇게 미리 옵션을 통해서 예측할 수 있는 부분들은 맞춰둔다.
const reactiveObj: FlickingReactiveObject = reactive({
isReachStart: false, // 이 값을 전달 받은 옵션을 이용해서 설정. getProps()를 통해 전달된 옵션을 사용하여 설정할 수 있습니다.
isReachEnd: false, //
totalPanelCount: 0, //
currentPanelIndex: 0, //
progress: 0, //
indexProgress: 0, //
isReachStart: (options?.defaultIndex === 0 && !options.circular),
isReachEnd: false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

totalPanelCount에 대한 options도 있었으면 좋겠습니다.

totalPanelCount: 0,
currentPanelIndex: options?.defaultIndex ?? 0,
progress: 0,
indexProgress: 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indexProgress: options?.defaultIndex ?? 0,

moveTo
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daybrush 님, 전달받은 옵션을 통해서 미리 맞춰둘 수 있는 초기값은 isReachStart, currentPanelIndex 정도인 것 같아서 이 정도로만 일단 작성했습니다.
얘기하신게 이게 맞는지 한번 확인 부탁드리고, 혹시 다른 값들도 미리 맞춰둘 수 있는 게 있다고 보시면 말씀 부탁드립니다.


// Update state when panel changes
const onChanged = () => {
if (flicking === null) return;
if (flicking === undefined) return;

reactiveObj.isReachStart = getIsReachStart(flicking);
reactiveObj.isReachEnd = getIsReachEnd(flicking);
Expand All @@ -193,23 +201,23 @@ FlickingReactiveData

// Update state when panel count changes
const onPanelChange = () => {
if (flicking === null) return;
if (flicking === undefined) return;

onChanged();
reactiveObj.totalPanelCount = getTotalPanelCount(flicking);
};

// Update progress when camera moves
const onMove = () => {
if (flicking === null) return;
if (flicking === undefined) return;

reactiveObj.progress = getProgress(flicking);
reactiveObj.indexProgress = getIndexProgress(flicking);
};

onInit((inst, data) => {
flicking = data.flicking;
if (flicking === null) return;
if (flicking === undefined) return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

== null


reactiveObj.isReachStart = getIsReachStart(flicking);
reactiveObj.isReachEnd = getIsReachEnd(flicking);
Expand All @@ -235,7 +243,8 @@ FlickingReactiveData
/**
* Connect Flicking instance to reactive API
* @ko Flicking 인스턴스를 반응형 API에 연결합니다
* @param flicking - Flicking instance to connect<ko>연결할 Flicking 인스턴스</ko>
* @param {Flicking} flicking - Flicking instance to connect<ko>연결할 Flicking 인스턴스</ko>
* @param {FlickingOptions} [options] - Flicking options<ko>Flicking 옵션</ko>
* @returns {FlickingReactiveObject} Reactive object with Flicking state and methods<ko>Flicking 상태와 메서드를 포함한 반응형 객체</ko>
* @example
* ```js
Expand All @@ -261,8 +270,8 @@ FlickingReactiveData
* reactiveObj.moveTo(2); // Move to third panel
* ```
*/
const connectFlickingReactiveAPI = (flicking: Flicking) => {
const obj = adaptReactive(flickingReactiveAPIAdapter, () => ({ flicking }));
const connectFlickingReactiveAPI = (flicking: Flicking, options?: FlickingOptions) => {
const obj = adaptReactive(flickingReactiveAPIAdapter, () => ({ flicking, options }));
obj.mounted();
const instance = obj.instance();
obj.init();
Expand Down
Loading