-
Notifications
You must be signed in to change notification settings - Fork 124
feat(genui): add A2UI Slider component #2663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MoonfaceX
merged 1 commit into
lynx-family:main
from
MoonfaceX:p/xiamengfei.moonface/a2ui-slider
May 18, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
|
|
||
| --- | ||
|
|
||
| Add Slider to the private A2UI ReactLynx catalog. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| // Copyright 2026 The Lynx Authors. All rights reserved. | ||
| // Licensed under the Apache License Version 2.0 that can be found in the | ||
| // LICENSE file in the root directory of this source tree. | ||
| import { | ||
| SliderIndicator, | ||
| SliderRoot, | ||
| SliderThumb, | ||
| SliderTrack, | ||
| } from '@lynx-js/lynx-ui'; | ||
| import { useState } from '@lynx-js/react'; | ||
|
|
||
| import { | ||
| fromSliderRatio, | ||
| normalizeSliderLabel, | ||
| normalizeSliderNumber, | ||
| normalizeSliderRange, | ||
| toSliderRatio, | ||
| toSliderStepRatio, | ||
| } from './utils.js'; | ||
| import { useChecks } from '../../react/useChecks.js'; | ||
| import type { CheckLike } from '../../react/useChecks.js'; | ||
| import type { GenericComponentProps } from '../../store/types.js'; | ||
|
|
||
| import '../../../styles/catalog/Slider.css'; | ||
|
|
||
| /** | ||
| * @a2uiCatalog Slider | ||
| */ | ||
| export interface SliderProps extends GenericComponentProps { | ||
| /** The label for the slider. */ | ||
| label?: string | { path: string } | { | ||
| call: string; | ||
| args: Record<string, unknown>; | ||
| returnType?: | ||
| | 'string' | ||
| | 'number' | ||
| | 'boolean' | ||
| | 'array' | ||
| | 'object' | ||
| | 'any' | ||
| | 'void'; | ||
| }; | ||
| /** The minimum value of the slider. */ | ||
| min?: number; | ||
| /** The maximum value of the slider. */ | ||
| max: number; | ||
| /** The current value of the slider. */ | ||
| value: number | { path: string } | { | ||
| call: string; | ||
| args: Record<string, unknown>; | ||
| returnType?: | ||
| | 'string' | ||
| | 'number' | ||
| | 'boolean' | ||
| | 'array' | ||
| | 'object' | ||
| | 'any' | ||
| | 'void'; | ||
| }; | ||
| /** A list of checks to perform. */ | ||
| checks?: Array<{ | ||
| /** The condition that indicates whether the check passes. */ | ||
| condition: boolean | { path: string } | { | ||
| call: string; | ||
| args: Record<string, unknown>; | ||
| returnType?: | ||
| | 'string' | ||
| | 'number' | ||
| | 'boolean' | ||
| | 'array' | ||
| | 'object' | ||
| | 'any' | ||
| | 'void'; | ||
| }; | ||
| /** The error message to display if the check fails. */ | ||
| message: string; | ||
| }>; | ||
| } | ||
|
|
||
| export function Slider( | ||
| props: SliderProps, | ||
| ): import('@lynx-js/react').ReactNode { | ||
| const { | ||
| id, | ||
| label, | ||
| max, | ||
| min, | ||
| setValue, | ||
| surface, | ||
| dataContextPath, | ||
| } = props; | ||
| const minValue = min ?? props['minValue']; | ||
| const maxValue = max ?? props['maxValue']; | ||
| const range = normalizeSliderRange(minValue, maxValue); | ||
| const step = normalizeSliderNumber(props['step'], Number.NaN); | ||
| const stepRatio = toSliderStepRatio(step, range); | ||
| const stepProps = stepRatio === undefined ? {} : { step: stepRatio }; | ||
| const ratio = toSliderRatio(props.value, range); | ||
| const [displayValue, setDisplayValue] = useState<number>( | ||
| Math.round(fromSliderRatio(ratio, range, step)), | ||
| ); | ||
| const labelText = normalizeSliderLabel(label); | ||
| const checks = props.checks as CheckLike[] | undefined; | ||
|
|
||
| const { ok, firstFailureMessage } = useChecks({ | ||
| checks, | ||
| componentId: id ?? '', | ||
| surface, | ||
| dataContextPath, | ||
| }); | ||
|
|
||
| const handleValueChange = (nextRatio: number) => { | ||
| const nextValue = fromSliderRatio(nextRatio, range, step); | ||
| setValue?.('value', nextValue); | ||
| setDisplayValue(Math.round(nextValue)); | ||
| }; | ||
|
MoonfaceX marked this conversation as resolved.
|
||
|
|
||
| return ( | ||
| <view | ||
| key={id} | ||
| className={`slider${ok ? '' : ' slider-invalid'}`} | ||
| > | ||
| {labelText | ||
| ? ( | ||
| <view className='slider-header'> | ||
| <text className='slider-label'>{labelText}</text> | ||
| <text className='slider-value'>{String(displayValue)}</text> | ||
| </view> | ||
| ) | ||
| : null} | ||
| <view className='slider-control'> | ||
| <SliderRoot | ||
| {...stepProps} | ||
| className='slider-root' | ||
| value={ratio} | ||
| onValueChange={handleValueChange} | ||
| > | ||
| <SliderTrack className='slider-track'> | ||
| <SliderIndicator className='slider-indicator' /> | ||
| <SliderThumb className='slider-thumb'> | ||
| <view className='slider-thumb-dot' /> | ||
| </SliderThumb> | ||
| </SliderTrack> | ||
| </SliderRoot> | ||
| </view> | ||
| {!ok && firstFailureMessage | ||
| ? <text className='slider-error'>{firstFailureMessage}</text> | ||
| : null} | ||
| </view> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // Copyright 2026 The Lynx Authors. All rights reserved. | ||
| // Licensed under the Apache License Version 2.0 that can be found in the | ||
| // LICENSE file in the root directory of this source tree. | ||
|
|
||
| const DEFAULT_MIN = 0; | ||
| const DEFAULT_MAX = 100; | ||
|
|
||
| export interface SliderRange { | ||
| min: number; | ||
| max: number; | ||
| } | ||
|
|
||
| export function normalizeSliderNumber( | ||
| value: unknown, | ||
| fallback: number, | ||
| ): number { | ||
| const numberValue = typeof value === 'number' | ||
| ? value | ||
| : (typeof value === 'string' | ||
| ? Number(value) | ||
| : Number.NaN); | ||
|
|
||
| return Number.isFinite(numberValue) ? numberValue : fallback; | ||
| } | ||
|
|
||
| export function normalizeSliderRange( | ||
| minValue: unknown, | ||
| maxValue: unknown, | ||
| ): SliderRange { | ||
| const min = normalizeSliderNumber(minValue, DEFAULT_MIN); | ||
| const max = normalizeSliderNumber(maxValue, DEFAULT_MAX); | ||
|
|
||
| if (max > min) { | ||
| return { min, max }; | ||
| } | ||
|
|
||
| return { min: DEFAULT_MIN, max: DEFAULT_MAX }; | ||
| } | ||
|
|
||
| export function clampSliderValue(value: number, range: SliderRange): number { | ||
| return Math.min(Math.max(value, range.min), range.max); | ||
| } | ||
|
|
||
| export function toSliderRatio(value: unknown, range: SliderRange): number { | ||
| const numericValue = normalizeSliderNumber(value, range.min); | ||
| return (clampSliderValue(numericValue, range) - range.min) | ||
| / (range.max - range.min); | ||
| } | ||
|
|
||
| export function fromSliderRatio( | ||
| ratio: number, | ||
| range: SliderRange, | ||
| step?: number, | ||
| ): number { | ||
| const value = clampSliderValue( | ||
| range.min + ratio * (range.max - range.min), | ||
| range, | ||
| ); | ||
| if (!step || step <= 0) { | ||
| return trimFloatingPoint(value); | ||
| } | ||
| const stepped = range.min + Math.round((value - range.min) / step) * step; | ||
| return trimFloatingPoint(clampSliderValue(stepped, range)); | ||
| } | ||
|
|
||
| export function toSliderStepRatio( | ||
| step: unknown, | ||
| range: SliderRange, | ||
| ): number | undefined { | ||
| const stepValue = normalizeSliderNumber(step, Number.NaN); | ||
| if (!Number.isFinite(stepValue) || stepValue <= 0) { | ||
| return undefined; | ||
| } | ||
| return Math.min(stepValue / (range.max - range.min), 1); | ||
| } | ||
|
|
||
| export function normalizeSliderLabel(value: unknown): string { | ||
| if (value === null || value === undefined) return ''; | ||
| if ( | ||
| typeof value === 'string' | ||
| || typeof value === 'number' | ||
| || typeof value === 'boolean' | ||
| ) { | ||
| return String(value); | ||
| } | ||
| return ''; | ||
| } | ||
|
|
||
| function trimFloatingPoint(value: number): number { | ||
| return Number(value.toFixed(12)); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.