Skip to content
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

feat(admin-ui): Slider / Range Slider components #4348

Merged
merged 46 commits into from
Nov 5, 2024

Conversation

leopuleo
Copy link
Contributor

@leopuleo leopuleo commented Oct 22, 2024

Changes

This PR introduces two new components to the @webiny/admin-ui package:

  • Slider
  • RangeSlider

along with their form variants.

Basic Component (<Slider />)

For the base slider, two components are exported:

  • <ComposableSlider />: Accepts subcomponent VMs (ViewModels) and renders Radix UI components. This component serves as the foundation for both the simple and form variants.
  • <Slider />: Receives custom-designed props, retrieves the necessary VMs via the presenter, and passes them to <ComposableSlider />.

The component state is managed by a presenter and linked to the main component through a dedicated hook, such as useSlider(). This hook acts as the composition root in React, establishing a three-layered architecture:

  • Presenter (SliderPresenter.ts): Initializes with the component props, manages the component’s state, and exposes the VMs.
  • Hook (useSlider.ts): Serves as the composition root. It receives the component props, integrates with React, and returns the VMs.
  • Component (Slider.ts): Renders the Radix UI components.

Form Component (<Slider />)

The form variant may need to manage its own state and use the simple component with others like <Label />. In this scenario, the component state is managed by a presenter and linked to the main component through a dedicated hook.

How Has This Been Tested?

Manually with Storybook + Presenter tested via Jest

@leopuleo leopuleo self-assigned this Oct 28, 2024
@leopuleo leopuleo marked this pull request as ready for review October 28, 2024 14:53
export const useRangeSlider = (props: RangeSliderProps) => {
const presenter = useMemo(() => {
const rangeSliderPresenter = new RangeSliderPresenter(
omit(props, ["label", "labelPosition"])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it possible that your props change, but you want to maintain the state of the Presenter?

packages/admin-ui/src/Form/RangeSlider/useRangeSlider.ts Outdated Show resolved Hide resolved

class FormRangeSliderPresenter implements IRangeSliderPresenter {
private rangeSliderPresenter: IRangeSliderPresenter;
private localValues: number[];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure you even need this property. You can always derive labels from the value of rangeSliderPresenter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before the admin-ui component supported both controlled and uncontrolled states, like the Radix UI base components. (Radix UI docs, see value and defaultValue).

Now the admin-ui component does not support the defaultValue component, developers must provide externally controlled value and onValueChange callback. Doing so we can derive labels from values.

packages/admin-ui/src/Form/Slider/SliderPresenter.test.ts Outdated Show resolved Hide resolved
packages/admin-ui/src/RangeSlider/useRangeSlider.ts Outdated Show resolved Hide resolved
@@ -15,10 +14,10 @@ type Props = FormComponentProps & {
description?: string;

// The minimum value of the Slider.
min: number | string;
min?: number | string;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is string supported?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No idea 😄 it's coming from the previous implementation

packages/admin-ui/src/Form/Slider/Slider.tsx Outdated Show resolved Hide resolved
@leopuleo leopuleo merged commit 00f2d85 into feat/new-admin-ui Nov 5, 2024
10 checks passed
@leopuleo leopuleo deleted the leo/feat/ui-slider branch December 5, 2024 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants