reboottime / React-Develpment

Some notes, thought, articles or source code readings aggregated here about React Web development.
0 stars 0 forks source link

[Mantine UI Series] Stepper #20

Open reboottime opened 1 year ago

reboottime commented 1 year ago

Overview

This article talks about Mantine UI Stepper component, including:


When and How Stepper Components Are Used

When user are in a complex flow of doing something, such as

In Front end Development domain and real world use cases:

reboottime commented 1 year ago

The Mantine UI Stepper component

Mantine UI Stepper component's API design has considered nearly all the usage scenarios mentioned earlier.


Good React practices for Developers

type StepperComponent = ForwardRefWithStaticComponents<
  StepperProps,
  {
    Step: typeof Step;
    Completed: typeof StepCompleted;
  }
>;

Stepper.Step = Step;
Stepper.Completed = StepCompleted;
Stepper.displayName = '@mantine/core/Stepper';
export type ForwardRefWithStaticComponents<
  Props extends Record<string, any>,
  Static extends Record<string, any>
> = ((props: Props) => React.ReactElement) &
  Static & {
    displayName: string;
  };

Utilizing the composition pattern enhances developer flexibility in structuring code, but poses challenges in enforcing expected usage.

To avoid unexpected content rendered inside of the Stepper, Mantine handles the concern in the following way

 const _children = convertedChildren.filter((child) => child.type === Step);
 const completedStep = convertedChildren.find((item) => item.type === StepCompleted);

  acc.push( cloneElement(item, {/**some other code**/}));

The side note, the child.type can either be a html tag name, or a reference to the type of a React component. See example bellow

import React from 'react';

const Title = () => <h1>Hello, Title!</h1>;

const App = () => {
  const htmlChild = <div>This is a div.</div>;
  const componentChild = <Title />;

  console.log('HTML child type:', htmlChild.type); // Outputs: "div"
  console.log('Component child type:', componentChild.type); // Outputs: Reference to the Title component
};

export default App;
reboottime commented 1 year ago

The Design Flaw of the Stepper Component


Lack of Consideration for Horizontal Mode


There are situations where you'd like to control the layout between Step content and the Stepper progress bar. Although the Stepper component provides two orientation modes: vertical and horizontal, the horizontal mode does not necessarily mean that the Mantine Stepper aligns its Step content on the right side, as shown bellow:

Horizontal Mode

Given the source code of the Stepper, the solutions for achieving the desired UI layout are as follows:

Stepper Source Code



Mental block on understanding allowStepClick and allowStepSelect Logic

The logical design of allowStepClick and allowStepSelect might not be immediately intuitive for developer users, unless they delve into the source code.

reboottime commented 1 year ago

References

  1. Mantine UI, source code of Stepper
  2. Carbon design system progress bar