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

[charts] Allow controlling the demo form from the example #13796

Merged
merged 5 commits into from
Jul 12, 2024

Conversation

JCQuintas
Copy link
Member

  • Make ChartDemoPropsForm only work in controlled mode. Controller is now ChartsUsageDemo
  • This allows us to pass the onChange handler to the render function, which allows children(examples) to change the knobs from their own logic
    • Useful on zoom to prevent throwing errors, we can check if prop.start > prop.end and update the knobs accordingly.
  • Cleaned up typing a and logic (removed undefined knob logic)
  • Added slider component

For testing the slider locally.

import * as React from 'react';
import Box from '@mui/material/Box';
import ChartsUsageDemo from 'docsx/src/modules/components/ChartsUsageDemo';
import { ScatterChart } from '@mui/x-charts/ScatterChart';
import { Chance } from 'chance';

const chance = new Chance(42);

const data = Array.from({ length: 200 }, () => ({
  x: chance.floating({ min: -25, max: 25 }),
  y: chance.floating({ min: -25, max: 25 }),
})).map((d, index) => ({ ...d, id: index }));

const knobs = [{ propName: 'tickSize', knob: 'slider', defaultValue: 6 }];
export default function AxisCustomizationNoSnap() {
  return (
    <ChartsUsageDemo
      componentName="Alert"
      data={knobs}
      renderDemo={(props, onChange) => (
        <Box sx={{ width: '100%', maxWidth: 400 }}>
          <button onClick={() => onChange({ ...props, tickSize: 50 })}>
            Change
          </button>
          <ScatterChart
            series={[
              {
                type: 'scatter',
                id: 'linear',
                data,
              },
            ]}
            leftAxis={null}
            bottomAxis={{
              ...props,
            }}
            height={300}
            margin={{ top: 10, left: 20, right: 20 }}
          />
        </Box>
      )}
      getCode={({ props }) =>
        [
          ...Object.keys(props).map(
            (prop) =>
              `    ${prop}: ${
                typeof props[prop] === 'string' ? `"${props[prop]}"` : props[prop]
              },`,
          ),
        ].join('\n')
      }
    />
  );
}

@JCQuintas JCQuintas added docs Improvements or additions to the documentation enhancement This is not a bug, nor a new feature component: charts This is the name of the generic UI component, not the React module! labels Jul 10, 2024
@JCQuintas JCQuintas self-assigned this Jul 10, 2024
@mui-bot
Copy link

mui-bot commented Jul 10, 2024

Deploy preview: https://deploy-preview-13796--material-ui-x.netlify.app/

Generated by 🚫 dangerJS against e8f9171

Copy link
Member

@alexfauquette alexfauquette left a comment

Choose a reason for hiding this comment

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

Looks good. Just one potential improvement suggestion

@JCQuintas JCQuintas enabled auto-merge (squash) July 12, 2024 11:39
@JCQuintas JCQuintas merged commit c3b2f37 into mui:master Jul 12, 2024
15 checks passed
@JCQuintas JCQuintas deleted the allow-controlled-charts-usage-demo branch July 12, 2024 11:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: charts This is the name of the generic UI component, not the React module! docs Improvements or additions to the documentation enhancement This is not a bug, nor a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants