radix form

礼记有言:独学而无友,则孤陋而寡闻
让我们一起了解更多便捷方法,缩短开发时间去摸鱼,嘿嘿。

radix ui 这个库用的还挺多。
来看看它的form表单,验证和错误提示吧。

email 的验证和错误提示

import { Root, Field, Label, Message, Control, Submit } from "@radix-ui/react-form";
/**
 * 
 * match 的值:
 *    valid:有效的
 *    valueMissing:值缺失
 *    typeMismatch:类型不匹配
 *    badInput:坏的输入,
 *    'patternMismatch' :规则错误
 *    'rangeOverflow' 
 *    'rangeUnderflow'
 *    'stepMismatch' 
 *    'tooLong'
 *    'tooShort'
 *    match={(value, formData) => boolean}
 *        例如:--> match={(value, formData) => value !== "John"}
 * 
 */

const errorsStyles = "text-red text-sm"
export function StudyForm() {
  return (
    <Root>
      <Field name={"form"}>
        <div>
          <Label>Email</Label>
          <Control asChild>
            <input className="border border-gray-400 p-1 ml-1 rounded-lg" type="email" required />
          </Control>
          <div>
            {/* 邮箱类型不正确的时候提示错误 */}
            <Message match="typeMismatch" className={errorsStyles}>
              Please provide a valid email
            </Message>
            {/* 值缺失的时候,提示错误 */}
            <Message match="valueMissing" className={errorsStyles}>
              Please enter your email
            </Message>
          </div>
        </div>
      </Field>
      <Submit asChild>
        <button className="w-[100px] p-2 border border-gray-400  rounded-[8px]">Submit</button>
      </Submit>
    </Root>
  )
}

radio
讲真的,我不是很习惯这个库,就像radio,值为空的时候,只有required来控制,若写 error 提示,还需借助 onValueChange。

{/* radio */}
      <RadioGroup.Root name="friend" required defaultValue="carrot" className="flex gap-4">
        {['carrot', 'monet']?.map((v: string, index: number) => {
          return (
            <div className="flex items-center mb-3 w-fit" key={index}>
              <RadioGroup.Item
                className={`w-4 h-4 bg-white rounded-[50%] border border-gray-400 flex justify-center items-center
                      data-[state=checked]:border-blue-500`}
                defaultChecked
                id={v}
                value={v}
              >
                <RadioGroup.Indicator className="data-[state=checked]:text-blue-500">
                  <CheckIcon />
                </RadioGroup.Indicator>
              </RadioGroup.Item>
              <label className="ml-2" htmlFor={v}>
                {v}
              </label>
            </div>
          )
        })}
      </RadioGroup.Root>
Certainly! Below is a MATLAB function `TDPC_encoder` based on the specifications given: ```matlab function encoded_vector = TDPC_encoder(message_vector, radix, rows, columns) % Check input consistency if ~isscalar(radix) || ~isscalar(rows) || ~isscalar(columns) || ... (radix < 2) || (rows < 2) || (columns < 2) || ... any(message_vector >= radix) || ... (length(message_vector) ~= (rows-1)*(columns-1)) error('Invalid input arguments'); end % Reshape message into matrix X msg_len = length(message_vector); X = reshape(message_vector, rows-1, columns-1, 'F'); % Initialize Y with X and add empty row and column Y = zeros(rows, columns); Y(1:rows-1, 1:columns-1) = X; % Compute parity bits for rows and columns for r = 1:rows Y(r, columns) = mod(-sum(Y(r, 1:columns-1)), radix); % Row parity bit end for c = 1:columns-1 Y(rows, c) = mod(-sum(Y(1:rows-1, c)), radix); % Column parity bit end % Ensure last cell meets requirements Y(rows, columns) = mod(-sum(Y(rows, 1:columns-1)) - sum(Y(1:rows-1, columns)), radix); % Reshape back into vector form encoded_vector = Y(:).'; end ``` This code defines a function that takes a message vector along with parameters for the TDPC encoding process. It checks whether the inputs are valid, then rearranges the message into a matrix format, computes the necessary parity checks for rows and columns, and finally flattens the resulting matrix into an encoded vector. You can run this function with a sample message vector and specified parameters to perform TDPC encoding according to the described method. Make sure to also test its correctness and efficiency against the provided testing script or additional test cases.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值