|
|
import { isAnimatedString } from '@react-spring/shared' |
|
|
import { AnimatedObject } from './AnimatedObject' |
|
|
import { AnimatedString } from './AnimatedString' |
|
|
import { AnimatedValue } from './AnimatedValue' |
|
|
|
|
|
type Value = number | string |
|
|
type Source = AnimatedValue<Value>[] |
|
|
|
|
|
|
|
|
export class AnimatedArray< |
|
|
T extends ReadonlyArray<Value> = Value[], |
|
|
> extends AnimatedObject { |
|
|
declare protected source: Source |
|
|
constructor(source: T) { |
|
|
super(source) |
|
|
} |
|
|
|
|
|
|
|
|
static create<T extends ReadonlyArray<Value>>(source: T) { |
|
|
return new AnimatedArray(source) |
|
|
} |
|
|
|
|
|
getValue(): T { |
|
|
return this.source.map(node => node.getValue()) as any |
|
|
} |
|
|
|
|
|
setValue(source: T) { |
|
|
const payload = this.getPayload() |
|
|
|
|
|
if (source.length == payload.length) { |
|
|
return payload.map((node, i) => node.setValue(source[i])).some(Boolean) |
|
|
} |
|
|
|
|
|
super.setValue(source.map(makeAnimated)) |
|
|
return true |
|
|
} |
|
|
} |
|
|
|
|
|
function makeAnimated(value: any) { |
|
|
const nodeType = isAnimatedString(value) ? AnimatedString : AnimatedValue |
|
|
return nodeType.create(value) |
|
|
} |
|
|
|