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

optionのvalueの型をnumberからTにする #1213

Closed
wants to merge 6 commits into from
Closed

Conversation

okakyo
Copy link
Collaborator

@okakyo okakyo commented Feb 18, 2024

タスク

resolve: #1190

行ったこと

  • React で、Option の value を number → T型に修正
    • Selectbox などの一部のコンポーネントにて、デフォルト値を value = -1 としている場合は number 型のままにした

TODO

  • Vue.js で、option の Value 型を number から T に変更
  • Search Selector など、default 値が number で固定されているものについて Number 型に固定された箇所の修正

generics を利用して Vue.js の型定義を投げるようになったが、computed, methods の引数に型を渡せなかったため、別PR で対応した

https://ja.vuejs.org/api/sfc-script-setup#generics

Sorry, something went wrong.

Copy link

changeset-bot bot commented Feb 18, 2024

🦋 Changeset detected

Latest commit: bb00b0d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@wizleap-inc/wiz-ui-react Minor
@wizleap-inc/wiz-ui-next Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Feb 18, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
wiz-ui-next ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 21, 2024 6:51am
wiz-ui-react ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 21, 2024 6:51am

Copy link

reg-suit bot commented Feb 18, 2024

reg-suit detected visual differences.

Check this report, and review them.

🔴

🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵

What do the circles mean? The number of circles represent the number of changed images.
🔴 : Changed items, ⚪ : New items, ⚫ : Deleted items, and 🔵 Passed items

How can I change the check status? If reviewers approve this PR, the reg context status will be green automatically.

Copy link
Collaborator

@ichi-h ichi-h left a comment

Choose a reason for hiding this comment

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

いくつかコメントいたしましたが、大まかな方向はこちらで問題ないと思います!
あとはvueやreactのコンポーネントで型引数を受け付けてあげればうまくいきそうな感じがします!
引き続きよろしくお願いいたします! 🙏

Comment on lines 4 to 6

export type SearchInputOption = {
export type SearchInputOption<T = number> = {
label: string;
Copy link
Collaborator

Choose a reason for hiding this comment

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

型引数の初期値numberはできればなくしたいなという気持ちがあります!
numberが初期値で指定されていると、「とりあえずnumberを突っ込んでおくか」というコードを生み出しそうな予感がしておりまして、、、 💦

valueがnumber型になっているものをoptionsに突っ込んだときに、型が自動的に SearchInputOption<number> へと切り替わったり、string型のoptionsを突っ込んだときに SearchInputOption<string> となっていればOKなので、要は型引数の初期値を気にすることはないのかなと考えています!

ただ、初期値にnumberを入れないと破壊的変更になってしまうといった問題があれば、一旦はこれでも良いかなと考えております!

Comment on lines 41 to 43
dropdownMaxHeight?: string;
onChange: (value: number | null) => void;
onChange: (value: unknown) => void;
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

ここのunknownはPropsの型引数で置き換えられると思います!

Comment on lines 30 to 32

type Props = BaseProps & {
options: SelectBoxOption[];
value: number | null;
type Props<T = number | null> = BaseProps & {
options: SelectBoxOption<T>[];
Copy link
Collaborator

Choose a reason for hiding this comment

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

ジェネリクスでnullableを許容するというよりは、valueやonChangeのnullableを許容するところに絞って適用するのが良いかなと思います!
(型引数の初期値を使わないのが一番良さそうですが!)

@okakyo
Copy link
Collaborator Author

okakyo commented Mar 10, 2024

色々試行錯誤してみたのですが、Vue の generics が Props, Emits に 型を適用できたが、ref, methods などへ型を引き継ぐことはできなさそうでした。この解決方法について資料を調べてみたのですが、解決策が見つからないので解決策が見つかるまで一旦保留させてください。

@ichi-h
Copy link
Collaborator

ichi-h commented Mar 12, 2024

@okakyo
reactの方だけって可能でしょうか……?
vueのところだけ分離して別タスクでやるというのもありかなと考えておりまして……!

Copy link
Collaborator

@ichi-h ichi-h left a comment

Choose a reason for hiding this comment

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

ありがとうございます! 🙏 🙏 🙏
何点かコメントがあったので、ご確認のほどよろしくお願いいたします! 🙇
(まだ動作確認まで見れていないので改めて行います 🙇 )

Comment on lines 126 to 128
style={{ width: "100%" }}
value={option.value}
value={String(option.value)}
id={`${option.label}-${option.value}`}
Copy link
Collaborator

Choose a reason for hiding this comment

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

check-box-newのvalueにはstring[]といった型も一応入りうるので、Stringでパースしてしまうとバグりそうだなという印象を受けました……!
check-box-newのvalueが受け付ける型は、string | number | readonly string[] | undefined のどれかなので、SearchPopupPanelの型引数を T extends string | number | readonly string[] | undefined とすれば型の問題は回避できそうです!

Comment on lines +33 to +35
}: Props<T>) => {
// TODO: value は number に固定
const [activeValue, setActiveValue] = useState<T | null>(null);
Copy link
Collaborator

Choose a reason for hiding this comment

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

TODOのコメントアウト残っていますが消しても大丈夫なものですかね……?

Comment on lines +128 to 130

// FIXME: value の default 値が -1 なので number 型に固定
return shouldShowAddButton
Copy link
Collaborator

@ichi-h ichi-h Mar 28, 2024

Choose a reason for hiding this comment

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

-1となっているのはaddableが有効になっているときに表示されるボタンだと思うのですが、そのボタンに当てられるvalueは使うことのない値なので、 適当に0とかの値を割り当てて、 型アサーションを使ってTで型を上書きしてしまってもよいかなぁと思っています、、、笑(もちろん事情をコメントアウトに残す前提)

search-selectorもジェネリクス化が望まれているコンポーネントなので、ここもnumber以外に対応できるとめちゃくちゃありがたいです……! 🙇 🙇 🙇

(24/4/1 追記)
0を突っ込むと、value = 0の要素が選択されたときにバグる説がありそうですね……
追加ボタンのvalueに、options内のvalueが合致するものがなければOKなので、uidのような値を突っ込んで型アサーションで T としてしまうのがよいかなと思いました!

Comment on lines 196 to 201
<WizSelectBox
options={statusOptions}
value={status}
onChange={(selectedValue) => onChangeStatus?.(selectedValue)}
onChange={(selectedValue) =>
onChangeStatus?.(selectedValue as T)
}
placeholder={statusPlaceholder}
expand
/>
Copy link
Collaborator

Choose a reason for hiding this comment

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

ここの型アサーションは回避できそうでして、例えば、

Suggested change
<WizSelectBox
options={statusOptions}
value={status}
onChange={(selectedValue) => onChangeStatus?.(selectedValue)}
onChange={(selectedValue) =>
onChangeStatus?.(selectedValue as T)
}
placeholder={statusPlaceholder}
expand
/>
<WizSelectBox<T>
options={statusOptions}
value={status}
onChange={(selectedValue) => onChangeStatus?.(selectedValue)}
placeholder={statusPlaceholder}
expand
/>

として、こうするとstatusOptionsの型エラーが出るので、今度はPropsのstatusOptionsを、

type Props<T> = BaseProps & {
  // 中略
  statusOptions?: ComponentProps<typeof WizSelectBox<T>>["options"];
  // 中略
};

とすれば、型アサーションなしで実装できると思います!

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

Successfully merging this pull request may close these issues.

Feat(select系): optionのvalueの型をnumberからTにしたい (react)
3 participants