2

So, I'm in the case where I need to build some dynamic borders, in several case, also the case when the color is red in not correct validation.

Here is the dynamic so-called border I have build:

const dynamicBorders = {
        borderRight: `1px solid ${color}`,
        borderLeft: `1px solid ${color}`,
        borderTop: props.isFirst === true ? `1px solid ${color}`: `.5px solid ${color}`,
        borderBottom: props.isLast === true ? `1px solid ${color}` : `.5px solid ${color}`,
        borderRadius: props.isFirst === true && props.isLast === false ? '10px 10px 0px 0px': props.isFirst === false && props.isLast === true ? '0px 0px 10px 10px' : props.isFirst === true && props.isLast === true && '10px' ? props.isFirst === false && props.isLast === false && '0' : '0px 0px 0px 0px',
    };

So with this I have achieved this, but still the border in middle is not correct, I don't know.

here is the items

So this is one answer from a member, but that doesn't work on error mode, but that fixes the duplicate order.

const dynamicBorders = {
    borderRight: `1px solid ${color}`,
    borderLeft: `1px solid ${color}`,
    borderTop: `1px solid ${color}`,
    borderBottom: props.isLast === true ? `1px solid ${color}` : '',
    borderRadius: props.isFirst === true && props.isLast === false ? '10px 10px 0px 0px': props.isFirst === false && props.isLast === true ? '0px 0px 10px 10px' : props.isFirst === true && props.isLast === true && '10px' ? props.isFirst === false && props.isLast === false && '0' : '0px 0px 0px 0px',
};

But here is the item when it has an error:

enter image description here

And this is because: const color = props.errorMode === true ? props.error === true ? '#F79DA1' : '#EDEDED' : '#EDEDED'

nevoni3008
  • 227
  • 1
  • 11

1 Answers1

0

Your code is not working, because you can not set border width less than 1px, you can check here. So you can try this

const dynamicBorders = {
    borderRight: `1px solid ${color}`,
    borderLeft: `1px solid ${color}`,
    borderTop: `1px solid ${color}`,
    borderBottom: props.isLast === true ? `1px solid ${color}` : '',
    borderRadius: props.isFirst === true && props.isLast === false ? '10px 10px 0px 0px': props.isFirst === false && props.isLast === true ? '0px 0px 10px 10px' : props.isFirst === true && props.isLast === true && '10px' ? props.isFirst === false && props.isLast === false && '0' : '0px 0px 0px 0px',
};

Update answer: You should have colorTop and colorBottom to set color for items below and above error item. I've created a simple code

const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
export default function App() {
  const [errors, setErrors] = useState([]);
  const onClick = (idx) => {
    const newErrors = errors.includes(idx)
      ? errors.filter((i) => i !== idx)
      : [...errors, idx];
    setErrors(newErrors);
  };
  return (
    <div className="App">
      {array.map((i, index) => (
        <Item
          key={i}
          errors={errors}
          idx={index}
          isFirst={index === 0}
          isLast={index === array.length - 1}
          onClick={() => onClick(index)}
        />
      ))}
    </div>
  );
}

const Item = (props) => {
  const error = props.errors.includes(props.idx);
  const color = error === true ? "#F79DA1" : "#EDEDED";
  const colorTop = error
    ? color
    : props.errors.map((i) => i + 1).includes(props.idx)
    ? "#F79DA1"
    : "#EDEDED";
  const colorBottom = error
    ? color
    : props.errors.map((i) => i - 1).includes(props.idx)
    ? "#F79DA1"
    : "#EDEDED";
  const dynamicBorders = {
    borderTop: `1px solid ${colorTop}`,
    borderRight: `1px solid ${color}`,
    borderBottom: props.isLast === true ? `1px solid ${colorBottom}` : ``,
    borderLeft: `1px solid ${color}`
  };

  return (
    <div onClick={props.onClick} style={dynamicBorders}>
      test item
    </div>
  );
};

You can check in my codesandbox

iamhuynq
  • 5,357
  • 1
  • 13
  • 36