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

Marks do not correctly re-render on state change #183

Open
johnlahut opened this issue Oct 5, 2022 · 2 comments
Open

Marks do not correctly re-render on state change #183

johnlahut opened this issue Oct 5, 2022 · 2 comments

Comments

@johnlahut
Copy link

Having trouble dynamically updating marks on the line. My goal is to conditionally render marks on a timeline. Our domain is around a year, with each step representing a minute.

After updating the marks state variable, the renderMark function does not seem to correctly render.

If I resize the parent container, the component updates and the marks suddenly appear.

import { Thumb } from "./components/Thumb"
import { Track } from "./components/Track"

export const Timeline = ({
  min,
  max,
  step,
  initialValues
}: {
  min?: number,
  max?: number,
  step?: number,
  initialValues?: number[],
}) => {

  const [values, setValues] = useState<number[]>(initialValues ?? [MAX_TIME])
  const [marks, setMarks] = useState<number[]>([]);

  return (
    <div style={{
      padding: '20px',
      display: 'flex',
      justifyContent: 'center',
      flexWrap: 'wrap'
    }}>

      <Range
        min={min ?? MIN_TIME}
        max={max ?? MAX_TIME}
        step={step ?? STEP}
        values={values}
        onChange={(values: number[]) => setValues(values)}
        renderMark={({ props, index }) => {
          if (marks.includes(index)) {
            return (
              <div
                {...props}
                style={{
                  ...props.style,
                  height: '15px',

                  width: '5px',
                  backgroundColor: 'gray',
                }} />
            )
          }

          else {
            return undefined;
          }
        }}
      renderThumb={Thumb}
      renderTrack={Track} />

      <div>
        {values[0]}
      </div>

      <button onClick={() => setMarks([100, 200, 300])}>
        Set Marks
      </button>
    </div>
  )
}
@michaelmota
Copy link

Any updates on this?

@tarikyildizci
Copy link

<Range
  ref={(e) => {
    e?.calculateMarkOffsets()
  }}
/>

This is a workaround that worked for me, sometimes the lib cannot calculate correct offsets for some reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants